2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.db;
23 import org.apache.tinkerpop.gremlin.structure.Graph;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraphFactory;
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;
43 import java.io.UnsupportedEncodingException;
45 import java.net.URISyntaxException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
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;
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class VersionedScenariosTest extends AAISetup {
58 // to use, set thrown.expect to whatever your test needs
59 // this line establishes default of expecting no exception to be thrown
61 public ExpectedException thrown = ExpectedException.none();
63 protected static Graph graph;
66 protected EdgeSerializer edgeSer;
68 protected EdgeIngestor ei;
70 private SchemaVersion version;
71 private final ModelType introspectorFactoryType = ModelType.MOXY;
72 private Loader loader;
73 private TransactionalGraphEngine engine;
75 public QueryStyle queryStyle = QueryStyle.TRAVERSAL_URI;
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";
85 public static void init() {
86 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
91 public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
92 version = schemaVersions.getDefaultVersion();
93 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
94 engine = new JanusGraphDBEngine(queryStyle, loader);
99 public void cleanup() {
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
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);
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");
118 Introspector lintSource = loader.introspectorFromName("l-interface");
119 lintSource.setValue("interface-name", "source");
121 Introspector lintDestination = loader.introspectorFromName("l-interface");
122 lintDestination.setValue("interface-name", "destination");
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());
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));
135 assertTrue("generic-vnf created", engine.tx().traversal().V()
136 .has(AAIProperties.AAI_URI, gvnfUri)
138 assertTrue("source created", engine.tx().traversal().V()
139 .has(AAIProperties.AAI_URI, lintSourceUri)
141 assertTrue("destination created", engine.tx().traversal().V()
142 .has(AAIProperties.AAI_URI, lintDestinationUri)
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());
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));
166 assertTrue("logical-link created", engine.tx().traversal().V()
167 .has(AAIProperties.AAI_URI, llDefaultUri)
169 assertTrue("default source relationship created",engine.tx().traversal().V()
170 .has(AAIProperties.AAI_URI, llDefaultUri)
172 .has(AAIProperties.AAI_URI, lintSourceUri)
174 assertTrue("default destination relationship created", engine.tx().traversal().V()
175 .has(AAIProperties.AAI_URI, llDefaultUri)
177 .has(AAIProperties.AAI_URI, lintDestinationUri)
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());
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));
202 assertTrue("logical-link created", engine.tx().traversal().V()
203 .has(AAIProperties.AAI_URI, llLabeledUri)
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)
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)
219 public void verifyRelsOfLatestViewOfGenericVnf() throws AAIException, UnsupportedEncodingException {
220 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
222 String gvnfLatestView = serializer.getLatestVersionView(
223 engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfUri).next()).marshal(false);
224 assertThat(gvnfLatestView,
226 "$.l-interfaces.l-interface[*].relationship-list.relationship[*]",
229 assertThat(gvnfLatestView,
231 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
233 "/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
234 "/aai/" + schemaVersions.getDefaultVersion() + llDefaultUri,
235 "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri,
236 "/aai/" + schemaVersions.getDefaultVersion() + llLabeledUri
239 assertThat(gvnfLatestView,
241 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
243 "tosca.relationships.network.LinksTo",
244 "tosca.relationships.network.LinksTo",
245 "org.onap.relationships.inventory.Source",
246 "org.onap.relationships.inventory.Destination")
251 public void verifyRelsOfLatestViewOfLLDefault() throws AAIException, UnsupportedEncodingException {
252 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
254 String llDefaultLatestView = serializer.getLatestVersionView(
255 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llDefaultUri).next()).marshal(false);
256 assertThat(llDefaultLatestView,
258 "$.relationship-list.relationship[*]",
261 assertThat(llDefaultLatestView,
263 "$.relationship-list.relationship[*].related-link",
265 "/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
266 "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri
269 assertThat(llDefaultLatestView,
271 "$.relationship-list.relationship[*].relationship-label",
272 containsInAnyOrder("tosca.relationships.network.LinksTo","tosca.relationships.network.LinksTo")
274 assertThat(llDefaultLatestView,
276 "$.relationship-list.relationship[*].relationship-label",
277 not(contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination"))
283 public void verifyRelsOfLatestViewOfLLLabeled() throws AAIException, UnsupportedEncodingException {
284 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
286 String llLabeledLatestView = serializer.getLatestVersionView(
287 engine.tx().traversal().V().has(AAIProperties.AAI_URI, llLabeledUri).next()).marshal(false);
288 assertThat(llLabeledLatestView,
290 "$.relationship-list.relationship[*]",
293 assertThat(llLabeledLatestView,
295 "$.relationship-list.relationship[*].related-link",
297 "/aai/" + schemaVersions.getDefaultVersion() + lintSourceUri,
298 "/aai/" + schemaVersions.getDefaultVersion() + lintDestinationUri
301 assertThat(llLabeledLatestView,
303 "$.relationship-list.relationship[*].relationship-label",
304 not(containsInAnyOrder("tosca.relationships.network.LinksTo","tosca.relationships.network.LinksTo"))
306 assertThat(llLabeledLatestView,
308 "$.relationship-list.relationship[*].relationship-label",
309 contains("org.onap.relationships.inventory.Source", "org.onap.relationships.inventory.Destination")
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);
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")
326 assertThat(gvnfOldView,
328 "$.l-interfaces.l-interface[*].relationship-list.relationship[*]",
331 assertThat(gvnfOldView,
333 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].relationship-label",
334 emptyCollectionOf(String.class)
336 assertThat(gvnfOldView,
338 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
340 "/aai/" + oldVersion + llDefaultUri,
341 "/aai/" + oldVersion + llDefaultUri
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);
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")
357 assertThat(llDefaultOldView,
359 "$.relationship-list.relationship[*]",
362 assertThat(llDefaultOldView,
364 "$.relationship-list.relationship[*].relationship-label",
365 emptyCollectionOf(String.class)
367 assertThat(llDefaultOldView,
369 "$.relationship-list.relationship[*].related-link",
371 "/aai/" + oldVersion + lintSourceUri,
372 "/aai/" + oldVersion + lintDestinationUri
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);
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")
389 assertThat(llLabeledtOldView,
391 "$.relationship-list.relationship[*]"
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);
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),
409 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
411 "/aai/" + oldVersion + llDefaultUri,
412 "/aai/" + oldVersion + llDefaultUri
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));
419 verifyRelsOfLatestViewOfGenericVnf();
420 verifyRelsOfOldViewOfGenericVnf();