2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.aaiclient.client.aai.entities.uri;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertNotEquals;
 
  25 import java.io.ByteArrayInputStream;
 
  26 import java.io.ByteArrayOutputStream;
 
  27 import java.io.IOException;
 
  28 import java.io.ObjectInputStream;
 
  29 import java.io.ObjectOutputStream;
 
  31 import org.junit.Test;
 
  32 import org.onap.aaiclient.client.aai.AAIObjectPlurals;
 
  33 import org.onap.aaiclient.client.aai.AAIObjectType;
 
  34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
 
  35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
 
  36 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
 
  38 public class AAISimpleUriTest {
 
  43     public void relatedToTestPlural() {
 
  44         AAIPluralResourceUri uri =
 
  45                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
 
  46         String uriOutput = uri.build().toString();
 
  48         String expected = "/network/generic-vnfs/generic-vnf/test1/related-to/pservers";
 
  49         assertEquals(expected, uriOutput);
 
  51         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
 
  52                 .relatedTo(Types.PSERVERS.getFragment());
 
  53         uriOutput = uri.build().toString();
 
  54         assertEquals(expected, uriOutput);
 
  58     public void relatedToTestSingular() {
 
  59         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
 
  60                 .relatedTo(AAIObjectType.PSERVER, "test2");
 
  61         String uriOutput = uri.build().toString();
 
  63         String expected = "/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2";
 
  64         assertEquals(expected, uriOutput);
 
  66         uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
 
  67                 .relatedTo(Types.PSERVER.getFragment("test2"));
 
  69         uriOutput = uri.build().toString();
 
  71         assertEquals(expected, uriOutput);
 
  76     public void cloneTestSingular() {
 
  77         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
 
  78         AAIResourceUri cloned = uri.clone();
 
  79         assertEquals("/network/generic-vnfs/generic-vnf/test1", cloned.build().toString());
 
  83         assertNotEquals(uri.build().toString(), cloned.build().toString());
 
  87     public void cloneTestPlural() {
 
  88         AAISimplePluralUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF);
 
  89         AAISimplePluralUri cloned = uri.clone();
 
  90         assertEquals("/network/generic-vnfs", cloned.build().toString());
 
  94     public void cloneTestWithRelatedTo() {
 
  95         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
 
  96                 .relatedTo(AAIObjectType.PSERVER, "test2");
 
  97         String uriOutput = uri.clone().build().toString();
 
  98         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput);
 
 102     public void cloneTestPluralWithRelatedTo() {
 
 103         AAIPluralResourceUri uri =
 
 104                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
 
 105         String uriOutput = uri.clone().build().toString();
 
 106         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput);
 
 110     public void getKeysTest() {
 
 112                 AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, "cloud1", "cloud2", "tenant1", "vserver1");
 
 113         Map<String, String> keys = uri.getURIKeys();
 
 114         System.out.println(keys);
 
 115         System.out.println(uri.build());
 
 116         assertEquals("vserver1", keys.get("vserver-id"));
 
 120     public void getEncodedKeyTest() {
 
 122                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "test1", "my value", "test3");
 
 123         Map<String, String> keys = uri.getURIKeys();
 
 125         assertEquals("my value", keys.get("service-type"));
 
 129     public void serializeTest() throws IOException, ClassNotFoundException {
 
 130         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
 
 132         uri.depth(Depth.ONE);
 
 134         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
 136         ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
 
 137         objectOutputStream.writeObject(uri);
 
 138         objectOutputStream.flush();
 
 139         objectOutputStream.close();
 
 141         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
 
 143         ObjectInputStream objectInputStream = new ObjectInputStream(bis);
 
 144         AAIResourceUri e2 = (AAIResourceUri) objectInputStream.readObject();
 
 145         objectInputStream.close();
 
 147         uri.queryParam("test", "value");
 
 148         e2.queryParam("test", "value");
 
 150         assertEquals(e2.build().toString(), uri.build().toString());
 
 154     public void fluentBuilderTest() {
 
 155         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure()
 
 156                 .cloudRegion("cloud1", "cloud-id").tenant("tenant-id").vserver("vserver-id"));
 
 159                 "/cloud-infrastructure/cloud-regions/cloud-region/cloud1/cloud-id/tenants/tenant/tenant-id/vservers/vserver/vserver-id",
 
 160                 uri.build().toString());