Skip to content

Commit

Permalink
Deleted unnecessary commit, parameterized the old/new tests and fixed…
Browse files Browse the repository at this point in the history
… typo.
  • Loading branch information
t-burch committed Aug 1, 2023
1 parent beea445 commit 64c3b41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class OpenAPIPublisherInterceptor extends AbstractInterceptor {
public static final String PATH = "/api-docs";
public static final String PATH_UI = "/api-docs/ui";

private static final Pattern patternMeta = Pattern.compile("/api-docs?/(.*)");
private static final Pattern patternUI = Pattern.compile("/api-docs?/ui/(.*)");
private static final Pattern PATTERN_META = Pattern.compile(PATH + "?/(.*)");
private static final Pattern PATTERN_UI = Pattern.compile(PATH + "?/ui/(.*)");

protected Map<String, OpenAPIRecord> apis;

Expand All @@ -73,7 +73,7 @@ private Template createTemplate(String filePath) throws ClassNotFoundException,
@Override
public Outcome handleRequest(Exchange exc) throws Exception {

if (exc.getRequest().getUri().matches(valueOf(patternUI))) {
if (exc.getRequest().getUri().matches(valueOf(PATTERN_UI))) {
return handleSwaggerUi(exc);
}

Expand All @@ -85,7 +85,7 @@ public Outcome handleRequest(Exchange exc) throws Exception {
}

private Outcome handleOverviewOpenAPIDoc(Exchange exc) throws IOException, URISyntaxException {
Matcher m = patternMeta.matcher(exc.getRequest().getUri());
Matcher m = PATTERN_META.matcher(exc.getRequest().getUri());
if (!m.matches()) { // No id specified
if (acceptsHtmlExplicit(exc)) {
return returnHtmlOverview(exc);
Expand Down Expand Up @@ -127,7 +127,7 @@ private Outcome returnOpenApiAsYaml(Exchange exc, OpenAPIRecord rec) throws IOEx
}

private Outcome handleSwaggerUi(Exchange exc) {
Matcher m = patternUI.matcher(exc.getRequest().getUri());
Matcher m = PATTERN_UI.matcher(exc.getRequest().getUri());

// No id specified
if (!m.matches()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.predic8.membrane.core.util.*;
import io.swagger.v3.parser.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.*;
import java.util.*;
Expand All @@ -33,13 +35,14 @@
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class OpenAPIPublisherInterceptorTest {

private final ObjectMapper omYaml = ObjectMapperFactory.createYaml();
private final ObjectMapper om = new ObjectMapper();

private static final String metaOld = "/api-doc";
private static final String uiOld = "/api-doc/ui";
private static final String META_OLD = "/api-doc";
private static final String UI_OLD = "/api-doc/ui";

OpenAPIRecordFactory openAPIRecordFactory;
OpenAPIPublisherInterceptor interceptor;
Expand All @@ -65,7 +68,7 @@ void setUp() throws Exception {
}

@Test
public void constuctor() {
public void constructor() {
assertTrue(interceptor.apis.size() >= 27);
assertNotNull(interceptor.apis.get("references-test-v1-0"));
assertNotNull(interceptor.apis.get("strings-test-api-v1-0"));
Expand All @@ -75,81 +78,60 @@ public void constuctor() {
assertNotNull(interceptor.apis.get("references-response-test-v1-0"));
}

@Test
public void getApiDirectoryOld() throws Exception {
get.getRequest().setUri(metaOld);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(TestUtils.getMapFromResponse(get).size() >= 27);
final List<String> uiParameters() {
return new ArrayList<>() {{
add(UI_OLD);
add(OpenAPIPublisherInterceptor.PATH_UI);
}};
}

@Test
public void getApiDirectory() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(TestUtils.getMapFromResponse(get).size() >= 27);
final List<String> metaParameters() {
return new ArrayList<>() {{
add(META_OLD);
add(OpenAPIPublisherInterceptor.PATH);
}};
}

@Test
public void getHTMLOverviewOld() throws Exception {
get.getRequest().setUri(metaOld);
Header header = new Header();
header.setAccept("html");
get.getRequest().setHeader(header);
@ParameterizedTest
@MethodSource("metaParameters")
public void getApiDirectory(String testPath) throws Exception {
get.getRequest().setUri(testPath);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(get.getResponse().getBodyAsStringDecoded().contains("<a href=\"" + OpenAPIPublisherInterceptor.PATH_UI + "/servers-1-api-v1-0\">Servers 1 API</a>"));
assertTrue(TestUtils.getMapFromResponse(get).size() >= 27);
}

@Test
public void getHTMLOverview() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH);
@ParameterizedTest
@MethodSource("metaParameters")
public void getHTMLOverview(String testPath) throws Exception {
get.getRequest().setUri(testPath);
Header header = new Header();
header.setAccept("html");
get.getRequest().setHeader(header);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(get.getResponse().getBodyAsStringDecoded().contains("<a href=\"" + OpenAPIPublisherInterceptor.PATH_UI + "/servers-1-api-v1-0\">Servers 1 API</a>"));
}

@Test
public void getSwaggerUIOld() throws Exception {
get.getRequest().setUri(uiOld + "/nested-objects-and-arrays-test-api-v1-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(get.getResponse().getBodyAsStringDecoded().contains("html"));
}

@Test
public void getSwaggerUI() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI + "/nested-objects-and-arrays-test-api-v1-0");
@ParameterizedTest
@MethodSource("uiParameters")
public void getSwaggerUI(String testPath) throws Exception {
get.getRequest().setUri(testPath + "/nested-objects-and-arrays-test-api-v1-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(get.getResponse().getBodyAsStringDecoded().contains("html"));
}

@Test
public void getSwaggerUIWrongIdOld() throws Exception {
get.getRequest().setUri(uiOld + "/wrong-id-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSON(get);
}

@Test
public void getSwaggerUIWrongId() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI + "/wrong-id-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSON(get);
}

@Test
public void getSwaggerUINoIdOld() throws Exception {
get.getRequest().setUri(uiOld);
@ParameterizedTest
@MethodSource("uiParameters")
public void getSwaggerUIWrongId(String testPath) throws Exception {
get.getRequest().setUri(testPath + "/wrong-id-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSON(get);
}

@Test
public void getSwaggerUINoId() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI);
@ParameterizedTest
@MethodSource("uiParameters")
public void getSwaggerUINoId(String testPath) throws Exception {
get.getRequest().setUri(testPath);
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSON(get);
Expand All @@ -165,17 +147,10 @@ private void checkHasValidProblemJSON(Exchange exc) throws IOException {
assertTrue(json.has("type"));
}

@Test
public void getApiByIdOld() throws Exception {
get.getRequest().setUri(metaOld + "/nested-objects-and-arrays-test-api-v1-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals("application/x-yaml", get.getResponse().getHeader().getContentType());
assertEquals("Nested Objects and Arrays Test API", getJsonFromYamlResponse(get).get("info").get("title").textValue());
}

@Test
public void getApiById() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH + "/nested-objects-and-arrays-test-api-v1-0");
@ParameterizedTest
@MethodSource("metaParameters")
public void getApiById(String testPath) throws Exception {
get.getRequest().setUri(testPath + "/nested-objects-and-arrays-test-api-v1-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals("application/x-yaml", get.getResponse().getHeader().getContentType());
assertEquals("Nested Objects and Arrays Test API", getJsonFromYamlResponse(get).get("info").get("title").textValue());
Expand Down

This file was deleted.

42 changes: 0 additions & 42 deletions core/src/test/resources/openapi/specs/paths/paths.yml

This file was deleted.

0 comments on commit 64c3b41

Please sign in to comment.