11package com .advenoh .controller ;
22
3+ import com .advenoh .model .Address ;
34import com .advenoh .model .Employee ;
45import lombok .extern .slf4j .Slf4j ;
56import org .junit .Test ;
67import org .junit .runner .RunWith ;
78import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .springframework .core .ParameterizedTypeReference ;
810import org .springframework .http .HttpEntity ;
911import org .springframework .http .HttpHeaders ;
1012import org .springframework .http .HttpMethod ;
1517import org .springframework .util .MultiValueMap ;
1618import org .springframework .web .client .RestTemplate ;
1719
20+ import java .util .HashMap ;
21+ import java .util .List ;
22+ import java .util .Map ;
23+
1824@ Slf4j
1925@ RunWith (SpringRunner .class )
2026@ SpringBootTest
2127public class EmployeeControllerTest {
22- final String BASE_URL = "http://localhost:8080/employee " ;
28+ final String BASE_URL = "http://localhost:8080" ;
2329
2430 RestTemplate restTemplate = new RestTemplate ();
2531
@@ -35,17 +41,19 @@ public class EmployeeControllerTest {
3541 * put() : 주어진 URL 주소로 HTTP PUT 메서드를 실행한다.
3642 * execute():
3743 */
44+
45+ //GET
3846 @ Test
3947 public void test_getForObject () {
40- Employee employee = restTemplate .getForObject (BASE_URL + "/{id}" , Employee .class , 25 );
48+ Employee employee = restTemplate .getForObject (BASE_URL + "/employee/ {id}" , Employee .class , 25 );
4149 log .info ("employee: {}" , employee );
4250 }
4351
4452 @ Test
4553 public void test_getForEntity () {
46- ResponseEntity <Employee > empEntity = restTemplate .getForEntity (BASE_URL + "/{id}" , Employee .class , 25 );
47- log .info ("statusCode: {}" , empEntity .getStatusCode ());
48- log .info ("getBody: {}" , empEntity .getBody ());
54+ ResponseEntity <String > responseEntity = restTemplate .getForEntity (BASE_URL + "/employee/ {id}" , String .class , 25 );
55+ log .info ("statusCode: {}" , responseEntity .getStatusCode ());
56+ log .info ("getBody: {}" , responseEntity .getBody ());
4957 }
5058
5159 @ Test
@@ -54,62 +62,21 @@ public void test_getForEntity() {
5462 params .add ("name" , "Frank Oh" );
5563 params .add ("country" , "US" );
5664
57- ResponseEntity <Employee > empEntity = restTemplate .getForEntity (BASE_URL + "/{name}/{country}" , Employee .class , params );
58- log .info ("statusCode: {}" , empEntity .getStatusCode ());
59- log .info ("getBody: {}" , empEntity .getBody ());
60- }
61-
62- @ Test
63- public void test_get_lists_of_objects () {
64- //https://www.baeldung.com/spring-rest-template-list
65+ ResponseEntity <Employee > responseEntity = restTemplate .getForEntity (BASE_URL + "/employee/{name}/{country}" , Employee .class , params );
66+ log .info ("statusCode: {}" , responseEntity .getStatusCode ());
67+ log .info ("getBody: {}" , responseEntity .getBody ());
6568 }
6669
70+ //POST
6771 @ Test
68- public void test_exchange () {
69- HttpHeaders headers = new HttpHeaders ();
70- headers .setContentType (MediaType .APPLICATION_JSON );
71- HttpEntity <String > entity = new HttpEntity <>("Hello World!" , headers );
72- log .info ("entity: {}" , entity );
72+ public void testPostForObject () {
73+ Employee newEmployee = Employee .builder ().name ("Frank" ).address (Address .builder ().country ("US" ).build ()).build ();
7374
74- ResponseEntity <Employee > empEntity = restTemplate .exchange (BASE_URL + "/exchange/{id}" , HttpMethod .GET , entity , Employee .class , 50 );
75- log .info ("empEntity: {}" , empEntity );
75+ // final HttpEntity<Employee> request = new HttpEntity<>(newEmployee);
76+ Employee employee = restTemplate .postForObject (BASE_URL , newEmployee , Employee .class , 23 );
77+ log .info ("employee: {}" , employee );
7678 }
7779
78- // @Test
79- // public void testExchange() {
80- // String uri = "http://localhost:8080/data/exchange/{id}";
81- // HttpHeaders headers = new HttpHeaders();
82- // headers.setContentType(MediaType.APPLICATION_JSON);
83- // HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
84- // log.info("entity: {}", entity);
85- //
86- // ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
87- // System.out.println("ID:" + personEntity.getBody().getId());
88- // System.out.println("Name:" + personEntity.getBody().getName());
89- // System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
90- // }
91- //
92- // @Test
93- // public void testHeadForHeaders() {
94- // String url = "http://localhost:8080/data/fetch/{id}";
95- // HttpHeaders httpHeaders = restTemplate.headForHeaders(url, 100);
96- // System.out.println(httpHeaders.getDate());
97- // System.out.println(httpHeaders.getContentType());
98- // }
99- //
100- //
101- // @Test
102- // public void testPostForObject() {
103- // String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
104- // Map<String, String> map = new HashMap<>();
105- // map.put("id", "111");
106- // map.put("name", "Shyam");
107- // Address address = new Address("Dhananjaypur", "Varanasi", "UP");
108- // Person person = restTemplate.postForObject(url, address, Person.class, map);
109- // System.out.println(person.getName());
110- // System.out.println(person.getAddress().getVillage());
111- // }
112- //
11380 // @Test
11481 // public void testPostForEntity() {
11582 // String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
@@ -131,6 +98,29 @@ public void test_exchange() {
13198 // System.out.println(uri.getPath());
13299 // }
133100 //
101+
102+ // @Test
103+ // public void testExchange() {
104+ // String uri = "http://localhost:8080/data/exchange/{id}";
105+ // HttpHeaders headers = new HttpHeaders();
106+ // headers.setContentType(MediaType.APPLICATION_JSON);
107+ // HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
108+ // log.info("entity: {}", entity);
109+ //
110+ // ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
111+ // System.out.println("ID:" + personEntity.getBody().getId());
112+ // System.out.println("Name:" + personEntity.getBody().getName());
113+ // System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
114+ // }
115+ //
116+ // @Test
117+ // public void testHeadForHeaders() {
118+ // String url = "http://localhost:8080/data/fetch/{id}";
119+ // HttpHeaders httpHeaders = restTemplate.headForHeaders(url, 100);
120+ // System.out.println(httpHeaders.getDate());
121+ // System.out.println(httpHeaders.getContentType());
122+ // }
123+
134124 // @Test
135125 // public void testDelete() {
136126 // String url = "http://localhost:8080/data/delete/{name}/{village}";
@@ -149,4 +139,23 @@ public void test_exchange() {
149139 // Address address = new Address("Dhananjaypur", "Varanasi", "UP");
150140 // restTemplate.put(url, address, map);
151141 // }
142+
143+ @ Test
144+ public void test_exchange () {
145+ HttpHeaders headers = new HttpHeaders ();
146+ headers .setContentType (MediaType .APPLICATION_JSON );
147+ HttpEntity <String > entity = new HttpEntity <>("Hello World!" , headers );
148+ log .info ("entity: {}" , entity );
149+
150+ ResponseEntity <Employee > empEntity = restTemplate .exchange (BASE_URL + "/exchange/{id}" , HttpMethod .GET , entity , Employee .class , 50 );
151+ log .info ("empEntity: {}" , empEntity );
152+ }
153+
154+ @ Test
155+ public void test_get_lists_of_objects () {
156+ ResponseEntity <List <Employee >> responseEntity = restTemplate
157+ .exchange (BASE_URL + "/employees" , HttpMethod .GET , null , new ParameterizedTypeReference <List <Employee >>() {
158+ });
159+ log .info ("responseEntity: {}" , responseEntity );
160+ }
152161}
0 commit comments