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