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;
32 import java.util.Collections;
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.junit.After;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.openecomp.aai.dbmap.DBConnectionType;
43 import org.openecomp.aai.exceptions.AAIException;
44 import org.openecomp.aai.introspection.Introspector;
45 import org.openecomp.aai.introspection.Loader;
46 import org.openecomp.aai.introspection.LoaderFactory;
47 import org.openecomp.aai.introspection.ModelType;
48 import org.openecomp.aai.introspection.Version;
49 import org.openecomp.aai.parsers.query.QueryParser;
50 import org.openecomp.aai.schema.enums.PropertyMetadata;
51 import org.openecomp.aai.serialization.engines.QueryStyle;
52 import org.openecomp.aai.serialization.engines.TitanDBEngine;
53 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
55 import com.thinkaurelius.titan.core.TitanFactory;
56 import com.thinkaurelius.titan.core.TitanGraph;
59 public class DbAliasTest {
63 private TitanGraph graph;
64 private final Version version = Version.v9;
65 private final ModelType introspectorFactoryType = ModelType.MOXY;
66 private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
67 private final DBConnectionType type = DBConnectionType.REALTIME;
68 private Loader loader;
69 private TransactionalGraphEngine dbEngine;
71 public void setup() throws NoSuchFieldException, SecurityException, Exception {
72 graph = TitanFactory.build().set("storage.backend","inmemory").open();
73 System.setProperty("AJSC_HOME", ".");
74 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
75 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
76 dbEngine = new TitanDBEngine(
83 public void tearDown() {
84 graph.tx().rollback();
89 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
90 final String property = "persona-model-customization-id";
91 String dbPropertyName = property;
92 TransactionalGraphEngine spy = spy(this.dbEngine);
93 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
94 Graph g = graph.newTransaction();
95 GraphTraversalSource traversal = g.traversal();
96 when(spy.asAdmin()).thenReturn(adminSpy);
97 when(adminSpy.getTraversalSource()).thenReturn(traversal);
98 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
99 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
100 Introspector obj = loader.introspectorFromName("generic-vnf");
101 Vertex v = g.addVertex();
103 obj.setValue("vnf-id", "key1");
104 obj.setValue(property, "hello");
105 serializer.serializeToDb(obj, v, uriQuery, "", "");
107 v = graph.traversal().V(id).next();
108 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
109 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
110 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
113 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
114 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
115 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
120 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
121 final String property = "persona-model-customization-id";
123 TransactionalGraphEngine spy = spy(dbEngine);
124 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
125 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
127 Graph g = graph.newTransaction();
128 GraphTraversalSource traversal = g.traversal();
129 when(spy.asAdmin()).thenReturn(adminSpy);
130 when(adminSpy.getTraversalSource()).thenReturn(traversal);
131 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
132 Introspector obj = loader.introspectorFromName("generic-vnf");
133 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
135 assertEquals("dbAlias property exists", "hello", obj.getValue(property));