Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / serialization / db / DbAliasTest.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.serialization.db;
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.UnsupportedEncodingException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.Map;
33
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40
41 import org.openecomp.aai.dbmap.DBConnectionType;
42 import org.openecomp.aai.exceptions.AAIException;
43 import org.openecomp.aai.introspection.Introspector;
44 import org.openecomp.aai.introspection.Loader;
45 import org.openecomp.aai.introspection.LoaderFactory;
46 import org.openecomp.aai.introspection.ModelType;
47 import org.openecomp.aai.introspection.Version;
48 import org.openecomp.aai.parsers.query.QueryParser;
49 import org.openecomp.aai.schema.enums.PropertyMetadata;
50 import org.openecomp.aai.serialization.engines.QueryStyle;
51 import org.openecomp.aai.serialization.engines.TitanDBEngine;
52 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
53 import com.thinkaurelius.titan.core.TitanFactory;
54 import com.thinkaurelius.titan.core.TitanGraph;
55 import com.thinkaurelius.titan.core.TitanTransaction;
56
57 import java.util.Collections;
58
59 @Ignore("Causing problems when only parallel tests are run")
60 public class DbAliasTest {
61
62         
63         
64         private TitanGraph graph;
65         private final Version version = Version.v9;
66         private final ModelType introspectorFactoryType = ModelType.MOXY;
67         private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
68         private final DBConnectionType type = DBConnectionType.REALTIME;
69         private Loader loader;
70         private TransactionalGraphEngine dbEngine;
71         @Before
72         public void setup() throws NoSuchFieldException, SecurityException, Exception {
73                 graph = TitanFactory.build().set("storage.backend","inmemory").open();
74                 System.setProperty("AJSC_HOME", "./src/test/resources/");
75                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
76                 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
77                 dbEngine = new TitanDBEngine(
78                                 queryStyle,
79                                 type,
80                                 loader);
81         }
82         
83         @After
84         public void tearDown() {
85                 graph.tx().rollback();
86                 graph.close();
87         }
88         
89         @Test
90         public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
91                 final String property = "persona-model-customization-id";
92                 String dbPropertyName = property;
93                 TransactionalGraphEngine spy = spy(this.dbEngine);
94                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
95                 TitanTransaction g = graph.newTransaction();
96                 GraphTraversalSource traversal = g.traversal();
97                 when(spy.asAdmin()).thenReturn(adminSpy);
98                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
99                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
100                 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
101                 Introspector obj = loader.introspectorFromName("generic-vnf");
102                 Vertex v = g.addVertex();
103                 Object id = v.id();
104                 obj.setValue("vnf-id", "key1");
105                 obj.setValue(property, "hello");
106                 serializer.serializeToDb(obj, v, uriQuery, "", "");
107                 g.commit();
108                 v = graph.traversal().V(id).next();
109                 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
110                 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
111                         dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
112                 }
113                 
114                 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
115                 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
116                 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
117         
118         }
119         
120         @Test
121         public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
122                 final String property = "persona-model-customization-id";
123                 
124                 TransactionalGraphEngine spy = spy(dbEngine);
125                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
126                 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
127                 graph.tx().commit();
128                 TitanTransaction g = graph.newTransaction();
129                 GraphTraversalSource traversal = g.traversal();
130                 when(spy.asAdmin()).thenReturn(adminSpy);
131                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
132                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
133                 Introspector obj = loader.introspectorFromName("generic-vnf");
134                 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
135                 
136                 assertEquals("dbAlias property exists", "hello", obj.getValue(property));
137                 
138         }
139         
140 }