[AAI] Revert some testing changes to ignore tests and rollback of eclipse perisistence
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / sideeffect / AOwnerCheckTest.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
56 @RunWith(value = Parameterized.class)
57
58 public class AOwnerCheckTest extends AAISetup {
59
60     private static JanusGraph graph;
61     private final static ModelType introspectorFactoryType = ModelType.MOXY;
62     private static Loader loader;
63     private static TransactionalGraphEngine dbEngine;
64
65     @Rule
66     public ExpectedException thrown = ExpectedException.none();
67
68     @Parameterized.Parameter
69     public QueryStyle queryStyle;
70
71     @Parameterized.Parameters(name = "QueryStyle.{0}")
72     public static Collection<Object[]> data() {
73         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
74     }
75
76     @BeforeClass
77     public static void setup() {
78         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
79         System.setProperty("AJSC_HOME", ".");
80         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
81
82         graph.traversal()
83             .addV("pnf")
84             .property("aai-node-type", "pnf")
85             .property("pnf-name", "my-pnf")
86             .property("data-owner", "Operator")
87             .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf")
88             .property("model-invariant-id", "key1")
89             .as("v1")
90             .property(EdgeProperty.CONTAINS.toString(), true)
91             .addV("model-ver")
92             .property("aai-node-type", "model-ver")
93             .property("model-ver", "myValue")
94             .property("model-version-id", "key2")
95             .property("model-version", "testValue")
96             .property(AAIProperties.AAI_URI, "/network/pnfs/pnf/my-pnf/model-vers/model-ver/key2")
97             .as("v2")
98             .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
99             .property(EdgeProperty.CONTAINS.toString(), true)
100             .addV("model")
101             .property("aai-node-type", "model")
102             .property("model-invariant-id", "key3")
103             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
104             .as("v3")
105             .addV()
106             .property("aai-node-type", "model-ver")
107             .property("model-ver", "myValue")
108             .property("model-version-id", "key4")
109             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
110             .as("v4")
111             .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
112             .property(EdgeProperty.CONTAINS.toString(), true)
113             .next();
114         graph.tx().commit();
115     }
116
117     @AfterClass
118     public static void tearDown() {
119         graph.tx().rollback();
120         graph.close();
121     }
122
123     @Before
124     public void initMock() {
125         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
126         MockitoAnnotations.initMocks(this);
127         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
128     }
129
130     @Test
131     public void shouldFailIfGroupsNotContainsDataOwner() throws Exception  {
132
133         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
134         final Introspector obj = loader.introspectorFromName("pnf");
135         obj.setValue("pnf-name", "my-pnf");
136         obj.setValue("model-invariant-id", "key1");
137         obj.setValue("model-version-id", "key2");
138         TransactionalGraphEngine spy = spy(dbEngine);
139         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
140         Graph g = graph.newTransaction();
141         GraphTraversalSource traversal = g.traversal();
142         when(spy.asAdmin()).thenReturn(adminSpy);
143         when(adminSpy.getTraversalSource()).thenReturn(traversal);
144         DBSerializer serializer =
145             new DBSerializer(schemaVersions.getDefaultVersion(),
146                 spy, introspectorFactoryType,
147                 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorI", "OperatorII")));
148
149         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
150
151         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
152
153         thrown.expect(AAIException.class);
154         thrown.expectMessage("Group(s) :[OperatorII, OperatorI] not authorized to perform function");
155         ownerCheck.execute();
156         g.tx().rollback();
157
158     }
159
160     @Test
161     public void shouldPassIfGroupsContainsDataOwner() throws Exception  {
162
163         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
164         final Introspector obj = loader.introspectorFromName("pnf");
165         obj.setValue("pnf-name", "my-pnf");
166         obj.setValue("model-invariant-id", "key1");
167         obj.setValue("model-version-id", "key2");
168         TransactionalGraphEngine spy = spy(dbEngine);
169         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
170         Graph g = graph.newTransaction();
171         GraphTraversalSource traversal = g.traversal();
172         when(spy.tx()).thenReturn(g);
173         when(spy.asAdmin()).thenReturn(adminSpy);
174         when(adminSpy.getTraversalSource()).thenReturn(traversal);
175
176         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
177
178         DBSerializer serializer =
179             new DBSerializer(schemaVersions.getDefaultVersion(),
180                 spy, introspectorFactoryType,
181                 "AAI_TEST", new HashSet<>(Arrays.asList("OperatorIII", "Operator")));
182
183         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
184         assertNotNull(ownerCheck);
185
186         ownerCheck.execute();
187         g.tx().rollback();
188     }
189
190     @Test
191     public void shouldPassIfGroupsIsEmpty() throws Exception  {
192
193         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
194         final Introspector obj = loader.introspectorFromName("pnf");
195         obj.setValue("pnf-name", "my-pnf");
196         obj.setValue("model-invariant-id", "key1");
197         obj.setValue("model-version-id", "key2");
198         TransactionalGraphEngine spy = spy(dbEngine);
199         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
200         Graph g = graph.newTransaction();
201         GraphTraversalSource traversal = g.traversal();
202         when(spy.asAdmin()).thenReturn(adminSpy);
203         when(adminSpy.getTraversalSource()).thenReturn(traversal);
204         DBSerializer serializer =
205             new DBSerializer(schemaVersions.getDefaultVersion(),
206                 spy, introspectorFactoryType,
207                 "AAI_TEST");
208
209         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
210
211         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
212         assertNotNull(ownerCheck);
213
214         ownerCheck.execute();
215         g.tx().rollback();
216     }
217
218     @Test
219     public void shouldPassIfDataOwnerIsNull() throws Exception  {
220
221         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
222         final Introspector obj = loader.introspectorFromName("pnf");
223         obj.setValue("pnf-name", "my-pnf");
224         obj.setValue("model-invariant-id", "key1");
225         obj.setValue("model-version-id", "key2");
226         obj.setValue("data-owner", null);
227         TransactionalGraphEngine spy = spy(dbEngine);
228         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
229         Graph g = graph.newTransaction();
230         GraphTraversalSource traversal = g.traversal();
231         when(spy.asAdmin()).thenReturn(adminSpy);
232         when(adminSpy.getTraversalSource()).thenReturn(traversal);
233         DBSerializer serializer =
234             new DBSerializer(schemaVersions.getDefaultVersion(),
235                 spy, introspectorFactoryType,
236                 "AAI_TEST");
237
238         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
239
240         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
241         assertNotNull(ownerCheck);
242
243         ownerCheck.execute();
244         g.tx().rollback();
245     }
246
247     @Test
248     public void shouldPassIfDataOwnerIsEmpty() throws Exception  {
249
250         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
251         final Introspector obj = loader.introspectorFromName("pnf");
252         obj.setValue("pnf-name", "my-pnf");
253         obj.setValue("model-invariant-id", "key1");
254         obj.setValue("model-version-id", "key2");
255         obj.setValue("data-owner", "");
256         TransactionalGraphEngine spy = spy(dbEngine);
257         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
258         Graph g = graph.newTransaction();
259         GraphTraversalSource traversal = g.traversal();
260         when(spy.asAdmin()).thenReturn(adminSpy);
261         when(adminSpy.getTraversalSource()).thenReturn(traversal);
262         DBSerializer serializer =
263             new DBSerializer(schemaVersions.getDefaultVersion(),
264                 spy, introspectorFactoryType,
265                 "AAI_TEST");
266
267         Vertex selfV = g.traversal().V().has("aai-node-type", "pnf").next();
268
269         OwnerCheck ownerCheck = new OwnerCheck(obj, selfV, spy, serializer);
270         assertNotNull(ownerCheck);
271
272         ownerCheck.execute();
273         g.tx().rollback();
274     }
275 }