Skip to content

Commit

Permalink
Use a data object parameter for CollectionsApiImpl.getFeatures()
Browse files Browse the repository at this point in the history
Simplify/improve tests readability
  • Loading branch information
groldan committed Oct 31, 2023
1 parent 9608f7c commit e182904
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ public ResponseEntity<FeatureCollection> getFeatures(//
String datetime, //
String filter) {

DataQuery dataQuery = toDataQuery(collectionId, limit, bbox, datetime, filter);
FeaturesQuery fq = FeaturesQuery.of(collectionId).withLimit(limit).withBbox(bbox).withDatetime(datetime)
.withFilter(filter);
return getFeatures(fq);
}

public ResponseEntity<FeatureCollection> getFeatures(@NonNull FeaturesQuery query) {

DataQuery dataQuery = toDataQuery(query);

FeatureCollection fc = repository.query(dataQuery);
HttpHeaders headers = getFeaturesHeaders(collectionId);
HttpHeaders headers = getFeaturesHeaders(query.getCollectionId());
fc = addLinks(fc, dataQuery);
return ResponseEntity.status(200).headers(headers).body(fc);
}
Expand Down Expand Up @@ -199,21 +206,9 @@ private Link link(String href, String rel, String type, String title) {
return link;
}

DataQuery toDataQuery(String collectionId, //
Integer limit, //
List<BigDecimal> bbox, //
String datetime, //
String filter) {

// query collection id from whatever datasource is defined as index
DataQuery q = DataQuery.fromUri(URI.create("index://default")).withLayerName(collectionId);
if (null != limit && limit >= 0) {
// a negative limit can be used to return the whole dataset
q = q.withLimit(limit);
}
if (null != filter) {
q = q.withFilter(filter);
}
DataQuery toDataQuery(FeaturesQuery query) {
DataQuery q = DataQuery.fromUri(URI.create("index://default")).withLayerName(query.getCollectionId())
.withLimit(query.getLimit()).withOffset(query.getOffset()).withFilter(query.getFilter());
return q;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.camptocamp.opendata.ogc.features.server.impl;

import java.math.BigDecimal;
import java.util.List;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.With;

@Value
@With
@RequiredArgsConstructor
public class FeaturesQuery {

private @NonNull String collectionId;
private Integer offset;
private Integer limit;
private List<BigDecimal> bbox;
private String datetime;
private String filter;
private String filterLang;

public static FeaturesQuery of(String collectionId) {
return new FeaturesQuery(collectionId, null, null, null, null, null, null);
}

public FeaturesQuery withBbox(double minx, double miny, double maxx, double maxy) {
return withBbox(List.of(BigDecimal.valueOf(minx), BigDecimal.valueOf(miny), BigDecimal.valueOf(maxx),
BigDecimal.valueOf(maxy)));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.camptocamp.opendata.ogc.features.server.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
Expand All @@ -15,12 +17,20 @@
import com.camptocamp.opendata.ogc.features.model.Collections;
import com.camptocamp.opendata.ogc.features.model.FeatureCollection;

public class AbstractCollectionsApiImplIT {
public abstract class AbstractCollectionsApiImplIT {

protected @Autowired CollectionsApiImpl collectionsApi;

protected @Autowired NativeWebRequest req;

protected MockHttpServletRequest actualRequest;

@BeforeEach
void prepRequest() {
actualRequest = (MockHttpServletRequest) req.getNativeRequest();
actualRequest.addHeader("Accept", "application/json");
}

@Test
public void testGetCollections() {

Expand All @@ -38,20 +48,16 @@ public void testGetCollections() {

@Test
public void testGetItems() {
MockHttpServletRequest actualRequest = (MockHttpServletRequest) req.getNativeRequest();
actualRequest.addHeader("Accept", "application/json");
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", 10, null, null, null);

FeaturesQuery query = FeaturesQuery.of("locations").withLimit(10);
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures(query);
assertThat(response.getBody().getFeatures().toList().size()).isEqualTo(10);
}

@Test
public void testGetItemsWithFilter() {
MockHttpServletRequest actualRequest = (MockHttpServletRequest) req.getNativeRequest();
actualRequest.addHeader("Accept", "application/json");
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", -1, null, null,
"number = 140");
FeaturesQuery query = FeaturesQuery.of("locations").withFilter("number = 140");
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures(query);

assertThat(response.getBody().getFeatures().toList().size()).isEqualTo(1);
assertThat(response.getBody().getFeatures().count()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
Expand Down Expand Up @@ -170,30 +169,26 @@ public void testGetCollections_survives_drop_table(TestInfo testInfo) throws SQL

@Test
public void testGetItems_survives_schema_change() throws SQLException {
MockHttpServletRequest actualRequest = (MockHttpServletRequest) req.getNativeRequest();
actualRequest.addHeader("Accept", "application/json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", 10, null, null, null);
FeaturesQuery query = FeaturesQuery.of("locations").withLimit(10);
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures(query);
assertThat(response.getBody().getFeatures().toList().size()).isEqualTo(10);

renameColumn("locations", "year", "año");

response = collectionsApi.getFeatures("locations", 10, null, null, null);
response = collectionsApi.getFeatures(query);
assertThat(response.getBody().getFeatures().toList().size()).isEqualTo(10);

dropColumn("locations", "año");

response = collectionsApi.getFeatures("locations", 10, null, null, null);
response = collectionsApi.getFeatures(query);
assertThat(response.getBody().getFeatures().toList().size()).isEqualTo(10);
}

@Test
public void testGetItem_survives_schema_change() throws SQLException {
MockHttpServletRequest actualRequest = (MockHttpServletRequest) req.getNativeRequest();
actualRequest.addHeader("Accept", "application/json");

GeodataRecord before = collectionsApi.getFeatures("locations", 1, null, null, null).getBody().getFeatures()
.toList().get(0);
FeaturesQuery query = FeaturesQuery.of("locations").withLimit(1);
GeodataRecord before = collectionsApi.getFeatures(query).getBody().getFeatures().findFirst().orElseThrow();
assertThat(before.getProperty("number")).isPresent();

final String id = before.getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,58 @@
package com.camptocamp.opendata.ogc.features.server.impl;

import com.camptocamp.opendata.ogc.features.app.OgcFeaturesApp;
import com.camptocamp.opendata.ogc.features.model.FeatureCollection;
import com.camptocamp.opendata.ogc.features.model.Link;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.camptocamp.opendata.ogc.features.app.OgcFeaturesApp;
import com.camptocamp.opendata.ogc.features.model.FeatureCollection;
import com.camptocamp.opendata.ogc.features.model.Link;

@SpringBootTest(classes = OgcFeaturesApp.class)
@ActiveProfiles("sample-data")
public class CollectionsApiImplTest extends AbstractCollectionsApiImplIT {

@Test
public void testGetItemsLinks() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.addParameter("f", "geojson");
mockedReq.addHeader("Accept", "application/geo+json");
actualRequest.setParameter("f", "geojson");
actualRequest.removeHeader("Accept");
actualRequest.addHeader("Accept", "application/geo+json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);
FeaturesQuery query = FeaturesQuery.of("locations").withLimit(10);
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures(query);

List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=geojson"));
Link nextLink = links.stream().filter(l -> "next".equals(l.getRel())).findFirst().orElseThrow();
assertThat(nextLink.getHref()).contains("f=geojson");
}

public @Test void testGetItemsLinksNoFParam() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.removeParameter("f");
mockedReq.addHeader("Accept", "application/json");
actualRequest.removeParameter("f");
actualRequest.removeHeader("Accept");
actualRequest.addHeader("Accept", "application/json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);
FeaturesQuery query = FeaturesQuery.of("locations").withLimit(10);
ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures(query);

List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=json"));
Link nextLink = links.stream().filter(l -> "next".equals(l.getRel())).findFirst().orElseThrow();
assertThat(nextLink.getHref()).contains("f=json");
}

public @Test void testGetItemsLinksFParamAndDifferentHeaderParams() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.addParameter("f", "ooxml");
mockedReq.addHeader("Accept", "application/json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);
actualRequest.addHeader("Accept", "application/json");
actualRequest.setParameter("f", "ooxml");

ResponseEntity<FeatureCollection> response = collectionsApi
.getFeatures(FeaturesQuery.of("locations").withLimit(2));
List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=ooxml"));
Link nextLink = links.stream().filter(l -> "next".equals(l.getRel())).findFirst().orElseThrow();
assertThat(nextLink.getHref()).contains("f=ooxml");
}

}

0 comments on commit e182904

Please sign in to comment.