Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / sideeffect / PrivateEdgeTest.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.introspection.sideeffect;
22
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;
29 import org.junit.*;
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;
45
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.List;
49
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;
59
60 @RunWith(value = Parameterized.class)
61 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
62 public class PrivateEdgeTest extends AAISetup {
63
64     private static JanusGraph graph;
65     private final static ModelType introspectorFactoryType = ModelType.MOXY;
66     private static TransactionalGraphEngine dbEngine;
67
68     @Rule
69     public ExpectedException thrown = ExpectedException.none();
70
71     @Parameterized.Parameter
72     public QueryStyle queryStyle;
73
74     @Parameterized.Parameters(name = "QueryStyle.{0}")
75     public static Collection<Object[]> data() {
76         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
77     }
78
79     @BeforeClass
80     public static void setup() {
81
82         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
83
84         System.setProperty("AJSC_HOME", ".");
85         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
86
87         graph.traversal()
88             .addV()
89             .property("aai-node-type", "model")
90             .property("model-invariant-id", "key1")
91             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key1")
92             .as("v1")
93             .addV()
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")
99             .as("v2")
100             .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
101             .property(EdgeProperty.CONTAINS.toString(), true)
102             .addV()
103             .property("aai-node-type", "model")
104             .property("model-invariant-id", "key100")
105             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key100")
106             .as("v3")
107             .addV()
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")
113             .as("v4")
114             .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
115             .property(EdgeProperty.CONTAINS.toString(), true)
116             .addV()
117             .property("aai-node-type", "model")
118             .property("model-invariant-id", "key3")
119             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
120             .as("v5")
121             .addV()
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")
126             .as("v6")
127             .addE("org.onap.relationships.inventory.BelongsTo").to("v5").from("v6")
128             .property(EdgeProperty.CONTAINS.toString(), true)
129             .next();
130         graph.tx().commit();
131     }
132
133     @AfterClass
134     public static void tearDown() {
135         graph.tx().rollback();
136         graph.close();
137     }
138
139     @Before
140     public void initMock() {
141         Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
142         MockitoAnnotations.initMocks(this);
143         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
144     }
145
146     @Test
147     public void testWhenPrivateEdgeThrowsExceptionWhenHavingOnlyOnePartOfKey() throws Exception {
148
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);
159
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")
165             .next();
166
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();
172
173         List<Edge> edgeList = traversal.E().has("private", true).toList();
174
175         assertNull(edgeList);
176         assertThat(edgeList, is(not(empty())));
177         assertEquals(1, edgeList.size());
178
179         g.tx().rollback();
180     }
181
182 }