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