From 1ad5da3633e161dc0ecdb1e6d9cf885ea863fbb1 Mon Sep 17 00:00:00 2001 From: Lyn Elisa Goltz Date: Thu, 28 Jun 2018 09:13:35 +0200 Subject: [PATCH] #8 - moved useful methods to JsonUtils --- .../FeatureCollectionsMetadataOperation.java | 100 +++----------- .../opengis/cite/wfs30/util/JsonUtils.java | 123 ++++++++++++++++++ .../cite/wfs30/util/JsonUtilsTest.java | 72 ++++++++++ 3 files changed, 213 insertions(+), 82 deletions(-) create mode 100644 src/main/java/org/opengis/cite/wfs30/util/JsonUtils.java create mode 100644 src/test/java/org/opengis/cite/wfs30/util/JsonUtilsTest.java diff --git a/src/main/java/org/opengis/cite/wfs30/collections/FeatureCollectionsMetadataOperation.java b/src/main/java/org/opengis/cite/wfs30/collections/FeatureCollectionsMetadataOperation.java index 4139d701..79458a39 100644 --- a/src/main/java/org/opengis/cite/wfs30/collections/FeatureCollectionsMetadataOperation.java +++ b/src/main/java/org/opengis/cite/wfs30/collections/FeatureCollectionsMetadataOperation.java @@ -5,6 +5,11 @@ import static org.opengis.cite.wfs30.SuiteAttribute.API_MODEL; import static org.opengis.cite.wfs30.WFS3.PATH.COLLECTIONS; import static org.opengis.cite.wfs30.openapi3.OpenApiUtils.retrieveTestPoints; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinkToItself; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinksWithSupportedMediaTypeByRel; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinksWithoutRelOrType; +import static org.opengis.cite.wfs30.util.JsonUtils.findUnsupportedTypes; +import static org.opengis.cite.wfs30.util.JsonUtils.linkIncludesRelAndType; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; @@ -116,7 +121,7 @@ public void storeCollectionsInTestContext( ITestContext testContext ) { * the test point to test, never null */ @Test(description = "Implements A.4.4.4. Validate the Feature Collections Metadata Operation (Requirement 9, 10)", groups = "collections", dataProvider = "collectionsUris", dependsOnGroups = "apidefinition") - public void validateFeatureCollectionsMetadataOperation(TestPoint testPoint ) { + public void validateFeatureCollectionsMetadataOperation( TestPoint testPoint ) { String testPointUri = testPoint.createUri(); Response response = init().baseUri( testPointUri ).accept( JSON ).when().request( GET ); response.then().statusCode( 200 ); @@ -149,19 +154,19 @@ public void validateFeatureCollectionsMetadataOperationResponse_Links( TestPoint throw new SkipException( "Could not find a response for test point " + testPoint ); JsonPath jsonPath = response.jsonPath(); + List> links = jsonPath.getList( "links" ); // Validate that the retrieved document includes links for: Itself, - Map linkToSelf = findLinkToItself( jsonPath ); + Map linkToSelf = findLinkToItself( links ); assertNotNull( linkToSelf, "Feature Collection Metadata document must include a link for itself" ); assertTrue( linkIncludesRelAndType( linkToSelf ), "Link to itself must include a rel and type parameter" ); // Validate that the retrieved document includes links for: Itself, Alternate encodings of this document in // every other media type as identified by the compliance classes for this server. List mediaTypesToSupport = createListOfMediaTypesToSupport( testPoint, linkToSelf ); - List> alternateLinks = findLinksWithSupportedMediaTypeByRel( jsonPath.getList( "links" ), - mediaTypesToSupport, + List> alternateLinks = findLinksWithSupportedMediaTypeByRel( links, mediaTypesToSupport, "alternate" ); - List typesWithoutLink = findLinksWithoutTypes( alternateLinks, mediaTypesToSupport ); + List typesWithoutLink = findUnsupportedTypes( alternateLinks, mediaTypesToSupport ); assertTrue( typesWithoutLink.isEmpty(), "Feature Collection Metadata document must include links for alternate encodings. Missing links for types " + typesWithoutLink ); @@ -227,7 +232,7 @@ public void validateFeatureCollectionsMetadataOperationResponse_Collections( Tes * the collection to test, never null */ @Test(description = "Implements A.4.4.6. Validate a Collections Metadata document (Requirement 13)", groups = "collections", dataProvider = "collections", dependsOnMethods = "validateFeatureCollectionsMetadataOperationResponse_Collections") - public void validateCollectionsMetadataDocument_Links(TestPoint testPoint, Map collection ) { + public void validateCollectionsMetadataDocument_Links( TestPoint testPoint, Map collection ) { String collectionName = (String) collection.get( "name" ); List testPointsForNamedCollection = OpenApiUtils.retrieveTestPoints( apiModel, COLLECTIONS, collectionName ); @@ -236,11 +241,11 @@ public void validateCollectionsMetadataDocument_Links(TestPoint testPoint, Map mediaTypesToSupport = createListOfMediaTypesToSupport( testPointsForNamedCollection.get( 0 ), null ); - List links = (List) collection.get( "links" ); + List> links = (List>) collection.get( "links" ); List> alternateLinks = findLinksWithSupportedMediaTypeByRel( links, mediaTypesToSupport, "item" ); - List typesWithoutLink = findLinksWithoutTypes( alternateLinks, mediaTypesToSupport ); + List typesWithoutLink = findUnsupportedTypes( alternateLinks, mediaTypesToSupport ); assertTrue( typesWithoutLink.isEmpty(), "Collections Metadata document must include links with relation 'item' for each supported encodings. Missing links for types " + typesWithoutLink ); @@ -269,7 +274,7 @@ public void validateCollectionsMetadataDocument_Links(TestPoint testPoint, Mapnull */ @Test(description = "Implements A.4.4.6. Validate a Collections Metadata document (Requirement 14)", groups = "collections", dataProvider = "collections", dependsOnMethods = "validateFeatureCollectionsMetadataOperationResponse_Collections") - public void validateCollectionsMetadataDocument_Extent(TestPoint testPoint, Map collection ) { + public void validateCollectionsMetadataDocument_Extent( TestPoint testPoint, Map collection ) { // TODO: validate the extent property } @@ -283,7 +288,8 @@ public void validateCollectionsMetadataDocument_Extent(TestPoint testPoint, Map< * the collection to test, never null */ @Test(description = "Implements A.4.4.7. Validate the Feature Collection Metadata Operation (Requirement 15) and A.4.4.8. Validate the Feature Collection Metadata Operation Response (Requirement 16)", groups = "collections", dataProvider = "collections", dependsOnMethods = "validateFeatureCollectionsMetadataOperationResponse_Collections") - public void validateTheFeatureCollectionMetadataOperationAndResponse(TestPoint testPoint, Map collection ) { + public void validateTheFeatureCollectionMetadataOperationAndResponse( TestPoint testPoint, + Map collection ) { String collectionName = (String) collection.get( "name" ); List testPointsForNamedCollection = OpenApiUtils.retrieveTestPoints( apiModel, COLLECTIONS, collectionName ); @@ -319,7 +325,7 @@ public void validateTheFeatureCollectionMetadataOperationAndResponse(TestPoint t * @param testPoint * to test, never null */ - private Response validateTheFeatureCollectionMetadataOperationAndResponse(TestPoint testPoint ) { + private Response validateTheFeatureCollectionMetadataOperationAndResponse( TestPoint testPoint ) { String testPointUri = testPoint.createUri(); Response response = init().baseUri( testPointUri ).accept( JSON ).when().request( GET ); response.then().statusCode( 200 ); @@ -360,68 +366,6 @@ private List findMissingCollectionNames( List collections ) { return missingCollectionNames; } - private Map findLinkToItself( JsonPath jsonPath ) { - List links = jsonPath.getList( "links" ); - for ( Object link : links ) { - Map linkItem = (Map) link; - Object rel = linkItem.get( "rel" ); - if ( "self".equals( rel ) ) - return linkItem; - } - return null; - } - - private List> findLinksWithSupportedMediaTypeByRel( List links, - List mediaTypesToSupport, - String expectedRel ) { - List> alternateLinks = new ArrayList<>(); - for ( Object link : links ) { - Map linkItem = (Map) link; - Object type = linkItem.get( "type" ); - Object rel = linkItem.get( "rel" ); - if ( expectedRel.equals( rel ) && isSupportedMediaType( type, mediaTypesToSupport ) ) - alternateLinks.add( linkItem ); - } - return alternateLinks; - } - - private List findLinksWithoutTypes( List> alternateLinks, - List mediaTypesToSuppport ) { - List missingLinksForType = new ArrayList<>(); - for ( String contentMediaType : mediaTypesToSuppport ) { - boolean hasLinkForContentType = hasLinkForContentType( alternateLinks, contentMediaType ); - if ( !hasLinkForContentType ) - missingLinksForType.add( contentMediaType ); - } - return missingLinksForType; - } - - private boolean hasLinkForContentType( List> alternateLinks, String mediaType ) { - for ( Map alternateLink : alternateLinks ) { - Object type = alternateLink.get( "type" ); - if ( mediaType.equals( type ) ) - return true; - } - return false; - } - - private List findLinksWithoutRelOrType( List> alternateLinks ) { - List linksWithoutRelOrType = new ArrayList<>(); - for ( Map alternateLink : alternateLinks ) { - if ( !linkIncludesRelAndType( alternateLink ) ) - linksWithoutRelOrType.add( (String) alternateLink.get( "href" ) ); - } - return linksWithoutRelOrType; - } - - private boolean linkIncludesRelAndType( Map link ) { - Object rel = link.get( "rel" ); - Object type = link.get( "type" ); - if ( rel != null && type != null ) - return true; - return false; - } - private Map findCollectionByName( String collectionNameFromLandingPage, List collections ) { for ( Object collectionObject : collections ) { Map collection = (Map) collectionObject; @@ -432,14 +376,6 @@ private Map findCollectionByName( String collectionNameFromLandi return null; } - private boolean isSupportedMediaType( Object type, List contentMediaTypes ) { - for ( String contentMediaType : contentMediaTypes ) { - if ( contentMediaType.equals( type ) ) - return true; - } - return false; - } - private List> createCollectionsMap( List collections ) { List> collectionsMap = new ArrayList<>(); for ( Object collection : collections ) @@ -456,4 +392,4 @@ private List createListOfMediaTypesToSupport( TestPoint testPoint, MapLyn Goltz + */ +public class JsonUtils { + + private JsonUtils() { + } + + /** + * Parses all links with 'type' of one of the passed mediaTypes and the 'rel' property with the passed value. + * + * @param links + * list of all links, never null + * @param mediaTypesToSupport + * a list of media types the links searched for should support, may be empty but never null + * @param expectedRel + * the expected value of the property 'rel', never null + * @return a list of links supporting one of the media types and with the expected 'rel' property, may be empty but + * never null + */ + public static List> findLinksWithSupportedMediaTypeByRel( List> links, + List mediaTypesToSupport, + String expectedRel ) { + List> alternateLinks = new ArrayList<>(); + for ( Map link : links ) { + Object type = link.get( "type" ); + Object rel = link.get( "rel" ); + if ( expectedRel.equals( rel ) && isSupportedMediaType( type, mediaTypesToSupport ) ) + alternateLinks.add( link ); + } + return alternateLinks; + } + + /** + * Parsing the media types which does not have a link woth property 'type' for. + * + * @param links + * list of links to search in, never null + * @param mediaTypesToSuppport + * a list of media types which should be supported, never null + * @return the media types which does not have a link for. + */ + public static List findUnsupportedTypes( List> links, List mediaTypesToSuppport ) { + List unsupportedType = new ArrayList<>(); + for ( String contentMediaType : mediaTypesToSuppport ) { + boolean hasLinkForContentType = hasLinkForContentType( links, contentMediaType ); + if ( !hasLinkForContentType ) + unsupportedType.add( contentMediaType ); + } + return unsupportedType; + } + + /** + * Parses the links without 'rel' or 'type' property. + * + * @param links + * list of links to search in, never null + * @return the links without 'rel' or 'type' property + */ + public static List findLinksWithoutRelOrType( List> links ) { + List linksWithoutRelOrType = new ArrayList<>(); + for ( Map alternateLink : links ) { + if ( !linkIncludesRelAndType( alternateLink ) ) + linksWithoutRelOrType.add( (String) alternateLink.get( "href" ) ); + } + return linksWithoutRelOrType; + } + + /** + * Parses the link with 'rel=self'. + * + * @param links + * list of links to search in, never null + * @return the link to itself or null if no such link exists + */ + public static Map findLinkToItself( List> links ) { + for ( Map link : links ) { + Object rel = link.get( "rel" ); + if ( "self".equals( rel ) ) + return link; + } + return null; + } + + /** + * Checks if the passed link contains 'rel' and 'type' properties. + * + * @param link + * to check, never null + * @return true if the link contains 'rel' and 'type' properties, false otherwise + */ + public static boolean linkIncludesRelAndType( Map link ) { + Object rel = link.get( "rel" ); + Object type = link.get( "type" ); + if ( rel != null && type != null ) + return true; + return false; + } + + private static boolean hasLinkForContentType( List> alternateLinks, String mediaType ) { + for ( Map alternateLink : alternateLinks ) { + Object type = alternateLink.get( "type" ); + if ( mediaType.equals( type ) ) + return true; + } + return false; + } + + private static boolean isSupportedMediaType( Object type, List contentMediaTypes ) { + for ( String contentMediaType : contentMediaTypes ) { + if ( contentMediaType.equals( type ) ) + return true; + } + return false; + } + +} diff --git a/src/test/java/org/opengis/cite/wfs30/util/JsonUtilsTest.java b/src/test/java/org/opengis/cite/wfs30/util/JsonUtilsTest.java new file mode 100644 index 00000000..0f88b70f --- /dev/null +++ b/src/test/java/org/opengis/cite/wfs30/util/JsonUtilsTest.java @@ -0,0 +1,72 @@ +package org.opengis.cite.wfs30.util; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinkToItself; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinksWithSupportedMediaTypeByRel; +import static org.opengis.cite.wfs30.util.JsonUtils.findLinksWithoutRelOrType; +import static org.opengis.cite.wfs30.util.JsonUtils.linkIncludesRelAndType; + +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.junit.BeforeClass; +import org.junit.Test; + +import io.restassured.path.json.JsonPath; + +/** + * @author Lyn Goltz + */ +public class JsonUtilsTest { + + private static JsonPath jsonPath; + + @BeforeClass + public static void parseJson() { + InputStream json = JsonUtilsTest.class.getResourceAsStream( "../collections/collections.json" ); + jsonPath = new JsonPath( json ); + } + + @Test + public void testFindLinkToItself() { + List> links = jsonPath.getList( "links" ); + Map linkToItself = findLinkToItself( links ); + + assertThat( linkToItself.get( "href" ), + is( "http://www.ldproxy.nrw.de/rest/services/kataster/collections/?f=json" ) ); + assertThat( linkToItself.get( "rel" ), is( "self" ) ); + assertThat( linkToItself.get( "type" ), is( "application/json" ) ); + assertThat( linkToItself.get( "title" ), is( "this document" ) ); + } + + @Test + public void testLinkIncludesRelAndType() { + List> links = jsonPath.getList( "links" ); + Map linkToItself = findLinkToItself( links ); + boolean includesRelAndType = linkIncludesRelAndType( linkToItself ); + + assertThat( includesRelAndType, is( true ) ); + } + + @Test + public void testFindLinksWithoutRelOrType() { + List> links = jsonPath.getList( "links" ); + List linksWithoutRelOrType = findLinksWithoutRelOrType( links ); + + assertThat( linksWithoutRelOrType.size(), is( 0 ) ); + } + + @Test + public void testFindLinksWithSupportedMediaTypeByRel() { + List> links = jsonPath.getList( "links" ); + List mediaTypes = Arrays.asList( "text/html", "application/json" ); + List> linksWithMediaTypes = findLinksWithSupportedMediaTypeByRel( links, mediaTypes, + "alternate" ); + + assertThat( linksWithMediaTypes.size(), is( 1 ) ); + } + +}