2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.serialization.db;
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;
42 import java.io.UnsupportedEncodingException;
43 import java.lang.reflect.InvocationTargetException;
44 import java.net.MalformedURLException;
46 import java.net.URISyntaxException;
47 import java.util.Arrays;
48 import java.util.Collection;
49 import java.util.Collections;
52 import static org.junit.Assert.assertEquals;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.when;
56 @RunWith(value = Parameterized.class)
57 public class DbAliasTest extends AAISetup {
59 private JanusGraph graph;
61 private final Version version = Version.v9;
62 private final ModelType introspectorFactoryType = ModelType.MOXY;
63 private final DBConnectionType type = DBConnectionType.REALTIME;
64 private Loader loader;
65 private TransactionalGraphEngine dbEngine;
67 @Parameterized.Parameter(value = 0)
68 public QueryStyle queryStyle;
70 @Parameterized.Parameters(name = "QueryStyle.{0}")
71 public static Collection<Object[]> data() {
72 return Arrays.asList(new Object[][]{
73 {QueryStyle.TRAVERSAL}
78 public void setup() throws Exception {
79 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
80 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
81 dbEngine = new JanusGraphDBEngine(
88 public void tearDown() {
89 graph.tx().rollback();
94 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
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 = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
105 Introspector obj = loader.introspectorFromName("generic-vnf");
106 Vertex v = g.addVertex();
108 obj.setValue("vnf-id", "key1");
109 obj.setValue(property, "hello");
110 serializer.serializeToDb(obj, v, uriQuery, "", "");
112 v = graph.traversal().V(id).next();
113 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
114 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
115 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
118 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
119 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
120 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
125 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
126 final String property = "persona-model-customization-id";
128 TransactionalGraphEngine spy = spy(dbEngine);
129 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
130 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
132 Graph g = graph.newTransaction();
133 GraphTraversalSource traversal = g.traversal();
134 when(spy.asAdmin()).thenReturn(adminSpy);
135 when(adminSpy.getTraversalSource()).thenReturn(traversal);
136 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
137 Introspector obj = loader.introspectorFromName("generic-vnf");
138 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
140 assertEquals("dbAlias property exists", "hello", obj.getValue(property));