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)
60 public class OwnerCheckTest extends AAISetup {
62 private static JanusGraph graph;
63 private final static ModelType introspectorFactoryType = ModelType.MOXY;
64 private static Loader loader;
65 private static TransactionalGraphEngine dbEngine;
68 public ExpectedException thrown = ExpectedException.none();
70 @Parameterized.Parameter
71 public QueryStyle queryStyle;
73 @Parameterized.Parameters(name = "QueryStyle.{0}")
74 public static Collection<Object[]> data() {
75 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
79 public static void setup() {
80 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
81 System.setProperty("AJSC_HOME", ".");
82 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
86 .property("aai-node-type", "pnf")
87 .property("pnf-name", "my-pnf")
88 .property("data-owner", "Operator")
89 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf")
90 .property("model-invariant-id", "key1")
92 .property(EdgeProperty.CONTAINS.toString(), true)
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, "/network/pnfs/pnf/my-pnf/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", "key3")
105 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
108 .property("aai-node-type", "model-ver")
109 .property("model-ver", "myValue")
110 .property("model-version-id", "key4")
111 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
113 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
114 .property(EdgeProperty.CONTAINS.toString(), true)
120 public static void tearDown() {
121 graph.tx().rollback();
126 public void initMock() {
127 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
128 MockitoAnnotations.initMocks(this);
129 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
133 public void shouldFailIfGroupsNotContainsDataOwner() throws Exception {
135 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
136 final Introspector obj = loader.introspectorFromName("pnf");
137 obj.setValue("pnf-name", "my-pnf");
138 obj.setValue("model-invariant-id", "key1");
139 obj.setValue("model-version-id", "key2");
140 TransactionalGraphEngine spy = spy(dbEngine);
141 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
142 Graph g = graph.newTransaction();
143 GraphTraversalSource traversal = g.traversal();
144 when(spy.asAdmin()).thenReturn(adminSpy);
145 when(adminSpy.getTraversalSource()).thenReturn(traversal);
146 DBSerializer serializer =
147 new DBSerializer(schemaVersions.getDefaultVersion(),
148 spy, introspectorFactoryType,
149 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorI", "OperatorII")));
151 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
153 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
155 thrown.expect(AAIException.class);
156 thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
157 ownerCheck.execute();
163 public void shouldPassIfGroupsContainsDataOwner() throws Exception {
165 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
166 final Introspector obj = loader.introspectorFromName("pnf");
167 obj.setValue("pnf-name", "my-pnf");
168 obj.setValue("model-invariant-id", "key1");
169 obj.setValue("model-version-id", "key2");
170 TransactionalGraphEngine spy = spy(dbEngine);
171 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
172 Graph g = graph.newTransaction();
173 GraphTraversalSource traversal = g.traversal();
174 when(spy.tx()).thenReturn(g);
175 when(spy.asAdmin()).thenReturn(adminSpy);
176 when(adminSpy.getTraversalSource()).thenReturn(traversal);
178 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
180 DBSerializer serializer =
181 new DBSerializer(schemaVersions.getDefaultVersion(),
182 spy, introspectorFactoryType,
183 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
185 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
186 assertNotNull(ownerCheck);
188 ownerCheck.execute();
193 public void shouldPassIfGroupsIsEmpty() throws Exception {
195 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
196 final Introspector obj = loader.introspectorFromName("pnf");
197 obj.setValue("pnf-name", "my-pnf");
198 obj.setValue("model-invariant-id", "key1");
199 obj.setValue("model-version-id", "key2");
200 TransactionalGraphEngine spy = spy(dbEngine);
201 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
202 Graph g = graph.newTransaction();
203 GraphTraversalSource traversal = g.traversal();
204 when(spy.asAdmin()).thenReturn(adminSpy);
205 when(adminSpy.getTraversalSource()).thenReturn(traversal);
206 DBSerializer serializer =
207 new DBSerializer(schemaVersions.getDefaultVersion(),
208 spy, introspectorFactoryType,
211 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
213 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
214 assertNotNull(ownerCheck);
216 ownerCheck.execute();
221 public void shouldPassIfDataOwnerIsNull() throws Exception {
223 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
224 final Introspector obj = loader.introspectorFromName("pnf");
225 obj.setValue("pnf-name", "my-pnf");
226 obj.setValue("model-invariant-id", "key1");
227 obj.setValue("model-version-id", "key2");
228 obj.setValue("data-owner", null);
229 TransactionalGraphEngine spy = spy(dbEngine);
230 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
231 Graph g = graph.newTransaction();
232 GraphTraversalSource traversal = g.traversal();
233 when(spy.asAdmin()).thenReturn(adminSpy);
234 when(adminSpy.getTraversalSource()).thenReturn(traversal);
235 DBSerializer serializer =
236 new DBSerializer(schemaVersions.getDefaultVersion(),
237 spy, introspectorFactoryType,
240 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
242 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
243 assertNotNull(ownerCheck);
245 ownerCheck.execute();
250 public void shouldPassIfDataOwnerIsEmpty() throws Exception {
252 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
253 final Introspector obj = loader.introspectorFromName("pnf");
254 obj.setValue("pnf-name", "my-pnf");
255 obj.setValue("model-invariant-id", "key1");
256 obj.setValue("model-version-id", "key2");
257 obj.setValue("data-owner", "");
258 TransactionalGraphEngine spy = spy(dbEngine);
259 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
260 Graph g = graph.newTransaction();
261 GraphTraversalSource traversal = g.traversal();
262 when(spy.asAdmin()).thenReturn(adminSpy);
263 when(adminSpy.getTraversalSource()).thenReturn(traversal);
264 DBSerializer serializer =
265 new DBSerializer(schemaVersions.getDefaultVersion(),
266 spy, introspectorFactoryType,
269 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
271 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
272 assertNotNull(ownerCheck);
274 ownerCheck.execute();