Skip to content

Commit

Permalink
test: add tests for API version segment in path
Browse files Browse the repository at this point in the history
  • Loading branch information
stempler committed Apr 12, 2023
1 parent 0c7cbca commit 2abbb9e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions deegree-ogcapi-features/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@
<artifactId>joda-time</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
package org.deegree.services.oaf.resource;

import org.apache.commons.io.IOUtils;
import org.deegree.commons.utils.TunableParameter;
import org.deegree.services.oaf.OgcApiFeaturesMediaType;
import org.deegree.services.oaf.filter.ApiVersionPathFilter;
import org.deegree.services.oaf.openapi.OpenApiCreator;
import org.deegree.services.oaf.workspace.DeegreeWorkspaceInitializer;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
Expand All @@ -49,8 +51,10 @@
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static org.deegree.services.oaf.TestData.mockWorkspaceInitializer;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -74,6 +78,8 @@ public static void copyToTmpFolder()

@Override
protected Application configure() {
TunableParameter.resetCache();

enable(TestProperties.LOG_TRAFFIC);
ServletContext servletContext = mock(ServletContext.class);
when(servletContext.getContextPath()).thenReturn("");
Expand All @@ -82,6 +88,7 @@ protected Application configure() {
when(servletConfig.getServletContext()).thenReturn(servletContext);
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.packages("org.deegree.services.oaf.resource");
resourceConfig.register(ApiVersionPathFilter.class);
resourceConfig.register(new AbstractBinder() {
@Override
protected void configure() {
Expand Down Expand Up @@ -133,5 +140,17 @@ public void test_OpenApiContent() {
assertThat(json, hasJsonPath("$.paths./collections/strassenbaumkataster/items/{featureId}"));
assertThat(json, hasJsonPath("$.paths./api"));
}

/**
* Test that the version segment is not contained in server URL.
*/
@Test
public void test_OpenApi_serverUrl() {
String json = target("/datasets/oaf/api").request(OgcApiFeaturesMediaType.APPLICATION_OPENAPI).get(
String.class);

assertThat(json, isJson());
assertThat(json, hasJsonPath("$.servers[0].url", not(endsWith("/v1"))));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*-
* #%L
* deegree-ogcapi-features - OGC API Features (OAF) implementation - Querying and modifying of geospatial data objects
* %%
* Copyright (C) 2019 - 2020 lat/lon GmbH, [email protected], www.lat-lon.de
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.deegree.services.oaf.resource;

import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.deegree.services.oaf.OgcApiFeaturesMediaType;
import org.deegree.services.oaf.filter.ApiVersionPathFilter;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;

/**
* Test OpenAPI resource with optional version segment enabled for path.
*/
public class OpenApiVersionSegmentTest extends OpenApiTest {

@Rule
public final ProvideSystemProperty providesSystemPropertyRule = new ProvideSystemProperty(ApiVersionPathFilter.PARAMETER_ENABLE_VERSION_SEGMENT, "true");

/**
* Test if API definition is also available on path with version segment.
*/
@Test
public void test_OpenApiDeclarationShouldBeAvailableWithVersionSegment() {
int status = target("/datasets/oaf/v1/api").request(
OgcApiFeaturesMediaType.APPLICATION_OPENAPI).get().getStatus();
assertThat(status, is(200));
}

/**
* Test that version segment is contained in server URL.
*/
@Test
public void test_OpenApi_serverUrl() {
String json = target("/datasets/oaf/api").request(OgcApiFeaturesMediaType.APPLICATION_OPENAPI).get(
String.class);

assertThat(json, isJson());
assertThat(json, hasJsonPath("$.servers[0].url", endsWith("/v1")));
}

}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,12 @@
<version>${jersey-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v3</artifactId>
Expand Down

0 comments on commit 2abbb9e

Please sign in to comment.