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;
55 import org.junit.Ignore;
57 @RunWith(value = Parameterized.class)
59 public class OwnerCheckTest extends AAISetup {
61 private static JanusGraph graph;
62 private final static ModelType introspectorFactoryType = ModelType.MOXY;
63 private static Loader loader;
64 private static TransactionalGraphEngine dbEngine;
67 public ExpectedException thrown = ExpectedException.none();
69 @Parameterized.Parameter
70 public QueryStyle queryStyle;
72 @Parameterized.Parameters(name = "QueryStyle.{0}")
73 public static Collection<Object[]> data() {
74 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
78 public static void setup() {
79 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
80 System.setProperty("AJSC_HOME", ".");
81 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
85 .property("aai-node-type", "pnf")
86 .property("pnf-name", "my-pnf")
87 .property("data-owner", "Operator")
88 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf")
89 .property("model-invariant-id", "key1")
91 .property(EdgeProperty.CONTAINS.toString(), true)
93 .property("aai-node-type", "model-ver")
94 .property("model-ver", "myValue")
95 .property("model-version-id", "key2")
96 .property("model-version", "testValue")
97 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf/model-vers/model-ver/key2")
99 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
100 .property(EdgeProperty.CONTAINS.toString(), true)
102 .property("aai-node-type", "model")
103 .property("model-invariant-id", "key3")
104 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
107 .property("aai-node-type", "model-ver")
108 .property("model-ver", "myValue")
109 .property("model-version-id", "key4")
110 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
112 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
113 .property(EdgeProperty.CONTAINS.toString(), true)
119 public static void tearDown() {
120 graph.tx().rollback();
125 public void initMock() {
126 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
127 MockitoAnnotations.initMocks(this);
128 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
132 public void shouldFailIfGroupsNotContainsDataOwner() throws Exception {
134 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
135 final Introspector obj = loader.introspectorFromName("pnf");
136 obj.setValue("pnf-name", "my-pnf");
137 obj.setValue("model-invariant-id", "key1");
138 obj.setValue("model-version-id", "key2");
139 TransactionalGraphEngine spy = spy(dbEngine);
140 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
141 Graph g = graph.newTransaction();
142 GraphTraversalSource traversal = g.traversal();
143 when(spy.asAdmin()).thenReturn(adminSpy);
144 when(adminSpy.getTraversalSource()).thenReturn(traversal);
145 DBSerializer serializer =
146 new DBSerializer(schemaVersions.getDefaultVersion(),
147 spy, introspectorFactoryType,
148 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorI", "OperatorII")));
150 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
152 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
154 thrown.expect(AAIException.class);
155 thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
156 ownerCheck.execute();
162 public void shouldPassIfGroupsContainsDataOwner() throws Exception {
164 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
165 final Introspector obj = loader.introspectorFromName("pnf");
166 obj.setValue("pnf-name", "my-pnf");
167 obj.setValue("model-invariant-id", "key1");
168 obj.setValue("model-version-id", "key2");
169 TransactionalGraphEngine spy = spy(dbEngine);
170 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
171 Graph g = graph.newTransaction();
172 GraphTraversalSource traversal = g.traversal();
173 when(spy.tx()).thenReturn(g);
174 when(spy.asAdmin()).thenReturn(adminSpy);
175 when(adminSpy.getTraversalSource()).thenReturn(traversal);
177 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
179 DBSerializer serializer =
180 new DBSerializer(schemaVersions.getDefaultVersion(),
181 spy, introspectorFactoryType,
182 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
184 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
185 assertNotNull(ownerCheck);
187 ownerCheck.execute();
192 public void shouldPassIfGroupsIsEmpty() throws Exception {
194 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
195 final Introspector obj = loader.introspectorFromName("pnf");
196 obj.setValue("pnf-name", "my-pnf");
197 obj.setValue("model-invariant-id", "key1");
198 obj.setValue("model-version-id", "key2");
199 TransactionalGraphEngine spy = spy(dbEngine);
200 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
201 Graph g = graph.newTransaction();
202 GraphTraversalSource traversal = g.traversal();
203 when(spy.asAdmin()).thenReturn(adminSpy);
204 when(adminSpy.getTraversalSource()).thenReturn(traversal);
205 DBSerializer serializer =
206 new DBSerializer(schemaVersions.getDefaultVersion(),
207 spy, introspectorFactoryType,
210 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
212 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
213 assertNotNull(ownerCheck);
215 ownerCheck.execute();
220 public void shouldPassIfDataOwnerIsNull() throws Exception {
222 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
223 final Introspector obj = loader.introspectorFromName("pnf");
224 obj.setValue("pnf-name", "my-pnf");
225 obj.setValue("model-invariant-id", "key1");
226 obj.setValue("model-version-id", "key2");
227 obj.setValue("data-owner", null);
228 TransactionalGraphEngine spy = spy(dbEngine);
229 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
230 Graph g = graph.newTransaction();
231 GraphTraversalSource traversal = g.traversal();
232 when(spy.asAdmin()).thenReturn(adminSpy);
233 when(adminSpy.getTraversalSource()).thenReturn(traversal);
234 DBSerializer serializer =
235 new DBSerializer(schemaVersions.getDefaultVersion(),
236 spy, introspectorFactoryType,
239 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
241 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
242 assertNotNull(ownerCheck);
244 ownerCheck.execute();
249 public void shouldPassIfDataOwnerIsEmpty() throws Exception {
251 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
252 final Introspector obj = loader.introspectorFromName("pnf");
253 obj.setValue("pnf-name", "my-pnf");
254 obj.setValue("model-invariant-id", "key1");
255 obj.setValue("model-version-id", "key2");
256 obj.setValue("data-owner", "");
257 TransactionalGraphEngine spy = spy(dbEngine);
258 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
259 Graph g = graph.newTransaction();
260 GraphTraversalSource traversal = g.traversal();
261 when(spy.asAdmin()).thenReturn(adminSpy);
262 when(adminSpy.getTraversalSource()).thenReturn(traversal);
263 DBSerializer serializer =
264 new DBSerializer(schemaVersions.getDefaultVersion(),
265 spy, introspectorFactoryType,
268 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
270 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
271 assertNotNull(ownerCheck);
273 ownerCheck.execute();