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