From 147e9ee814448c8bbec4aa3aeac4b7118b7c1bc7 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Tue, 22 Jan 2019 11:48:03 -0500 Subject: validate number of uri keys test was setting unnecessary keys on uri changed plural to singular AAI class added in validation for number of keys provided Change-Id: I86f61301aa5f2ea676678908bae1e88b86d7e570 Issue-ID: SO-1409 Signed-off-by: Benjamin, Max (mb388a) --- .../so/client/aai/entities/uri/HttpLookupUri.java | 14 +++++- .../graphinventory/entities/uri/SimpleUri.java | 14 +++++- .../exceptions/IncorrectNumberOfUriKeys.java | 11 +++++ .../entities/uri/IncorrectNumberOfUriKeysTest.java | 50 ++++++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/exceptions/IncorrectNumberOfUriKeys.java create mode 100644 common/src/test/java/org/onap/so/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java (limited to 'common') diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java index 884f8c6ac4..324bb8abad 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java @@ -22,7 +22,6 @@ package org.onap.so.client.aai.entities.uri; import java.io.IOException; import java.net.URI; -import java.util.Arrays; import java.util.Map; import java.util.Optional; @@ -38,6 +37,7 @@ import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; +import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -118,6 +118,18 @@ public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri @Override public abstract HttpLookupUri clone(); + @Override + public void validateValuesSize(String template, Object... values) { + try { + super.validateValuesSize(template, values); + } catch (IncorrectNumberOfUriKeys e) { + if (values.length == 1) { + //Special case where we perform an http look up + } else { + throw e; + } + } + } public AAIResourcesClient getResourcesClient() { return new AAIResourcesClient(); } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java index 2876f77d82..93de9139f9 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java @@ -24,11 +24,11 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import java.util.Set; import javax.ws.rs.core.UriBuilder; @@ -39,6 +39,7 @@ import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals; import org.onap.so.client.graphinventory.GraphInventoryObjectType; import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser; import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl; +import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; import org.springframework.web.util.UriUtils; public class SimpleUri implements GraphInventoryResourceUri, Serializable { @@ -56,6 +57,7 @@ public class SimpleUri implements GraphInventoryResourceUri, Serializable { this.pluralType = null; this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); this.values = values; + validateValuesSize(this.getTemplate(type), values); } protected SimpleUri(GraphInventoryObjectType type, URI uri) { this.type = type; @@ -86,12 +88,14 @@ public class SimpleUri implements GraphInventoryResourceUri, Serializable { this.pluralType = type; this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); this.values = values; + validateValuesSize(this.getTemplate(type), values); } protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType, Object... childValues) { this.type = childType; this.pluralType = null; this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri()); this.values = childValues; + validateValuesSize(childType.partialUri(), values); } protected void setInternalURI(UriBuilder builder) { @@ -232,6 +236,14 @@ public class SimpleUri implements GraphInventoryResourceUri, Serializable { return this; } + public void validateValuesSize(String template, Object... values) { + UriParser parser = new UriParserSpringImpl(template); + Set variables = parser.getVariables(); + if (variables.size() != values.length) { + throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables)); + } + } + protected String getTemplate(GraphInventoryObjectType type) { return type.uriTemplate(); } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/exceptions/IncorrectNumberOfUriKeys.java b/common/src/main/java/org/onap/so/client/graphinventory/exceptions/IncorrectNumberOfUriKeys.java new file mode 100644 index 0000000000..c94e561274 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/exceptions/IncorrectNumberOfUriKeys.java @@ -0,0 +1,11 @@ +package org.onap.so.client.graphinventory.exceptions; + +public class IncorrectNumberOfUriKeys extends RuntimeException { + + private static final long serialVersionUID = 2189285428827817518L; + + public IncorrectNumberOfUriKeys(String message) { + super(message); + } + +} diff --git a/common/src/test/java/org/onap/so/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java b/common/src/test/java/org/onap/so/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java new file mode 100644 index 0000000000..729f0e50e9 --- /dev/null +++ b/common/src/test/java/org/onap/so/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java @@ -0,0 +1,50 @@ +package org.onap.so.client.aai.entities.uri; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.so.client.aai.AAIObjectPlurals; +import org.onap.so.client.aai.AAIObjectType; +import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; + +public class IncorrectNumberOfUriKeysTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void verifyIncorrectNumberOfKeysSingle() { + + thrown.expect(IncorrectNumberOfUriKeys.class); + thrown.expectMessage(equalTo("Expected 3 variables: [cloud-owner, cloud-region-id, volume-group-id]")); + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, "volume-group-id"); + + } + + @Test + public void verifyIncorrectNumberOfKeysPlural() { + + thrown.expect(IncorrectNumberOfUriKeys.class); + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VOLUME_GROUP, "my-cloud-owner"); + + } + + @Test + public void verifyIncorrectNumberOfKeysFromParent() { + + thrown.expect(IncorrectNumberOfUriKeys.class); + AAIResourceUri parentUri = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, "my-cloud-owner", "my-cloud-region-id"); + AAIResourceUri uri = AAIUriFactory.createResourceFromParentURI(parentUri, AAIObjectType.VOLUME_GROUP); + } + + @Test + public void verifyIncorrectNumberOfKeysHttpAware() { + + thrown.expect(IncorrectNumberOfUriKeys.class); + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "customer-id", "subscription-id"); + } +} -- cgit 1.2.3-korg