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 static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
24 import static org.hamcrest.Matchers.*;
25 import static org.junit.Assert.*;
27 import java.io.UnsupportedEncodingException;
29 import java.net.URISyntaxException;
30 import java.util.Arrays;
31 import java.util.Collections;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.JanusGraphFactory;
37 import org.junit.rules.ExpectedException;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.db.props.AAIProperties;
40 import org.onap.aai.edges.EdgeIngestor;
41 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.introspection.Introspector;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.ModelType;
45 import org.onap.aai.parsers.query.QueryParser;
46 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49 import org.onap.aai.setup.SchemaVersion;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.test.annotation.DirtiesContext;
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,
112 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());
142 System.out.println(gvnf.marshal(true));
143 Vertex gvnfV = serializer.createNewVertex(gvnf);
144 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
145 serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
147 assertTrue("generic-vnf-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).hasNext());
148 assertTrue("l-int created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintUri).hasNext());
149 assertTrue("l-int-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lintAUri).hasNext());
150 assertTrue("sriov-vf created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, sriovVfUri).hasNext());
151 assertTrue("sriov-vf-a created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, sriovVfAUri).hasNext());
153 gvnf = loader.introspectorFromName("generic-vnf");
154 gvnf.setValue("vnf-id", "gvnf-b" + SOURCE_OF_TRUTH);
155 gvnf.setValue("vnf-name", "gvnf" + SOURCE_OF_TRUTH + "-name");
157 lint = loader.introspectorFromName("l-interface");
158 lint.setValue("interface-name", "lint-b");
159 Introspector relationship = loader.introspectorFromName("relationship");
160 relationship.setValue("related-link", sriovVfUri);
161 Introspector relationshipList = loader.introspectorFromName("relationship-list");
162 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
163 lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
165 lints = loader.introspectorFromName("l-interfaces");
166 lints.setValue("l-interface", Collections.singletonList(lint.getUnderlyingObject()));
167 gvnf.setValue("l-interfaces", lints.getUnderlyingObject());
169 gvnfV = serializer.createNewVertex(gvnf);
170 uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
171 serializer.serializeToDb(gvnf, gvnfV, uriQuery, "generic-vnf", gvnf.marshal(false));
173 engine.tx().traversal().V().forEachRemaining(v -> System.out.println(v.<String>value(AAIProperties.AAI_URI)));
174 assertTrue("generic-vnf-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).hasNext());
175 assertTrue("l-int-b created", engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).hasNext());
176 assertTrue("l-interface relationship sriov-vf created", engine.tx().traversal().V()
177 .has(AAIProperties.AAI_URI, lIntBUri).both().has(AAIProperties.AAI_URI, sriovVfUri).hasNext());
182 public void verifyReadOfGenericVnfATest() throws AAIException, UnsupportedEncodingException {
183 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
184 AAIProperties.MINIMUM_DEPTH);
186 String gvnfALatestView =
187 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next())
190 assertThat(gvnfALatestView, hasJsonPath("$.l-interfaces.l-interface[*]", hasSize(2)));
191 assertThat(gvnfALatestView, hasJsonPath("$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]", hasSize(2)));
192 assertThat(gvnfALatestView, hasJsonPath(
193 "$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*].relationship-list.relationship[*].related-link",
194 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + lIntBUri)));
198 public void verifyReadOfGenericVnfBTest() throws AAIException, UnsupportedEncodingException {
199 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
200 AAIProperties.MINIMUM_DEPTH);
202 String gvnfBLatestView =
203 serializer.getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfBUri).next())
206 assertThat(gvnfBLatestView, hasJsonPath("$.l-interfaces.l-interface[*]", hasSize(1)));
207 assertThat(gvnfBLatestView, not(hasJsonPath("$.l-interfaces.l-interface[*].sriov-vfs.sriov-vf[*]")));
208 assertThat(gvnfBLatestView,
209 hasJsonPath("$.l-interfaces.l-interface[*].relationship-list.relationship[*].related-link",
210 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri)));
214 public void replaceRelationshipToSriovVfTest()
215 throws AAIException, UnsupportedEncodingException, URISyntaxException {
216 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
217 AAIProperties.MINIMUM_DEPTH);
219 Introspector lint = serializer
220 .getLatestVersionView(engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next());
221 String lintView = lint.marshal(false);
223 assertThat(lintView, hasJsonPath("$.relationship-list.relationship[*].related-link",
224 containsInAnyOrder("/aai/" + schemaVersions.getDefaultVersion() + sriovVfUri)));
226 Introspector relationship = loader.introspectorFromName("relationship");
227 relationship.setValue("related-link", sriovVfAUri);
228 Introspector relationshipList = loader.introspectorFromName("relationship-list");
229 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
230 lint.setValue("relationship-list", relationshipList.getUnderlyingObject());
232 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(lIntBUri));
233 serializer.serializeToDb(lint, engine.tx().traversal().V().has(AAIProperties.AAI_URI, lIntBUri).next(),
234 uriQuery, "generic-vnf", lint.marshal(false));
238 public void createRelationshipForNonExistentRuleTest()
239 throws AAIException, UnsupportedEncodingException, URISyntaxException {
240 DBSerializer serializer = new DBSerializer(version, engine, introspectorFactoryType, SOURCE_OF_TRUTH,
241 AAIProperties.MINIMUM_DEPTH);
243 Vertex gvnfAV = engine.tx().traversal().V().has(AAIProperties.AAI_URI, gvnfAUri).next();
244 Introspector gvnfA = serializer.getLatestVersionView(gvnfAV);
245 Introspector relationship = loader.introspectorFromName("relationship");
246 relationship.setValue("related-link", gvnfBUri);
247 Introspector relationshipList = loader.introspectorFromName("relationship-list");
248 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
249 gvnfA.setValue("relationship-list", relationshipList.getUnderlyingObject());
250 QueryParser uriQuery = engine.getQueryBuilder().createQueryFromURI(new URI(gvnfAUri));
252 serializer.serializeToDb(gvnfA, gvnfAV, uriQuery, "generic-vnf", gvnfA.marshal(false));
253 } catch (AAIException e) {
254 assertEquals("AAI_6120", e.getCode());
255 assertThat(e.getMessage(), containsString("generic-vnf"));