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 static org.junit.Assert.assertNotNull;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.structure.Graph;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.JanusGraphFactory;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.junit.runners.Parameterized;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aai.AAISetup;
45 import org.onap.aai.db.props.AAIProperties;
46 import org.onap.aai.edges.enums.EdgeProperty;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.Introspector;
49 import org.onap.aai.introspection.Loader;
50 import org.onap.aai.introspection.ModelType;
51 import org.onap.aai.serialization.db.DBSerializer;
52 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 @RunWith(value = Parameterized.class)
58 public class AOwnerCheckTest extends AAISetup {
60 private static JanusGraph graph;
61 private final static ModelType introspectorFactoryType = ModelType.MOXY;
62 private static Loader loader;
63 private static TransactionalGraphEngine dbEngine;
66 public ExpectedException thrown = ExpectedException.none();
68 @Parameterized.Parameter
69 public QueryStyle queryStyle;
71 @Parameterized.Parameters(name = "QueryStyle.{0}")
72 public static Collection<Object[]> data() {
73 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
77 public static void setup() {
78 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
79 System.setProperty("AJSC_HOME", ".");
80 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
84 .property("aai-node-type", "pnf")
85 .property("pnf-name", "my-pnf")
86 .property("data-owner", "Operator")
87 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf")
88 .property("model-invariant-id", "key1")
90 .property(EdgeProperty.CONTAINS.toString(), true)
92 .property("aai-node-type", "model-ver")
93 .property("model-ver", "myValue")
94 .property("model-version-id", "key2")
95 .property("model-version", "testValue")
96 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf/model-vers/model-ver/key2")
98 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
99 .property(EdgeProperty.CONTAINS.toString(), true)
101 .property("aai-node-type", "model")
102 .property("model-invariant-id", "key3")
103 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
106 .property("aai-node-type", "model-ver")
107 .property("model-ver", "myValue")
108 .property("model-version-id", "key4")
109 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
111 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
112 .property(EdgeProperty.CONTAINS.toString(), true)
118 public static void tearDown() {
119 graph.tx().rollback();
124 public void initMock() {
125 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
126 MockitoAnnotations.initMocks(this);
127 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
131 public void shouldFailIfGroupsNotContainsDataOwner() throws Exception {
133 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
134 final Introspector obj = loader.introspectorFromName("pnf");
135 obj.setValue("pnf-name", "my-pnf");
136 obj.setValue("model-invariant-id", "key1");
137 obj.setValue("model-version-id", "key2");
138 TransactionalGraphEngine spy = spy(dbEngine);
139 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
140 Graph g = graph.newTransaction();
141 GraphTraversalSource traversal = g.traversal();
142 when(spy.asAdmin()).thenReturn(adminSpy);
143 when(adminSpy.getTraversalSource()).thenReturn(traversal);
144 DBSerializer serializer =
145 new DBSerializer(schemaVersions.getDefaultVersion(),
146 spy, introspectorFactoryType,
147 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorI", "OperatorII")));
149 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
151 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
153 thrown.expect(AAIException.class);
154 thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
155 ownerCheck.execute();
161 public void shouldPassIfGroupsContainsDataOwner() throws Exception {
163 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
164 final Introspector obj = loader.introspectorFromName("pnf");
165 obj.setValue("pnf-name", "my-pnf");
166 obj.setValue("model-invariant-id", "key1");
167 obj.setValue("model-version-id", "key2");
168 TransactionalGraphEngine spy = spy(dbEngine);
169 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
170 Graph g = graph.newTransaction();
171 GraphTraversalSource traversal = g.traversal();
172 when(spy.tx()).thenReturn(g);
173 when(spy.asAdmin()).thenReturn(adminSpy);
174 when(adminSpy.getTraversalSource()).thenReturn(traversal);
176 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
178 DBSerializer serializer =
179 new DBSerializer(schemaVersions.getDefaultVersion(),
180 spy, introspectorFactoryType,
181 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
183 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
184 assertNotNull(ownerCheck);
186 ownerCheck.execute();
191 public void shouldPassIfGroupsIsEmpty() throws Exception {
193 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
194 final Introspector obj = loader.introspectorFromName("pnf");
195 obj.setValue("pnf-name", "my-pnf");
196 obj.setValue("model-invariant-id", "key1");
197 obj.setValue("model-version-id", "key2");
198 TransactionalGraphEngine spy = spy(dbEngine);
199 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
200 Graph g = graph.newTransaction();
201 GraphTraversalSource traversal = g.traversal();
202 when(spy.asAdmin()).thenReturn(adminSpy);
203 when(adminSpy.getTraversalSource()).thenReturn(traversal);
204 DBSerializer serializer =
205 new DBSerializer(schemaVersions.getDefaultVersion(),
206 spy, introspectorFactoryType,
209 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
211 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
212 assertNotNull(ownerCheck);
214 ownerCheck.execute();
219 public void shouldPassIfDataOwnerIsNull() throws Exception {
221 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
222 final Introspector obj = loader.introspectorFromName("pnf");
223 obj.setValue("pnf-name", "my-pnf");
224 obj.setValue("model-invariant-id", "key1");
225 obj.setValue("model-version-id", "key2");
226 obj.setValue("data-owner", null);
227 TransactionalGraphEngine spy = spy(dbEngine);
228 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
229 Graph g = graph.newTransaction();
230 GraphTraversalSource traversal = g.traversal();
231 when(spy.asAdmin()).thenReturn(adminSpy);
232 when(adminSpy.getTraversalSource()).thenReturn(traversal);
233 DBSerializer serializer =
234 new DBSerializer(schemaVersions.getDefaultVersion(),
235 spy, introspectorFactoryType,
238 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
240 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
241 assertNotNull(ownerCheck);
243 ownerCheck.execute();
248 public void shouldPassIfDataOwnerIsEmpty() throws Exception {
250 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
251 final Introspector obj = loader.introspectorFromName("pnf");
252 obj.setValue("pnf-name", "my-pnf");
253 obj.setValue("model-invariant-id", "key1");
254 obj.setValue("model-version-id", "key2");
255 obj.setValue("data-owner", "");
256 TransactionalGraphEngine spy = spy(dbEngine);
257 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
258 Graph g = graph.newTransaction();
259 GraphTraversalSource traversal = g.traversal();
260 when(spy.asAdmin()).thenReturn(adminSpy);
261 when(adminSpy.getTraversalSource()).thenReturn(traversal);
262 DBSerializer serializer =
263 new DBSerializer(schemaVersions.getDefaultVersion(),
264 spy, introspectorFactoryType,
267 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
269 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
270 assertNotNull(ownerCheck);
272 ownerCheck.execute();