Skip to content

Commit

Permalink
unit
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhm1991 committed Jan 24, 2025
1 parent 723fdbf commit 4d06787
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 5 deletions.
50 changes: 50 additions & 0 deletions src/test/java/com/cowave/commons/client/http/MockMultiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.cowave.commons.client.http;

import com.cowave.commons.client.http.multipart.MultiClientController;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
*
* @author shanhuiming
*
*/
@ContextConfiguration(classes = Application.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = DEFINED_PORT)
public class MockMultiTest {

@Autowired
private MultiClientController multiClientController;

private MockMvc mockMvc;

@BeforeEach
public void beforeEach() {
mockMvc = MockMvcBuilders.standaloneSetup(multiClientController).build();
}

@Test
public void multi1() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/client/multi1")
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id").value(1))
.andExpect(jsonPath("$.data.name").value("xxx"));
}
}
14 changes: 10 additions & 4 deletions src/test/java/com/cowave/commons/client/http/MockPostTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cowave.commons.client.http;

import com.cowave.commons.client.http.post.PostClientController;
import com.cowave.commons.client.http.post.ResponseDecoderService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -32,9 +31,6 @@ public class MockPostTest {
@Autowired
private PostClientController postClientController;

@Autowired
private ResponseDecoderService responseDecoderService;

private MockMvc mockMvc;

@BeforeEach
Expand All @@ -61,4 +57,14 @@ public void post2() throws Exception {
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("xxx"));
}

@Test
public void post3() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/client/post3")
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id").value(1))
.andExpect(jsonPath("$.data.name").value("xxx"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cowave.commons.client.http.multipart;

import com.cowave.commons.client.http.annotation.HttpClient;
import com.cowave.commons.client.http.annotation.HttpLine;
import com.cowave.commons.client.http.annotation.HttpMultiForm;
import com.cowave.commons.client.http.response.HttpResponse;

import java.util.Map;

/**
*
* @author shanhuiming
*
*/
@HttpClient(url = "http://127.0.0.1:8080")
public interface HttpUrlMultiService {

@HttpLine("POST /api/v1/service/submit")
HttpResponse<Object> submit(@HttpMultiForm Map<String, Object> multiBody);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cowave.commons.client.http.multipart;

import lombok.Data;

/**
*
* @author shanhuiming
*
*/
@Data
public class MultiBody {

private Integer id;

private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cowave.commons.client.http.multipart;

import com.cowave.commons.client.http.response.HttpResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author shanhuiming
*
*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/client")
public class MultiClientController {

private final HttpUrlMultiService httpUrlMultiService;

@GetMapping("/multi1")
public HttpResponse<Object> multi1() {
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("id", 1);
bodyMap.put("name", "xxx");
return httpUrlMultiService.submit(bodyMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cowave.commons.client.http.multipart;

import com.cowave.commons.client.http.post.PostBody;
import com.cowave.commons.client.http.response.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
* @author shanhuiming
*
*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/service")
public class MultiServiceController {

@PostMapping("/submit")
public Response<MultiBody> create(MultiBody multiBody){
return Response.success(multiBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.cowave.commons.client.http.annotation.HttpClient;
import com.cowave.commons.client.http.annotation.HttpHeaders;
import com.cowave.commons.client.http.annotation.HttpLine;
import com.cowave.commons.client.http.annotation.HttpParam;
import com.cowave.commons.client.http.response.HttpResponse;
import com.cowave.commons.client.http.response.Response;

import java.util.Map;

Expand All @@ -21,4 +21,8 @@ public interface HttpUrlPostService {
@HttpHeaders({X_Request_ID + ": 12345"})
@HttpLine("POST /api/v1/service/create")
HttpResponse<Object> create(Map<String, Object> bodyMap);

@HttpHeaders({X_Request_ID + ": 12345"})
@HttpLine("POST /api/v1/service/create")
Response<PostBody> create2(Map<String, Object> bodyMap);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cowave.commons.client.http.post;

import com.cowave.commons.client.http.response.HttpResponse;
import com.cowave.commons.client.http.response.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -38,4 +39,12 @@ public PostBody urlPost2() {
bodyMap.put("name", "xxx");
return responseDecoderService.create(bodyMap);
}

@GetMapping("/post3")
public Response<PostBody> urlPost3() {
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("id", 1);
bodyMap.put("name", "xxx");
return httpUrlPostService.create2(bodyMap);
}
}

0 comments on commit 4d06787

Please sign in to comment.