diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java index ccb3276d7..ebde88fec 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java @@ -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 apis; @@ -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); } @@ -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); @@ -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()) { diff --git a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java index 079600e54..a97745d05 100644 --- a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java +++ b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java @@ -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.*; @@ -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; @@ -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")); @@ -75,33 +78,32 @@ 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 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 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("Servers 1 API")); + 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); @@ -109,47 +111,27 @@ public void getHTMLOverview() throws Exception { assertTrue(get.getResponse().getBodyAsStringDecoded().contains("Servers 1 API")); } - @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); @@ -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()); diff --git a/core/src/test/java/com/predic8/membrane/core/openapi/validators/PathTest.java b/core/src/test/java/com/predic8/membrane/core/openapi/validators/PathTest.java deleted file mode 100644 index 0bbb8d17d..000000000 --- a/core/src/test/java/com/predic8/membrane/core/openapi/validators/PathTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.predic8.membrane.core.openapi.validators; - -import com.predic8.membrane.core.openapi.model.*; -import jakarta.mail.internet.*; -import org.junit.jupiter.api.*; - -import static com.predic8.membrane.core.http.MimeType.TEXT_PLAIN; -import static com.predic8.membrane.core.openapi.validators.ValidationContext.ValidatedEntityType.MEDIA_TYPE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class PathTest extends AbstractValidatorTest { - @Override - String getOpenAPIFileName() { - return "/openapi/specs/paths/paths.yml"; - } - - @Test - public void foos() throws ParseException { - ValidationErrors errors = validator.validate(Request.get().path("/apis/apix/foos")); - assertEquals(0,errors.size()); - } - - @Test - public void foosAndId() throws ParseException { - ValidationErrors errors = validator.validate(Request.get().path("/apis/apix/foos/13")); - assertEquals(0,errors.size()); - } - - @Test - public void randomPath() throws ParseException { - ValidationErrors errors = validator.validate(Request.get().path("/apis/apix/abc")); - System.out.println("errors = " + errors); - assertEquals(0,errors.size()); - } -} diff --git a/core/src/test/resources/openapi/specs/paths/paths.yml b/core/src/test/resources/openapi/specs/paths/paths.yml deleted file mode 100644 index a33609365..000000000 --- a/core/src/test/resources/openapi/specs/paths/paths.yml +++ /dev/null @@ -1,42 +0,0 @@ -openapi: '3.0.3' -info: - title: Path API - version: '1.0' -servers: - - url: https://api.predic8.de/apis/apix -paths: - /foos: - get: - responses: - '200': - description: OK - /foos/{id}: - parameters: - - $ref: "#/components/parameters/id" - get: - responses: - '200': - description: OK - /foos/{id}/{eid}: - parameters: - - $ref: "#/components/parameters/id" - - $ref: "#/components/parameters/eid" - get: - responses: - '200': - description: OK - -components: - parameters: - id: - required: true - in: path - name: id - schema: - type: string - eid: - required: true - in: path - name: eid - schema: - type: string \ No newline at end of file