1616import org .powermock .api .support .membermodification .MemberMatcher ;
1717import org .powermock .core .classloader .annotations .PrepareForTest ;
1818import org .powermock .modules .testng .PowerMockTestCase ;
19+ import org .testng .Assert ;
1920import org .testng .annotations .Test ;
2021
2122import static org .mockito .Matchers .any ;
4041@ PrepareForTest ({RestClientConfiguration .class , Properties .class , X509TrustManager .class })
4142public class RestClientConfigurationTest extends PowerMockTestCase {
4243
43- private Properties fakeProps = new Properties () {{
44- setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
45- setProperty ("contentMimeType" , "JSON" );
46- setProperty ("acceptMimeType" , "JSON" );
47- setProperty ("connectionTimeout" , "" );
48- setProperty ("readTimeout" , "" );
49- setProperty ("authenticationType" , "SPRING" );
50- setProperty ("restrictedHttpMethods" , "false" );
51- setProperty ("logHttpEntity" , "false" );
52- setProperty ("logHttp" , "false" );
53- }};
54-
5544 private Properties fakePropsWithUnproperAcceptMimeType = new Properties () {{
5645 setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
5746 setProperty ("contentMimeType" , "XML" );
@@ -69,7 +58,7 @@ public class RestClientConfigurationTest extends PowerMockTestCase {
6958 }};
7059
7160 @ Test (testName = "RestClientConfiguration_parametrized_constructor" )
72- public void should_invoke_default_constructor_and_pass_valid_URL_to_set_JasperReportsServerUrl_method () throws Exception {
61+ public void should_invoke_default_constructor_and_pass_valid_URL_to_set_JasperReportsServerUrl_method () {
7362 String fakedValidUrl = "http://localhost:8080/jasperserver-pro/" ;
7463 RestClientConfiguration config = new RestClientConfiguration (fakedValidUrl );
7564 assertNotNull (config .getTrustManagers ());
@@ -97,8 +86,18 @@ public void should_create_an_instance_of_RestClientConfiguration_but_without_set
9786
9887 @ Test (testName = "loadConfiguration" )
9988 public void should_load_configuration_from_property_file () throws Exception {
100-
10189 // Given
90+ Properties fakeProps = new Properties () {{
91+ setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
92+ setProperty ("contentMimeType" , "JSON" );
93+ setProperty ("acceptMimeType" , "JSON" );
94+ setProperty ("connectionTimeout" , "" );
95+ setProperty ("readTimeout" , "" );
96+ setProperty ("authenticationType" , "SPRING" );
97+ setProperty ("restrictedHttpMethods" , "false" );
98+ setProperty ("logHttpEntity" , "false" );
99+ setProperty ("logHttp" , "false" );
100+ }};
102101 mockStaticPartial (RestClientConfiguration .class , "loadProperties" );
103102 Method [] methods = MemberMatcher .methods (RestClientConfiguration .class , "loadProperties" );
104103
@@ -126,7 +125,8 @@ public void should_load_configuration_from_property_file() throws Exception {
126125 @ Test (testName = "loadConfiguration" )
127126 public void should_load_configuration_from_property_file_with_all_kind_of_setted_values () throws Exception {
128127 // Given
129- Properties fakePropsWithNotEmptyConnectionTimeoutAndReadTimeout = new Properties () {{
128+ // fake properties with not empty connection timeout and read timeout
129+ Properties fakeProps = new Properties () {{
130130 setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
131131 setProperty ("contentMimeType" , "JSON" );
132132 setProperty ("acceptMimeType" , "JSON" );
@@ -140,7 +140,7 @@ public void should_load_configuration_from_property_file_with_all_kind_of_setted
140140 mockStaticPartial (RestClientConfiguration .class , "loadProperties" );
141141 Method [] methods = MemberMatcher .methods (RestClientConfiguration .class , "loadProperties" );
142142
143- expectPrivate (RestClientConfiguration .class , methods [0 ], "superCoolPath" ).andReturn (fakePropsWithNotEmptyConnectionTimeoutAndReadTimeout );
143+ expectPrivate (RestClientConfiguration .class , methods [0 ], "superCoolPath" ).andReturn (fakeProps );
144144 replay (RestClientConfiguration .class );
145145
146146 // When
@@ -150,8 +150,8 @@ public void should_load_configuration_from_property_file_with_all_kind_of_setted
150150 assertEquals (configuration .getJasperReportsServerUrl (), fakeProps .getProperty ("url" ));
151151 assertEquals (configuration .getContentMimeType ().toString (), fakeProps .getProperty ("contentMimeType" ));
152152 assertEquals (configuration .getAcceptMimeType ().toString (), fakeProps .getProperty ("acceptMimeType" ));
153- assertSame (configuration .getConnectionTimeout (), Integer .valueOf (fakePropsWithNotEmptyConnectionTimeoutAndReadTimeout .getProperty ("connectionTimeout" ))); // should be 20
154- assertSame (configuration .getReadTimeout (), Integer .valueOf (fakePropsWithNotEmptyConnectionTimeoutAndReadTimeout .getProperty ("readTimeout" ))); // should be 100
153+ assertSame (configuration .getConnectionTimeout (), Integer .valueOf (fakeProps .getProperty ("connectionTimeout" ))); // should be 20
154+ assertSame (configuration .getReadTimeout (), Integer .valueOf (fakeProps .getProperty ("readTimeout" ))); // should be 100
155155
156156 assertEquals (configuration .getAuthenticationType ().toString (), fakeProps .getProperty ("authenticationType" ));
157157 assertEquals (configuration .getRestrictedHttpMethods ().toString (), fakeProps .getProperty ("restrictedHttpMethods" ));
@@ -186,6 +186,7 @@ public void should_load_configuration_from_property_file_with_unsupported_conten
186186 assertNull (configuration .getReadTimeout ());
187187 assertNotNull (configuration .getTrustManagers ());
188188 }
189+
189190 @ Test (testName = "loadConfiguration" )
190191 public void should_load_configuration_from_property_file_with_unsupported_acceptMimeType () throws Exception {
191192 // Given
@@ -304,19 +305,15 @@ public void should_return_trusted_manager() throws Exception {
304305 }
305306
306307 @ Test
307- public void should_throw_an_exception_while_loading_props_from_wrong_path () throws Exception {
308- try {
309- RestClientConfiguration .loadConfiguration ("path" );
310- } catch (Exception e ) {
311- assertNotNull (e );
312- assertTrue (e instanceof Exception );
313- assertEquals (e .getMessage (), null );
314- assertEquals (e .getCause (), null );
315- }
308+ public void should_return_empty_configuration () {
309+ // When
310+ RestClientConfiguration configuration = RestClientConfiguration .loadConfiguration ("path" );
311+ //Then
312+ assertEquals (configuration .getJasperReportsServerUrl (), null );
316313 }
317314
318315 @ Test
319- public void should_throw_an_exception_with_wrong_contentMimetype () throws Exception {
316+ public void should_not_throw_an_exception_with_wrong_contentMimetype () throws Exception {
320317 // Given
321318 Properties fakePropsWithUnproperContentMimeType = new Properties () {{
322319 setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
@@ -330,19 +327,13 @@ public void should_throw_an_exception_with_wrong_contentMimetype() throws Except
330327 replay (RestClientConfiguration .class );
331328
332329 // When
333-
334- try {
335- RestClientConfiguration .loadConfiguration ("superCoolPath" );
336- } catch (Exception e ) {
337- assertNotNull (e );
338- assertTrue (e instanceof Exception );
339- assertEquals (e .getMessage (), null );
340- assertEquals (e .getCause (), null );
341- }
330+ RestClientConfiguration configuration = RestClientConfiguration .loadConfiguration ("superCoolPath" );
331+ // Then
332+ Assert .assertEquals (configuration .getContentMimeType (), MimeType .JSON );
342333 }
343334
344335 @ Test
345- public void should_throw_an_exception_with_wrong_acceptMimetype () throws Exception {
336+ public void should_not_throw_an_exception_with_wrong_acceptMimetype () throws Exception {
346337 // Given
347338 Properties fakePropsWithUnproperAcceptMimeType = new Properties () {{
348339 setProperty ("url" , "http://localhost:8080/jasperserver-pro/" );
@@ -356,15 +347,9 @@ public void should_throw_an_exception_with_wrong_acceptMimetype() throws Excepti
356347 replay (RestClientConfiguration .class );
357348
358349 // When
359-
360- try {
361- RestClientConfiguration .loadConfiguration ("superCoolPath" );
362- } catch (Exception e ) {
363- assertNotNull (e );
364- assertTrue (e instanceof Exception );
365- assertEquals (e .getMessage (), null );
366- assertEquals (e .getCause (), null );
367- }
350+ RestClientConfiguration configuration = RestClientConfiguration .loadConfiguration ("superCoolPath" );
351+ // Then
352+ Assert .assertEquals (configuration .getAcceptMimeType (), MimeType .JSON );
368353 }
369354
370355 @ Test
@@ -381,15 +366,9 @@ public void should_throw_an_exception_with_wrong_jrs_version() throws Exception
381366 replay (RestClientConfiguration .class );
382367
383368 // When
384-
385- try {
386- RestClientConfiguration .loadConfiguration ("superCoolPath" );
387- } catch (Exception e ) {
388- assertNotNull (e );
389- assertTrue (e instanceof Exception );
390- assertEquals (e .getMessage (), null );
391- assertEquals (e .getCause (), null );
392- }
369+ RestClientConfiguration configuration = RestClientConfiguration .loadConfiguration ("superCoolPath" );
370+ // Then
371+ Assert .assertEquals (configuration .getJrsVersion (), JRSVersion .v5_5_0 );
393372 }
394373
395374 @ Test
@@ -406,15 +385,10 @@ public void should_throw_an_exception_with_wrong_authintication_type() throws Ex
406385 replay (RestClientConfiguration .class );
407386
408387 // When
388+ RestClientConfiguration configuration = RestClientConfiguration .loadConfiguration ("superCoolPath" );
409389
410- try {
411- RestClientConfiguration .loadConfiguration ("superCoolPath" );
412- } catch (Exception e ) {
413- assertNotNull (e );
414- assertTrue (e instanceof Exception );
415- assertEquals (e .getMessage (), null );
416- assertEquals (e .getCause (), null );
417- }
390+ // Then
391+ Assert .assertEquals (configuration .getAuthenticationType (), AuthenticationType .SPRING );
418392 }
419393
420394 @ Test
@@ -592,4 +566,4 @@ public void should_set_readTimeout_field() throws IllegalAccessException {
592566
593567
594568 }
595- }
569+ }
0 commit comments