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