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