Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / DbAliasTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.serialization.db;
21
22 import org.janusgraph.core.JanusGraphFactory;
23 import org.janusgraph.core.JanusGraph;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 import org.onap.aai.AAISetup;
33 import org.onap.aai.dbmap.DBConnectionType;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.*;
36 import org.onap.aai.parsers.query.QueryParser;
37 import org.onap.aai.schema.enums.PropertyMetadata;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.springframework.test.annotation.DirtiesContext;
43
44 import java.io.UnsupportedEncodingException;
45 import java.lang.reflect.InvocationTargetException;
46 import java.net.MalformedURLException;
47 import java.net.URI;
48 import java.net.URISyntaxException;
49 import java.util.Arrays;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.Map;
53
54 import static org.junit.Assert.assertEquals;
55 import static org.mockito.Mockito.spy;
56 import static org.mockito.Mockito.when;
57
58 @RunWith(value = Parameterized.class)
59 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
60 public class DbAliasTest extends AAISetup {
61
62         private JanusGraph graph;
63
64         private SchemaVersion version;
65         private final ModelType introspectorFactoryType = ModelType.MOXY;
66         private final DBConnectionType type = DBConnectionType.REALTIME;
67         private Loader loader;
68         private TransactionalGraphEngine dbEngine;
69
70         @Parameterized.Parameter(value = 0)
71         public QueryStyle queryStyle;
72
73         @Parameterized.Parameters(name = "QueryStyle.{0}")
74         public static Collection<Object[]> data() {
75                 return Arrays.asList(new Object[][]{
76                                 {QueryStyle.TRAVERSAL},
77                                 {QueryStyle.TRAVERSAL_URI}
78                 });
79         }
80
81         @Before
82         public void setup() throws Exception {
83             version = schemaVersions.getDepthVersion();
84                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
85                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
86                 dbEngine = new JanusGraphDBEngine(
87                                 queryStyle,
88                                 type,
89                                 loader);
90         }
91
92         @After
93         public void tearDown() {
94                 graph.tx().rollback();
95                 graph.close();
96         }
97
98         @Test
99         public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
100                 final String property = "persona-model-customization-id";
101                 String dbPropertyName = property;
102                 TransactionalGraphEngine spy = spy(this.dbEngine);
103                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
104                 Graph g = graph.newTransaction();
105                 GraphTraversalSource traversal = g.traversal();
106                 when(spy.asAdmin()).thenReturn(adminSpy);
107                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
108                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
109                 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
110                 Introspector obj = loader.introspectorFromName("generic-vnf");
111                 Vertex v = g.addVertex();
112                 Object id = v.id();
113                 obj.setValue("vnf-id", "key1");
114                 obj.setValue(property, "hello");
115                 serializer.serializeToDb(obj, v, uriQuery, "", "");
116                 g.tx().commit();
117                 v = graph.traversal().V(id).next();
118                 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
119                 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
120                         dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
121                 }
122
123                 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
124                 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
125                 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
126
127         }
128
129         @Test
130         public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
131                 final String property = "persona-model-customization-id";
132
133                 TransactionalGraphEngine spy = spy(dbEngine);
134                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
135                 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
136                 graph.tx().commit();
137                 Graph g = graph.newTransaction();
138                 GraphTraversalSource traversal = g.traversal();
139                 when(spy.asAdmin()).thenReturn(adminSpy);
140                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
141                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
142                 Introspector obj = loader.introspectorFromName("generic-vnf");
143                 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
144
145                 assertEquals("dbAlias property exists", "hello", obj.getValue(property));
146
147         }
148
149
150 }