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.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 import java.lang.reflect.InvocationTargetException;
31 import java.net.MalformedURLException;
32 import java.net.URISyntaxException;
33 import java.util.Arrays;
34 import java.util.Collection;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
38 import org.apache.tinkerpop.gremlin.structure.Graph;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
41 import org.janusgraph.core.JanusGraph;
42 import org.janusgraph.core.JanusGraphFactory;
44 import org.junit.rules.ExpectedException;
45 import org.junit.runner.RunWith;
46 import org.junit.runners.Parameterized;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.aai.AAISetup;
50 import org.onap.aai.db.props.AAIProperties;
51 import org.onap.aai.dbmap.DBConnectionType;
52 import org.onap.aai.edges.enums.EdgeProperty;
53 import org.onap.aai.exceptions.AAIException;
54 import org.onap.aai.introspection.*;
55 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
56 import org.onap.aai.parsers.query.QueryParser;
57 import org.onap.aai.serialization.db.DBSerializer;
58 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
59 import org.onap.aai.serialization.engines.QueryStyle;
60 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
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 final static DBConnectionType type = DBConnectionType.REALTIME;
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(value = 0)
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() throws NoSuchFieldException, SecurityException, Exception {
90 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
91 System.setProperty("AJSC_HOME", ".");
92 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
95 .addV("aai-node-type", "model", "model-invariant-id", "key1", AAIProperties.AAI_URI,
96 "/service-design-and-creation/models/model/key1")
98 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key2", "model-version",
99 "testValue", AAIProperties.AAI_URI,
100 "/service-design-and-creation/models/model/key1/model-vers/model-ver/key2")
101 .addOutE("org.onap.relationships.inventory.BelongsTo", "v1", EdgeProperty.CONTAINS.toString(), true)
102 .addV("aai-node-type", "model", "model-invariant-id", "key3", AAIProperties.AAI_URI,
103 "/service-design-and-creation/models/model/key3")
105 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key4",
106 AAIProperties.AAI_URI,
107 "/service-design-and-creation/models/model/key3/model-vers/model-ver/key4")
108 .addOutE("org.onap.relationships.inventory.BelongsTo", "v2", EdgeProperty.CONTAINS.toString(), true)
114 public static void tearDown() {
115 graph.tx().rollback();
120 public void initMock() {
121 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
122 MockitoAnnotations.initMocks(this);
123 dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
127 public void runPopulatePersonaModelVer() throws URISyntaxException, AAIException, UnsupportedEncodingException,
128 IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException,
129 InstantiationException, NoSuchMethodException, MalformedURLException {
131 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
132 final Introspector obj = loader.introspectorFromName("generic-vnf");
133 obj.setValue("vnf-id", "myId");
134 obj.setValue("model-invariant-id", "key1");
135 obj.setValue("model-version-id", "key2");
136 TransactionalGraphEngine spy = spy(dbEngine);
137 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
138 Graph g = graph.newTransaction();
139 GraphTraversalSource traversal = g.traversal();
140 when(spy.asAdmin()).thenReturn(adminSpy);
141 when(adminSpy.getTraversalSource()).thenReturn(traversal);
142 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
143 when(prop.orElse(null)).thenReturn(obj.getURI());
144 DBSerializer serializer =
145 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
146 SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
148 runner.execute(obj, self);
150 assertEquals("value populated", "testValue", obj.getValue("persona-model-version"));
157 public void verifyNestedSideEffect()
158 throws URISyntaxException, AAIException, IllegalAccessException, IllegalArgumentException,
159 InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IOException {
161 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
162 final Introspector obj = loader.unmarshal("customer", this.getJsonString("nested-case.json"));
163 // System.out.println(obj.marshal(true));
164 TransactionalGraphEngine spy = spy(dbEngine);
165 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
166 Graph g = graph.newTransaction();
167 GraphTraversalSource traversal = g.traversal();
168 when(spy.tx()).thenReturn(g);
169 when(spy.asAdmin()).thenReturn(adminSpy);
170 when(adminSpy.getTraversalSource()).thenReturn(traversal);
171 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
172 when(prop.orElse(null)).thenReturn(obj.getURI());
173 when(uriQuery.isDependent()).thenReturn(false);
174 DBSerializer serializer =
175 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
176 Vertex v = serializer.createNewVertex(obj);
177 serializer.serializeToDb(obj, v, uriQuery, obj.getURI(), "test");
179 assertEquals("nested value populated", "testValue", g.traversal().V()
180 .has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
187 public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
189 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
190 final Introspector obj = loader.introspectorFromName("generic-vnf");
191 obj.setValue("vnf-id", "myId");
192 obj.setValue("model-invariant-id", "key1");
194 TransactionalGraphEngine spy = spy(dbEngine);
195 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
196 Graph g = graph.newTransaction();
197 GraphTraversalSource traversal = g.traversal();
198 when(spy.asAdmin()).thenReturn(adminSpy);
199 when(adminSpy.getTraversalSource()).thenReturn(traversal);
200 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
201 when(prop.orElse(null)).thenReturn(obj.getURI());
202 DBSerializer serializer =
203 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
204 SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
206 thrown.expect(AAIMissingRequiredPropertyException.class);
207 runner.execute(obj, self);
211 public void expectedMissingPropertyExceptionForResultingObject() throws AAIException, UnsupportedEncodingException {
212 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
213 final Introspector obj = loader.introspectorFromName("generic-vnf");
214 obj.setValue("vnf-id", "myId");
215 obj.setValue("model-invariant-id", "key3");
216 obj.setValue("model-version-id", "key4");
218 TransactionalGraphEngine spy = spy(dbEngine);
219 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
220 Graph g = graph.newTransaction();
221 GraphTraversalSource traversal = g.traversal();
222 when(spy.asAdmin()).thenReturn(adminSpy);
223 when(adminSpy.getTraversalSource()).thenReturn(traversal);
224 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
225 when(prop.orElse(null)).thenReturn(obj.getURI());
226 DBSerializer serializer =
227 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
228 SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
230 thrown.expect(AAIMissingRequiredPropertyException.class);
231 runner.execute(obj, self);
235 public void expectNoProcessingWithNoProperties() throws AAIException, UnsupportedEncodingException {
236 final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
237 final Introspector obj = loader.introspectorFromName("generic-vnf");
238 obj.setValue("vnf-id", "myId");
240 TransactionalGraphEngine spy = spy(dbEngine);
241 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
242 Graph g = graph.newTransaction();
243 GraphTraversalSource traversal = g.traversal();
244 when(spy.asAdmin()).thenReturn(adminSpy);
245 when(adminSpy.getTraversalSource()).thenReturn(traversal);
246 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
247 when(prop.orElse(null)).thenReturn(obj.getURI());
248 DBSerializer serializer =
249 new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
250 SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
252 runner.execute(obj, self);
254 assertEquals("no model-version-id", true, obj.getValue("model-version-id") == null);
255 assertEquals("no model-invariant-id", true, obj.getValue("model-invariant-id") == null);
259 private String getJsonString(String filename) throws IOException {
261 FileInputStream is = new FileInputStream("src/test/resources/oxm/sideeffect/" + filename);
262 String s = IOUtils.toString(is, "UTF-8");
263 IOUtils.closeQuietly(is);