2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.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;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
39 import org.junit.rules.ExpectedException;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
43 import org.openecomp.aai.db.props.AAIProperties;
44 import org.openecomp.aai.dbmap.DBConnectionType;
45 import org.openecomp.aai.exceptions.AAIException;
46 import org.openecomp.aai.introspection.Introspector;
47 import org.openecomp.aai.introspection.Loader;
48 import org.openecomp.aai.introspection.LoaderFactory;
49 import org.openecomp.aai.introspection.ModelType;
50 import org.openecomp.aai.introspection.Version;
51 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
52 import org.openecomp.aai.parsers.query.QueryParser;
53 import org.openecomp.aai.serialization.db.DBSerializer;
54 import org.openecomp.aai.serialization.engines.QueryStyle;
55 import org.openecomp.aai.serialization.engines.TitanDBEngine;
56 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
57 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
58 import org.openecomp.aai.util.AAIConstants;
59 import com.thinkaurelius.titan.core.TitanFactory;
60 import com.thinkaurelius.titan.core.TitanGraph;
61 import com.thinkaurelius.titan.core.TitanTransaction;
64 public class DataCopyTest {
66 private static TitanGraph graph;
67 private final static Version version = Version.v10;
68 private final static ModelType introspectorFactoryType = ModelType.MOXY;
69 private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
70 private final static DBConnectionType type = DBConnectionType.REALTIME;
71 private static Loader loader;
72 private static TransactionalGraphEngine dbEngine;
73 @Mock private Vertex self;
74 @Mock private VertexProperty<String> prop;
75 @Mock private QueryParser uriQuery;
77 public ExpectedException thrown = ExpectedException.none();
81 public static void setup() throws NoSuchFieldException, SecurityException, Exception {
82 graph = TitanFactory.build().set("storage.backend","inmemory").open();
83 System.setProperty("AJSC_HOME", ".");
84 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
85 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
86 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
87 dbEngine = new TitanDBEngine(
92 graph.traversal().addV("aai-node-type", "model", "model-invariant-id", "key1").as("v1")
93 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key2", "model-version", "testValue").addInE("has", "v1", "isParent", true)
94 .addV("aai-node-type", "model", "model-invariant-id", "key3").as("v2")
95 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key4").addInE("has", "v2", "isParent", true)
101 public static void tearDown() {
102 graph.tx().rollback();
107 public void initMock() {
108 MockitoAnnotations.initMocks(this);
112 public void runPopulatePersonaModelVer() throws URISyntaxException, AAIException, UnsupportedEncodingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, MalformedURLException {
114 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
115 final Introspector obj = loader.introspectorFromName("test-object");
116 obj.setValue("vnf-id", "myId");
117 obj.setValue("model-invariant-id", "key1");
118 obj.setValue("model-version-id", "key2");
119 TransactionalGraphEngine spy = spy(dbEngine);
120 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
121 TitanTransaction g = graph.newTransaction();
122 GraphTraversalSource traversal = g.traversal();
123 when(spy.asAdmin()).thenReturn(adminSpy);
124 when(adminSpy.getTraversalSource()).thenReturn(traversal);
125 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
126 when(prop.orElse(null)).thenReturn(obj.getURI());
127 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
128 SideEffectRunner runner = new SideEffectRunner
129 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
131 runner.execute(obj, self);
133 assertEquals("value populated", "testValue", obj.getValue("persona-model-ver"));
141 public void runPopulateModelVersionId() throws URISyntaxException, AAIException, UnsupportedEncodingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, MalformedURLException {
143 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v9);
144 final Introspector obj = loader.introspectorFromName("test-object");
145 obj.setValue("vnf-id", "myId");
146 obj.setValue("persona-model-id", "key1");
147 obj.setValue("persona-model-version", "testValue");
148 TransactionalGraphEngine spy = spy(dbEngine);
149 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
150 TitanTransaction 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 = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
157 SideEffectRunner runner = new SideEffectRunner
158 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
160 runner.execute(obj, self);
162 assertEquals("value populated", "key2", obj.getValue("model-version-id"));
168 public void verifyNestedSideEffect() throws URISyntaxException, AAIException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IOException {
170 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
171 final Introspector obj = loader.unmarshal("customer", this.getJsonString("nested-case.json"));
172 System.out.println(obj.marshal(true));
173 TransactionalGraphEngine spy = spy(dbEngine);
174 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
175 TitanTransaction 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 = new DBSerializer(version, 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().has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
194 public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
196 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
197 final Introspector obj = loader.introspectorFromName("test-object");
198 obj.setValue("vnf-id", "myId");
199 obj.setValue("model-invariant-id", "key1");
201 TransactionalGraphEngine spy = spy(dbEngine);
202 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
203 TitanTransaction g = graph.newTransaction();
204 GraphTraversalSource traversal = g.traversal();
205 when(spy.asAdmin()).thenReturn(adminSpy);
206 when(adminSpy.getTraversalSource()).thenReturn(traversal);
207 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
208 when(prop.orElse(null)).thenReturn(obj.getURI());
209 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
210 SideEffectRunner runner = new SideEffectRunner
211 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
213 thrown.expect(AAIMissingRequiredPropertyException.class);
214 runner.execute(obj, self);
218 public void expectedMissingPropertyExceptionForResultingObject() throws AAIException, UnsupportedEncodingException {
219 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
220 final Introspector obj = loader.introspectorFromName("test-object");
221 obj.setValue("vnf-id", "myId");
222 obj.setValue("model-invariant-id", "key3");
223 obj.setValue("model-version-id", "key4");
225 TransactionalGraphEngine spy = spy(dbEngine);
226 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
227 TitanTransaction g = graph.newTransaction();
228 GraphTraversalSource traversal = g.traversal();
229 when(spy.asAdmin()).thenReturn(adminSpy);
230 when(adminSpy.getTraversalSource()).thenReturn(traversal);
231 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
232 when(prop.orElse(null)).thenReturn(obj.getURI());
233 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
234 SideEffectRunner runner = new SideEffectRunner
235 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
237 thrown.expect(AAIMissingRequiredPropertyException.class);
238 runner.execute(obj, self);
242 public void expectNoProcessingWithNoProperties() throws AAIException, UnsupportedEncodingException {
243 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
244 final Introspector obj = loader.introspectorFromName("test-object");
245 obj.setValue("vnf-id", "myId");
247 TransactionalGraphEngine spy = spy(dbEngine);
248 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
249 TitanTransaction g = graph.newTransaction();
250 GraphTraversalSource traversal = g.traversal();
251 when(spy.asAdmin()).thenReturn(adminSpy);
252 when(adminSpy.getTraversalSource()).thenReturn(traversal);
253 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
254 when(prop.orElse(null)).thenReturn(obj.getURI());
255 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
256 SideEffectRunner runner = new SideEffectRunner
257 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
259 runner.execute(obj, self);
261 assertEquals("no model-version-id", true, obj.getValue("model-version-id") == null);
262 assertEquals("no model-invariant-id", true, obj.getValue("model-invariant-id") == null);
266 private String getJsonString(String filename) throws IOException {
269 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/introspection/sideeffect/" + filename);
270 String s = IOUtils.toString(is, "UTF-8");
271 IOUtils.closeQuietly(is);