1515 */
1616package feign ;
1717
18- import com .google .common .collect .ImmutableList ;
1918import com .google .gson .reflect .TypeToken ;
2019import java .net .URI ;
21- import java .util .Arrays ;
2220import java .util .List ;
2321import javax .inject .Named ;
2422import org .junit .Rule ;
2523import org .junit .Test ;
2624import org .junit .rules .ExpectedException ;
2725
28- import static feign .Util .UTF_8 ;
29- import static org .junit .Assert .assertEquals ;
30- import static org .junit .Assert .assertFalse ;
31- import static org .junit .Assert .assertNull ;
32- import static org .junit .Assert .assertTrue ;
26+ import static feign .assertj .FeignAssertions .assertThat ;
27+ import static java .util .Arrays .asList ;
28+ import static org .assertj .core .data .MapEntry .entry ;
3329
3430/**
3531 * Tests interfaces defined per {@link Contract.Default} are interpreted into expected {@link feign
@@ -52,14 +48,17 @@ interface Methods {
5248 }
5349
5450 @ Test public void httpMethods () throws Exception {
55- assertEquals ("POST" ,
56- contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("post" )).template ().method ());
57- assertEquals ("PUT" ,
58- contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("put" )).template ().method ());
59- assertEquals ("GET" ,
60- contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("get" )).template ().method ());
61- assertEquals ("DELETE" ,
62- contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("delete" )).template ().method ());
51+ assertThat (contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("post" )).template ())
52+ .hasMethod ("POST" );
53+
54+ assertThat (contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("put" )).template ())
55+ .hasMethod ("PUT" );
56+
57+ assertThat (contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("get" )).template ())
58+ .hasMethod ("GET" );
59+
60+ assertThat (contract .parseAndValidatateMetadata (Methods .class .getDeclaredMethod ("delete" )).template ())
61+ .hasMethod ("DELETE" );
6362 }
6463
6564 interface BodyParams {
@@ -69,14 +68,12 @@ interface BodyParams {
6968 }
7069
7170 @ Test public void bodyParamIsGeneric () throws Exception {
72- MethodMetadata md = contract .parseAndValidatateMetadata (BodyParams .class .getDeclaredMethod ("post" ,
73- List .class ));
74- assertNull (md .template ().body ());
75- assertNull (md .template ().bodyTemplate ());
76- assertNull (md .urlIndex ());
77- assertEquals (md .bodyIndex (), Integer .valueOf (0 ));
78- assertEquals (md .bodyType (), new TypeToken <List <String >>() {
79- }.getType ());
71+ MethodMetadata md = contract .parseAndValidatateMetadata (BodyParams .class .getDeclaredMethod ("post" , List .class ));
72+
73+ assertThat (md .bodyIndex ())
74+ .isEqualTo (0 );
75+ assertThat (md .bodyType ())
76+ .isEqualTo (new TypeToken <List <String >>(){}.getType ());
8077 }
8178
8279 @ Test public void tooManyBodies () throws Exception {
@@ -86,20 +83,14 @@ interface BodyParams {
8683 BodyParams .class .getDeclaredMethod ("tooMany" , List .class , List .class ));
8784 }
8885
89- interface CustomMethodAndURIParam {
90- @ RequestLine ("PATCH" ) Response patch (URI nextLink );
86+ interface CustomMethod {
87+ @ RequestLine ("PATCH" ) Response patch ();
9188 }
9289
93- @ Test public void requestLineOnlyRequiresMethod () throws Exception {
94- MethodMetadata md = contract .parseAndValidatateMetadata (CustomMethodAndURIParam .class .getDeclaredMethod ("patch" ,
95- URI .class ));
96- assertEquals ("PATCH" , md .template ().method ());
97- assertEquals ("" , md .template ().url ());
98- assertTrue (md .template ().queries ().isEmpty ());
99- assertTrue (md .template ().headers ().isEmpty ());
100- assertNull (md .template ().body ());
101- assertNull (md .template ().bodyTemplate ());
102- assertEquals (Integer .valueOf (0 ), md .urlIndex ());
90+ @ Test public void customMethodWithoutPath () throws Exception {
91+ assertThat (contract .parseAndValidatateMetadata (CustomMethod .class .getDeclaredMethod ("patch" )).template ())
92+ .hasMethod ("PATCH" )
93+ .hasUrl ("" );
10394 }
10495
10596 interface WithQueryParamsInPath {
@@ -115,41 +106,38 @@ interface WithQueryParamsInPath {
115106 }
116107
117108 @ Test public void queryParamsInPathExtract () throws Exception {
118- {
119- MethodMetadata md = contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("none" ));
120- assertEquals ("/" , md .template ().url ());
121- assertTrue (md .template ().queries ().isEmpty ());
122- assertEquals ("GET / HTTP/1.1\n " , md .template ().toString ());
123- }
124- {
125- MethodMetadata md = contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("one" ));
126- assertEquals ("/" , md .template ().url ());
127- assertEquals (Arrays .asList ("GetUser" ), md .template ().queries ().get ("Action" ));
128- assertEquals ("GET /?Action=GetUser HTTP/1.1\n " , md .template ().toString ());
129- }
130- {
131- MethodMetadata md = contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("two" ));
132- assertEquals ("/" , md .template ().url ());
133- assertEquals (Arrays .asList ("GetUser" ), md .template ().queries ().get ("Action" ));
134- assertEquals (Arrays .asList ("2010-05-08" ), md .template ().queries ().get ("Version" ));
135- assertEquals ("GET /?Action=GetUser&Version=2010-05-08 HTTP/1.1\n " , md .template ().toString ());
136- }
137- {
138- MethodMetadata md = contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("three" ));
139- assertEquals ("/" , md .template ().url ());
140- assertEquals (Arrays .asList ("GetUser" ), md .template ().queries ().get ("Action" ));
141- assertEquals (Arrays .asList ("2010-05-08" ), md .template ().queries ().get ("Version" ));
142- assertEquals (Arrays .asList ("1" ), md .template ().queries ().get ("limit" ));
143- assertEquals ("GET /?Action=GetUser&Version=2010-05-08&limit=1 HTTP/1.1\n " , md .template ().toString ());
144- }
145- {
146- MethodMetadata md = contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("empty" ));
147- assertEquals ("/" , md .template ().url ());
148- assertTrue (md .template ().queries ().containsKey ("flag" ));
149- assertEquals (Arrays .asList ("GetUser" ), md .template ().queries ().get ("Action" ));
150- assertEquals (Arrays .asList ("2010-05-08" ), md .template ().queries ().get ("Version" ));
151- assertEquals ("GET /?flag&Action=GetUser&Version=2010-05-08 HTTP/1.1\n " , md .template ().toString ());
152- }
109+ assertThat (contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("none" )).template ())
110+ .hasUrl ("/" )
111+ .hasQueries ();
112+
113+ assertThat (contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("one" )).template ())
114+ .hasUrl ("/" )
115+ .hasQueries (
116+ entry ("Action" , asList ("GetUser" ))
117+ );
118+
119+ assertThat (contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("two" )).template ())
120+ .hasUrl ("/" )
121+ .hasQueries (
122+ entry ("Action" , asList ("GetUser" )),
123+ entry ("Version" , asList ("2010-05-08" ))
124+ );
125+
126+ assertThat (contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("three" )).template ())
127+ .hasUrl ("/" )
128+ .hasQueries (
129+ entry ("Action" , asList ("GetUser" )),
130+ entry ("Version" , asList ("2010-05-08" )),
131+ entry ("limit" , asList ("1" ))
132+ );
133+
134+ assertThat (contract .parseAndValidatateMetadata (WithQueryParamsInPath .class .getDeclaredMethod ("empty" )).template ())
135+ .hasUrl ("/" )
136+ .hasQueries (
137+ entry ("flag" , asList (new String [] { null })),
138+ entry ("Action" , asList ("GetUser" )),
139+ entry ("Version" , asList ("2010-05-08" ))
140+ );
153141 }
154142
155143 interface BodyWithoutParameters {
@@ -160,33 +148,37 @@ interface BodyWithoutParameters {
160148
161149 @ Test public void bodyWithoutParameters () throws Exception {
162150 MethodMetadata md = contract .parseAndValidatateMetadata (BodyWithoutParameters .class .getDeclaredMethod ("post" ));
163- assertEquals ("<v01:getAccountsListOfUser/>" , new String (md .template ().body (), UTF_8 ));
164- assertFalse (md .template ().bodyTemplate () != null );
165- assertTrue (md .formParams ().isEmpty ());
166- assertTrue (md .indexToName ().isEmpty ());
151+
152+ assertThat (md .template ())
153+ .hasBody ("<v01:getAccountsListOfUser/>" );
167154 }
168155
169156 @ Test public void producesAddsContentTypeHeader () throws Exception {
170157 MethodMetadata md = contract .parseAndValidatateMetadata (BodyWithoutParameters .class .getDeclaredMethod ("post" ));
171- assertEquals (Arrays .asList ("application/xml" ), md .template ().headers ().get ("Content-Type" ));
158+
159+ assertThat (md .template ())
160+ .hasHeaders (
161+ entry ("Content-Type" , asList ("application/xml" )),
162+ entry ("Content-Length" , asList (String .valueOf (md .template ().body ().length )))
163+ );
172164 }
173165
174166 interface WithURIParam {
175167 @ RequestLine ("GET /{1}/{2}" ) Response uriParam (@ Named ("1" ) String one , URI endpoint , @ Named ("2" ) String two );
176168 }
177169
178- @ Test public void methodCanHaveUriParam () throws Exception {
179- MethodMetadata md = contract .parseAndValidatateMetadata (WithURIParam .class .getDeclaredMethod ("uriParam" , String .class ,
180- URI .class , String .class ));
181- assertEquals (Integer .valueOf (1 ), md .urlIndex ());
182- }
170+ @ Test public void withPathAndURIParam () throws Exception {
171+ MethodMetadata md = contract .parseAndValidatateMetadata (
172+ WithURIParam .class .getDeclaredMethod ("uriParam" , String .class , URI .class , String .class ));
183173
184- @ Test public void pathParamsParseIntoIndexToName () throws Exception {
185- MethodMetadata md = contract .parseAndValidatateMetadata (WithURIParam .class .getDeclaredMethod ("uriParam" , String .class ,
186- URI .class , String .class ));
187- assertEquals ("/{1}/{2}" , md .template ().url ());
188- assertEquals (Arrays .asList ("1" ), md .indexToName ().get (0 ));
189- assertEquals (Arrays .asList ("2" ), md .indexToName ().get (2 ));
174+ assertThat (md .indexToName ())
175+ .containsExactly (
176+ entry (0 , asList ("1" )),
177+ // Skips 1 as it is a url index!
178+ entry (2 , asList ("2" ))
179+ );
180+
181+ assertThat (md .urlIndex ()).isEqualTo (1 );
190182 }
191183
192184 interface WithPathAndQueryParams {
@@ -195,19 +187,18 @@ Response recordsByNameAndType(@Named("domainId") int id, @Named("name") String n
195187 @ Named ("type" ) String typeFilter );
196188 }
197189
198- @ Test public void mixedRequestLineParams () throws Exception {
190+ @ Test public void pathAndQueryParams () throws Exception {
199191 MethodMetadata md = contract .parseAndValidatateMetadata (WithPathAndQueryParams .class .getDeclaredMethod
200192 ("recordsByNameAndType" , int .class , String .class , String .class ));
201- assertNull (md .template ().body ());
202- assertNull (md .template ().bodyTemplate ());
203- assertTrue (md .template ().headers ().isEmpty ());
204- assertEquals ("/domains/{domainId}/records" , md .template ().url ());
205- assertEquals (Arrays .asList ("{name}" ), md .template ().queries ().get ("name" ));
206- assertEquals (Arrays .asList ("{type}" ), md .template ().queries ().get ("type" ));
207- assertEquals (Arrays .asList ("domainId" ), md .indexToName ().get (0 ));
208- assertEquals (Arrays .asList ("name" ), md .indexToName ().get (1 ));
209- assertEquals (Arrays .asList ("type" ), md .indexToName ().get (2 ));
210- assertEquals ("GET /domains/{domainId}/records?name={name}&type={type} HTTP/1.1\n " , md .template ().toString ());
193+
194+ assertThat (md .template ())
195+ .hasQueries (entry ("name" , asList ("{name}" )), entry ("type" , asList ("{type}" )));
196+
197+ assertThat (md .indexToName ()).containsExactly (
198+ entry (0 , asList ("domainId" )),
199+ entry (1 , asList ("name" )),
200+ entry (2 , asList ("type" ))
201+ );
211202 }
212203
213204 interface FormParams {
@@ -218,18 +209,26 @@ void login(
218209 @ Named ("user_name" ) String user , @ Named ("password" ) String password );
219210 }
220211
212+ @ Test public void bodyWithTemplate () throws Exception {
213+ MethodMetadata md = contract .parseAndValidatateMetadata (FormParams .class .getDeclaredMethod ("login" , String .class ,
214+ String .class , String .class ));
215+
216+ assertThat (md .template ())
217+ .hasBodyTemplate ("%7B\" customer_name\" : \" {customer_name}\" , \" user_name\" : \" {user_name}\" , \" password\" : \" {password}\" %7D" );
218+ }
219+
221220 @ Test public void formParamsParseIntoIndexToName () throws Exception {
222221 MethodMetadata md = contract .parseAndValidatateMetadata (FormParams .class .getDeclaredMethod ("login" , String .class ,
223222 String .class , String .class ));
224223
225- assertFalse (md .template (). body () != null );
226- assertEquals (
227- "%7B \" customer_name \" : \" {customer_name} \" , \" user_name \" : \" {user_name} \" , \" password \" : \" {password} \" %7D" ,
228- md .template (). bodyTemplate ());
229- assertEquals ( ImmutableList . of ("customer_name" , "user_name" , "password" ), md . formParams ());
230- assertEquals ( Arrays . asList ("customer_name" ), md . indexToName (). get ( 0 ));
231- assertEquals ( Arrays . asList ("user_name" ), md . indexToName (). get ( 1 ));
232- assertEquals ( Arrays . asList ( "password" ), md . indexToName (). get ( 2 ) );
224+ assertThat (md .formParams ())
225+ . containsExactly ( "customer_name" , "user_name" , "password" );
226+
227+ assertThat ( md .indexToName ()). containsExactly (
228+ entry ( 0 , asList ("customer_name" )),
229+ entry ( 1 , asList ("user_name" )),
230+ entry ( 2 , asList ("password" ))
231+ );
233232 }
234233
235234 interface HeaderParams {
@@ -240,7 +239,9 @@ interface HeaderParams {
240239 @ Test public void headerParamsParseIntoIndexToName () throws Exception {
241240 MethodMetadata md = contract .parseAndValidatateMetadata (HeaderParams .class .getDeclaredMethod ("logout" , String .class ));
242241
243- assertEquals (Arrays .asList ("{Auth-Token}" ), md .template ().headers ().get ("Auth-Token" ));
244- assertEquals (Arrays .asList ("Auth-Token" ), md .indexToName ().get (0 ));
242+ assertThat (md .template ()).hasHeaders (entry ("Auth-Token" , asList ("{Auth-Token}" )));
243+
244+ assertThat (md .indexToName ())
245+ .containsExactly (entry (0 , asList ("Auth-Token" )));
245246 }
246247}
0 commit comments