Removing jackson to mitigate cve-2017-4995
[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 java.util.ArrayList;
20 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.onap.aai.model.GenericVnf;
25 import org.onap.aai.model.Relationship;
26 import org.onap.aai.model.RelationshipData;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31 import org.slf4j.Logger;
32
33 import static io.reactivex.Observable.error;
34 import static junit.framework.TestCase.assertEquals;
35
36 public class TestAbstractManager extends TestBase {
37     @Mock
38     private AAIRestApiProvider aaiRestApiProvider;
39     private DummyManager dummyManager;
40
41     @Before
42     public void init() {
43         dummyManager = new DummyManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
44     }
45
46     /**
47      * if the REST resource does not exists the provided instance is used
48      */
49     @Test
50     public void testIfResourceDoesNotExists() throws Exception {
51         GenericVnf newInstance = new GenericVnf();
52         //when
53         GenericVnf actualInstance = dummyManager.createOrGet(error(new RuntimeException()), 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 = new GenericVnf();
64         GenericVnf existingInstance = new GenericVnf();
65         //when
66         GenericVnf actualInstance = dummyManager.createOrGet(buildObservable(existingInstance), newInstance);
67         //verify
68         assertEquals(existingInstance, actualInstance);
69     }
70
71     /**
72      * Test relationship data builder
73      */
74     @Test
75     public void testBuildRelationshipData() {
76         RelationshipData relationshipData = AbstractManager.buildRelationshipData("key", "value");
77         assertEquals("key", relationshipData.getRelationshipKey());
78         assertEquals("value", relationshipData.getRelationshipValue());
79     }
80
81     /**
82      * test mandatory value extraction
83      */
84     @Test
85     public void testExtractMandatoryValue() {
86         JsonObject object = new JsonObject();
87         object.addProperty("key", "value");
88         assertEquals("value", AbstractManager.extractMandatoryValue(object, "key"));
89     }
90
91     /**
92      * the same relation is replaced
93      */
94     @Test
95     public void testAddSingletonRelationForExisting() {
96         List<Relationship> relationships = new ArrayList<>();
97         Relationship relation = new Relationship();
98         relation.setRelatedTo("unknownRelation");
99         relation.setRelationshipData(new ArrayList<>());
100         relationships.add(relation);
101         Relationship sameRelation = new Relationship();
102         sameRelation.setRelatedTo("relatedTo");
103         relationships.add(sameRelation);
104         RelationshipData data = new RelationshipData();
105         data.setRelationshipValue("v");
106         data.setRelationshipKey("k");
107         sameRelation.setRelationshipData(new ArrayList<>());
108         sameRelation.getRelationshipData().add(data);
109
110         Relationship newRelation = new Relationship();
111         newRelation.setRelatedTo("relatedTo");
112         RelationshipData data2 = new RelationshipData();
113         data2.setRelationshipValue("v2");
114         data2.setRelationshipKey("k2");
115         newRelation.setRelationshipData(new ArrayList<>());
116         newRelation.getRelationshipData().add(data2);
117
118         //when
119         AbstractManager.addSingletonRelation(relationships, newRelation);
120         //verify
121
122         assertEquals(2, relationships.size());
123         assertEquals(1, relationships.get(1).getRelationshipData().size());
124         assertEquals("k2", relationships.get(1).getRelationshipData().get(0).getRelationshipKey());
125         assertEquals("v2", relationships.get(1).getRelationshipData().get(0).getRelationshipValue());
126     }
127
128     /**
129      * the missing relation is created
130      */
131     @Test
132     public void testAddSingletonRelation() {
133         Relationship relation = new Relationship();
134         relation.setRelatedTo("unknownRelation");
135         List<Relationship> relationships = new ArrayList<>();
136
137         relationships.add(relation);
138
139         Relationship newRelation = new Relationship();
140         newRelation.setRelatedTo("relatedTo");
141         RelationshipData data2 = new RelationshipData();
142         ;
143         data2.setRelationshipValue("v2");
144         data2.setRelationshipKey("k2");
145         newRelation.setRelationshipData(new ArrayList<>());
146         newRelation.getRelationshipData().add(data2);
147
148         //when
149         AbstractManager.addSingletonRelation(relationships, newRelation);
150         //verify
151         assertEquals(2, relationships.size());
152         assertEquals(1, relationships.get(1).getRelationshipData().size());
153         assertEquals("k2", relationships.get(1).getRelationshipData().get(0).getRelationshipKey());
154         assertEquals("v2", relationships.get(1).getRelationshipData().get(0).getRelationshipValue());
155     }
156
157     /**
158      * the same relation is replaced
159      */
160     @Test
161     public void testAddMissingRelationForExisting() {
162         List<Relationship> relationships = new ArrayList<>();
163         Relationship relation = new Relationship();
164         relation.setRelatedTo("unknownRelation");
165         relationships.add(relation);
166         Relationship sameRelation = new Relationship();
167         sameRelation.setRelatedTo("relatedTo");
168         relationships.add(sameRelation);
169         RelationshipData data = new RelationshipData();
170         ;
171         data.setRelationshipValue("v");
172         data.setRelationshipKey("k");
173         sameRelation.setRelationshipData(new ArrayList<>());
174         sameRelation.getRelationshipData().add(data);
175
176         Relationship newRelation = new Relationship();
177         newRelation.setRelatedTo("relatedTo");
178         RelationshipData data2 = new RelationshipData();
179         ;
180         data2.setRelationshipValue("v2");
181         data2.setRelationshipKey("k2");
182         newRelation.setRelationshipData(new ArrayList<>());
183         newRelation.getRelationshipData().add(data2);
184
185         //when
186         AbstractManager.addMissingRelation(relationships, newRelation);
187         //verify
188
189         assertEquals(3, relationships.size());
190         assertEquals(1, relationships.get(1).getRelationshipData().size());
191         assertEquals("k", relationships.get(1).getRelationshipData().get(0).getRelationshipKey());
192         assertEquals("v", relationships.get(1).getRelationshipData().get(0).getRelationshipValue());
193         assertEquals("k2", relationships.get(2).getRelationshipData().get(0).getRelationshipKey());
194         assertEquals("v2", relationships.get(2).getRelationshipData().get(0).getRelationshipValue());
195     }
196
197     /**
198      * adding the same relation is not duplicated
199      */
200     @Test
201     public void testAddMissingRelation() {
202         Relationship relation = new Relationship();
203         relation.setRelatedTo("unknownRelation");
204         List<Relationship> relationships = new ArrayList<>();
205         relationships.add(relation);
206
207         Relationship sameRelation = new Relationship();
208         sameRelation.setRelatedTo("relatedTo");
209         relationships.add(sameRelation);
210         RelationshipData data = new RelationshipData();
211         ;
212         data.setRelationshipValue("v");
213         data.setRelationshipKey("k");
214         sameRelation.setRelationshipData(new ArrayList<>());
215         sameRelation.getRelationshipData().add(data);
216
217         Relationship newRelation = new Relationship();
218         newRelation.setRelatedTo("relatedTo");
219         RelationshipData data2 = new RelationshipData();
220         ;
221         data2.setRelationshipValue("v");
222         data2.setRelationshipKey("k");
223         newRelation.setRelationshipData(new ArrayList<>());
224         newRelation.getRelationshipData().add(data2);
225
226         //when
227         AbstractManager.addMissingRelation(relationships, newRelation);
228         //verify
229         assertEquals(2, relationships.size());
230         assertEquals(1, relationships.get(1).getRelationshipData().size());
231         assertEquals("k", relationships.get(1).getRelationshipData().get(0).getRelationshipKey());
232         assertEquals("v", relationships.get(1).getRelationshipData().get(0).getRelationshipValue());
233     }
234
235     class DummyManager extends AbstractManager {
236
237         DummyManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
238             super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
239         }
240
241         @Override
242         protected Logger getLogger() {
243             return logger;
244         }
245     }
246
247
248 }