public class ProductContRestController {
RestTemplate restTemplate;
@BeforeEach
public void setup() {
restTemplate = new RestTemplate();
}
// 127.0.0.1:9000/ROOT/api/productcount/insert.json
// @RequestBody
@Test
public void insertProductCount() {
ProductCountEntity pcount = new ProductCountEntity();
pcount.setCnt(300L); // 12개
pcount.setType("O"); // 입고
ProductEntity product = new ProductEntity();
product.setNo(1003L); // 1003번 물품
pcount.setProduct(product);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ProductCountEntity> entity = new HttpEntity<>(pcount, headers);
ResponseEntity<String> result = restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/productcount/insert.json",
HttpMethod.POST, entity, String.class);
System.out.println(result.getBody());
// 상태코드는 값이 어떻든 200이 넘어옴. restcontroller가 동작하면 200
// assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
assertThat(result.getBody().toString()).contains("\"status\":200");
// 내부에서 200을 주면 ok
}
// 127.0.0.1:9000/ROOT/api/productcount/selectcount.json
// @RequestParam
@Test
public void selectCountCount() {
String url = "http://127.0.0.1:9090/ROOT/api/productcount/selectcount.json?no=1003";
String result = restTemplate.getForObject(url, String.class);
System.out.println(result);
assertThat(result).contains("\"status\":200");
}
}
// http://127.0.0.1:9090/ROOT/api/product/insert.json
// @ModelAttribute
@Test
public void insertTest() {
// 전송하고자 하는 값 생성
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("name", "테스트용");
body.add("price", 1234L);
// headers 생성
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
// url, 메소드, 엔티티, 반환되는 타입
ResponseEntity<String> result = restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/product/insert.json",
HttpMethod.POST, entity, String.class);
// System.out.println(result);
assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
System.out.println(result.getStatusCode().toString());
}
// http://127.0.0.1:9090/ROOT/api/product/update.json
// @RequestBody
@Test
public void patchTest() {
// 전송하고자 하는 값 생성
ProductEntity product = new ProductEntity();
product.setNo(1022L);
product.setName("가나다라");
product.setPrice(9999L);
// headers 생성
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ProductEntity> entity = new HttpEntity<>(product, headers);
// url, 메소드, 엔티티, 반환되는 타입
ResponseEntity<String> result = restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/product/update.json",
HttpMethod.PUT, entity, String.class);
// System.out.println(result);
assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
System.out.println(result.getStatusCode().toString());
}