1+ package com .baeldung .spring .cloud .bootstrap .gateway ;
2+
3+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
4+ import org .apache .http .entity .ContentType ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+ import org .springframework .boot .test .web .client .TestRestTemplate ;
8+ import org .springframework .http .*;
9+ import org .springframework .util .LinkedMultiValueMap ;
10+ import org .springframework .util .MultiValueMap ;
11+
12+ public class IntegrationTest {
13+
14+ private TestRestTemplate testRestTemplate = new TestRestTemplate ();
15+ private String testUrl = "http://localhost:8080" ;
16+
17+ @ Test
18+ public void testAccess () throws Exception {
19+ ResponseEntity <String > response = testRestTemplate .getForEntity (testUrl + "/book-service/books" , String .class );
20+ Assert .assertEquals (HttpStatus .OK , response .getStatusCode ());
21+ Assert .assertNotNull (response .getBody ());
22+
23+ //try the protected resource and confirm the redirect to login
24+ response = testRestTemplate .getForEntity (testUrl + "/book-service/books/1" , String .class );
25+ Assert .assertEquals (HttpStatus .FOUND , response .getStatusCode ());
26+ Assert .assertEquals ("http://localhost:8080/login" , response .getHeaders ().get ("Location" ).get (0 ));
27+
28+ //login as user/password
29+ MultiValueMap <String , String > form = new LinkedMultiValueMap <>();
30+ form .add ("username" , "user" );
31+ form .add ("password" , "password" );
32+ response = testRestTemplate .postForEntity (testUrl + "/login" , form , String .class );
33+
34+ //extract the session from the cookie and propagate it to the next request
35+ String sessionCookie = response .getHeaders ().get ("Set-Cookie" ).get (0 ).split (";" )[0 ];
36+ HttpHeaders headers = new HttpHeaders ();
37+ headers .add ("Cookie" , sessionCookie );
38+ HttpEntity <String > httpEntity = new HttpEntity <>(headers );
39+
40+ addBook ();
41+
42+ //request the protected resource
43+ response = testRestTemplate .exchange (testUrl + "/book-service/books/1" , HttpMethod .GET , httpEntity , String .class );
44+ Assert .assertEquals (HttpStatus .OK , response .getStatusCode ());
45+ Assert .assertNotNull (response .getBody ());
46+
47+ addRatings ();
48+
49+ //request the admin protected resource to determine it is still protected
50+ response = testRestTemplate .exchange (testUrl + "/rating-service/ratings" , HttpMethod .GET , httpEntity , String .class );
51+ Assert .assertEquals (HttpStatus .FORBIDDEN , response .getStatusCode ());
52+
53+ //login as the admin
54+ form .clear ();
55+ form .add ("username" , "admin" );
56+ form .add ("password" , "admin" );
57+ response = testRestTemplate .postForEntity (testUrl + "/login" , form , String .class );
58+
59+ //extract the session from the cookie and propagate it to the next request
60+ sessionCookie = response .getHeaders ().get ("Set-Cookie" ).get (0 ).split (";" )[0 ];
61+ headers = new HttpHeaders ();
62+ headers .add ("Cookie" , sessionCookie );
63+ httpEntity = new HttpEntity <>(headers );
64+
65+ //request the protected resource
66+ response = testRestTemplate .exchange (testUrl + "/rating-service/ratings" , HttpMethod .GET , httpEntity , String .class );
67+ Assert .assertEquals (HttpStatus .OK , response .getStatusCode ());
68+ Assert .assertNotNull (response .getBody ());
69+
70+ //request the discovery resources as the admin
71+ response = testRestTemplate .exchange (testUrl + "/discovery" , HttpMethod .GET , httpEntity , String .class );
72+ Assert .assertEquals (HttpStatus .OK , response .getStatusCode ());
73+ }
74+
75+ private void addRatings () {
76+ //login as user/password
77+ MultiValueMap <String , String > form = new LinkedMultiValueMap <>();
78+ form .add ("username" , "user" );
79+ form .add ("password" , "password" );
80+ ResponseEntity <String > response = testRestTemplate .postForEntity (testUrl + "/login" , form , String .class );
81+
82+ //extract the session from the cookie and propagate it to the next request
83+ String sessionCookie = response .getHeaders ().get ("Set-Cookie" ).get (0 ).split (";" )[0 ];
84+ HttpHeaders headers = new HttpHeaders ();
85+ headers .add ("Cookie" , sessionCookie );
86+ headers .add ("ContentType" , ContentType .APPLICATION_JSON .getMimeType ());
87+ Rating rating = new Rating (1L , 4 );
88+
89+ HttpEntity <Rating > httpEntity = new HttpEntity <>(rating , headers );
90+
91+ //request the protected resource
92+ ResponseEntity <Rating > bookResponse = testRestTemplate .postForEntity (testUrl + "/rating-service/ratings" , httpEntity , Rating .class );
93+ Assert .assertEquals (HttpStatus .OK , bookResponse .getStatusCode ());
94+ Assert .assertEquals (rating .getBookId (), bookResponse .getBody ().getBookId ());
95+ Assert .assertEquals (rating .getStars (), bookResponse .getBody ().getStars ());
96+ }
97+
98+ private void addBook (){
99+ //login as user/password
100+ MultiValueMap <String , String > form = new LinkedMultiValueMap <>();
101+ form .add ("username" , "admin" );
102+ form .add ("password" , "admin" );
103+ ResponseEntity <String > response = testRestTemplate .postForEntity (testUrl + "/login" , form , String .class );
104+
105+ //extract the session from the cookie and propagate it to the next request
106+ String sessionCookie = response .getHeaders ().get ("Set-Cookie" ).get (0 ).split (";" )[0 ];
107+ HttpHeaders headers = new HttpHeaders ();
108+ headers .add ("Cookie" , sessionCookie );
109+ headers .add ("ContentType" , ContentType .APPLICATION_JSON .getMimeType ());
110+ Book book = new Book ("Baeldung" , "How to spring cloud" );
111+
112+ HttpEntity <Book > httpEntity = new HttpEntity <>(book , headers );
113+
114+ //request the protected resource
115+ ResponseEntity <Book > bookResponse = testRestTemplate .postForEntity (testUrl + "/book-service/books" , httpEntity , Book .class );
116+ Assert .assertEquals (HttpStatus .OK , bookResponse .getStatusCode ());
117+ Assert .assertEquals (book .getAuthor (), bookResponse .getBody ().getAuthor ());
118+ Assert .assertEquals (book .getTitle (), bookResponse .getBody ().getTitle ());
119+ }
120+
121+ @ JsonIgnoreProperties (ignoreUnknown = true )
122+ public static class Book {
123+
124+ private Long id ;
125+ private String author ;
126+ private String title ;
127+
128+ public Book () {
129+ }
130+
131+ public Book (String author , String title ) {
132+ this .author = author ;
133+ this .title = title ;
134+ }
135+
136+ public String getAuthor () {
137+ return author ;
138+ }
139+
140+ public void setAuthor (String author ) {
141+ this .author = author ;
142+ }
143+
144+ public String getTitle () {
145+ return title ;
146+ }
147+
148+ public void setTitle (String title ) {
149+ this .title = title ;
150+ }
151+
152+ public Long getId () {
153+ return id ;
154+ }
155+
156+ public void setId (Long id ) {
157+ this .id = id ;
158+ }
159+ }
160+
161+ @ JsonIgnoreProperties (ignoreUnknown = true )
162+ public static class Rating {
163+ private Long id ;
164+ private Long bookId ;
165+ private int stars ;
166+
167+ public Rating () {
168+ }
169+
170+ public Rating (Long bookId , int stars ) {
171+ this .bookId = bookId ;
172+ this .stars = stars ;
173+ }
174+
175+ public Long getId () {
176+ return id ;
177+ }
178+
179+ public void setId (Long id ) {
180+ this .id = id ;
181+ }
182+
183+ public Long getBookId () {
184+ return bookId ;
185+ }
186+
187+ public void setBookId (Long bookId ) {
188+ this .bookId = bookId ;
189+ }
190+
191+ public int getStars () {
192+ return stars ;
193+ }
194+
195+ public void setStars (int stars ) {
196+ this .stars = stars ;
197+ }
198+ }
199+
200+
201+ }
0 commit comments