Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / introspection / sideeffect / DataCopyTest.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 org.apache.commons.io.IOUtils;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
28 import org.janusgraph.core.JanusGraph;
29 import org.janusgraph.core.JanusGraphFactory;
30 import org.junit.*;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.db.props.AAIProperties;
38 import org.onap.aai.edges.enums.EdgeProperty;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Introspector;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.ModelType;
43 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
44 import org.onap.aai.parsers.query.QueryParser;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49
50 import java.io.FileInputStream;
51 import java.io.IOException;
52 import java.io.UnsupportedEncodingException;
53 import java.nio.charset.StandardCharsets;
54 import java.util.Arrays;
55 import java.util.Collection;
56
57 import static org.junit.Assert.assertEquals;
58 import static org.junit.Assert.assertNull;
59 import static org.mockito.Mockito.spy;
60 import static org.mockito.Mockito.when;
61
62 @RunWith(value = Parameterized.class)
63
64 public class DataCopyTest extends AAISetup {
65
66     private static JanusGraph graph;
67     private final static ModelType introspectorFactoryType = ModelType.MOXY;
68     private static Loader loader;
69     private static TransactionalGraphEngine dbEngine;
70     @Mock
71     private Vertex self;
72     @Mock
73     private VertexProperty<String> prop;
74     @Mock
75     private QueryParser uriQuery;
76     @Rule
77     public ExpectedException thrown = ExpectedException.none();
78
79     @Parameterized.Parameter
80     public QueryStyle queryStyle;
81
82     @Parameterized.Parameters(name = "QueryStyle.{0}")
83     public static Collection<Object[]> data() {
84         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
85     }
86
87     @BeforeClass
88     public static void setup() {
89         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
90         System.setProperty("AJSC_HOME", ".");
91         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
92
93         graph.traversal()
94             .addV("model")
95             .property("aai-node-type", "model")
96             .property("model-invariant-id", "key1")
97             .property(AAIProperties.AAI_URI,"/service-design-and-creation/models/model/key1")
98             .as("v1")
99             .addV("model-ver")
100             .property("aai-node-type", "model-ver")
101             .property("model-ver", "myValue")
102             .property("model-version-id", "key2")
103             .property("model-version", "testValue")
104             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key1/model-vers/model-ver/key2")
105             .as("v2")
106             .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
107             .property(EdgeProperty.CONTAINS.toString(), true)
108             .addV("model")
109             .property("aai-node-type", "model")
110             .property("model-invariant-id", "key3")
111             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
112             .as("v3")
113             .addV()
114             .property("aai-node-type", "model-ver")
115             .property("model-ver", "myValue")
116             .property("model-version-id", "key4")
117             .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
118             .as("v4")
119             .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
120             .property(EdgeProperty.CONTAINS.toString(), true)
121             .next();
122         graph.tx().commit();
123     }
124
125     @AfterClass
126     public static void tearDown() {
127         graph.tx().rollback();
128         graph.close();
129     }
130
131     @Before
132     public void initMock() {
133         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
134         MockitoAnnotations.initMocks(this);
135         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
136     }
137
138     @Test
139     public void runPopulatePersonaModelVer() throws AAIException, UnsupportedEncodingException,
140             IllegalArgumentException, SecurityException {
141
142         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
143         final Introspector obj = loader.introspectorFromName("generic-vnf");
144         obj.setValue("vnf-id", "myId");
145         obj.setValue("model-invariant-id", "key1");
146         obj.setValue("model-version-id", "key2");
147         TransactionalGraphEngine spy = spy(dbEngine);
148         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
149         Graph g = graph.newTransaction();
150         GraphTraversalSource traversal = g.traversal();
151         when(spy.asAdmin()).thenReturn(adminSpy);
152         when(adminSpy.getTraversalSource()).thenReturn(traversal);
153         when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
154         when(prop.orElse(null)).thenReturn(obj.getURI());
155         DBSerializer serializer =
156                 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
157         SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
158
159         runner.execute(obj, self);
160
161         assertEquals("value populated", "testValue", obj.getValue("persona-model-version"));
162
163         g.tx().rollback();
164
165     }
166
167     @Test
168     public void verifyNestedSideEffect() throws AAIException, IllegalArgumentException, SecurityException, IOException {
169
170         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
171         final Introspector obj = loader.unmarshal("customer", this.getJsonString("nested-case.json"));
172         TransactionalGraphEngine spy = spy(dbEngine);
173         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
174         Graph g = graph.newTransaction();
175         GraphTraversalSource traversal = g.traversal();
176         when(spy.tx()).thenReturn(g);
177         when(spy.asAdmin()).thenReturn(adminSpy);
178         when(adminSpy.getTraversalSource()).thenReturn(traversal);
179         when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
180         when(prop.orElse(null)).thenReturn(obj.getURI());
181         when(uriQuery.isDependent()).thenReturn(false);
182         DBSerializer serializer =
183                 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
184         Vertex v = serializer.createNewVertex(obj);
185         serializer.serializeToDb(obj, v, uriQuery, obj.getURI(), "test");
186
187         assertEquals("nested value populated", "testValue", g.traversal().V()
188                 .has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
189
190         g.tx().rollback();
191
192     }
193
194     @Test
195     public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
196
197         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
198         final Introspector obj = loader.introspectorFromName("generic-vnf");
199         obj.setValue("vnf-id", "myId");
200         obj.setValue("model-invariant-id", "key1");
201
202         TransactionalGraphEngine spy = spy(dbEngine);
203         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
204         Graph g = graph.newTransaction();
205         GraphTraversalSource traversal = g.traversal();
206         when(spy.asAdmin()).thenReturn(adminSpy);
207         when(adminSpy.getTraversalSource()).thenReturn(traversal);
208         when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
209         when(prop.orElse(null)).thenReturn(obj.getURI());
210         DBSerializer serializer =
211                 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
212         SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
213
214         thrown.expect(AAIMissingRequiredPropertyException.class);
215         runner.execute(obj, self);
216     }
217
218     @Test
219     public void expectedMissingPropertyExceptionForResultingObject() throws AAIException, UnsupportedEncodingException {
220         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
221         final Introspector obj = loader.introspectorFromName("generic-vnf");
222         obj.setValue("vnf-id", "myId");
223         obj.setValue("model-invariant-id", "key3");
224         obj.setValue("model-version-id", "key4");
225
226         TransactionalGraphEngine spy = spy(dbEngine);
227         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
228         Graph g = graph.newTransaction();
229         GraphTraversalSource traversal = g.traversal();
230         when(spy.asAdmin()).thenReturn(adminSpy);
231         when(adminSpy.getTraversalSource()).thenReturn(traversal);
232         when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
233         when(prop.orElse(null)).thenReturn(obj.getURI());
234         DBSerializer serializer =
235                 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
236         SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
237
238         thrown.expect(AAIMissingRequiredPropertyException.class);
239         runner.execute(obj, self);
240     }
241
242     @Test
243     public void expectNoProcessingWithNoProperties() throws AAIException, UnsupportedEncodingException {
244         final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
245         final Introspector obj = loader.introspectorFromName("generic-vnf");
246         obj.setValue("vnf-id", "myId");
247
248         TransactionalGraphEngine spy = spy(dbEngine);
249         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
250         Graph g = graph.newTransaction();
251         GraphTraversalSource traversal = g.traversal();
252         when(spy.asAdmin()).thenReturn(adminSpy);
253         when(adminSpy.getTraversalSource()).thenReturn(traversal);
254         when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
255         when(prop.orElse(null)).thenReturn(obj.getURI());
256         DBSerializer serializer =
257                 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
258         SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
259
260         runner.execute(obj, self);
261
262         assertNull("no model-version-id", obj.getValue("model-version-id"));
263         assertNull("no model-invariant-id", obj.getValue("model-invariant-id"));
264
265     }
266
267     private String getJsonString(String filename) throws IOException {
268
269         FileInputStream is = new FileInputStream("src/test/resources/oxm/sideeffect/" + filename);
270         String s = IOUtils.toString(is, StandardCharsets.UTF_8);
271         IOUtils.closeQuietly(is);
272
273         return s;
274     }
275 }