9
9
*/
10
10
package org .zowe .apiml .apicatalog .staticapi ;
11
11
12
+ import org .junit .jupiter .api .Nested ;
12
13
import org .junit .jupiter .api .Test ;
13
14
import org .junit .jupiter .api .extension .ExtendWith ;
14
15
import org .mockito .InjectMocks ;
@@ -28,6 +29,14 @@ class StaticAPIServiceTest {
28
29
29
30
private static final String REFRESH_ENDPOINT = "discovery/api/v1/staticApi" ;
30
31
32
+ private static final String DISCOVERY_LOCATION = "https://localhost:60004/eureka/" ;
33
+ private static final String DISCOVERY_LOCATION_2 = "https://localhost:60005/eureka/" ;
34
+ private static final String DISCOVERY_URL = "https://localhost:60004/" ;
35
+ private static final String DISCOVERY_URL_2 = "https://localhost:60005/" ;
36
+
37
+ private static final String DISCOVERY_LOCATION_HTTP = "http://localhost:60004/eureka/" ;
38
+ private static final String DISCOVERY_URL_HTTP = "http://localhost:60004/" ;
39
+
31
40
@ InjectMocks
32
41
private StaticAPIService staticAPIService ;
33
42
@@ -36,43 +45,94 @@ class StaticAPIServiceTest {
36
45
@ Mock
37
46
private DiscoveryConfigProperties discoveryConfigProperties ;
38
47
39
- @ Test
40
- void givenRefreshAPIWithSecureDiscoveryService_whenRefreshEndpointPresentResponse_thenReturnApiResponseCodeWithBody () {
41
- String discoveryUrl = "https://localhost:60004/" ;
42
- when (discoveryConfigProperties .getLocations ()).thenReturn ("https://localhost:60004/eureka/" );
48
+ @ Nested
49
+ class WhenRefreshEndpointPresentsResponse {
43
50
44
- when (restTemplate .exchange (
45
- discoveryUrl + REFRESH_ENDPOINT ,
46
- HttpMethod .POST , getHttpEntity (discoveryUrl ), String .class ))
47
- .thenReturn (new ResponseEntity <>("This is body" , HttpStatus .OK ));
51
+ @ Test
52
+ void givenRefreshAPIWithSecureDiscoveryService_thenReturnApiResponseCodeWithBody () {
53
+ when (discoveryConfigProperties .getLocations ()).thenReturn (new String []{DISCOVERY_LOCATION });
48
54
55
+ mockRestTemplateExchange (DISCOVERY_URL , new ResponseEntity <>("This is body" , HttpStatus .OK ));
49
56
50
- StaticAPIResponse actualResponse = staticAPIService .refresh ();
51
- StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "This is body" );
52
- assertEquals (expectedResponse , actualResponse );
57
+ StaticAPIResponse actualResponse = staticAPIService .refresh ();
58
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "This is body" );
59
+ assertEquals (expectedResponse , actualResponse );
60
+ }
61
+
62
+ @ Test
63
+ void givenRefreshAPIWithUnSecureDiscoveryService_thenReturnApiResponseCodeWithBody () {
64
+ when (discoveryConfigProperties .getLocations ()).thenReturn (new String []{DISCOVERY_LOCATION_HTTP });
65
+
66
+ mockRestTemplateExchange (DISCOVERY_URL_HTTP , new ResponseEntity <>("This is body" , HttpStatus .OK ));
67
+
68
+ StaticAPIResponse actualResponse = staticAPIService .refresh ();
69
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "This is body" );
70
+ assertEquals (expectedResponse , actualResponse );
71
+ }
72
+
73
+ @ Nested
74
+ class GivenTwoDiscoveryUrls {
75
+ private final String [] discoveryLocations = {DISCOVERY_LOCATION , DISCOVERY_LOCATION_2 };
76
+
77
+ @ Test
78
+ void whenFirstFails_thenReturnResponseFromSecond () {
79
+ when (discoveryConfigProperties .getLocations ()).thenReturn (discoveryLocations );
80
+
81
+ mockRestTemplateExchange (DISCOVERY_URL , new ResponseEntity <>(HttpStatus .NOT_FOUND ));
82
+ mockRestTemplateExchange (DISCOVERY_URL_2 , new ResponseEntity <>("body" , HttpStatus .OK ));
83
+
84
+ StaticAPIResponse actualResponse = staticAPIService .refresh ();
85
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "body" );
86
+ assertEquals (expectedResponse , actualResponse );
87
+ }
88
+
89
+ @ Test
90
+ void whenFirstSucceeds_thenReturnResponseFromFirst () {
91
+ when (discoveryConfigProperties .getLocations ()).thenReturn (discoveryLocations );
92
+
93
+ mockRestTemplateExchange (DISCOVERY_URL , new ResponseEntity <>("body" , HttpStatus .OK ));
94
+
95
+ StaticAPIResponse actualResponse = staticAPIService .refresh ();
96
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "body" );
97
+ assertEquals (expectedResponse , actualResponse );
98
+ }
99
+
100
+ @ Test
101
+ void whenBothFail_thenReturnResponseFromSecond () {
102
+ when (discoveryConfigProperties .getLocations ()).thenReturn (discoveryLocations );
103
+
104
+ mockRestTemplateExchange (DISCOVERY_URL , new ResponseEntity <>(HttpStatus .INTERNAL_SERVER_ERROR ));
105
+ mockRestTemplateExchange (DISCOVERY_URL_2 , new ResponseEntity <>("body" , HttpStatus .NOT_FOUND ));
106
+
107
+ StaticAPIResponse actualResponse = staticAPIService .refresh ();
108
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (404 , "body" );
109
+ assertEquals (expectedResponse , actualResponse );
110
+ }
111
+ }
53
112
}
54
113
55
114
@ Test
56
- void givenRefreshAPIWithUnSecureDiscoveryService_whenRefreshEndpointPresentResponse_thenReturnApiResponseCodeWithBody () {
57
- String discoveryUrl = "http://localhost:60004/" ;
58
- when (discoveryConfigProperties .getLocations ()).thenReturn ("http://localhost:60004/eureka/" );
59
-
60
- when (restTemplate .exchange (
61
- discoveryUrl + REFRESH_ENDPOINT ,
62
- HttpMethod .POST , getHttpEntity (discoveryUrl ), String .class ))
63
- .thenReturn (new ResponseEntity <>("This is body" , HttpStatus .OK ));
115
+ void givenNoDiscoveryLocations_whenAttemptRefresh_thenReturn500 () {
116
+ when (discoveryConfigProperties .getLocations ()).thenReturn (new String []{});
64
117
65
118
StaticAPIResponse actualResponse = staticAPIService .refresh ();
66
- StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "This is body " );
119
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (500 , "Error making static API refresh request to the Discovery Service " );
67
120
assertEquals (expectedResponse , actualResponse );
68
121
}
69
122
123
+ private void mockRestTemplateExchange (String discoveryUrl , ResponseEntity expectedResponse ) {
124
+ when (restTemplate .exchange (
125
+ discoveryUrl + REFRESH_ENDPOINT ,
126
+ HttpMethod .POST , getHttpEntity (discoveryUrl ), String .class
127
+ )).thenReturn (expectedResponse );
128
+ }
129
+
70
130
private HttpEntity <?> getHttpEntity (String discoveryServiceUrl ) {
71
131
boolean isHttp = discoveryServiceUrl .startsWith ("http://" );
72
132
HttpHeaders httpHeaders = new HttpHeaders ();
73
133
httpHeaders .add ("Accept" , "application/json" );
74
134
if (isHttp ) {
75
- String basicToken = "Basic " + Base64 .getEncoder ().encodeToString ( "null:null" .getBytes ());
135
+ String basicToken = "Basic " + Base64 .getEncoder ().encodeToString ("null:null" .getBytes ());
76
136
httpHeaders .add ("Authorization" , basicToken );
77
137
}
78
138
0 commit comments