Reduce the number of problems in aai-common by removing unused imports
[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.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.Arrays;
31 import java.util.Collection;
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.Vertex;
38 import org.janusgraph.core.JanusGraph;
39 import org.janusgraph.core.JanusGraphFactory;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.onap.aai.DataLinkSetup;
46 import org.onap.aai.db.props.AAIProperties;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.Introspector;
49 import org.onap.aai.introspection.Loader;
50 import org.onap.aai.introspection.ModelType;
51 import org.onap.aai.parsers.query.QueryParser;
52 import org.onap.aai.schema.enums.PropertyMetadata;
53 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
54 import org.onap.aai.serialization.engines.QueryStyle;
55 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 import org.onap.aai.setup.SchemaVersion;
57 import org.springframework.test.annotation.DirtiesContext;
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,
94             IllegalArgumentException {
95         final String property = "persona-model-customization-id";
96         String dbPropertyName = property;
97         TransactionalGraphEngine spy = spy(this.dbEngine);
98         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
99         Graph g = graph.newTransaction();
100         GraphTraversalSource traversal = g.traversal();
101         when(spy.asAdmin()).thenReturn(adminSpy);
102         when(adminSpy.getTraversalSource()).thenReturn(traversal);
103         DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
104         QueryParser uriQuery =
105                 spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
106         Introspector obj = loader.introspectorFromName("generic-vnf");
107         Vertex v = g.addVertex();
108         v.property("aai-uri", "abc");
109         v.property("aai-uuid", "b");
110         v.property(AAIProperties.CREATED_TS, 123L);
111         v.property(AAIProperties.SOURCE_OF_TRUTH, "sot");
112         v.property(AAIProperties.RESOURCE_VERSION, "123");
113         v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot");
114         v.property(AAIProperties.LAST_MOD_TS, 123L);
115         Object id = v.id();
116         obj.setValue("vnf-id", "key1");
117         obj.setValue(property, "hello");
118         serializer.serializeToDb(obj, v, uriQuery, "", "");
119         g.tx().commit();
120         v = graph.traversal().V(id).next();
121         Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
122         if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
123             dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
124         }
125
126         assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
127         assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
128         assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
129
130     }
131
132     @Test
133     public void checkOnRead()
134             throws AAIException, UnsupportedEncodingException, SecurityException, IllegalArgumentException {
135         final String property = "persona-model-customization-id";
136
137         TransactionalGraphEngine spy = spy(dbEngine);
138         TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
139         Vertex v =
140                 graph.traversal().addV().property("vnf-id", "key1").property("model-customization-id", "hello").next();
141         graph.tx().commit();
142         Graph g = graph.newTransaction();
143         GraphTraversalSource traversal = g.traversal();
144         when(spy.asAdmin()).thenReturn(adminSpy);
145         when(adminSpy.getTraversalSource()).thenReturn(traversal);
146         DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
147         Introspector obj = loader.introspectorFromName("generic-vnf");
148         serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
149
150         assertEquals("dbAlias property exists", "hello", obj.getValue(property));
151
152     }
153
154 }