2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.db;
24 import com.thinkaurelius.titan.core.TitanFactory;
25 import com.thinkaurelius.titan.core.TitanGraph;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
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.TitanDBEngine;
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.Collections;
50 import static org.junit.Assert.assertEquals;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.when;
54 public class DbAliasTest extends AAISetup {
56 private TitanGraph graph;
58 private final Version version = Version.v9;
59 private final ModelType introspectorFactoryType = ModelType.MOXY;
60 private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
61 private final DBConnectionType type = DBConnectionType.REALTIME;
62 private Loader loader;
63 private TransactionalGraphEngine dbEngine;
66 public void setup() throws Exception {
67 graph = TitanFactory.build().set("storage.backend","inmemory").open();
68 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
69 dbEngine = new TitanDBEngine(
76 public void tearDown() {
77 graph.tx().rollback();
82 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException {
83 final String property = "persona-model-customization-id";
84 String dbPropertyName = property;
85 TransactionalGraphEngine spy = spy(this.dbEngine);
86 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
87 Graph g = graph.newTransaction();
88 GraphTraversalSource traversal = g.traversal();
89 when(spy.asAdmin()).thenReturn(adminSpy);
90 when(adminSpy.getTraversalSource()).thenReturn(traversal);
91 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
92 QueryParser uriQuery = spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
93 Introspector obj = loader.introspectorFromName("generic-vnf");
94 Vertex v = g.addVertex();
96 obj.setValue("vnf-id", "key1");
97 obj.setValue(property, "hello");
98 serializer.serializeToDb(obj, v, uriQuery, "", "");
100 v = graph.traversal().V(id).next();
101 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
102 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
103 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
106 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
107 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
108 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
113 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, InterruptedException, MalformedURLException {
114 final String property = "persona-model-customization-id";
116 TransactionalGraphEngine spy = spy(dbEngine);
117 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
118 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
120 Graph g = graph.newTransaction();
121 GraphTraversalSource traversal = g.traversal();
122 when(spy.asAdmin()).thenReturn(adminSpy);
123 when(adminSpy.getTraversalSource()).thenReturn(traversal);
124 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
125 Introspector obj = loader.introspectorFromName("generic-vnf");
126 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
128 assertEquals("dbAlias property exists", "hello", obj.getValue(property));