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;
61 import org.junit.Ignore;
63 @RunWith(value = Parameterized.class)
65 public class DataCopyTest extends AAISetup {
67 private static JanusGraph graph;
68 private final static ModelType introspectorFactoryType = ModelType.MOXY;
69 private static Loader loader;
70 private static TransactionalGraphEngine dbEngine;
74 private VertexProperty<String> prop;
76 private QueryParser uriQuery;
78 public ExpectedException thrown = ExpectedException.none();
80 @Parameterized.Parameter
81 public QueryStyle queryStyle;
83 @Parameterized.Parameters(name = "QueryStyle.{0}")
84 public static Collection<Object[]> data() {
85 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
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");
96 .property("aai-node-type", "model")
97 .property("model-invariant-id", "key1")
98 .property(AAIProperties.AAI_URI,"/service-design-and-creation/models/model/key1")
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")
107 .addE("org.onap.relationships.inventory.BelongsTo").to("v1").from("v2")
108 .property(EdgeProperty.CONTAINS.toString(), true)
110 .property("aai-node-type", "model")
111 .property("model-invariant-id", "key3")
112 .property(AAIProperties.AAI_URI, "/service-design-and-creation/models/model/key3")
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")
120 .addE("org.onap.relationships.inventory.BelongsTo").to("v3").from("v4")
121 .property(EdgeProperty.CONTAINS.toString(), true)
127 public static void tearDown() {
128 graph.tx().rollback();
133 public void initMock() {
134 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
135 MockitoAnnotations.initMocks(this);
136 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
140 public void runPopulatePersonaModelVer() throws AAIException, UnsupportedEncodingException,
141 IllegalArgumentException, SecurityException {
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();
160 runner.execute(obj, self);
162 assertEquals("value populated", "testValue", obj.getValue("persona-model-version"));
169 public void verifyNestedSideEffect() throws AAIException, IllegalArgumentException, SecurityException, IOException {
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");
188 assertEquals("nested value populated", "testValue", g.traversal().V()
189 .has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
196 public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
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");
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();
215 thrown.expect(AAIMissingRequiredPropertyException.class);
216 runner.execute(obj, self);
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");
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();
239 thrown.expect(AAIMissingRequiredPropertyException.class);
240 runner.execute(obj, self);
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");
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();
261 runner.execute(obj, self);
263 assertNull("no model-version-id", obj.getValue("model-version-id"));
264 assertNull("no model-invariant-id", obj.getValue("model-invariant-id"));
268 private String getJsonString(String filename) throws IOException {
270 FileInputStream is = new FileInputStream("src/test/resources/oxm/sideeffect/" + filename);
271 String s = IOUtils.toString(is, StandardCharsets.UTF_8);
272 IOUtils.closeQuietly(is);