2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.aai.serialization.db;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import java.io.UnsupportedEncodingException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.net.MalformedURLException;
31 import java.net.URISyntaxException;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
40 import org.openecomp.aai.dbmap.DBConnectionType;
41 import org.openecomp.aai.exceptions.AAIException;
42 import org.openecomp.aai.introspection.Introspector;
43 import org.openecomp.aai.introspection.Loader;
44 import org.openecomp.aai.introspection.LoaderFactory;
45 import org.openecomp.aai.introspection.ModelType;
46 import org.openecomp.aai.introspection.Version;
47 import org.openecomp.aai.parsers.query.QueryParser;
48 import org.openecomp.aai.schema.enums.PropertyMetadata;
49 import org.openecomp.aai.serialization.engines.QueryStyle;
50 import org.openecomp.aai.serialization.engines.TitanDBEngine;
51 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
52 import com.thinkaurelius.titan.core.TitanFactory;
53 import com.thinkaurelius.titan.core.TitanGraph;
54 import com.thinkaurelius.titan.core.TitanTransaction;
56 import java.util.Collections;
58 public class DbAliasTest {
62 private TitanGraph graph;
63 private final Version version = Version.v9;
64 private final ModelType introspectorFactoryType = ModelType.MOXY;
65 private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
66 private final DBConnectionType type = DBConnectionType.REALTIME;
67 private Loader loader;
68 private TransactionalGraphEngine dbEngine;
70 public void setup() throws NoSuchFieldException, SecurityException, Exception {
71 graph = TitanFactory.build().set("storage.backend","inmemory").open();
72 System.setProperty("AJSC_HOME", "./src/test/resources/");
73 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
74 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
75 dbEngine = new TitanDBEngine(
82 public void tearDown() {
83 graph.tx().rollback();
88 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
89 final String property = "persona-model-customization-id";
90 String dbPropertyName = property;
91 TransactionalGraphEngine spy = spy(this.dbEngine);
92 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
93 TitanTransaction g = graph.newTransaction();
94 GraphTraversalSource traversal = g.traversal();
95 when(spy.asAdmin()).thenReturn(adminSpy);
96 when(adminSpy.getTraversalSource()).thenReturn(traversal);
97 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
98 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
99 Introspector obj = loader.introspectorFromName("generic-vnf");
100 Vertex v = g.addVertex();
102 obj.setValue("vnf-id", "key1");
103 obj.setValue(property, "hello");
104 serializer.serializeToDb(obj, v, uriQuery, "", "");
106 v = graph.traversal().V(id).next();
107 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
108 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
109 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
112 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
113 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
114 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
119 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
120 final String property = "persona-model-customization-id";
122 TransactionalGraphEngine spy = spy(dbEngine);
123 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
124 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
126 TitanTransaction g = graph.newTransaction();
127 GraphTraversalSource traversal = g.traversal();
128 when(spy.asAdmin()).thenReturn(adminSpy);
129 when(adminSpy.getTraversalSource()).thenReturn(traversal);
130 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
131 Introspector obj = loader.introspectorFromName("generic-vnf");
132 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
134 assertEquals("dbAlias property exists", "hello", obj.getValue(property));