move aai-client into its own project
[so.git] / graph-inventory / aai-client / src / test / java / org / onap / so / client / aai / entities / uri / AAISimpleUriTest.java
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.so.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.so.client.aai.AAIObjectPlurals;
33 import org.onap.so.client.aai.AAIObjectType;
34 import org.onap.so.client.graphinventory.entities.uri.Depth;
35
36 public class AAISimpleUriTest {
37
38
39
40     @Test
41     public void relatedToTestPlural() {
42         AAIPluralResourceUri uri =
43                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
44         String uriOutput = uri.build().toString();
45         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput);
46     }
47
48     @Test
49     public void relatedToTestSingular() {
50         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
51                 .relatedTo(AAIObjectType.PSERVER, "test2");
52         String uriOutput = uri.build().toString();
53         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput);
54     }
55
56     @Test
57     public void cloneTestSingular() {
58         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
59         AAIResourceUri cloned = uri.clone();
60         assertEquals("/network/generic-vnfs/generic-vnf/test1", cloned.build().toString());
61
62         cloned.limit(2);
63
64         assertNotEquals(uri.build().toString(), cloned.build().toString());
65     }
66
67     @Test
68     public void cloneTestPlural() {
69         AAISimplePluralUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF);
70         AAISimplePluralUri cloned = uri.clone();
71         assertEquals("/network/generic-vnfs", cloned.build().toString());
72     }
73
74     @Test
75     public void cloneTestWithRelatedTo() {
76         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1")
77                 .relatedTo(AAIObjectType.PSERVER, "test2");
78         String uriOutput = uri.clone().build().toString();
79         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers/pserver/test2", uriOutput);
80     }
81
82     @Test
83     public void cloneTestPluralWithRelatedTo() {
84         AAIPluralResourceUri uri =
85                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1").relatedTo(AAIObjectPlurals.PSERVER);
86         String uriOutput = uri.clone().build().toString();
87         assertEquals("/network/generic-vnfs/generic-vnf/test1/related-to/pservers", uriOutput);
88     }
89
90     @Test
91     public void getKeysTest() {
92         AAIResourceUri uri =
93                 AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, "cloud1", "cloud2", "tenant1", "vserver1");
94         Map<String, String> keys = uri.getURIKeys();
95         System.out.println(keys);
96         System.out.println(uri.build());
97         assertEquals("vserver1", keys.get("vserver-id"));
98     }
99
100     @Test
101     public void getEncodedKeyTest() {
102         AAIResourceUri uri =
103                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "test1", "my value", "test3");
104         Map<String, String> keys = uri.getURIKeys();
105
106         assertEquals("my value", keys.get("service-type"));
107     }
108
109     @Test
110     public void serializeTest() throws IOException, ClassNotFoundException {
111         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
112
113         uri.depth(Depth.ONE);
114         uri.limit(1);
115         ByteArrayOutputStream bos = new ByteArrayOutputStream();
116
117         ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
118         objectOutputStream.writeObject(uri);
119         objectOutputStream.flush();
120         objectOutputStream.close();
121
122         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
123
124         ObjectInputStream objectInputStream = new ObjectInputStream(bis);
125         AAIResourceUri e2 = (AAIResourceUri) objectInputStream.readObject();
126         objectInputStream.close();
127
128         uri.queryParam("test", "value");
129         e2.queryParam("test", "value");
130
131         assertEquals(e2.build().toString(), uri.build().toString());
132     }
133 }