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