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.introspection.sideeffect;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.structure.Edge;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.janusgraph.core.JanusGraph;
28 import org.janusgraph.core.JanusGraphFactory;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.db.props.AAIProperties;
36 import org.onap.aai.edges.enums.EdgeProperty;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.serialization.db.DBSerializer;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.springframework.test.annotation.DirtiesContext;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.List;
50 import static org.hamcrest.MatcherAssert.assertThat;
51 import static org.hamcrest.Matchers.empty;
52 import static org.hamcrest.Matchers.not;
53 import static org.hamcrest.core.Is.is;
54 import static org.hamcrest.core.StringContains.containsString;
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.assertNull;
57 import static org.mockito.Mockito.spy;
58 import static org.mockito.Mockito.when;
60 @RunWith(value = Parameterized.class)
61 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
62 public class PrivateEdgeTest extends AAISetup {
64 private static JanusGraph graph;
65 private final static ModelType introspectorFactoryType = ModelType.MOXY;
66 private static TransactionalGraphEngine dbEngine;
69 public ExpectedException thrown = ExpectedException.none();
71 @Parameterized.Parameter
72 public QueryStyle queryStyle;
74 @Parameterized.Parameters(name = "QueryStyle.{0}")
75 public static Collection<Object[]> data() {
76 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
80 public static void setup() {
82 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
84 System.setProperty("AJSC_HOME", ".");
85 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
89 .property("aai-node-type", "model")
90 .property("model-invariant-id", "key1")
91 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key1")
94 .property("aai-node-type", "model-ver")
95 .property("model-ver", "myValue")
96 .property("model-version-id", "key2")
97 .property("model-version", "testValue")
98 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key1/model-vers/model-ver/key2")
100 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
101 .property(EdgeProperty.CONTAINS.toString(), true)
103 .property("aai-node-type", "model")
104 .property("model-invariant-id", "key100")
105 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key100")
108 .property("aai-node-type", "model-ver")
109 .property("model-ver", "myValue")
110 .property("model-version-id", "key200")
111 .property("model-version", "testValue")
112 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key100/model-vers/model-ver/key200")
114 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
115 .property(EdgeProperty.CONTAINS.toString(), true)
117 .property("aai-node-type", "model")
118 .property("model-invariant-id", "key3")
119 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
122 .property("aai-node-type", "model-ver")
123 .property("model-ver", "myValue")
124 .property("model-version-id", "key4")
125 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
127 .addE("org.onap.relationships.inventory.BelongsTo").to("v5").from("v6")
128 .property(EdgeProperty.CONTAINS.toString(), true)
134 public static void tearDown() {
135 graph.tx().rollback();
140 public void initMock() {
141 Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
142 MockitoAnnotations.initMocks(this);
143 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
147 public void testWhenPrivateEdgeThrowsExceptionWhenHavingOnlyOnePartOfKey() throws Exception {
149 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
150 final Introspector obj = loader.introspectorFromName("generic-vnf");
151 obj.setValue("vnf-id", "myId");
152 obj.setValue("model-invariant-id", "key1");
153 TransactionalGraphEngine spy = spy(dbEngine);
154 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
155 Graph g = graph.newTransaction();
156 GraphTraversalSource traversal = g.traversal();
157 when(spy.asAdmin()).thenReturn(adminSpy);
158 when(adminSpy.getTraversalSource()).thenReturn(traversal);
160 Vertex selfV = traversal.addV("generic-vnf")
161 .property("aai-node-type", "generic-vnf")
162 .property("vnf-id", "myId")
163 .property("aai-uri", obj.getURI())
164 .property("model-invariant-id", "key1")
167 thrown.expectMessage(containsString("Cannot complete privateEdge uri"));
168 DBSerializer serializer =
169 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
170 PrivateEdge privateEdge = new PrivateEdge(obj, selfV, spy, serializer);
171 privateEdge.execute();
173 List<Edge> edgeList = traversal.E().has("private", true).toList();
175 assertNull(edgeList);
176 assertThat(edgeList, is(not(empty())));
177 assertEquals(1, edgeList.size());