2495daf1f664e4fce0cd25a2dd7a1a5344283d95
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / TestAbstractManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.google.gson.JsonObject;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.onap.aai.domain.yang.v11.*;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
27 import org.slf4j.Logger;
28
29 import java.util.NoSuchElementException;
30
31 import static junit.framework.TestCase.assertEquals;
32 import static org.mockito.Mockito.when;
33
34 public class TestAbstractManager extends TestBase {
35     private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
36     @Mock
37     private AAIRestApiProvider aaiRestApiProvider;
38     private DummyManager dummyManager;
39
40     @Before
41     public void init() {
42         dummyManager = new DummyManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
43     }
44
45     /**
46      * if the REST resource does not exists the provided instance is used
47      */
48     @Test
49     public void testIfResourceDoesNotExists() throws Exception {
50         GenericVnf newInstance = OBJECT_FACTORY.createGenericVnf();
51         when(aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "url", GenericVnf.class)).thenThrow(new NoSuchElementException());
52         //when
53         GenericVnf actualInstance = dummyManager.createOrGet(AAIRestApiProvider.AAIService.CLOUD, "url", newInstance);
54         //verify
55         assertEquals(newInstance, actualInstance);
56     }
57
58     /**
59      * if the REST resource exists it is not recreated
60      */
61     @Test
62     public void testIfResourceExists() throws Exception {
63         GenericVnf newInstance = OBJECT_FACTORY.createGenericVnf();
64         GenericVnf existingInstance = OBJECT_FACTORY.createGenericVnf();
65         existingInstance.setVnfId("id");
66         when(aaiRestApiProvider.get(logger, AAIRestApiProvider.AAIService.CLOUD, "url", GenericVnf.class)).thenReturn(existingInstance);
67         //when
68         GenericVnf actualInstance = dummyManager.createOrGet(AAIRestApiProvider.AAIService.CLOUD, "url", newInstance);
69         //verify
70         assertEquals(existingInstance, actualInstance);
71     }
72
73     @Test
74     public void testBuildRelationshipData() {
75         RelationshipData relationshipData = AbstractManager.buildRelationshipData("key", "value");
76         assertEquals("key", relationshipData.getRelationshipKey());
77         assertEquals("value", relationshipData.getRelationshipValue());
78     }
79
80     @Test
81     public void testExtractMandatoryValue() {
82         JsonObject object = new JsonObject();
83         object.addProperty("key", "value");
84         assertEquals("value", AbstractManager.extractMandatoryValue(object, "key"));
85     }
86
87     /**
88      * the same relation is replaced
89      */
90     @Test
91     public void testAddSingletonRelationForExisting() {
92         RelationshipList relations = OBJECT_FACTORY.createRelationshipList();
93         Relationship relation = OBJECT_FACTORY.createRelationship();
94         relation.setRelatedTo("unknownRelation");
95         relations.getRelationship().add(relation);
96         Relationship sameRelation = OBJECT_FACTORY.createRelationship();
97         sameRelation.setRelatedTo("relatedTo");
98         relations.getRelationship().add(sameRelation);
99         RelationshipData data = OBJECT_FACTORY.createRelationshipData();
100         data.setRelationshipValue("v");
101         data.setRelationshipKey("k");
102         sameRelation.getRelationshipData().add(data);
103
104         Relationship newRelation = OBJECT_FACTORY.createRelationship();
105         newRelation.setRelatedTo("relatedTo");
106         RelationshipData data2 = OBJECT_FACTORY.createRelationshipData();
107         data2.setRelationshipValue("v2");
108         data2.setRelationshipKey("k2");
109         newRelation.getRelationshipData().add(data2);
110
111         //when
112         AbstractManager.addSingletonRelation(relations, newRelation);
113         //verify
114
115         assertEquals(2, relations.getRelationship().size());
116         assertEquals(1, relations.getRelationship().get(1).getRelationshipData().size());
117         assertEquals("k2", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipKey());
118         assertEquals("v2", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipValue());
119     }
120
121     /**
122      * the missing relation is created
123      */
124     @Test
125     public void testAddSingletonRelation() {
126         RelationshipList relations = OBJECT_FACTORY.createRelationshipList();
127         Relationship relation = OBJECT_FACTORY.createRelationship();
128         relation.setRelatedTo("unknownRelation");
129         relations.getRelationship().add(relation);
130
131         Relationship newRelation = OBJECT_FACTORY.createRelationship();
132         newRelation.setRelatedTo("relatedTo");
133         RelationshipData data2 = OBJECT_FACTORY.createRelationshipData();
134         data2.setRelationshipValue("v2");
135         data2.setRelationshipKey("k2");
136         newRelation.getRelationshipData().add(data2);
137
138         //when
139         AbstractManager.addSingletonRelation(relations, newRelation);
140         //verify
141         assertEquals(2, relations.getRelationship().size());
142         assertEquals(1, relations.getRelationship().get(1).getRelationshipData().size());
143         assertEquals("k2", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipKey());
144         assertEquals("v2", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipValue());
145     }
146
147     /**
148      * the same relation is replaced
149      */
150     @Test
151     public void testAddMissingRelationForExisting() {
152         RelationshipList relations = OBJECT_FACTORY.createRelationshipList();
153         Relationship relation = OBJECT_FACTORY.createRelationship();
154         relation.setRelatedTo("unknownRelation");
155         relations.getRelationship().add(relation);
156         Relationship sameRelation = OBJECT_FACTORY.createRelationship();
157         sameRelation.setRelatedTo("relatedTo");
158         relations.getRelationship().add(sameRelation);
159         RelationshipData data = OBJECT_FACTORY.createRelationshipData();
160         data.setRelationshipValue("v");
161         data.setRelationshipKey("k");
162         sameRelation.getRelationshipData().add(data);
163
164         Relationship newRelation = OBJECT_FACTORY.createRelationship();
165         newRelation.setRelatedTo("relatedTo");
166         RelationshipData data2 = OBJECT_FACTORY.createRelationshipData();
167         data2.setRelationshipValue("v2");
168         data2.setRelationshipKey("k2");
169         newRelation.getRelationshipData().add(data2);
170
171         //when
172         AbstractManager.addMissingRelation(relations, newRelation);
173         //verify
174
175         assertEquals(3, relations.getRelationship().size());
176         assertEquals(1, relations.getRelationship().get(1).getRelationshipData().size());
177         assertEquals("k", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipKey());
178         assertEquals("v", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipValue());
179         assertEquals("k2", relations.getRelationship().get(2).getRelationshipData().get(0).getRelationshipKey());
180         assertEquals("v2", relations.getRelationship().get(2).getRelationshipData().get(0).getRelationshipValue());
181     }
182
183     /**
184      * adding the same relation is not duplicated
185      */
186     @Test
187     public void testAddMissingRelation() {
188         RelationshipList relations = OBJECT_FACTORY.createRelationshipList();
189         Relationship relation = OBJECT_FACTORY.createRelationship();
190         relation.setRelatedTo("unknownRelation");
191         relations.getRelationship().add(relation);
192
193         Relationship sameRelation = OBJECT_FACTORY.createRelationship();
194         sameRelation.setRelatedTo("relatedTo");
195         relations.getRelationship().add(sameRelation);
196         RelationshipData data = OBJECT_FACTORY.createRelationshipData();
197         data.setRelationshipValue("v");
198         data.setRelationshipKey("k");
199         sameRelation.getRelationshipData().add(data);
200
201         Relationship newRelation = OBJECT_FACTORY.createRelationship();
202         newRelation.setRelatedTo("relatedTo");
203         RelationshipData data2 = OBJECT_FACTORY.createRelationshipData();
204         data2.setRelationshipValue("v");
205         data2.setRelationshipKey("k");
206         newRelation.getRelationshipData().add(data2);
207
208         //when
209         AbstractManager.addMissingRelation(relations, newRelation);
210         //verify
211         assertEquals(2, relations.getRelationship().size());
212         assertEquals(1, relations.getRelationship().get(1).getRelationshipData().size());
213         assertEquals("k", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipKey());
214         assertEquals("v", relations.getRelationship().get(1).getRelationshipData().get(0).getRelationshipValue());
215     }
216
217     class DummyManager extends AbstractManager {
218
219         DummyManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
220             super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
221         }
222
223         @Override
224         protected Logger getLogger() {
225             return logger;
226         }
227     }
228
229
230 }