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;
29 import java.net.URISyntaxException;
30 import java.util.Arrays;
31 import java.util.Collection;
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.janusgraph.core.JanusGraph;
39 import org.janusgraph.core.JanusGraphFactory;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.onap.aai.DataLinkSetup;
46 import org.onap.aai.db.props.AAIProperties;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.Introspector;
49 import org.onap.aai.introspection.Loader;
50 import org.onap.aai.introspection.ModelType;
51 import org.onap.aai.parsers.query.QueryParser;
52 import org.onap.aai.schema.enums.PropertyMetadata;
53 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
54 import org.onap.aai.serialization.engines.QueryStyle;
55 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 import org.onap.aai.setup.SchemaVersion;
57 import org.springframework.test.annotation.DirtiesContext;
59 @RunWith(value = Parameterized.class)
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
61 public class DbAliasTest extends DataLinkSetup {
63 private JanusGraph graph;
65 private SchemaVersion version;
66 private final ModelType introspectorFactoryType = ModelType.MOXY;
67 private Loader loader;
68 private TransactionalGraphEngine dbEngine;
70 @Parameterized.Parameter
71 public QueryStyle queryStyle;
73 @Parameterized.Parameters(name = "QueryStyle.{0}")
74 public static Collection<Object[]> data() {
75 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
80 version = schemaVersions.getDepthVersion();
81 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
82 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
83 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
87 public void tearDown() {
88 graph.tx().rollback();
93 public void checkOnWrite() throws AAIException, UnsupportedEncodingException, URISyntaxException, SecurityException,
94 IllegalArgumentException {
95 final String property = "persona-model-customization-id";
96 String dbPropertyName = property;
97 TransactionalGraphEngine spy = spy(this.dbEngine);
98 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
99 Graph g = graph.newTransaction();
100 GraphTraversalSource traversal = g.traversal();
101 when(spy.asAdmin()).thenReturn(adminSpy);
102 when(adminSpy.getTraversalSource()).thenReturn(traversal);
103 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
104 QueryParser uriQuery =
105 spy.getQueryBuilder().createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1"));
106 Introspector obj = loader.introspectorFromName("generic-vnf");
107 Vertex v = g.addVertex();
108 v.property("aai-uri", "abc");
109 v.property("aai-uuid", "b");
110 v.property(AAIProperties.CREATED_TS, 123L);
111 v.property(AAIProperties.SOURCE_OF_TRUTH, "sot");
112 v.property(AAIProperties.RESOURCE_VERSION, "123");
113 v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot");
114 v.property(AAIProperties.LAST_MOD_TS, 123L);
116 obj.setValue("vnf-id", "key1");
117 obj.setValue(property, "hello");
118 serializer.serializeToDb(obj, v, uriQuery, "", "");
120 v = graph.traversal().V(id).next();
121 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(property);
122 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
123 dbPropertyName = map.get(PropertyMetadata.DB_ALIAS);
126 assertEquals("dbAlias is ", "model-customization-id", dbPropertyName);
127 assertEquals("dbAlias property exists", "hello", v.property(dbPropertyName).orElse(""));
128 assertEquals("model property does not", "missing", v.property(property).orElse("missing"));
133 public void checkOnRead()
134 throws AAIException, UnsupportedEncodingException, SecurityException, IllegalArgumentException {
135 final String property = "persona-model-customization-id";
137 TransactionalGraphEngine spy = spy(dbEngine);
138 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
140 graph.traversal().addV().property("vnf-id", "key1").property("model-customization-id", "hello").next();
142 Graph g = graph.newTransaction();
143 GraphTraversalSource traversal = g.traversal();
144 when(spy.asAdmin()).thenReturn(adminSpy);
145 when(adminSpy.getTraversalSource()).thenReturn(traversal);
146 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
147 Introspector obj = loader.introspectorFromName("generic-vnf");
148 serializer.dbToObject(Collections.singletonList(v), obj, 0, true, "false");
150 assertEquals("dbAlias property exists", "hello", obj.getValue(property));