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 com.thinkaurelius.titan.core.TitanFactory;
23 import com.thinkaurelius.titan.core.TitanGraph;
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.onap.aai.AAISetup;
31 import org.onap.aai.dbmap.DBConnectionType;
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.introspection.*;
34 import org.onap.aai.parsers.query.QueryParser;
35 import org.onap.aai.schema.enums.PropertyMetadata;
36 import org.onap.aai.serialization.engines.QueryStyle;
37 import org.onap.aai.serialization.engines.TitanDBEngine;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
40 import java.io.UnsupportedEncodingException;
41 import java.lang.reflect.InvocationTargetException;
42 import java.net.MalformedURLException;
44 import java.net.URISyntaxException;
45 import java.util.Collections;
48 import static org.junit.Assert.assertEquals;
49 import static org.mockito.Mockito.spy;
50 import static org.mockito.Mockito.when;
52 public class DbAliasTest extends AAISetup {
54 private TitanGraph graph;
56 private final Version version = Version.v9;
57 private final ModelType introspectorFactoryType = ModelType.MOXY;
58 private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
59 private final DBConnectionType type = DBConnectionType.REALTIME;
60 private Loader loader;
61 private TransactionalGraphEngine dbEngine;
64 public void setup() throws Exception {
65 graph = TitanFactory.build().set("storage.backend","inmemory").open();
66 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
67 dbEngine = new TitanDBEngine(
74 public void tearDown() {
75 graph.tx().rollback();
80 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
81 final String property = "persona-model-customization-id";
82 String dbPropertyName = property;
83 TransactionalGraphEngine spy = spy(this.dbEngine);
84 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
85 Graph g = graph.newTransaction();
86 GraphTraversalSource traversal = g.traversal();
87 when(spy.asAdmin()).thenReturn(adminSpy);
88 when(adminSpy.getTraversalSource()).thenReturn(traversal);
89 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
90 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
91 Introspector obj = loader.introspectorFromName("generic-vnf");
92 Vertex v = g.addVertex();
94 obj.setValue("vnf-id", "key1");
95 obj.setValue(property, "hello");
96 serializer.serializeToDb(obj, v, uriQuery, "", "");
98 v = graph.traversal().V(id).next();
99 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
100 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
101 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
104 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
105 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
106 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
111 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
112 final String property = "persona-model-customization-id";
114 TransactionalGraphEngine spy = spy(dbEngine);
115 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
116 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
118 Graph g = graph.newTransaction();
119 GraphTraversalSource traversal = g.traversal();
120 when(spy.asAdmin()).thenReturn(adminSpy);
121 when(adminSpy.getTraversalSource()).thenReturn(traversal);
122 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
123 Introspector obj = loader.introspectorFromName("generic-vnf");
124 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
126 assertEquals("dbAlias property exists", "hello", obj.getValue(property));