33import com .concretepage .entity .Address ;
44import com .concretepage .entity .Company ;
55import com .concretepage .entity .Person ;
6+ import lombok .extern .slf4j .Slf4j ;
67import org .junit .Before ;
78import org .junit .Test ;
89import org .springframework .http .HttpEntity ;
1617import java .util .HashMap ;
1718import java .util .Map ;
1819
20+ @ Slf4j
1921public class RestTemplateTest {
20- RestTemplate restTemplate ;
22+ private RestTemplate restTemplate ;
2123
2224 @ Before
2325 public void setUp () throws Exception {
2426 restTemplate = new RestTemplate ();
2527 }
2628
2729 @ Test
28- public void testDelete () {
29- String url = "http://localhost:8080/data/delete/{name}/{village}" ;
30- Map <String , String > map = new HashMap <String , String >();
31- map .put ("name" , "Mahesh" );
32- map .put ("village" , "Dhananjaypur" );
33- restTemplate .delete (url , map );
30+ public void testGetForObjectWithJson () {
31+ Person person = restTemplate .getForObject ("http://localhost:8080/data/fetchjson/{id}" , Person .class , 200 );
32+ System .out .println ("ID: " + person .getId ());
33+ System .out .println ("Name: " + person .getName ());
34+ System .out .println ("Village Name: " + person .getAddress ().getVillage ());
3435 }
3536
3637 @ Test
37- public void testExchange () {
38- String uri = "http://localhost:8080/data/exchange/{id}" ;
39- HttpHeaders headers = new HttpHeaders ();
40- headers .setContentType (MediaType .APPLICATION_JSON );
41- HttpEntity <String > entity = new HttpEntity <String >("Hello World!" , headers );
42- ResponseEntity <Person > personEntity = restTemplate .exchange (uri , HttpMethod .GET , entity , Person .class , 100 );
43- System .out .println ("ID:" + personEntity .getBody ().getId ());
44- System .out .println ("Name:" + personEntity .getBody ().getName ());
45- System .out .println ("Village:" + personEntity .getBody ().getAddress ().getVillage ());
38+ public void testGetObjectWithXml () {
39+ Company company = restTemplate .getForObject ("http://localhost:8080/data/fetchxml/{id}" , Company .class , 200 );
40+ System .out .println ("ID: " + company .getId ());
41+ System .out .println ("Company: " + company .getCompanyName ());
42+ System .out .println ("CEO: " + company .getCeoName ());
4643 }
4744
4845 @ Test
4946 public void testGetForEntity () {
5047 String url = "http://localhost:8080/data/fetch/{name}/{village}" ;
51- Map <String , String > map = new HashMap <String , String >();
48+ Map <String , String > map = new HashMap <>();
5249 map .put ("name" , "Mahesh" );
5350 map .put ("village" , "Dhananjaypur" );
5451 ResponseEntity <Person > personEntity = restTemplate .getForEntity (url , Person .class , map );
@@ -57,21 +54,17 @@ public void testGetForEntity() {
5754 }
5855
5956 @ Test
60- public void testGetForObjectWithJson () {
61- Person person = restTemplate .getForObject ("http://localhost:8080/data/fetchjson/{id}" , Person .class , 200 );
62- System .out .println ("ID: " + person .getId ());
63- System .out .println ("Name: " + person .getName ());
64- System .out .println ("Village Name: " + person .getAddress ().getVillage ());
65- }
66-
67- @ Test
68- public void testGetObjectWithXml () {
69-
70- Company company = restTemplate .getForObject ("http://localhost:8080/data/fetchxml/{id}" , Company .class , 200 );
71- System .out .println ("ID: " + company .getId ());
72- System .out .println ("Company: " + company .getCompanyName ());
73- System .out .println ("CEO: " + company .getCeoName ());
57+ public void testExchange () {
58+ String uri = "http://localhost:8080/data/exchange/{id}" ;
59+ HttpHeaders headers = new HttpHeaders ();
60+ headers .setContentType (MediaType .APPLICATION_JSON );
61+ HttpEntity <String > entity = new HttpEntity <>("Hello World!" , headers );
62+ log .info ("entity: {}" , entity );
7463
64+ ResponseEntity <Person > personEntity = restTemplate .exchange (uri , HttpMethod .GET , entity , Person .class , 100 );
65+ System .out .println ("ID:" + personEntity .getBody ().getId ());
66+ System .out .println ("Name:" + personEntity .getBody ().getName ());
67+ System .out .println ("Village:" + personEntity .getBody ().getAddress ().getVillage ());
7568 }
7669
7770 @ Test
@@ -82,10 +75,23 @@ public void testHeadForHeaders() {
8275 System .out .println (httpHeaders .getContentType ());
8376 }
8477
78+
79+ @ Test
80+ public void testPostForObject () {
81+ String url = "http://localhost:8080/data/saveinfo/{id}/{name}" ;
82+ Map <String , String > map = new HashMap <>();
83+ map .put ("id" , "111" );
84+ map .put ("name" , "Shyam" );
85+ Address address = new Address ("Dhananjaypur" , "Varanasi" , "UP" );
86+ Person person = restTemplate .postForObject (url , address , Person .class , map );
87+ System .out .println (person .getName ());
88+ System .out .println (person .getAddress ().getVillage ());
89+ }
90+
8591 @ Test
8692 public void testPostForEntity () {
8793 String url = "http://localhost:8080/data/saveinfo/{id}/{name}" ;
88- Map <String , String > map = new HashMap <String , String >();
94+ Map <String , String > map = new HashMap <>();
8995 map .put ("id" , "111" );
9096 map .put ("name" , "Shyam" );
9197 Address address = new Address ("Dhananjaypur" , "Varanasi" , "UP" );
@@ -99,19 +105,17 @@ public void testPostForLocation() {
99105 String url = "http://localhost:8080/data/location/{id}/{name}" ;
100106 Address address = new Address ("Dhananjaypur" , "Varanasi" , "UP" );
101107 URI uri = restTemplate .postForLocation (url , address , 111 , "Shyam" );
108+ log .info ("uri: {}" , uri );
102109 System .out .println (uri .getPath ());
103110 }
104111
105112 @ Test
106- public void testPostForObject () {
107- String url = "http://localhost:8080/data/saveinfo/{id}/{name}" ;
108- Map <String , String > map = new HashMap <String , String >();
109- map .put ("id" , "111" );
110- map .put ("name" , "Shyam" );
111- Address address = new Address ("Dhananjaypur" , "Varanasi" , "UP" );
112- Person person = restTemplate .postForObject (url , address , Person .class , map );
113- System .out .println (person .getName ());
114- System .out .println (person .getAddress ().getVillage ());
113+ public void testDelete () {
114+ String url = "http://localhost:8080/data/delete/{name}/{village}" ;
115+ Map <String , String > map = new HashMap <>();
116+ map .put ("name" , "Mahesh" );
117+ map .put ("village" , "Dhananjaypur" );
118+ restTemplate .delete (url , map );
115119 }
116120
117121 @ Test
@@ -123,4 +127,4 @@ public void testPut() {
123127 Address address = new Address ("Dhananjaypur" , "Varanasi" , "UP" );
124128 restTemplate .put (url , address , map );
125129 }
126- }
130+ }
0 commit comments