Skip to content

Commit

Permalink
Updated links and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
t-burch committed Jul 31, 2023
1 parent 4b99166 commit 81080e0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class OpenAPIPublisherInterceptor extends AbstractInterceptor {
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?/(.*)");
private static final Pattern patternUI = Pattern.compile("/api-docs?/ui(.*)");

protected Map<String, OpenAPIRecord> apis;

Expand All @@ -75,8 +75,7 @@ public Outcome handleRequest(Exchange exc) throws Exception {
if (!exc.getRequest().getUri().startsWith("/api-doc"))
return CONTINUE;

// Should be called with /api-doc/ui and /api-docs/ui Pattern Match?
if (exc.getRequest().getUri().startsWith(PATH_UI)) {
if (exc.getRequest().getUri().matches("/api-docs?/ui")) {
return handleSwaggerUi(exc);
}

Expand Down Expand Up @@ -131,7 +130,7 @@ private Outcome handleSwaggerUi(Exchange exc) {
// No id specified
if (!m.matches()) {
Map<String, Object> details = new HashMap<>();
details.put("message", "Please specify an id of an OpenAPI document. Path should match this pattern: /api-doc/ui/<<id>>");
details.put("message", "Please specify an id of an OpenAPI document. Path should match this pattern: /api-docs/ui/<<id>>");
exc.setResponse(createProblemDetails(404, "/openapi/wrong-id", "No OpenAPI document id", details));
return RETURN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ 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";

OpenAPIRecordFactory openAPIRecordFactory;
OpenAPIPublisherInterceptor interceptor;
Map<String, OpenAPIRecord> records;
Expand Down Expand Up @@ -74,46 +77,82 @@ public void constuctor() {

@Test
public void getApiDirectoryOld() throws Exception {
get.getRequest().setUri("/api-doc");
get.getRequest().setUri(metaOld);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(TestUtils.getMapFromResponse(get).size() >= 27);
}

// TODO Same Test with /api-docs
@Test
public void getApiDirectory() throws Exception {
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(TestUtils.getMapFromResponse(get).size() >= 27);
}

@Test
public void getHTMLOverviewOld() throws Exception {
get.getRequest().setUri("/api-doc");
get.getRequest().setUri(metaOld);
Header header = new Header();
header.setAccept("html");
get.getRequest().setHeader(header);
assertEquals( RETURN, interceptor.handleRequest(get));
assertTrue(get.getResponse().getBodyAsStringDecoded().contains("<a href=\"" + uiOld + "/servers-1-api-v1-0\">Servers 1 API</a>"));
}

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

// TODO ditto
@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("/api-doc/ui/nested-objects-and-arrays-test-api-v1-0");
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI + "/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("/api-doc/ui/wrong-id-0");
get.getRequest().setUri(uiOld + "/wrong-id-0");
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSOM(get);
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);
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSON(get);
}

@Test
public void getSwaggerUINoId() throws Exception {
get.getRequest().setUri("/api-doc");
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI);
assertEquals( RETURN, interceptor.handleRequest(get));
assertEquals( 404, get.getResponse().getStatusCode());
checkHasValidProblemJSOM(get);
checkHasValidProblemJSON(get);
}

private void checkHasValidProblemJSON(Exchange exc) throws IOException {
Expand All @@ -126,9 +165,17 @@ 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("/api-doc/nested-objects-and-arrays-test-api-v1-0");
get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH + "/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

0 comments on commit 81080e0

Please sign in to comment.