Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / VersionedScenariosTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.aai.serialization.db;
22
23 import org.apache.tinkerpop.gremlin.structure.Graph;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraphFactory;
26 import org.junit.*;
27 import org.junit.rules.ExpectedException;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.edges.EdgeIngestor;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.introspection.ModelType;
35 import org.onap.aai.parsers.query.QueryParser;
36 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
37 import org.onap.aai.serialization.engines.QueryStyle;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
39 import org.onap.aai.setup.SchemaVersion;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.test.annotation.DirtiesContext;
42
43 import java.io.UnsupportedEncodingException;
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
49
50 import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
51 import static org.hamcrest.Matchers.*;
52 import static org.junit.Assert.assertThat;
53 import static org.junit.Assert.assertTrue;
54
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class VersionedScenariosTest extends AAISetup {
57
58     // to use, set thrown.expect to whatever your test needs
59     // this line establishes default of expecting no exception to be thrown
60     @Rule
61     public ExpectedException thrown = ExpectedException.none();
62
63     protected static Graph graph;
64
65     @Autowired
66     protected EdgeSerializer edgeSer;
67     @Autowired
68     protected EdgeIngestor ei;
69
70     private SchemaVersion version;
71     private final ModelType introspectorFactoryType = ModelType.MOXY;
72     private Loader loader;
73     private TransactionalGraphEngine engine;
74
75     public QueryStyle queryStyle = QueryStyle.TRAVERSAL_URI;
76
77     public static final String SOURCE_OF_TRUTH = "VersionedScenariosTest";
78     private static final String gvnfUri = "/network/generic-vnfs/generic-vnf/gvnf" + SOURCE_OF_TRUTH;
79     private static final String llDefaultUri = "/network/logical-links/logical-link/llDefault";
80     private static final String lintSourceUri = gvnfUri + "/l-interfaces/l-interface/source";
81     private static final String lintDestinationUri = gvnfUri + "/l-interfaces/l-interface/destination";
82     private static final String llLabeledUri = "/network/logical-links/logical-link/llLabeled";
83
84     @BeforeClass
85     public static void init() {
86         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
87
88     }
89
90     @Before
91     public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
92         version = schemaVersions.getDefaultVersion();
93         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
94         engine = new JanusGraphDBEngine(queryStyle, loader);
95         initData();
96     }
97
98     @After
99     public void cleanup() {
100         engine.rollback();
101     }
102
103     /**
104      * Using latest version (see schema-ingest.properties)
105      * Create generic-vnf with l-interfaces source, destination
106      * Create logical-link with relationship to interfaces source, destination (will use the default labels)
107      * Create logical-link with relationship to interfaces source, destination with specific labels
108      */
109     private void initData() throws UnsupportedEncodingException, AAIException, URISyntaxException {
110         engine.startTransaction();
111         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
112
113
114         Introspector gvnf = loader.introspectorFromName("generic-vnf");
115         gvnf.setValue("vnf-id", "gvnf" + SOURCE_OF_TRUTH);
116         gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
117
118         Introspector lintSource = loader.introspectorFromName("l-interface");
119         lintSource.setValue("interface-name", "source");
120
121         Introspector lintDestination = loader.introspectorFromName("l-interface");
122         lintDestination.setValue("interface-name", "destination");
123
124         List<Object> lIntList = new ArrayList<>();
125         lIntList.add(lintSource.getUnderlyingObject());
126         lIntList.add(lintDestination.getUnderlyingObject());
127         Introspector lints = loader.introspectorFromName("l-interfaces");
128         lints.setValue("l-interface", lIntList);
129         gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
130
131         Vertex gvnfV = serializer.createNewVertex(gvnf);
132         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfUri));
133         serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
134
135         assertTrue("generic-vnf created", engine.tx().traversal().V()
136             .has(AAIProperties.AAI_URI, gvnfUri)
137             .hasNext());
138         assertTrue("source created", engine.tx().traversal().V()
139             .has(AAIProperties.AAI_URI, lintSourceUri)
140             .hasNext());
141         assertTrue("destination created", engine.tx().traversal().V()
142             .has(AAIProperties.AAI_URI, lintDestinationUri)
143             .hasNext());
144
145
146
147         Introspector llDefault = loader.introspectorFromName("logical-link");
148         llDefault.setValue("link-name", "llDefault");
149         List<Object> relList = new ArrayList<>();
150         Introspector relationship = loader.introspectorFromName("relationship");
151         //relationship.setValue("related-to", "l-interface");
152         relationship.setValue("related-link", lintSourceUri);
153         relList.add(relationship.getUnderlyingObject());
154         relationship = loader.introspectorFromName("relationship");
155         //relationship.setValue("related-to", "l-interface");
156         relationship.setValue("related-link", lintDestinationUri);
157         relList.add(relationship.getUnderlyingObject());
158         Introspector relationshipList = loader.introspectorFromName("relationship-list");
159         relationshipList.setValue("relationship", relList);
160         llDefault.setValue("relationship-list", relationshipList.getUnderlyingObject());
161
162         Vertex llDefaultV = serializer.createNewVertex(llDefault);
163         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(llDefaultUri));
164         serializer.serializeToDb(llDefault, llDefaultV, uriQuery, "logical-link", llDefault.marshal(false));
165
166         assertTrue("logical-link created", engine.tx().traversal().V()
167             .has(AAIProperties.AAI_URI, llDefaultUri)
168             .hasNext());
169         assertTrue("default source relationship created",engine.tx().traversal().V()
170             .has(AAIProperties.AAI_URI, llDefaultUri)
171             .both()
172             .has(AAIProperties.AAI_URI, lintSourceUri)
173             .hasNext());
174         assertTrue("default destination relationship created", engine.tx().traversal().V()
175             .has(AAIProperties.AAI_URI, llDefaultUri)
176             .both()
177             .has(AAIProperties.AAI_URI, lintDestinationUri)
178             .hasNext());
179
180
181         Introspector llLabeled = loader.introspectorFromName("logical-link");
182         llLabeled.setValue("link-name", "llLabeled");
183         relList = new ArrayList<>();
184         relationship = loader.introspectorFromName("relationship");
185         relationship.setValue("related-to", "l-interface");
186         relationship.setValue("relationship-label", "org.onap.relationships.inventory.Source");
187         relationship.setValue("related-link", lintSourceUri);
188         relList.add(relationship.getUnderlyingObject());
189         relationship = loader.introspectorFromName("relationship");
190         relationship.setValue("related-to", "l-interface");
191         relationship.setValue("relationship-label", "org.onap.relationships.inventory.Destination");
192         relationship.setValue("related-link", lintDestinationUri);
193         relList.add(relationship.getUnderlyingObject());
194         relationshipList = loader.introspectorFromName("relationship-list");
195         relationshipList.setValue("relationship", relList);
196         llLabeled.setValue("relationship-list", relationshipList.getUnderlyingObject());
197
198         Vertex llLabeledV = serializer.createNewVertex(llLabeled);
199         uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(llLabeledUri));
200         serializer.serializeToDb(llLabeled, llLabeledV, uriQuery, "logical-link", llLabeled.marshal(false));
201
202         assertTrue("logical-link created", engine.tx().traversal().V()
203             .has(AAIProperties.AAI_URI, llLabeledUri)
204             .hasNext());
205         assertTrue("labeled source relationship created",engine.tx().traversal().V()
206             .has(AAIProperties.AAI_URI, llLabeledUri)
207             .both("org.onap.relationships.inventory.Source")
208             .has(AAIProperties.AAI_URI, lintSourceUri)
209             .hasNext());
210         assertTrue("labeled destination relationship created", engine.tx().traversal().V()
211             .has(AAIProperties.AAI_URI, llLabeledUri)
212             .both("org.onap.relationships.inventory.Destination")
213             .has(AAIProperties.AAI_URI, lintDestinationUri)
214             .hasNext());
215     }
216
217
218     @Test
219     public void verifyRelsOfLatestViewOfGenericVnf() throws AAIException, UnsupportedEncodingException {
220         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
221
222         String gvnfLatestView = serializer.getLatestVersionView(
223             engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next()).marshal(false);
224         assertThat(gvnfLatestView,
225             hasJsonPath(
226                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*]",
227                 hasSize(4)
228             ));
229         assertThat(gvnfLatestView,
230             hasJsonPath(
231                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
232                 containsInAnyOrder(
233                     "/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
234                     "/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
235                     "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri,
236                     "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri
237                 )
238             ));
239         assertThat(gvnfLatestView,
240             hasJsonPath(
241                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
242                 containsInAnyOrder(
243                     "tosca.relationships.network.LinksTo",
244                     "tosca.relationships.network.LinksTo",
245                     "org.onap.relationships.inventory.Source",
246                     "org.onap.relationships.inventory.Destination")
247             ));
248     }
249
250     @Test
251     public void verifyRelsOfLatestViewOfLLDefault() throws AAIException, UnsupportedEncodingException {
252         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
253
254         String llDefaultLatestView = serializer.getLatestVersionView(
255             engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).next()).marshal(false);
256         assertThat(llDefaultLatestView,
257             hasJsonPath(
258                 "$.relationship-list.relationship[*]",
259                 hasSize(2)
260             ));
261         assertThat(llDefaultLatestView,
262             hasJsonPath(
263                 "$.relationship-list.relationship[*].related-link",
264                 containsInAnyOrder(
265                     "/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
266                     "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri
267                 )
268             ));
269         assertThat(llDefaultLatestView,
270             hasJsonPath(
271                 "$.relationship-list.relationship[*].relationship-label",
272                 containsInAnyOrder("tosca.relationships.network.LinksTo","tosca.relationships.network.LinksTo")
273             ));
274         assertThat(llDefaultLatestView,
275             hasJsonPath(
276                 "$.relationship-list.relationship[*].relationship-label",
277                 not(contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination"))
278             ));
279
280     }
281
282     @Test
283     public void verifyRelsOfLatestViewOfLLLabeled() throws AAIException, UnsupportedEncodingException {
284         DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
285
286         String llLabeledLatestView = serializer.getLatestVersionView(
287             engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).next()).marshal(false);
288         assertThat(llLabeledLatestView,
289             hasJsonPath(
290                 "$.relationship-list.relationship[*]",
291                 hasSize(2)
292             ));
293         assertThat(llLabeledLatestView,
294             hasJsonPath(
295                 "$.relationship-list.relationship[*].related-link",
296                 containsInAnyOrder(
297                     "/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
298                     "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri
299                 )
300             ));
301         assertThat(llLabeledLatestView,
302             hasJsonPath(
303                 "$.relationship-list.relationship[*].relationship-label",
304                 not(containsInAnyOrder("tosca.relationships.network.LinksTo","tosca.relationships.network.LinksTo"))
305             ));
306         assertThat(llLabeledLatestView,
307             hasJsonPath(
308                 "$.relationship-list.relationship[*].relationship-label",
309                 contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination")
310             ));
311
312     }
313
314
315     @Test
316     public void verifyRelsOfOldViewOfGenericVnf() throws AAIException, UnsupportedEncodingException {
317         SchemaVersion oldVersion = new SchemaVersion("v11");
318         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
319         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
320
321         String gvnfOldView = oldSerializer.dbToObject(
322             Collections.singletonList(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next()),
323             oldLoader.introspectorFromName("generic-vnf"),
324             AAIProperties.MAXIMUM_DEPTH, false, "false")
325             .marshal(false);
326         assertThat(gvnfOldView,
327             hasJsonPath(
328                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*]",
329                 hasSize(2)
330             ));
331         assertThat(gvnfOldView,
332             hasJsonPath(
333                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
334                 emptyCollectionOf(String.class)
335             ));
336         assertThat(gvnfOldView,
337             hasJsonPath(
338                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
339                 containsInAnyOrder(
340                     "/aai/" + oldVersion + llDefaultUri,
341                     "/aai/" + oldVersion + llDefaultUri
342                 )
343             ));
344     }
345
346     @Test
347     public void verifyRelsOfOldViewOfLLDefault() throws AAIException, UnsupportedEncodingException {
348         SchemaVersion oldVersion = new SchemaVersion("v11");
349         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
350         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
351
352         String llDefaultOldView = oldSerializer.dbToObject(
353             Collections.singletonList(engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).next()),
354             oldLoader.introspectorFromName("logical-link"),
355             AAIProperties.MAXIMUM_DEPTH, false, "false")
356             .marshal(false);
357         assertThat(llDefaultOldView,
358             hasJsonPath(
359                 "$.relationship-list.relationship[*]",
360                 hasSize(2)
361             ));
362         assertThat(llDefaultOldView,
363             hasJsonPath(
364                 "$.relationship-list.relationship[*].relationship-label",
365                 emptyCollectionOf(String.class)
366             ));
367         assertThat(llDefaultOldView,
368             hasJsonPath(
369                 "$.relationship-list.relationship[*].related-link",
370                 containsInAnyOrder(
371                     "/aai/" + oldVersion + lintSourceUri,
372                     "/aai/" + oldVersion + lintDestinationUri
373                 )
374             ));
375
376     }
377
378     @Test
379     public void verifyRelsOfOldViewOfLLLabeled() throws AAIException, UnsupportedEncodingException {
380         SchemaVersion oldVersion = new SchemaVersion("v11");
381         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
382         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
383
384         String llLabeledtOldView = oldSerializer.dbToObject(
385             Collections.singletonList(engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).next()),
386             oldLoader.introspectorFromName("logical-link"),
387             AAIProperties.MAXIMUM_DEPTH, false, "false")
388             .marshal(false);
389         assertThat(llLabeledtOldView,
390             not(hasJsonPath(
391                 "$.relationship-list.relationship[*]"
392             )));
393     }
394
395
396     @Test
397     public void useOldVersionToUpdatedGenericVnfAndVerifyLatestVersionRels() throws AAIException, UnsupportedEncodingException, URISyntaxException {
398         SchemaVersion oldVersion = new SchemaVersion("v11");
399         Loader oldLoader = loaderFactory.getLoaderStrategy(introspectorFactoryType, oldVersion);
400         DBSerializer oldSerializer = new DBSerializer(oldVersion, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
401
402         Vertex oldGvnfV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next();
403         Introspector oldGvnf = oldSerializer.dbToObject(
404             Collections.singletonList(oldGvnfV),
405             oldLoader.introspectorFromName("generic-vnf"),
406             AAIProperties.MAXIMUM_DEPTH, false, "false");
407         assertThat(oldGvnf.marshal(false),
408             hasJsonPath(
409                 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
410                 containsInAnyOrder(
411                     "/aai/" + oldVersion + llDefaultUri,
412                     "/aai/" + oldVersion + llDefaultUri
413                 )
414             ));
415         oldGvnf.setValue("in-maint", true);
416         QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfUri));
417         oldSerializer.serializeToDb(oldGvnf, oldGvnfV, uriQuery, "generic-vnf", oldGvnf.marshal(false));
418
419         verifyRelsOfLatestViewOfGenericVnf();
420         verifyRelsOfOldViewOfGenericVnf();
421     }
422
423
424
425
426 }