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;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.structure.Graph;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.janusgraph.core.JanusGraph;
35 import org.janusgraph.core.JanusGraphFactory;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Ignore;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.aai.AAISetup;
47 import org.onap.aai.db.props.AAIProperties;
48 import org.onap.aai.edges.enums.EdgeProperty;
49 import org.onap.aai.exceptions.AAIException;
50 import org.onap.aai.introspection.Introspector;
51 import org.onap.aai.introspection.Loader;
52 import org.onap.aai.introspection.ModelType;
53 import org.onap.aai.serialization.db.DBSerializer;
54 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
55 import org.onap.aai.serialization.engines.QueryStyle;
56 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
58 @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");
84 graph.traversal().addV("pnf").property("aai-node-type", "pnf").property("pnf-name", "my-pnf")
85 .property("data-owner", "Operator").property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf")
86 .property("model-invariant-id", "key1").as("v1").property(EdgeProperty.CONTAINS.toString(), true)
87 .addV("model-ver").property("aai-node-type", "model-ver").property("model-ver", "myValue")
88 .property("model-version-id", "key2").property("model-version", "testValue")
89 .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf/model-vers/model-ver/key2").as("v2")
90 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
91 .property(EdgeProperty.CONTAINS.toString(), true).addV("model").property("aai-node-type", "model")
92 .property("model-invariant-id", "key3")
93 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3").as("v3").addV()
94 .property("aai-node-type", "model-ver").property("model-ver", "myValue")
95 .property("model-version-id", "key4")
96 .property(AAIProperties.AAI_URI,
97 "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
98 .as("v4").addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
99 .property(EdgeProperty.CONTAINS.toString(), true).next();
104 public static void tearDown() {
105 graph.tx().rollback();
110 public void initMock() {
111 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
112 MockitoAnnotations.initMocks(this);
113 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
117 public void shouldFailIfGroupsNotContainsDataOwner() throws Exception {
119 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
120 final Introspector obj = loader.introspectorFromName("pnf");
121 obj.setValue("pnf-name", "my-pnf");
122 obj.setValue("model-invariant-id", "key1");
123 obj.setValue("model-version-id", "key2");
124 TransactionalGraphEngine spy = spy(dbEngine);
125 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
126 Graph g = graph.newTransaction();
127 GraphTraversalSource traversal = g.traversal();
128 when(spy.asAdmin()).thenReturn(adminSpy);
129 when(adminSpy.getTraversalSource()).thenReturn(traversal);
130 DBSerializer serializer = new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType,
131 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorI", "OperatorII")));
133 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
135 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
137 thrown.expect(AAIException.class);
138 thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
139 ownerCheck.execute();
145 public void shouldPassIfGroupsContainsDataOwner() throws Exception {
147 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
148 final Introspector obj = loader.introspectorFromName("pnf");
149 obj.setValue("pnf-name", "my-pnf");
150 obj.setValue("model-invariant-id", "key1");
151 obj.setValue("model-version-id", "key2");
152 TransactionalGraphEngine spy = spy(dbEngine);
153 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
154 Graph g = graph.newTransaction();
155 GraphTraversalSource traversal = g.traversal();
156 when(spy.tx()).thenReturn(g);
157 when(spy.asAdmin()).thenReturn(adminSpy);
158 when(adminSpy.getTraversalSource()).thenReturn(traversal);
160 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
162 DBSerializer serializer = new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType,
163 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
165 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
166 assertNotNull(ownerCheck);
168 ownerCheck.execute();
173 public void shouldPassIfGroupsIsEmpty() throws Exception {
175 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
176 final Introspector obj = loader.introspectorFromName("pnf");
177 obj.setValue("pnf-name", "my-pnf");
178 obj.setValue("model-invariant-id", "key1");
179 obj.setValue("model-version-id", "key2");
180 TransactionalGraphEngine spy = spy(dbEngine);
181 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
182 Graph g = graph.newTransaction();
183 GraphTraversalSource traversal = g.traversal();
184 when(spy.asAdmin()).thenReturn(adminSpy);
185 when(adminSpy.getTraversalSource()).thenReturn(traversal);
186 DBSerializer serializer =
187 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
189 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
191 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
192 assertNotNull(ownerCheck);
194 ownerCheck.execute();
199 public void shouldPassIfDataOwnerIsNull() throws Exception {
201 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
202 final Introspector obj = loader.introspectorFromName("pnf");
203 obj.setValue("pnf-name", "my-pnf");
204 obj.setValue("model-invariant-id", "key1");
205 obj.setValue("model-version-id", "key2");
206 obj.setValue("data-owner", null);
207 TransactionalGraphEngine spy = spy(dbEngine);
208 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
209 Graph g = graph.newTransaction();
210 GraphTraversalSource traversal = g.traversal();
211 when(spy.asAdmin()).thenReturn(adminSpy);
212 when(adminSpy.getTraversalSource()).thenReturn(traversal);
213 DBSerializer serializer =
214 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
216 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
218 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
219 assertNotNull(ownerCheck);
221 ownerCheck.execute();
226 public void shouldPassIfDataOwnerIsEmpty() throws Exception {
228 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
229 final Introspector obj = loader.introspectorFromName("pnf");
230 obj.setValue("pnf-name", "my-pnf");
231 obj.setValue("model-invariant-id", "key1");
232 obj.setValue("model-version-id", "key2");
233 obj.setValue("data-owner", "");
234 TransactionalGraphEngine spy = spy(dbEngine);
235 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
236 Graph g = graph.newTransaction();
237 GraphTraversalSource traversal = g.traversal();
238 when(spy.asAdmin()).thenReturn(adminSpy);
239 when(adminSpy.getTraversalSource()).thenReturn(traversal);
240 DBSerializer serializer =
241 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
243 Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
245 OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
246 assertNotNull(ownerCheck);
248 ownerCheck.execute();