2a0e9df9ed31c1bd24910984798118eefd458161
[so.git] / common / 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.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.collection.IsEmptyCollection.empty;
25 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
26 import static org.junit.Assert.assertEquals;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
35 import java.util.Map;
36
37 import org.junit.Test;
38 import org.onap.so.client.aai.AAIObjectPlurals;
39 import org.onap.so.client.aai.AAIObjectType;
40 import org.onap.so.client.graphinventory.entities.uri.Depth;
41 import org.onap.so.client.graphinventory.entities.uri.SimpleUri;
42
43 public class AAISimpleUriTest {
44
45         
46         
47         @Test
48         public void relatedToTestPlural() {
49                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
50                 uri.relatedTo(AAIObjectPlurals.PSERVER);
51                 String uriOutput = uri.build().toString();
52                 assertEquals(true, uriOutput.contains("related-to"));
53         }
54         
55         @Test
56         public void relatedToTestSingular() {
57                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
58                 uri.relatedTo(AAIObjectType.PSERVER, "test2");
59                 String uriOutput = uri.build().toString();
60                 assertEquals(true, uriOutput.contains("related-to"));
61         }
62         
63         @Test
64         public void cloneTestSingular() {
65                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
66                 AAIResourceUri cloned = uri.clone();
67                 Map<String,String> keys = cloned.getURIKeys();
68                 assertThat(keys.values(), contains("test1"));
69         }
70         
71         @Test
72         public void cloneTestPlural() {
73                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF);
74                 AAIResourceUri cloned = uri.clone();
75                 Map<String,String> keys = cloned.getURIKeys();
76                 assertThat(keys.values(), empty());
77         }
78         
79         @Test
80         public void getKeysTest() {
81                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, "cloud1", "cloud2", "tenant1", "vserver1");
82                 Map<String,String> keys = uri.getURIKeys();
83                 System.out.println(keys);
84                 System.out.println(uri.build());
85                 assertEquals("vserver1", keys.get("vserver-id"));
86         }
87         
88         @Test
89         public void getEncodedKeyTest() {
90                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "test1", "my value", "test3");
91                 Map<String,String> keys = uri.getURIKeys();
92                 
93                 assertEquals("my value", keys.get("service-type"));
94         }
95         
96         @Test
97         public void serializeTest() throws IOException, ClassNotFoundException {
98                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
99                 
100                 uri.depth(Depth.ONE);
101                 uri.limit(1);
102                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
103
104             ObjectOutputStream objectOutputStream 
105               = new ObjectOutputStream(bos);
106             objectOutputStream.writeObject(uri);
107             objectOutputStream.flush();
108             objectOutputStream.close();
109             
110             ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
111            
112             ObjectInputStream objectInputStream 
113               = new ObjectInputStream(bis);
114             AAIResourceUri e2 = (AAIResourceUri) objectInputStream.readObject();
115             objectInputStream.close();
116             
117             uri.queryParam("test", "value");
118             e2.queryParam("test", "value");
119
120             assertEquals(e2.build().toString(), uri.build().toString());
121         }
122 }