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.Arrays;
47 import java.util.Collections;
49 import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
50 import static org.hamcrest.Matchers.*;
51 import static org.junit.Assert.*;
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
54 public class OneToOneEdgeUpdateTest extends AAISetup {
56 // to use, set thrown.expect to whatever your test needs
57 // this line establishes default of expecting no exception to be thrown
59 public ExpectedException thrown = ExpectedException.none();
61 protected static Graph graph;
64 protected EdgeSerializer edgeSer;
66 protected EdgeIngestor ei;
68 private SchemaVersion version;
69 private final ModelType introspectorFactoryType = ModelType.MOXY;
70 private Loader loader;
71 private TransactionalGraphEngine engine;
73 public QueryStyle queryStyle = QueryStyle.TRAVERSAL_URI;
75 public static final String SOURCE_OF_TRUTH = "EdgerPairCanBeBothCousinAndParentChildTest";
76 private static final String gvnfAUri = "/network/generic-vnfs/generic-vnf/gvnf-a" + SOURCE_OF_TRUTH;
77 private static final String lintUri = gvnfAUri + "/l-interfaces/l-interface/lint";
78 private static final String lintAUri = gvnfAUri + "/l-interfaces/l-interface/lint-a";
79 private static final String sriovVfUri = lintUri + "/sriov-vfs/sriov-vf/sriov-vf";
80 private static final String sriovVfAUri = lintAUri + "/sriov-vfs/sriov-vf/sriov-vf-a";
82 private static final String gvnfBUri = "/network/generic-vnfs/generic-vnf/gvnf-b" + SOURCE_OF_TRUTH;
83 private static final String lIntBUri = gvnfBUri + "/l-interfaces/l-interface/lint-b";
86 public static void init() {
87 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
92 public void setup() throws UnsupportedEncodingException, AAIException, URISyntaxException {
93 version = schemaVersions.getDefaultVersion();
94 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
95 engine = new JanusGraphDBEngine(queryStyle, loader);
100 public void cleanup() {
105 * Using latest version (see schema-ingest.properties) - sriov-vf to l-interface is one ot one
106 * Create generic-vnf with l-interface that has an sriov-vf
107 * Create generic-vnf with l-interface relationship the sriov-vf
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-a" + SOURCE_OF_TRUTH);
116 gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
118 Introspector lint = loader.introspectorFromName("l-interface");
119 lint.setValue("interface-name", "lint");
121 Introspector sriovVf = loader.introspectorFromName("sriov-vf");
122 sriovVf.setValue("pci-id", "sriov-vf");
124 Introspector sriovVfs = loader.introspectorFromName("sriov-vfs");
125 sriovVfs.setValue("sriov-vf", Collections.singletonList(sriovVf.getUnderlyingObject()));
126 lint.setValue("sriov-vfs", sriovVfs.getUnderlyingObject());
128 Introspector lintA = loader.introspectorFromName("l-interface");
129 lintA.setValue("interface-name", "lint-a");
131 Introspector sriovVfA = loader.introspectorFromName("sriov-vf");
132 sriovVfA.setValue("pci-id", "sriov-vf-a");
134 sriovVfs = loader.introspectorFromName("sriov-vfs");
135 sriovVfs.setValue("sriov-vf", Collections.singletonList(sriovVfA.getUnderlyingObject()));
136 lintA.setValue("sriov-vfs", sriovVfs.getUnderlyingObject());
138 Introspector lints = loader.introspectorFromName("l-interfaces");
139 lints.setValue("l-interface", Arrays.asList(lint.getUnderlyingObject(), lintA.getUnderlyingObject()));
140 gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
143 System.out.println(gvnf.marshal(true));
144 Vertex gvnfV = serializer.createNewVertex(gvnf);
145 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
146 serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
148 assertTrue("generic-vnf-a created", engine.tx().traversal().V()
149 .has(AAIProperties.AAI_URI, gvnfAUri)
151 assertTrue("l-int created", engine.tx().traversal().V()
152 .has(AAIProperties.AAI_URI, lintUri)
154 assertTrue("l-int-a created", engine.tx().traversal().V()
155 .has(AAIProperties.AAI_URI, lintAUri)
157 assertTrue("sriov-vf created", engine.tx().traversal().V()
158 .has(AAIProperties.AAI_URI, sriovVfUri)
160 assertTrue("sriov-vf-a created", engine.tx().traversal().V()
161 .has(AAIProperties.AAI_URI, sriovVfAUri)
165 gvnf = loader.introspectorFromName("generic-vnf");
166 gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
167 gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
169 lint = loader.introspectorFromName("l-interface");
170 lint.setValue("interface-name", "lint-b");
171 Introspector relationship = loader.introspectorFromName("relationship");
172 relationship.setValue("related-link", sriovVfUri);
173 Introspector relationshipList = loader.introspectorFromName("relationship-list");
174 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
175 lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
177 lints = loader.introspectorFromName("l-interfaces");
178 lints.setValue("l-interface", Collections.singletonList(lint.getUnderlyingObject()));
179 gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
181 gvnfV = serializer.createNewVertex(gvnf);
182 uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
183 serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
185 engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
186 assertTrue("generic-vnf-b created", engine.tx().traversal().V()
187 .has(AAIProperties.AAI_URI, gvnfBUri)
189 assertTrue("l-int-b created", engine.tx().traversal().V()
190 .has(AAIProperties.AAI_URI, lIntBUri)
192 assertTrue("l-interface relationship sriov-vf created", engine.tx().traversal().V()
193 .has(AAIProperties.AAI_URI, lIntBUri)
195 .has(AAIProperties.AAI_URI, sriovVfUri)
202 public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
203 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
205 String gvnfALatestView = serializer.getLatestVersionView(
206 engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next()).marshal(false);
208 assertThat(gvnfALatestView,
210 "$.l-interfaces.l-interface[*]",
213 assertThat(gvnfALatestView,
215 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]",
218 assertThat(gvnfALatestView,
220 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*].relationship-list.relationship[*].related-link",
222 "/aai/" + schemaVersions.getDefaultVersion() + lIntBUri
228 public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
229 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
231 String gvnfBLatestView = serializer.getLatestVersionView(
232 engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next()).marshal(false);
234 assertThat(gvnfBLatestView,
236 "$.l-interfaces.l-interface[*]",
239 assertThat(gvnfBLatestView,
241 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]"
243 assertThat(gvnfBLatestView,
245 "$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
247 "/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri
253 public void replaceRelationshipToSriovVfTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
254 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
256 Introspector lint = serializer.getLatestVersionView(
257 engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next());
258 String lintView = lint.marshal(false);
262 "$.relationship-list.relationship[*].related-link",
264 "/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri
268 Introspector relationship = loader.introspectorFromName("relationship");
269 relationship.setValue("related-link", sriovVfAUri);
270 Introspector relationshipList = loader.introspectorFromName("relationship-list");
271 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
272 lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
274 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(lIntBUri));
275 serializer.serializeToDb(lint, engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next(),
276 uriQuery, "generic-vnf", lint.marshal(false));
281 public void createRelationshipForNonExistentRuleTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
282 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH, AAIProperties.MINIMUM_DEPTH);
284 Vertex gvnfAV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next();
285 Introspector gvnfA = serializer.getLatestVersionView(gvnfAV);
286 Introspector relationship = loader.introspectorFromName("relationship");
287 relationship.setValue("related-link", gvnfBUri);
288 Introspector relationshipList = loader.introspectorFromName("relationship-list");
289 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
290 gvnfA.setValue("relationship-list", relationshipList.getUnderlyingObject());
291 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
293 serializer.serializeToDb(gvnfA, gvnfAV, uriQuery, "generic-vnf", gvnfA.marshal(false));
294 } catch (AAIException e) {
295 assertEquals("AAI_6120", e.getCode());
296 assertThat(e.getMessage(), containsString("generic-vnf"));