11
11
12
12
import com .jayway .jsonpath .DocumentContext ;
13
13
import com .jayway .jsonpath .JsonPath ;
14
+ import io .restassured .RestAssured ;
15
+ import io .restassured .response .Response ;
16
+ import io .restassured .specification .RequestSpecification ;
14
17
import lombok .extern .slf4j .Slf4j ;
15
18
import net .minidev .json .JSONArray ;
16
19
import org .apache .http .HttpResponse ;
17
20
import org .apache .http .HttpStatus ;
18
21
import org .apache .http .client .methods .HttpGet ;
19
- import org .apache .http .client .methods .HttpPost ;
20
- import org .apache .http .entity .StringEntity ;
21
22
import org .apache .http .util .EntityUtils ;
22
- import org .junit .jupiter .api .BeforeEach ;
23
- import org .junit .jupiter .api .Disabled ;
24
- import org .junit .jupiter .api .Nested ;
25
- import org .junit .jupiter .api .Test ;
23
+ import org .junit .jupiter .api .*;
24
+ import org .springframework .http .MediaType ;
26
25
import org .zowe .apiml .util .TestWithStartedInstances ;
27
26
import org .zowe .apiml .util .categories .CatalogTest ;
28
- import org .zowe .apiml .util .config .ConfigReader ;
29
- import org .zowe .apiml .util .config .GatewayServiceConfiguration ;
30
- import org .zowe .apiml .util .http .HttpClientUtils ;
31
- import org .zowe .apiml .util .http .HttpRequestUtils ;
32
- import org .zowe .apiml .util .http .HttpSecurityUtils ;
27
+ import org .zowe .apiml .util .config .*;
28
+ import org .zowe .apiml .util .http .*;
33
29
34
- import java .io .File ;
35
30
import java .io .IOException ;
36
31
import java .net .URI ;
37
32
import java .util .LinkedHashMap ;
33
+ import java .util .UUID ;
38
34
35
+ import static io .restassured .RestAssured .given ;
39
36
import static org .hamcrest .MatcherAssert .assertThat ;
40
37
import static org .hamcrest .Matchers .equalTo ;
41
38
import static org .junit .jupiter .api .Assertions .*;
39
+ import static org .junit .jupiter .api .MethodOrderer .OrderAnnotation ;
40
+ import static org .junit .jupiter .api .TestInstance .Lifecycle .PER_CLASS ;
41
+ import static org .zowe .apiml .util .SecurityUtils .COOKIE_NAME ;
42
+ import static org .zowe .apiml .util .SecurityUtils .gatewayToken ;
43
+ import static org .zowe .apiml .util .http .HttpRequestUtils .getUriFromGateway ;
42
44
43
45
@ CatalogTest
44
46
@ Slf4j
@@ -48,8 +50,6 @@ class ApiCatalogEndpointIntegrationTest implements TestWithStartedInstances {
48
50
private static final String GET_CONTAINER_BY_INVALID_ID_ENDPOINT = "/apicatalog/api/v1/containers/bad" ;
49
51
private static final String GET_API_CATALOG_API_DOC_ENDPOINT = "/apicatalog/api/v1/apidoc/apicatalog/v1" ;
50
52
private static final String INVALID_API_CATALOG_API_DOC_ENDPOINT = "/apicatalog/api/v1/apidoc/apicatalog/v2" ;
51
- private static final String REFRESH_STATIC_APIS_ENDPOINT = "/apicatalog/api/v1/static-api/refresh" ;
52
- private static final String STATIC_DEFINITION_GENERATE_ENDPOINT = "/apicatalog/api/v1/static-api/generate" ;
53
53
54
54
private String baseHost ;
55
55
@@ -148,51 +148,54 @@ void whenInvalidApiDocVersion_thenReturnFirstDoc() throws Exception {
148
148
}
149
149
150
150
@ Nested
151
+ @ TestMethodOrder (OrderAnnotation .class )
152
+ @ TestInstance (PER_CLASS )
151
153
class StaticApis {
152
- // Functional
153
- @ Test
154
- @ Disabled
155
- void whenCallStaticApiRefresh_thenResponseOk () throws IOException {
156
- final HttpResponse response = getStaticApiResponse (REFRESH_STATIC_APIS_ENDPOINT , HttpStatus .SC_OK , null );
157
-
158
- // When
159
- final String jsonResponse = EntityUtils .toString (response .getEntity ());
160
154
161
- JSONArray errors = JsonPath .parse (jsonResponse ).read ("$.errors" );
155
+ private static final String STATIC_DEFINITION_GENERATE_ENDPOINT = "/apicatalog/api/v1/static-api/generate" ;
156
+ private static final String STATIC_DEFINITION_DELETE_ENDPOINT = "/apicatalog/api/v1/static-api/delete" ;
157
+ private static final String REFRESH_STATIC_APIS_ENDPOINT = "/apicatalog/api/v1/static-api/refresh" ;
158
+ private String staticDefinitionServiceId = "a" + UUID .randomUUID ().toString ().replace ("-" , "" ).substring (0 , 10 );
159
+
160
+ @ AfterAll
161
+ void cleanupStaticDefinition () {
162
+ given ().relaxedHTTPSValidation ()
163
+ .when ()
164
+ .header ("Service-Id" , staticDefinitionServiceId )
165
+ .cookie (COOKIE_NAME , gatewayToken ())
166
+ .delete (getUriFromGateway (STATIC_DEFINITION_DELETE_ENDPOINT ));
167
+ }
162
168
163
- assertEquals ("[]" , errors .toString ());
169
+ @ Test
170
+ @ Order (1 )
171
+ void whenCallStaticApiRefresh_thenResponseOk () throws IOException {
172
+ getStaticApiResponse (REFRESH_STATIC_APIS_ENDPOINT , null , HttpStatus .SC_OK , null );
164
173
}
165
174
166
175
@ Test
167
- @ Disabled
176
+ @ Order ( 30 )
168
177
void whenCallStaticDefinitionGenerate_thenResponse201 () throws IOException {
169
- String location ;
170
- if (System .getenv ("APIML_DISCOVERY_STATICAPIDEFINITIONSDIRECTORIES" ) == null ) {
171
- location = "config/local/api-defs" ;
172
- } else {
173
- location = System .getenv ("APIML_DISCOVERY_STATICAPIDEFINITIONSDIRECTORIES" );
174
- }
175
- log .info ("apiml.discovery.staticApiDefinitionsDirectories: " + location );
176
-
177
- String json = "services:\n - serviceId: test-service-1\\ n title: \n description: \n instanceBaseUrls:\n description: \n " ;
178
-
179
- final HttpResponse response = getStaticApiResponse (STATIC_DEFINITION_GENERATE_ENDPOINT , HttpStatus .SC_CREATED , json );
180
-
181
- // When
182
- final String jsonResponse = EntityUtils .toString (response .getEntity ());
183
-
184
- assertEquals ("The static definition file has been created by the user! Its location is: .//usr/local/etc/config/api-defs/test-service-1.yml" , jsonResponse );
185
-
186
- File staticDef = new File ("../" + location + "/test-service-1.yml" );
178
+ String json = "# Dummy content" ;
179
+ getStaticApiResponse (STATIC_DEFINITION_GENERATE_ENDPOINT , staticDefinitionServiceId ,HttpStatus .SC_CREATED , json );
180
+ }
187
181
188
- // Delete the created test file
189
- if (staticDef .delete ()) {
190
- log .info (staticDef .getName () + " has been deleted" );
191
- } else {
192
- log .info ("Failed to delete the file!" );
182
+ private Response getStaticApiResponse (String endpoint , String definitionFileName , int returnCode , String body ) throws IOException {
183
+ URI uri = getUriFromGateway (endpoint );
184
+ RestAssured .enableLoggingOfRequestAndResponseIfValidationFails ();
185
+
186
+ RequestSpecification requestSpecification = given ().config (SslContext .tlsWithoutCert ).relaxedHTTPSValidation ()
187
+ .when ()
188
+ .cookie (COOKIE_NAME , gatewayToken ())
189
+ .header ("Accept" , MediaType .APPLICATION_JSON_VALUE );
190
+ if (body != null ) {
191
+ requestSpecification
192
+ .header ("Service-Id" , definitionFileName )
193
+ .body (body );
193
194
}
194
- }
195
195
196
+ return requestSpecification .post (uri ).then ()
197
+ .statusCode (returnCode ).extract ().response ();
198
+ }
196
199
}
197
200
198
201
// Execute the endpoint and check the response for a return code
@@ -201,35 +204,11 @@ private HttpResponse getResponse(String endpoint, int returnCode) throws IOExcep
201
204
String cookie = HttpSecurityUtils .getCookieForGateway ();
202
205
HttpSecurityUtils .addCookie (request , cookie );
203
206
204
- // When
205
207
HttpResponse response = HttpClientUtils .client ().execute (request );
206
-
207
- // Then
208
208
assertThat (response .getStatusLine ().getStatusCode (), equalTo (returnCode ));
209
209
210
210
return response ;
211
211
}
212
212
213
- // Execute the static apis endpoints and check the response for a return code
214
- private HttpResponse getStaticApiResponse (String endpoint , int returnCode , String body ) throws IOException {
215
- URI uri = HttpRequestUtils .getUriFromGateway (endpoint );
216
- HttpPost request = new HttpPost (uri );
217
- request .addHeader ("Accept" , "application/json" );
218
- if (body != null ) {
219
- request .addHeader ("Service-Id" , "test-service-1" );
220
- StringEntity entity = new StringEntity (body );
221
- request .setEntity (entity );
222
- }
223
- String cookie = HttpSecurityUtils .getCookieForGateway ();
224
- HttpSecurityUtils .addCookie (request , cookie );
225
213
226
- // When
227
- HttpResponse response = HttpClientUtils .client ().execute (request );
228
-
229
- // Then
230
- assertThat (response .getStatusLine ().getStatusCode (), equalTo (returnCode ));
231
-
232
-
233
- return response ;
234
- }
235
214
}
0 commit comments