Skip to content

Commit

Permalink
fix: static generation (#1761)
Browse files Browse the repository at this point in the history
Signed-off-by: jandadav <janda.david@gmail.com>
  • Loading branch information
jandadav committed Sep 7, 2021
1 parent e4c22dc commit b6790cb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 80 deletions.
8 changes: 8 additions & 0 deletions api-catalog-package/src/main/resources/bin/start.sh
Expand Up @@ -57,6 +57,13 @@ then
LOG_LEVEL=${APIML_DIAG_MODE_ENABLED}
fi

# If set append $ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES to $STATIC_DEF_CONFIG_DIR
export APIML_STATIC_DEF=${STATIC_DEF_CONFIG_DIR}
if [[ ! -z "$ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES" ]]
then
export APIML_STATIC_DEF="${APIML_STATIC_DEF};${ZWEAD_EXTERNAL_STATIC_DEF_DIRECTORIES}"
fi

EXPLORER_HOST=${ZOWE_EXPLORER_HOST:-localhost}

if [[ -z "${GATEWAY_HOST}" ]]
Expand Down Expand Up @@ -94,6 +101,7 @@ _BPX_JOBNAME=${ZOWE_PREFIX}${CATALOG_CODE} java \
-Dapiml.service.eurekaUserId=eureka \
-Dapiml.service.eurekaPassword=password \
-Dapiml.logs.location=${WORKSPACE_DIR}/api-mediation/logs \
-Dapiml.discovery.staticApiDefinitionsDirectories=${APIML_STATIC_DEF} \
-Dapiml.security.ssl.verifySslCertificatesOfServices=${VERIFY_CERTIFICATES:-false} \
-Dapiml.security.ssl.nonStrictVerifySslCertificatesOfServices=${NONSTRICT_VERIFY_CERTIFICATES:-false} \
-Dspring.profiles.include=$LOG_LEVEL \
Expand Down
Expand Up @@ -15,9 +15,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand Down Expand Up @@ -84,14 +82,14 @@ private StaticAPIResponse getInvalidResponse(String serviceId) {
return new StaticAPIResponse(400, "The service ID format is not valid.");
}

private String formatFile(String file) {
file = file.replace("\\n", System.lineSeparator());
file = file.substring(1, file.length() - 1);
private String normalizeToUnixLineEndings(String file) {
file = file.replaceAll("\\r\\n", "\n");
file = file.replaceAll("\\r", "\n");
return file;
}

private StaticAPIResponse writeContentToFile(String fileContent, String fileName, String message) throws IOException {
fileContent = formatFile(fileContent);
fileContent = normalizeToUnixLineEndings(fileContent);
try (FileOutputStream fos = new FileOutputStream(fileName)) {
fos.write(fileContent.getBytes(StandardCharsets.UTF_8));
log.debug(message);
Expand Down
Expand Up @@ -11,34 +11,36 @@

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.springframework.http.MediaType;
import org.zowe.apiml.util.TestWithStartedInstances;
import org.zowe.apiml.util.categories.CatalogTest;
import org.zowe.apiml.util.config.ConfigReader;
import org.zowe.apiml.util.config.GatewayServiceConfiguration;
import org.zowe.apiml.util.http.HttpClientUtils;
import org.zowe.apiml.util.http.HttpRequestUtils;
import org.zowe.apiml.util.http.HttpSecurityUtils;
import org.zowe.apiml.util.config.*;
import org.zowe.apiml.util.http.*;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.zowe.apiml.util.SecurityUtils.COOKIE_NAME;
import static org.zowe.apiml.util.SecurityUtils.gatewayToken;
import static org.zowe.apiml.util.http.HttpRequestUtils.getUriFromGateway;

@CatalogTest
@Slf4j
Expand All @@ -48,8 +50,6 @@ class ApiCatalogEndpointIntegrationTest implements TestWithStartedInstances {
private static final String GET_CONTAINER_BY_INVALID_ID_ENDPOINT = "/apicatalog/api/v1/containers/bad";
private static final String GET_API_CATALOG_API_DOC_ENDPOINT = "/apicatalog/api/v1/apidoc/apicatalog/v1";
private static final String INVALID_API_CATALOG_API_DOC_ENDPOINT = "/apicatalog/api/v1/apidoc/apicatalog/v2";
private static final String REFRESH_STATIC_APIS_ENDPOINT = "/apicatalog/api/v1/static-api/refresh";
private static final String STATIC_DEFINITION_GENERATE_ENDPOINT = "/apicatalog/api/v1/static-api/generate";

private String baseHost;

Expand Down Expand Up @@ -148,51 +148,54 @@ void whenInvalidApiDocVersion_thenReturnFirstDoc() throws Exception {
}

@Nested
@TestMethodOrder(OrderAnnotation.class)
@TestInstance(PER_CLASS)
class StaticApis {
// Functional
@Test
@Disabled
void whenCallStaticApiRefresh_thenResponseOk() throws IOException {
final HttpResponse response = getStaticApiResponse(REFRESH_STATIC_APIS_ENDPOINT, HttpStatus.SC_OK, null);

// When
final String jsonResponse = EntityUtils.toString(response.getEntity());

JSONArray errors = JsonPath.parse(jsonResponse).read("$.errors");
private static final String STATIC_DEFINITION_GENERATE_ENDPOINT = "/apicatalog/api/v1/static-api/generate";
private static final String STATIC_DEFINITION_DELETE_ENDPOINT = "/apicatalog/api/v1/static-api/delete";
private static final String REFRESH_STATIC_APIS_ENDPOINT = "/apicatalog/api/v1/static-api/refresh";
private String staticDefinitionServiceId = "a" + UUID.randomUUID().toString().replace("-", "").substring(0, 10);

@AfterAll
void cleanupStaticDefinition() {
given().relaxedHTTPSValidation()
.when()
.header("Service-Id", staticDefinitionServiceId)
.cookie(COOKIE_NAME, gatewayToken())
.delete(getUriFromGateway(STATIC_DEFINITION_DELETE_ENDPOINT));
}

assertEquals("[]", errors.toString());
@Test
@Order(1)
void whenCallStaticApiRefresh_thenResponseOk() throws IOException {
getStaticApiResponse(REFRESH_STATIC_APIS_ENDPOINT, null, HttpStatus.SC_OK, null);
}

@Test
@Disabled
@Order(30)
void whenCallStaticDefinitionGenerate_thenResponse201() throws IOException {
String location;
if (System.getenv("APIML_DISCOVERY_STATICAPIDEFINITIONSDIRECTORIES") == null) {
location = "config/local/api-defs";
} else {
location = System.getenv("APIML_DISCOVERY_STATICAPIDEFINITIONSDIRECTORIES");
}
log.info("apiml.discovery.staticApiDefinitionsDirectories: " + location);

String json = "services:\n - serviceId: test-service-1\\n title: \n description: \n instanceBaseUrls:\n description: \n";

final HttpResponse response = getStaticApiResponse(STATIC_DEFINITION_GENERATE_ENDPOINT, HttpStatus.SC_CREATED, json);

// When
final String jsonResponse = EntityUtils.toString(response.getEntity());

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

File staticDef = new File("../" + location + "/test-service-1.yml");
String json = "# Dummy content";
getStaticApiResponse(STATIC_DEFINITION_GENERATE_ENDPOINT, staticDefinitionServiceId ,HttpStatus.SC_CREATED, json);
}

// Delete the created test file
if (staticDef.delete()) {
log.info(staticDef.getName() + " has been deleted");
} else {
log.info("Failed to delete the file!");
private Response getStaticApiResponse(String endpoint, String definitionFileName, int returnCode, String body) throws IOException {
URI uri = getUriFromGateway(endpoint);
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

RequestSpecification requestSpecification = given().config(SslContext.tlsWithoutCert).relaxedHTTPSValidation()
.when()
.cookie(COOKIE_NAME, gatewayToken())
.header("Accept", MediaType.APPLICATION_JSON_VALUE);
if (body != null) {
requestSpecification
.header("Service-Id", definitionFileName)
.body(body);
}
}

return requestSpecification.post(uri).then()
.statusCode(returnCode).extract().response();
}
}

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

// When
HttpResponse response = HttpClientUtils.client().execute(request);

// Then
assertThat(response.getStatusLine().getStatusCode(), equalTo(returnCode));

return response;
}

// Execute the static apis endpoints and check the response for a return code
private HttpResponse getStaticApiResponse(String endpoint, int returnCode, String body) throws IOException {
URI uri = HttpRequestUtils.getUriFromGateway(endpoint);
HttpPost request = new HttpPost(uri);
request.addHeader("Accept", "application/json");
if (body != null) {
request.addHeader("Service-Id", "test-service-1");
StringEntity entity = new StringEntity(body);
request.setEntity(entity);
}
String cookie = HttpSecurityUtils.getCookieForGateway();
HttpSecurityUtils.addCookie(request, cookie);

// When
HttpResponse response = HttpClientUtils.client().execute(request);

// Then
assertThat(response.getStatusLine().getStatusCode(), equalTo(returnCode));


return response;
}
}

0 comments on commit b6790cb

Please sign in to comment.