ee8ff77cfb53fd47b9cfbac60d928c8d70c7d2bc
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / sideeffect / OwnerCheckTest.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 static org.junit.Assert.assertNotNull;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
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;
56
57 @RunWith(value = Parameterized.class)
58 @Ignore
59 public class OwnerCheckTest extends AAISetup {
60
61     private static JanusGraph graph;
62     private final static ModelType introspectorFactoryType = ModelType.MOXY;
63     private static Loader loader;
64     private static TransactionalGraphEngine dbEngine;
65
66     @Rule
67     public ExpectedException thrown = ExpectedException.none();
68
69     @Parameterized.Parameter
70     public QueryStyle queryStyle;
71
72     @Parameterized.Parameters(name = "QueryStyle.{0}")
73     public static Collection<Object[]> data() {
74         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
75     }
76
77     @BeforeClass
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");
82
83         graph.traversal()
84             .addV("pnf")
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")
90             .as("v1")
91             .property(EdgeProperty.CONTAINS.toString(), true)
92             .addV("model-ver")
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")
98             .as("v2")
99             .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
100             .property(EdgeProperty.CONTAINS.toString(), true)
101             .addV("model")
102             .property("aai-node-type", "model")
103             .property("model-invariant-id", "key3")
104             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
105             .as("v3")
106             .addV()
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")
111             .as("v4")
112             .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
113             .property(EdgeProperty.CONTAINS.toString(), true)
114             .next();
115         graph.tx().commit();
116     }
117
118     @AfterClass
119     public static void tearDown() {
120         graph.tx().rollback();
121         graph.close();
122     }
123
124     @Before
125     public void initMock() {
126         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
127         MockitoAnnotations.initMocks(this);
128         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
129     }
130
131     @Test
132     public void shouldFailIfGroupsNotContainsDataOwner() throws Exception  {
133
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")));
149
150         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
151
152         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
153
154         thrown.expect(AAIException.class);
155         thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
156         ownerCheck.execute();
157         g.tx().rollback();
158
159     }
160
161     @Test
162     public void shouldPassIfGroupsContainsDataOwner() throws Exception  {
163
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);
176
177         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
178
179         DBSerializer serializer =
180             new DBSerializer(schemaVersions.getDefaultVersion(),
181                 spy, introspectorFactoryType,
182                 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
183
184         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
185         assertNotNull(ownerCheck);
186
187         ownerCheck.execute();
188         g.tx().rollback();
189     }
190
191     @Test
192     public void shouldPassIfGroupsIsEmpty() throws Exception  {
193
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,
208                 "AAI_TEST");
209
210         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
211
212         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
213         assertNotNull(ownerCheck);
214
215         ownerCheck.execute();
216         g.tx().rollback();
217     }
218
219     @Test
220     public void shouldPassIfDataOwnerIsNull() throws Exception  {
221
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,
237                 "AAI_TEST");
238
239         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
240
241         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
242         assertNotNull(ownerCheck);
243
244         ownerCheck.execute();
245         g.tx().rollback();
246     }
247
248     @Test
249     public void shouldPassIfDataOwnerIsEmpty() throws Exception  {
250
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,
266                 "AAI_TEST");
267
268         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
269
270         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
271         assertNotNull(ownerCheck);
272
273         ownerCheck.execute();
274         g.tx().rollback();
275     }
276 }