Merge "Add .gitignore for swagger"
[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.DataLinkSetup;
34 import org.onap.aai.dbmap.DBConnectionType;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.*;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.schema.enums.PropertyMetadata;
39 import org.onap.aai.serialization.engines.QueryStyle;
40 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.setup.SchemaVersion;
43 import org.springframework.test.annotation.DirtiesContext;
44
45 import java.io.UnsupportedEncodingException;
46 import java.lang.reflect.InvocationTargetException;
47 import java.net.MalformedURLException;
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 final DBConnectionType type = DBConnectionType.REALTIME;
68         private Loader loader;
69         private TransactionalGraphEngine dbEngine;
70
71         @Parameterized.Parameter(value = 0)
72         public QueryStyle queryStyle;
73
74         @Parameterized.Parameters(name = "QueryStyle.{0}")
75         public static Collection<Object[]> data() {
76                 return Arrays.asList(new Object[][]{
77                                 {QueryStyle.TRAVERSAL},
78                                 {QueryStyle.TRAVERSAL_URI}
79                 });
80         }
81
82         @Before
83         public void setup() throws Exception {
84             version = schemaVersions.getDepthVersion();
85                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
86                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
87                 dbEngine = new JanusGraphDBEngine(
88                                 queryStyle,
89                                 type,
90                                 loader);
91         }
92
93         @After
94         public void tearDown() {
95                 graph.tx().rollback();
96                 graph.close();
97         }
98
99         @Test
100         public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
101                 final String property = "persona-model-customization-id";
102                 String dbPropertyName = property;
103                 TransactionalGraphEngine spy = spy(this.dbEngine);
104                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
105                 Graph g = graph.newTransaction();
106                 GraphTraversalSource traversal = g.traversal();
107                 when(spy.asAdmin()).thenReturn(adminSpy);
108                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
109                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
110                 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
111                 Introspector obj = loader.introspectorFromName("generic-vnf");
112                 Vertex v = g.addVertex();
113                 Object id = v.id();
114                 obj.setValue("vnf-id", "key1");
115                 obj.setValue(property, "hello");
116                 serializer.serializeToDb(obj, v, uriQuery, "", "");
117                 g.tx().commit();
118                 v = graph.traversal().V(id).next();
119                 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
120                 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
121                         dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
122                 }
123
124                 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
125                 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
126                 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
127
128         }
129
130         @Test
131         public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, 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
151 }