Skip to content

Commit

Permalink
add test case for ElasticIndexJson2CollectionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
david-blasby committed Sep 17, 2024
1 parent 9909a0e commit 6623ea7
Show file tree
Hide file tree
Showing 5 changed files with 1,482 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.fao.geonet.ogcapi.records.model;

import static org.fao.geonet.ogcapi.records.util.JsonUtils.getAsDouble;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
Expand All @@ -14,6 +16,7 @@
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;


/**
* cf. https://github.com/opengeospatial/ogcapi-features/blob/master/core/openapi/schemas/extent.yaml
*
Expand Down Expand Up @@ -112,8 +115,8 @@ public static OgcApiSpatialExtent fromGnIndexRecord(Map<String, Object> map) {

for (var myCoord : coords) {
var coord = (List) myCoord;
var x = (Double) coord.get(0);
var y = (Double) coord.get(1);
var x = getAsDouble(coord.get(0));
var y = getAsDouble(coord.get(1));
if (x < xmin) {
xmin = x;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* (c) 2024 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/

package org.fao.geonet.ogcapi.records.util;

import java.util.Map;
Expand Down Expand Up @@ -39,4 +44,17 @@ public static String getAsString(Object o) {
var result = o.toString();
return result;
}

/**
* Simple utility class to get a JSON value as a double.
*
* @param o json object (could be integer or double)
* @return null or double value
*/
public static Double getAsDouble(Object o) {
if (o == null) {
return null;
}
return ((Number) o).doubleValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* (c) 2024 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.fao.geonet.ogcapi.records.util;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.fao.geonet.ogcapi.records.controller.model.CollectionInfo;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ElasticIndexJson2CollectionInfoTest {


// sample Elastic Index JSON
public static String sample1ElasticIndexJson;
public static Map<String,Object> sample1ElasticIndexParsed;



ElasticIndexJson2CollectionInfo elasticIndexJson2CollectionInfo = new ElasticIndexJson2CollectionInfo();


@BeforeClass
public static void setupClass() throws IOException {




sample1ElasticIndexJson = IOUtils.toString(
ClassLoader.getSystemClassLoader().getResourceAsStream("org/fao/geonet/ogcapi/records/util/sampleCollectionServiceRecordElasticIndex.json"),
"UTF-8");

sample1ElasticIndexParsed = new ObjectMapper().readValue( sample1ElasticIndexJson,
TypeFactory.mapType(HashMap.class, String.class, Object.class));
sample1ElasticIndexParsed = (Map<String,Object> ) sample1ElasticIndexParsed.get("hits");
sample1ElasticIndexParsed = (Map<String,Object> ) ((List)sample1ElasticIndexParsed.get("hits")).get(0);
sample1ElasticIndexParsed = (Map<String,Object> )sample1ElasticIndexParsed.get("_source");


}


/**
* Simple test case to read in the Elastic Index JSON, and parse it into a CollectionInfo.
*/
@Test
public void testFull() {
var collectionInfo = new CollectionInfo();
elasticIndexJson2CollectionInfo.injectLinkedServiceRecordInfo(collectionInfo, sample1ElasticIndexParsed);

assertEquals("catalog", collectionInfo.getType());
assertEquals("record", collectionInfo.getItemType());

assertEquals("GeoCat Demo OGCIAPI sub-portal", collectionInfo.getTitle());
assertEquals("This is a sub-portal for testing OGCAPI.", collectionInfo.getDescription());

assertEquals("http://www.opengis.net/def/crs/OGC/1.3/CRS84", collectionInfo.getCrs().get(0));
assertEquals("2024-09-10T19:10:58.536961Z", collectionInfo.getCreated());
assertEquals("2024-09-16T17:07:51.673194Z", collectionInfo.getUpdated());
assertEquals("eng", collectionInfo.getLanguage().code);
assertEquals("use limitation", collectionInfo.getLicense());

//languages
assertEquals(3, collectionInfo.getLanguages().size());
assertEquals("dut", collectionInfo.getLanguages().get(0).code);
assertEquals("spa", collectionInfo.getLanguages().get(1).code);
assertEquals("eng", collectionInfo.getLanguages().get(2).code);

//keywords
assertEquals(6, collectionInfo.getKeywords().size());
assertEquals("GEONETWORK", collectionInfo.getKeywords().get(0));
assertEquals("OSGeo", collectionInfo.getKeywords().get(1));
assertEquals("GeoCat", collectionInfo.getKeywords().get(2));
assertEquals("OGCAPI", collectionInfo.getKeywords().get(3));
assertEquals("Algeria", collectionInfo.getKeywords().get(4));
assertEquals("Antarctica", collectionInfo.getKeywords().get(5));


//themes
assertEquals(2, collectionInfo.getThemes().size());
//first theme
assertEquals("otherKeywords-theme", collectionInfo.getThemes().get(0).schema);
assertEquals(4, collectionInfo.getThemes().get(0).getConcepts().size());
assertEquals("GEONETWORK", collectionInfo.getThemes().get(0).getConcepts().get(0).id);
assertEquals("OSGeo", collectionInfo.getThemes().get(0).getConcepts().get(1).id);
assertEquals("GeoCat", collectionInfo.getThemes().get(0).getConcepts().get(2).id);
assertEquals("OGCAPI", collectionInfo.getThemes().get(0).getConcepts().get(3).id);

//2nd theme
assertEquals("http://localhost:8080/geonetwork/srv/api/registries/vocabularies/external.place.regions", collectionInfo.getThemes().get(1).schema);
assertEquals(2, collectionInfo.getThemes().get(1).getConcepts().size());
assertEquals("Algeria", collectionInfo.getThemes().get(1).getConcepts().get(0).id);
assertEquals("Antarctica", collectionInfo.getThemes().get(1).getConcepts().get(1).id);

//contacts
assertEquals(1, collectionInfo.getContacts().size());
assertEquals("Jody Garnett", collectionInfo.getContacts().get(0).getName());
assertEquals("Hat Wearer Extraordinaire", collectionInfo.getContacts().get(0).getPosition());
assertEquals("GeoCat Canada Ltd", collectionInfo.getContacts().get(0).getOrganization());
//phones
assertEquals(1, collectionInfo.getContacts().get(0).getPhones().size());
assertEquals("+1 (250) 213-1219", collectionInfo.getContacts().get(0).getPhones().get(0).getValue());
//email
assertEquals(1, collectionInfo.getContacts().get(0).getEmails().size());
assertEquals("[email protected]", collectionInfo.getContacts().get(0).getEmails().get(0).getValue());
//address
assertEquals(1, collectionInfo.getContacts().get(0).getAddresses().size());
assertEquals(1, collectionInfo.getContacts().get(0).getAddresses().get(0).getDeliveryPoint().size());
assertEquals("3613 Doncaster Drr, Victoria, BC, V8P 3W5, Canada", collectionInfo.getContacts().get(0).getAddresses().get(0).getDeliveryPoint().get(0));

//extent - spatial
assertEquals("http://www.opengis.net/def/crs/OGC/1.3/CRS84", collectionInfo.getExtent().getSpatial().getCrs());
assertEquals(4, collectionInfo.getExtent().getSpatial().getBbox().size());
assertEquals(-180.0, collectionInfo.getExtent().getSpatial().getBbox().get(0).doubleValue() ,0);
assertEquals(-90.0, collectionInfo.getExtent().getSpatial().getBbox().get(1).doubleValue() ,0);
assertEquals(180.0, collectionInfo.getExtent().getSpatial().getBbox().get(2).doubleValue() ,0);
assertEquals(90.0, collectionInfo.getExtent().getSpatial().getBbox().get(3).doubleValue() ,0);
//extent - temporal
assertEquals("http://www.opengis.net/def/uom/ISO-8601/0/Gregorian", collectionInfo.getExtent().getTemporal().getTrs());
assertEquals(2, collectionInfo.getExtent().getTemporal().getInterval().size());
assertEquals("2016-02-29T20:00:00.000Z", collectionInfo.getExtent().getTemporal().getInterval().get(0));
assertEquals("2016-02-29T20:00:00.000Z", collectionInfo.getExtent().getTemporal().getInterval().get(1));

}
}
Loading

0 comments on commit 6623ea7

Please sign in to comment.