Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / introspection / sideeffect / DataCopyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.introspection.sideeffect;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
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
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;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46
47 import org.openecomp.aai.db.props.AAIProperties;
48 import org.openecomp.aai.dbmap.DBConnectionType;
49 import org.openecomp.aai.exceptions.AAIException;
50 import org.openecomp.aai.introspection.Introspector;
51 import org.openecomp.aai.introspection.Loader;
52 import org.openecomp.aai.introspection.LoaderFactory;
53 import org.openecomp.aai.introspection.ModelType;
54 import org.openecomp.aai.introspection.Version;
55 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
56 import org.openecomp.aai.parsers.query.QueryParser;
57 import org.openecomp.aai.serialization.db.DBSerializer;
58 import org.openecomp.aai.serialization.engines.QueryStyle;
59 import org.openecomp.aai.serialization.engines.TitanDBEngine;
60 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
61 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
62 import org.openecomp.aai.util.AAIConstants;
63 import com.thinkaurelius.titan.core.TitanFactory;
64 import com.thinkaurelius.titan.core.TitanGraph;
65 import com.thinkaurelius.titan.core.TitanTransaction;
66
67 public class DataCopyTest {
68
69         private static TitanGraph graph;
70         private final static Version version = Version.v10;
71         private final static ModelType introspectorFactoryType = ModelType.MOXY;
72         private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
73         private final static DBConnectionType type = DBConnectionType.REALTIME;
74         private static Loader loader;
75         private static TransactionalGraphEngine dbEngine;
76         @Mock private Vertex self;
77         @Mock private VertexProperty<String> prop;
78         @Mock private QueryParser uriQuery;
79         @Rule
80         public ExpectedException thrown = ExpectedException.none();
81         
82         
83         @BeforeClass
84         public static void setup() throws NoSuchFieldException, SecurityException, Exception {
85                 graph = TitanFactory.build().set("storage.backend","inmemory").open();
86                 System.setProperty("AJSC_HOME", "./src/test/resources/");
87                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
88                 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
89                 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
90                 dbEngine = new TitanDBEngine(
91                                 queryStyle,
92                                 type,
93                                 loader);
94                 
95                 graph.traversal().addV("aai-node-type", "model", "model-invariant-id", "key1").as("v1")
96                 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key2", "model-version", "testValue").addInE("has", "v1", "isParent", true)
97                 .addV("aai-node-type", "model", "model-invariant-id", "key3").as("v2")
98                 .addV("aai-node-type", "model-ver", "model-ver", "myValue", "model-version-id", "key4").addInE("has", "v2", "isParent", true)
99                 .next();
100                 graph.tx().commit();
101         }
102         
103         @AfterClass
104         public static void tearDown() {
105                 graph.tx().rollback();
106                 graph.close();
107         }
108         
109         @Before
110         public void initMock() {
111                 MockitoAnnotations.initMocks(this);
112         }
113         
114         @Test
115         public void runPopulatePersonaModelVer() throws URISyntaxException, AAIException, UnsupportedEncodingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, MalformedURLException {
116                 
117                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
118                 final Introspector obj = loader.introspectorFromName("test-object");
119                 obj.setValue("vnf-id", "myId");
120                 obj.setValue("model-invariant-id", "key1");
121                 obj.setValue("model-version-id", "key2");
122                 TransactionalGraphEngine spy = spy(dbEngine);
123                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
124                 TitanTransaction g = graph.newTransaction();
125                 GraphTraversalSource traversal = g.traversal();
126                 when(spy.asAdmin()).thenReturn(adminSpy);
127                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
128                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
129                 when(prop.orElse(null)).thenReturn(obj.getURI());
130                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
131                 SideEffectRunner runner = new SideEffectRunner
132                                 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
133                 
134                 runner.execute(obj, self);
135
136                 assertEquals("value populated", "testValue", obj.getValue("persona-model-ver"));
137                 
138                 g.rollback();
139                 
140                 
141         }
142         
143         @Test
144         public void runPopulateModelVersionId() throws URISyntaxException, AAIException, UnsupportedEncodingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, MalformedURLException {
145                 
146                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v9);
147                 final Introspector obj = loader.introspectorFromName("test-object");
148                 obj.setValue("vnf-id", "myId");
149                 obj.setValue("persona-model-id", "key1");
150                 obj.setValue("persona-model-version", "testValue");
151                 TransactionalGraphEngine spy = spy(dbEngine);
152                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
153                 TitanTransaction g = graph.newTransaction();
154                 GraphTraversalSource traversal = g.traversal();
155                 when(spy.asAdmin()).thenReturn(adminSpy);
156                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
157                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
158                 when(prop.orElse(null)).thenReturn(obj.getURI());
159                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
160                 SideEffectRunner runner = new SideEffectRunner
161                                 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
162                 
163                 runner.execute(obj, self);
164                 
165                 assertEquals("value populated", "key2", obj.getValue("model-version-id"));
166                 
167                 g.rollback();
168         }
169         
170         @Test
171         public void verifyNestedSideEffect() throws URISyntaxException, AAIException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IOException {
172                 
173                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
174                 final Introspector obj = loader.unmarshal("customer", this.getJsonString("nested-case.json"));
175                 System.out.println(obj.marshal(true));
176                 TransactionalGraphEngine spy = spy(dbEngine);
177                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
178                 TitanTransaction g = graph.newTransaction();
179                 GraphTraversalSource traversal = g.traversal();
180                 when(spy.tx()).thenReturn(g);
181                 when(spy.asAdmin()).thenReturn(adminSpy);
182                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
183                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
184                 when(prop.orElse(null)).thenReturn(obj.getURI());
185                 when(uriQuery.isDependent()).thenReturn(false);
186                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
187                 Vertex v= serializer.createNewVertex(obj);
188                 serializer.serializeToDb(obj, v, uriQuery, obj.getURI(), "test");
189                 
190                 assertEquals("nested value populated", "testValue", g.traversal().V().has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
191
192                 g.rollback();
193
194         }
195         
196         @Test
197         public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
198                 
199                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
200                 final Introspector obj = loader.introspectorFromName("test-object");
201                 obj.setValue("vnf-id", "myId");
202                 obj.setValue("model-invariant-id", "key1");
203
204                 TransactionalGraphEngine spy = spy(dbEngine);
205                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
206                 TitanTransaction g = graph.newTransaction();
207                 GraphTraversalSource traversal = g.traversal();
208                 when(spy.asAdmin()).thenReturn(adminSpy);
209                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
210                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
211                 when(prop.orElse(null)).thenReturn(obj.getURI());
212                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
213                 SideEffectRunner runner = new SideEffectRunner
214                                 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
215                 
216                 thrown.expect(AAIMissingRequiredPropertyException.class);
217                 runner.execute(obj, self);
218         }
219         
220         @Test
221         public void expectedMissingPropertyExceptionForResultingObject() throws AAIException, UnsupportedEncodingException {
222                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
223                 final Introspector obj = loader.introspectorFromName("test-object");
224                 obj.setValue("vnf-id", "myId");
225                 obj.setValue("model-invariant-id", "key3");
226                 obj.setValue("model-version-id", "key4");
227
228                 TransactionalGraphEngine spy = spy(dbEngine);
229                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
230                 TitanTransaction g = graph.newTransaction();
231                 GraphTraversalSource traversal = g.traversal();
232                 when(spy.asAdmin()).thenReturn(adminSpy);
233                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
234                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
235                 when(prop.orElse(null)).thenReturn(obj.getURI());
236                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
237                 SideEffectRunner runner = new SideEffectRunner
238                                 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
239                 
240                 thrown.expect(AAIMissingRequiredPropertyException.class);
241                 runner.execute(obj, self);
242         }
243         
244         @Test
245         public void expectNoProcessingWithNoProperties() throws AAIException, UnsupportedEncodingException {
246                 final Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
247                 final Introspector obj = loader.introspectorFromName("test-object");
248                 obj.setValue("vnf-id", "myId");
249
250                 TransactionalGraphEngine spy = spy(dbEngine);
251                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
252                 TitanTransaction g = graph.newTransaction();
253                 GraphTraversalSource traversal = g.traversal();
254                 when(spy.asAdmin()).thenReturn(adminSpy);
255                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
256                 when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
257                 when(prop.orElse(null)).thenReturn(obj.getURI());
258                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
259                 SideEffectRunner runner = new SideEffectRunner
260                                 .Builder(spy, serializer).addSideEffect(DataCopy.class).build();
261                 
262                 runner.execute(obj, self);
263                 
264                 assertEquals("no model-version-id", true, obj.getValue("model-version-id") == null);
265                 assertEquals("no model-invariant-id", true, obj.getValue("model-invariant-id") == null);
266                 
267         }
268         
269         private String getJsonString(String filename) throws IOException {
270                 
271                 
272                 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/introspection/sideeffect/" + filename);
273                 String s =  IOUtils.toString(is, "UTF-8"); 
274                 IOUtils.closeQuietly(is);
275                 
276                 return s;
277         }
278 }