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) <mb388a@us.att.com>
diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/orchestration/AAINetworkResourcesTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/orchestration/AAINetworkResourcesTest.java
index 2efdbde..f20c646 100644
--- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/orchestration/AAINetworkResourcesTest.java
+++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/orchestration/AAINetworkResourcesTest.java
@@ -159,7 +159,7 @@
 		final String content = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiVpnBinding.json")));
 		AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(content);
 		Optional<VpnBinding> oVpnBinding = Optional.empty();
-		AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, "ModelInvariantUUID", "serviceModelVersionId");
+		AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.VPN_BINDING, "ModelInvariantUUID");
 		
 		doReturn(aaiResultWrapper).when(MOCK_aaiResourcesClient).get(isA(AAIResourceUri.class));
 		oVpnBinding = aaiNetworkResources.getVpnBinding(aaiUri);
@@ -176,7 +176,7 @@
 		final String content = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPolicy.json")));
 		AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(content);
 		Optional<NetworkPolicy> oNetPolicy = Optional.empty();
-		AAIResourceUri netPolicyUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, "ModelInvariantUUID", "serviceModelVersionId");
+		AAIResourceUri netPolicyUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, "ModelInvariantUUID");
 		
 		doReturn(aaiResultWrapper).when(MOCK_aaiResourcesClient).get(isA(AAIResourceUri.class));
 		oNetPolicy = aaiNetworkResources.getNetworkPolicy(netPolicyUri);
@@ -193,7 +193,7 @@
 		final String content = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkTableRefs.json")));
 		AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(content);
 		Optional<RouteTableReference> oRtref = Optional.empty();
-		AAIResourceUri rTRefUri = AAIUriFactory.createResourceUri(AAIObjectType.ROUTE_TABLE_REFERENCE, "ModelInvariantUUID", "serviceModelVersionId");
+		AAIResourceUri rTRefUri = AAIUriFactory.createResourceUri(AAIObjectType.ROUTE_TABLE_REFERENCE, "ModelInvariantUUID");
 		
 		doReturn(aaiResultWrapper).when(MOCK_aaiResourcesClient).get(isA(AAIResourceUri.class));
 		oRtref = aaiNetworkResources.getRouteTable(rTRefUri);
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 884f8c6..324bb8a 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 @@
 
 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.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 @@
 	@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 2876f77..93de913 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.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.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 @@
 		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 @@
 		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 @@
 		return this;
 	}
 	
+	public void validateValuesSize(String template, Object... values) {
+		UriParser parser = new UriParserSpringImpl(template);
+		Set<String> 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 0000000..c94e561
--- /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 0000000..729f0e5
--- /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");
+	}
+}