Skip to content

Commit b6790cb

Browse files
authored
fix: static generation (#1761)
Signed-off-by: jandadav <janda.david@gmail.com>
1 parent e4c22dc commit b6790cb

File tree

3 files changed

+65
-80
lines changed

3 files changed

+65
-80
lines changed

api-catalog-package/src/main/resources/bin/start.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ then
5757
LOG_LEVEL=${APIML_DIAG_MODE_ENABLED}
5858
fi
5959

60+
# If set append $ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES to $STATIC_DEF_CONFIG_DIR
61+
export APIML_STATIC_DEF=${STATIC_DEF_CONFIG_DIR}
62+
if [[ ! -z "$ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES" ]]
63+
then
64+
export APIML_STATIC_DEF="${APIML_STATIC_DEF};${ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES}"
65+
fi
66+
6067
EXPLORER_HOST=${ZOWE_EXPLORER_HOST:-localhost}
6168

6269
if [[ -z "${GATEWAY_HOST}" ]]
@@ -94,6 +101,7 @@ _BPX_JOBNAME=${ZOWE_PREFIX}${CATALOG_CODE} java \
94101
-Dapiml.service.eurekaUserId=eureka \
95102
-Dapiml.service.eurekaPassword=password \
96103
-Dapiml.logs.location=${WORKSPACE_DIR}/api-mediation/logs \
104+
-Dapiml.discovery.staticApiDefinitionsDirectories=${APIML_STATIC_DEF} \
97105
-Dapiml.security.ssl.verifySslCertificatesOfServices=${VERIFY_CERTIFICATES:-false} \
98106
-Dapiml.security.ssl.nonStrictVerifySslCertificatesOfServices=${NONSTRICT_VERIFY_CERTIFICATES:-false} \
99107
-Dspring.profiles.include=$LOG_LEVEL \

api-catalog-services/src/main/java/org/zowe/apiml/apicatalog/staticapi/StaticDefinitionGenerator.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import org.springframework.beans.factory.annotation.Value;
1616
import org.springframework.stereotype.Service;
1717

18-
import java.io.File;
19-
import java.io.FileOutputStream;
20-
import java.io.IOException;
18+
import java.io.*;
2119
import java.nio.charset.StandardCharsets;
2220
import java.nio.file.FileAlreadyExistsException;
2321
import java.nio.file.Files;
@@ -84,14 +82,14 @@ private StaticAPIResponse getInvalidResponse(String serviceId) {
8482
return new StaticAPIResponse(400, "The service ID format is not valid.");
8583
}
8684

87-
private String formatFile(String file) {
88-
file = file.replace("\\n", System.lineSeparator());
89-
file = file.substring(1, file.length() - 1);
85+
private String normalizeToUnixLineEndings(String file) {
86+
file = file.replaceAll("\\r\\n", "\n");
87+
file = file.replaceAll("\\r", "\n");
9088
return file;
9189
}
9290

9391
private StaticAPIResponse writeContentToFile(String fileContent, String fileName, String message) throws IOException {
94-
fileContent = formatFile(fileContent);
92+
fileContent = normalizeToUnixLineEndings(fileContent);
9593
try (FileOutputStream fos = new FileOutputStream(fileName)) {
9694
fos.write(fileContent.getBytes(StandardCharsets.UTF_8));
9795
log.debug(message);

integration-tests/src/test/java/org/zowe/apiml/functional/apicatalog/ApiCatalogEndpointIntegrationTest.java

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,36 @@
1111

1212
import com.jayway.jsonpath.DocumentContext;
1313
import com.jayway.jsonpath.JsonPath;
14+
import io.restassured.RestAssured;
15+
import io.restassured.response.Response;
16+
import io.restassured.specification.RequestSpecification;
1417
import lombok.extern.slf4j.Slf4j;
1518
import net.minidev.json.JSONArray;
1619
import org.apache.http.HttpResponse;
1720
import org.apache.http.HttpStatus;
1821
import org.apache.http.client.methods.HttpGet;
19-
import org.apache.http.client.methods.HttpPost;
20-
import org.apache.http.entity.StringEntity;
2122
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;
2625
import org.zowe.apiml.util.TestWithStartedInstances;
2726
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.*;
3329

34-
import java.io.File;
3530
import java.io.IOException;
3631
import java.net.URI;
3732
import java.util.LinkedHashMap;
33+
import java.util.UUID;
3834

35+
import static io.restassured.RestAssured.given;
3936
import static org.hamcrest.MatcherAssert.assertThat;
4037
import static org.hamcrest.Matchers.equalTo;
4138
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;
4244

4345
@CatalogTest
4446
@Slf4j
@@ -48,8 +50,6 @@ class ApiCatalogEndpointIntegrationTest implements TestWithStartedInstances {
4850
private static final String GET_CONTAINER_BY_INVALID_ID_ENDPOINT = "/apicatalog/api/v1/containers/bad";
4951
private static final String GET_API_CATALOG_API_DOC_ENDPOINT = "/apicatalog/api/v1/apidoc/apicatalog/v1";
5052
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";
5353

5454
private String baseHost;
5555

@@ -148,51 +148,54 @@ void whenInvalidApiDocVersion_thenReturnFirstDoc() throws Exception {
148148
}
149149

150150
@Nested
151+
@TestMethodOrder(OrderAnnotation.class)
152+
@TestInstance(PER_CLASS)
151153
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());
160154

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+
}
162168

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);
164173
}
165174

166175
@Test
167-
@Disabled
176+
@Order(30)
168177
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+
}
187181

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);
193194
}
194-
}
195195

196+
return requestSpecification.post(uri).then()
197+
.statusCode(returnCode).extract().response();
198+
}
196199
}
197200

198201
// Execute the endpoint and check the response for a return code
@@ -201,35 +204,11 @@ private HttpResponse getResponse(String endpoint, int returnCode) throws IOExcep
201204
String cookie = HttpSecurityUtils.getCookieForGateway();
202205
HttpSecurityUtils.addCookie(request, cookie);
203206

204-
// When
205207
HttpResponse response = HttpClientUtils.client().execute(request);
206-
207-
// Then
208208
assertThat(response.getStatusLine().getStatusCode(), equalTo(returnCode));
209209

210210
return response;
211211
}
212212

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);
225213

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-
}
235214
}

0 commit comments

Comments
 (0)