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 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;
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;
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;
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;
62 @RunWith(value = Parameterized.class)
64 public class DataCopyTest extends AAISetup {
66 private static JanusGraph graph;
67 private final static ModelType introspectorFactoryType = ModelType.MOXY;
68 private static Loader loader;
69 private static TransactionalGraphEngine dbEngine;
73 private VertexProperty<String> prop;
75 private QueryParser uriQuery;
77 public ExpectedException thrown = ExpectedException.none();
79 @Parameterized.Parameter
80 public QueryStyle queryStyle;
82 @Parameterized.Parameters(name = "QueryStyle.{0}")
83 public static Collection<Object[]> data() {
84 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
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");
95 .property("aai-node-type", "model")
96 .property("model-invariant-id", "key1")
97 .property(AAIProperties.AAI_URI,"/service-design-and-creation/models/model/key1")
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")
106 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
107 .property(EdgeProperty.CONTAINS.toString(), true)
109 .property("aai-node-type", "model")
110 .property("model-invariant-id", "key3")
111 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
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")
119 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
120 .property(EdgeProperty.CONTAINS.toString(), true)
126 public static void tearDown() {
127 graph.tx().rollback();
132 public void initMock() {
133 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
134 MockitoAnnotations.initMocks(this);
135 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
139 public void runPopulatePersonaModelVer() throws AAIException, UnsupportedEncodingException,
140 IllegalArgumentException, SecurityException {
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();
159 runner.execute(obj, self);
161 assertEquals("value populated", "testValue", obj.getValue("persona-model-version"));
168 public void verifyNestedSideEffect() throws AAIException, IllegalArgumentException, SecurityException, IOException {
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");
187 assertEquals("nested value populated", "testValue", g.traversal().V()
188 .has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
195 public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
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");
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();
214 thrown.expect(AAIMissingRequiredPropertyException.class);
215 runner.execute(obj, self);
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");
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();
238 thrown.expect(AAIMissingRequiredPropertyException.class);
239 runner.execute(obj, self);
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");
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();
260 runner.execute(obj, self);
262 assertNull("no model-version-id", obj.getValue("model-version-id"));
263 assertNull("no model-invariant-id", obj.getValue("model-invariant-id"));
267 private String getJsonString(String filename) throws IOException {
269 FileInputStream is = new FileInputStream("src/test/resources/oxm/sideeffect/" + filename);
270 String s = IOUtils.toString(is, StandardCharsets.UTF_8);
271 IOUtils.closeQuietly(is);