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