f8dd1726d34d7216bc78ec8e8a4aacc997f3e422
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.aaiclient.client.aai.entities.uri;
22
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;
30 import java.util.Map;
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.graphinventory.entities.uri.Depth;
36
37 public class AAISimpleUriTest {
38
39
40
41     @Test
42     public void relatedToTestPlural() {
43         AAIPluralResourceUri uri =
44                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
45         String uriOutput = uri.build().toString();
46         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput);
47     }
48
49     @Test
50     public void relatedToTestSingular() {
51         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
52                 .relatedTo(AAIObjectType.PSERVER, "test2");
53         String uriOutput = uri.build().toString();
54         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput);
55     }
56
57     @Test
58     public void cloneTestSingular() {
59         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
60         AAIResourceUri cloned = uri.clone();
61         assertEquals("/network/generic-vnfs/generic-vnf/test1", cloned.build().toString());
62
63         cloned.limit(2);
64
65         assertNotEquals(uri.build().toString(), cloned.build().toString());
66     }
67
68     @Test
69     public void cloneTestPlural() {
70         AAISimplePluralUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF);
71         AAISimplePluralUri cloned = uri.clone();
72         assertEquals("/network/generic-vnfs", cloned.build().toString());
73     }
74
75     @Test
76     public void cloneTestWithRelatedTo() {
77         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
78                 .relatedTo(AAIObjectType.PSERVER, "test2");
79         String uriOutput = uri.clone().build().toString();
80         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput);
81     }
82
83     @Test
84     public void cloneTestPluralWithRelatedTo() {
85         AAIPluralResourceUri uri =
86                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
87         String uriOutput = uri.clone().build().toString();
88         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput);
89     }
90
91     @Test
92     public void getKeysTest() {
93         AAIResourceUri uri =
94                 AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, "cloud1", "cloud2", "tenant1", "vserver1");
95         Map<String, String> keys = uri.getURIKeys();
96         System.out.println(keys);
97         System.out.println(uri.build());
98         assertEquals("vserver1", keys.get("vserver-id"));
99     }
100
101     @Test
102     public void getEncodedKeyTest() {
103         AAIResourceUri uri =
104                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "test1", "my value", "test3");
105         Map<String, String> keys = uri.getURIKeys();
106
107         assertEquals("my value", keys.get("service-type"));
108     }
109
110     @Test
111     public void serializeTest() throws IOException, ClassNotFoundException {
112         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
113
114         uri.depth(Depth.ONE);
115         uri.limit(1);
116         ByteArrayOutputStream bos = new ByteArrayOutputStream();
117
118         ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
119         objectOutputStream.writeObject(uri);
120         objectOutputStream.flush();
121         objectOutputStream.close();
122
123         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
124
125         ObjectInputStream objectInputStream = new ObjectInputStream(bis);
126         AAIResourceUri e2 = (AAIResourceUri) objectInputStream.readObject();
127         objectInputStream.close();
128
129         uri.queryParam("test", "value");
130         e2.queryParam("test", "value");
131
132         assertEquals(e2.build().toString(), uri.build().toString());
133     }
134
135     @Test
136     public void fluentBuilderTest() {
137         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure()
138                 .cloudRegion("cloud1", "cloud-id").tenant("tenant-id").vserver("vserver-id"));
139
140         assertEquals(
141                 "/cloud-infrastructure/cloud-regions/cloud-region/cloud1/cloud-id/tenants/tenant/tenant-id/vservers/vserver/vserver-id",
142                 uri.build().toString());
143     }
144 }