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=========================================================
21 package org.onap.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.Arrays;
33 import java.util.Collection;
34 import java.util.Collections;
37 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
38 import org.apache.tinkerpop.gremlin.structure.Graph;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.janusgraph.core.JanusGraph;
41 import org.janusgraph.core.JanusGraphFactory;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.junit.runners.Parameterized;
47 import org.onap.aai.AAISetup;
48 import org.onap.aai.DataLinkSetup;
49 import org.onap.aai.dbmap.DBConnectionType;
50 import org.onap.aai.exceptions.AAIException;
51 import org.onap.aai.introspection.*;
52 import org.onap.aai.parsers.query.QueryParser;
53 import org.onap.aai.schema.enums.PropertyMetadata;
54 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
55 import org.onap.aai.serialization.engines.QueryStyle;
56 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
57 import org.onap.aai.setup.SchemaVersion;
58 import org.springframework.test.annotation.DirtiesContext;
60 @RunWith(value = Parameterized.class)
61 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
62 public class DbAliasTest extends DataLinkSetup {
64 private JanusGraph graph;
66 private SchemaVersion version;
67 private final ModelType introspectorFactoryType = ModelType.MOXY;
68 private final DBConnectionType type = DBConnectionType.REALTIME;
69 private Loader loader;
70 private TransactionalGraphEngine dbEngine;
72 @Parameterized.Parameter(value = 0)
73 public QueryStyle queryStyle;
75 @Parameterized.Parameters(name = "QueryStyle.{0}")
76 public static Collection<Object[]> data() {
77 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
81 public void setup() throws Exception {
82 version = schemaVersions.getDepthVersion();
83 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
84 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
85 dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
89 public void tearDown() {
90 graph.tx().rollback();
95 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException,
96 IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException,
97 NoSuchMethodException, InterruptedException {
98 final String property = "persona-model-customization-id";
99 String dbPropertyName = property;
100 TransactionalGraphEngine spy = spy(this.dbEngine);
101 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
102 Graph g = graph.newTransaction();
103 GraphTraversalSource traversal = g.traversal();
104 when(spy.asAdmin()).thenReturn(adminSpy);
105 when(adminSpy.getTraversalSource()).thenReturn(traversal);
106 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
107 QueryParser uriQuery =
108 spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
109 Introspector obj = loader.introspectorFromName("generic-vnf");
110 Vertex v = g.addVertex();
112 obj.setValue("vnf-id", "key1");
113 obj.setValue(property, "hello");
114 serializer.serializeToDb(obj, v, uriQuery, "", "");
116 v = graph.traversal().V(id).next();
117 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
118 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
119 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
122 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
123 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
124 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
129 public void checkOnRead() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException,
130 IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException,
131 NoSuchMethodException, InterruptedException, MalformedURLException {
132 final String property = "persona-model-customization-id";
134 TransactionalGraphEngine spy = spy(dbEngine);
135 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
136 Vertex v = graph.traversal().addV("vnf-id", "key1", "model-customization-id", "hello").next();
138 Graph g = graph.newTransaction();
139 GraphTraversalSource traversal = g.traversal();
140 when(spy.asAdmin()).thenReturn(adminSpy);
141 when(adminSpy.getTraversalSource()).thenReturn(traversal);
142 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
143 Introspector obj = loader.introspectorFromName("generic-vnf");
144 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
146 assertEquals("dbAlias property exists", "hello", obj.getValue(property));