diff options
Diffstat (limited to 'graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities')
10 files changed, 995 insertions, 0 deletions
diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/AAIResultWrapperTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/AAIResultWrapperTest.java new file mode 100644 index 0000000000..adc2e7284e --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/AAIResultWrapperTest.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.io.Serializable; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider; +import org.springframework.util.SerializationUtils; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(MockitoJUnitRunner.class) +public class AAIResultWrapperTest { + String json; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + AAIResultWrapper aaiResultWrapper; + AAIResultWrapper aaiResultWrapperEmpty; + + @Before + public void init() throws IOException { + final String RESOURCE_PATH = "src/test/resources/__files/aai/resources/"; + json = new String(Files.readAllBytes(Paths.get(RESOURCE_PATH + "e2e-complex.json"))); + + aaiResultWrapper = new AAIResultWrapper(json); + aaiResultWrapperEmpty = new AAIResultWrapper("{}"); + } + + @Test + public void testAAIResultWrapperIsSerializable() throws IOException { + AAIResultWrapper original = new AAIResultWrapper(""); + byte[] serialized = SerializationUtils.serialize(original); + AAIResultWrapper deserialized = (AAIResultWrapper) SerializationUtils.deserialize(serialized); + assertEquals(deserialized.getJson(), original.getJson()); + } + + @Test + public void testGetRelationshipsEmpty() { + Optional<Relationships> relationships = aaiResultWrapperEmpty.getRelationships(); + assertEquals("Compare relationships", Optional.empty(), relationships); + } + + @Test + public void testAsMap() throws JsonParseException, JsonMappingException, IOException { + ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper(); + Map<String, Object> expected = mapper.readValue(json, new TypeReference<Map<String, Object>>() {}); + + Map<String, Object> actual = aaiResultWrapper.asMap(); + assertEquals(expected, actual); + } + + @Test + public void testAsMapEmpty() { + Map<String, Object> actual = aaiResultWrapperEmpty.asMap(); + assertEquals(new HashMap<>(), actual); + } + + @Test + public void nullCases() { + + AAIResultWrapper wrapper = new AAIResultWrapper(null); + + assertEquals(Optional.empty(), wrapper.getRelationships()); + assertEquals("{}", wrapper.getJson()); + assertEquals(Optional.empty(), wrapper.asBean(GenericVnf.class)); + assertEquals(true, wrapper.asMap().isEmpty()); + assertEquals("{}", wrapper.toString()); + + } + + @Test + public void objectConstructor() { + AAIResultWrapper wrapper = new AAIResultWrapper(new GenericVnf()); + assertEquals("{}", wrapper.getJson()); + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/RelationshipsTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/RelationshipsTest.java new file mode 100644 index 0000000000..10162f83ee --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/RelationshipsTest.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities; + +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri; +import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; + +public class RelationshipsTest { + + private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/"; + + @Test + public void run() throws IOException { + final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "e2e-complex.json"))); + + AAIResultWrapper wrapper = new AAIResultWrapper(content); + Relationships relationships = wrapper.getRelationships().get(); + + List<AAIResourceUri> test = relationships.getRelatedUris(AAIObjectType.VCE); + List<AAIResourceUri> uris = Arrays.asList( + AAIUriFactory.createResourceUri(AAIObjectType.VCE, "a9fec18e-1ea3-40e4-a6c0-a89b3de07053"), + AAIUriFactory.createResourceUri(AAIObjectType.VCE, "8ae1e5f8-61f1-4c71-913a-b40cc4593cb9"), + AAIUriFactory.createResourceUri(AAIObjectType.VCE, "a2935fa9-b743-49f4-9813-a127f13c4e93"), + AAIUriFactory.createResourceUri(AAIObjectType.VCE, "c7fe7698-8063-4e26-8bd3-ca3edde0b0d4")); + + assertTrue(uris.containsAll(test) && test.containsAll(uris)); + + } + +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java new file mode 100644 index 0000000000..699eac9768 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java @@ -0,0 +1,74 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectType; + +public class AAIFluentTypeReverseLookupTest { + + + @Test + public void reverseParseEntryUri() { + String cloudRegion = + "http://localhost:8888/aai/v38/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}"; + String newvce = "/aai/v9/network/newvces/newvce/{vnf-id2}"; + + AAIFluentTypeReverseLookup lookup = new AAIFluentTypeReverseLookup(); + AAIObjectType type = lookup.fromName("cloud-region", cloudRegion); + + assertEquals("cloud-region", type.typeName()); + assertEquals("/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}", type.partialUri()); + assertEquals("/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}", + type.uriTemplate()); + + type = lookup.fromName("newvce", newvce); + + assertEquals("newvce", type.typeName()); + assertEquals("/newvces/newvce/{vnf-id2}", type.partialUri()); + assertEquals("/network/newvces/newvce/{vnf-id2}", type.uriTemplate()); + + type = lookup.fromName("unknown-type-of-something", "/some/endpoint"); + + assertEquals("unknown", type.typeName()); + + } + + @Test + public void reverseParseTest() { + + String pserverParent = + "/aai/v9/cloud-infrastructure/pservers/pserver/{hostname}/lag-interfaces/lag-interface/{interface-name}/l-interfaces/l-interface/{l-interface.interface-name}"; + String cloudRegionParent = + "http://localhost:8888/aai/v38/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{interface-name}"; + String newVceParent = "/network/newvces/newvce/{vnf-id2}/l-interfaces/l-interface/{l-interface.interface-name}"; + + + AAIFluentTypeReverseLookup lookup = new AAIFluentTypeReverseLookup(); + + AAIObjectType type = lookup.fromName("l-interface", pserverParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals( + "/cloud-infrastructure/pservers/pserver/{hostname}/lag-interfaces/lag-interface/{interface-name}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + type = lookup.fromName("l-interface", cloudRegionParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals( + "/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + type = lookup.fromName("l-interface", newVceParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals("/network/newvces/newvce/{vnf-id2}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + + + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromParentUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromParentUriTest.java new file mode 100644 index 0000000000..3823392b01 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromParentUriTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectType; + +public class AAISimpleUriFromParentUriTest { + + + @Test + public void appendChildren() { + + AAIResourceUri parentUri = + AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "key1", "key2", "key3"); + + AAIResourceUri uri = new AAISimpleUri(parentUri, AAIObjectType.ALLOTTED_RESOURCE, "key4"); + + assertEquals("path appended", + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/allotted-resources/allotted-resource/key4", + uri.build().toString()); + + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromUriTest.java new file mode 100644 index 0000000000..9030789cfd --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriFromUriTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import javax.ws.rs.core.UriBuilder; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectType; + +public class AAISimpleUriFromUriTest { + + + + @Test + public void removeHost() { + + AAIResourceUri uri = new AAISimpleUri(AAIObjectType.UNKNOWN, + UriBuilder + .fromUri("https://localhost:8443/aai/v9/network/vces/vce/a9fec18e-1ea3-40e4-a6c0-a89b3de07053") + .build()); + + assertEquals("root and version removed", "/network/vces/vce/a9fec18e-1ea3-40e4-a6c0-a89b3de07053", + uri.build().toString()); + + } + + @Test + public void noChange() { + + AAIResourceUri uri = new AAISimpleUri(AAIObjectType.UNKNOWN, + UriBuilder.fromUri("/network/vces/vce/a9fec18e-1ea3-40e4-a6c0-a89b3de07053").build()); + + assertEquals("no change", "/network/vces/vce/a9fec18e-1ea3-40e4-a6c0-a89b3de07053", uri.build().toString()); + + } + + @Test + public void encodingPreserved() { + + AAIResourceUri uri = new AAISimpleUri(AAIObjectType.UNKNOWN, + UriBuilder.fromUri("/network/vces/vce/a9f%20%20ec18e-1ea3-40e4-a6c0-a89b3de07053").build()); + + assertEquals("encoding preserved", "/network/vces/vce/a9f%20%20ec18e-1ea3-40e4-a6c0-a89b3de07053", + uri.build().toString()); + + } + + @Test + public void beforeBuildEquality() { + + AAIResourceUri uri = new AAISimpleUri(AAIObjectType.VCE, + UriBuilder.fromUri("/network/vces/vce/a9f%20%20ec18e-1ea3-40e4-a6c0-a89b3de07053").build()); + + AAIResourceUri uri2 = new AAISimpleUri(AAIObjectType.VCE, "a9f ec18e-1ea3-40e4-a6c0-a89b3de07053"); + assertEquals("are equal", uri2, uri); + + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java new file mode 100644 index 0000000000..f8dd1726d3 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java @@ -0,0 +1,144 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Map; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder; +import org.onap.aaiclient.client.graphinventory.entities.uri.Depth; + +public class AAISimpleUriTest { + + + + @Test + public void relatedToTestPlural() { + AAIPluralResourceUri uri = + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER); + String uriOutput = uri.build().toString(); + assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput); + } + + @Test + public void relatedToTestSingular() { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1") + .relatedTo(AAIObjectType.PSERVER, "test2"); + String uriOutput = uri.build().toString(); + assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput); + } + + @Test + public void cloneTestSingular() { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1"); + AAIResourceUri cloned = uri.clone(); + assertEquals("/network/generic-vnfs/generic-vnf/test1", cloned.build().toString()); + + cloned.limit(2); + + assertNotEquals(uri.build().toString(), cloned.build().toString()); + } + + @Test + public void cloneTestPlural() { + AAISimplePluralUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF); + AAISimplePluralUri cloned = uri.clone(); + assertEquals("/network/generic-vnfs", cloned.build().toString()); + } + + @Test + public void cloneTestWithRelatedTo() { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1") + .relatedTo(AAIObjectType.PSERVER, "test2"); + String uriOutput = uri.clone().build().toString(); + assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput); + } + + @Test + public void cloneTestPluralWithRelatedTo() { + AAIPluralResourceUri uri = + AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER); + String uriOutput = uri.clone().build().toString(); + assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput); + } + + @Test + public void getKeysTest() { + AAIResourceUri uri = + AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, "cloud1", "cloud2", "tenant1", "vserver1"); + Map<String, String> keys = uri.getURIKeys(); + System.out.println(keys); + System.out.println(uri.build()); + assertEquals("vserver1", keys.get("vserver-id")); + } + + @Test + public void getEncodedKeyTest() { + AAIResourceUri uri = + AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "test1", "my value", "test3"); + Map<String, String> keys = uri.getURIKeys(); + + assertEquals("my value", keys.get("service-type")); + } + + @Test + public void serializeTest() throws IOException, ClassNotFoundException { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1"); + + uri.depth(Depth.ONE); + uri.limit(1); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos); + objectOutputStream.writeObject(uri); + objectOutputStream.flush(); + objectOutputStream.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + + ObjectInputStream objectInputStream = new ObjectInputStream(bis); + AAIResourceUri e2 = (AAIResourceUri) objectInputStream.readObject(); + objectInputStream.close(); + + uri.queryParam("test", "value"); + e2.queryParam("test", "value"); + + assertEquals(e2.build().toString(), uri.build().toString()); + } + + @Test + public void fluentBuilderTest() { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure() + .cloudRegion("cloud1", "cloud-id").tenant("tenant-id").vserver("vserver-id")); + + assertEquals( + "/cloud-infrastructure/cloud-regions/cloud-region/cloud1/cloud-id/tenants/tenant/tenant-id/vservers/vserver/vserver-id", + uri.build().toString()); + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactoryTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactoryTest.java new file mode 100644 index 0000000000..ec40b1b477 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactoryTest.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; + +public class AAIUriFactoryTest { + + @Test + public void testCreateResourceUri() { + + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "VIP(VelocitytoIP)"); + + String expected = "/network/generic-vnfs/generic-vnf/VIP%28VelocitytoIP%29"; + assertEquals(expected, uri.build().toString()); + } + + @Test + public void testCreateNodesUri() { + + AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectType.GENERIC_VNF, "VIP(VelocitytoIP)"); + + String expected = "/nodes/generic-vnfs/generic-vnf/VIP%28VelocitytoIP%29"; + assertEquals(expected, uri.build().toString()); + } + + @Test + public void testCreateResourceFromExistingURI() { + + AAIResourceUri uri = new AAISimpleUri(AAIObjectType.GENERIC_VNF, "VIP(VelocitytoIP)"); + AAIResourceUri uri2 = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.GENERIC_VNF, uri.build()); + + String expected = "/network/generic-vnfs/generic-vnf/VIP%28VelocitytoIP%29"; + assertEquals(expected, uri2.build().toString()); + } + + @Test + public void testCreateResourceURIForPluralsWithValues() { + + AAIPluralResourceUri uri = + AAIUriFactory.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, "customerId", "serviceType"); + + String expected = + "/business/customers/customer/customerId/service-subscriptions/service-subscription/serviceType/service-instances"; + assertEquals(expected, uri.build().toString()); + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUriTest.java new file mode 100644 index 0000000000..e6add8cbc1 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AllottedResourceLookupUriTest.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import javax.ws.rs.core.UriBuilder; +import org.junit.Test; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryPayloadException; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; + +public class AllottedResourceLookupUriTest { + + @Test + public void oneKey() + throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + + AllottedResourceLookupUri instance = new AllottedResourceLookupUri("key1"); + AllottedResourceLookupUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/allotted-resources/allotted-resource/key4") + .when(spy).getObjectById(any(Object.class)); + + final URI result = spy.locateAndBuild(); + final URI expected = UriBuilder.fromPath( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/allotted-resources/allotted-resource/key4") + .build(); + assertEquals("result is equal", expected, result); + + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java new file mode 100644 index 0000000000..2cbf5bb090 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/IncorrectNumberOfUriKeysTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.hamcrest.CoreMatchers.equalTo; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.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); + AAISimplePluralUri 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"); + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUriTest.java new file mode 100644 index 0000000000..514a349095 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/ServiceInstanceUriTest.java @@ -0,0 +1,301 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaiclient.client.aai.entities.uri; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Optional; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.UriBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aaiclient.client.aai.AAIClient; +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.aai.AAIResourcesClient; +import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; +import org.onap.aaiclient.client.defaultproperties.DefaultAAIPropertiesImpl; +import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryPayloadException; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException; +import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +@RunWith(MockitoJUnitRunner.class) +public class ServiceInstanceUriTest { + + private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/"; + + @Rule + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Spy + public AAIClient client; + + @InjectMocks + public AAIResourcesClient aaiClient = new AAIResourcesClient(); + + @Before + public void beforeTest() { + doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties(); + } + + @Test + public void found() throws IOException { + final String content = new String( + Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json"))); + + ServiceInstanceUri instance = new ServiceInstanceUri("key3"); + final Optional<String> result = instance.extractRelatedLink(content); + final String expected = + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3"; + assertEquals("result is equal", expected, result.get()); + + } + + @Test + public void oneKey() + throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3") + .when(spy).getObjectById(any(Object.class)); + + final URI result = spy.locateAndBuild(); + final URI expected = UriBuilder.fromPath( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3") + .build(); + assertEquals("result is equal", expected, result); + + } + + @Test + public void oneKeyQueryParams() + throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3") + .when(spy).getObjectById(any(Object.class)); + + final URI result = ((HttpAwareUri) spy.resourceVersion("1234")).locateAndBuild(); + final URI expected = UriBuilder.fromUri( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234") + .build(); + assertEquals("result is equal", expected, result); + + } + + @Test + public void oneKeyEncoded() + throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space") + .when(spy).getObjectById(any(Object.class)); + + final URI result = spy.locateAndBuild(); + final URI expected = UriBuilder.fromUri( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space") + .build(); + assertEquals("result is equal", expected, result); + + } + + @Test + public void oneKeyGetKeys() + throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space") + .when(spy).getObjectById(any(Object.class)); + + assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space")); + + } + + @Test + public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + String uri = + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3"; + doReturn(Optional.of(uri)).when(spy).getCachedValue(); + final URI result = ((HttpAwareUri) spy.resourceVersion("1234").clone()).locateAndBuild(); + final URI expected = UriBuilder.fromUri( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234") + .build(); + assertEquals("result is equal", expected, result); + } + + @Test + public void threeKey() throws IOException { + + ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3"); + final URI result = instance.build(); + final URI expected = UriBuilder.fromPath( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3") + .build(); + assertEquals("result is equal", expected, result); + + } + + @Test + public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + final String content = + new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json"))); + + ServiceInstanceUri instance = new ServiceInstanceUri("key3"); + ServiceInstanceUri spy = spy(instance); + AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class); + AAIResultWrapper wrapper = mock(AAIResultWrapper.class); + when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class), + ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper); + when(wrapper.getJson()).thenReturn(content); + when(spy.getResourcesClient()).thenReturn(mockResourcesClient); + exception.expect(GraphInventoryUriComputationException.class); + spy.locateAndBuild(); + + } + + @Test + public void cloneTest() { + ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3"); + final URI result = instance.build(); + final URI result2 = instance.clone().queryParam("something", "new").build(); + assertEquals("uris are not equal", false, result.toString().equals(result2.toString())); + + } + + @Test + public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key3"); + ServiceInstanceUri spy = spy(instance); + AAIResourcesClient client = aaiClient; + doReturn(client).when(spy).getResourcesClient(); + wireMockRule + .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn( + aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile(""))); + exception.expect(NotFoundException.class); + spy.locateAndBuild(); + } + + @Test + public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException, + GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key3"); + final String content = new String( + Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json"))); + + ServiceInstanceUri spy = spy(instance); + AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class); + AAIResultWrapper wrapper = mock(AAIResultWrapper.class); + when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class), + ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper); + when(wrapper.getJson()).thenReturn(content); + when(spy.getResourcesClient()).thenReturn(mockResourcesClient); + spy.locateAndBuild(); + instance = spy.clone(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos); + objectOutputStream.writeObject(instance); + objectOutputStream.flush(); + objectOutputStream.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + + ObjectInputStream objectInputStream = new ObjectInputStream(bis); + ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject(); + objectInputStream.close(); + + ServiceInstanceUri spy2 = spy(e2); + + assertEquals(spy2.build().toString(), instance.build().toString()); + + // use the cached value do not call out to external system + verify(spy2, times(0)).getResourcesClient(); + } + + @Test + public void relatedToTest() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + doReturn( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3") + .when(spy).getObjectById(any(Object.class)); + + final URI result = spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name").build(); + final URI expected = UriBuilder.fromUri( + "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/related-to/generic-vnfs?vnf-name=my-vnf-name") + .build(); + assertEquals("result is equal", expected, result); + } + + @Test + public void relatedToEqualityTestBeforeBuildTest() + throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key1"); + ServiceInstanceUri spy = spy(instance); + + final AAIPluralResourceUri result = + spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name"); + + assertEquals("result is equal", result, result); + } +} |