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.db;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Optional;
35 import org.apache.commons.io.IOUtils;
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
37 import org.apache.tinkerpop.gremlin.structure.Graph;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.janusgraph.core.JanusGraphFactory;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.junit.runner.RunWith;
49 import org.junit.runners.Parameterized;
50 import org.onap.aai.AAISetup;
51 import org.onap.aai.exceptions.AAIException;
52 import org.onap.aai.introspection.Introspector;
53 import org.onap.aai.introspection.Loader;
54 import org.onap.aai.introspection.ModelType;
55 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
56 import org.onap.aai.serialization.db.DBSerializer;
57 import org.onap.aai.serialization.db.EdgeSerializer;
58 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
59 import org.onap.aai.serialization.engines.QueryStyle;
60 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
61 import org.onap.aai.setup.SchemaVersion;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.test.annotation.DirtiesContext;
65 @RunWith(value = Parameterized.class)
66 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
67 public class DbMethHelperTest extends AAISetup {
68 private DbMethHelper dbMethHelper = new DbMethHelper();
71 public ExpectedException thrown = ExpectedException.none();
72 protected static Graph graph;
75 protected EdgeSerializer edgeSer;
77 private SchemaVersion version;
78 private final ModelType introspectorFactoryType = ModelType.MOXY;
79 private Loader loader;
80 private TransactionalGraphEngine dbEngine;
81 private TransactionalGraphEngine engine; // for tests that aren't mocking the engine
82 private DBSerializer dbser;
83 private TransactionalGraphEngine spy;
84 private TransactionalGraphEngine.Admin adminSpy;
86 @Parameterized.Parameter
87 public QueryStyle queryStyle;
89 @Parameterized.Parameters(name = "QueryStyle.{0}")
90 public static Collection<Object[]> data() {
91 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}});
95 public static void init() {
96 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
100 public void setUp() throws Exception {
101 version = schemaVersions.getDefaultVersion();
102 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
103 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
105 adminSpy = spy(dbEngine.asAdmin());
107 engine = new JanusGraphDBEngine(queryStyle, loader);
108 dbser = new DBSerializer(version, dbEngine, introspectorFactoryType, "AAI-TEST");
109 dbMethHelper = new DbMethHelper(loader, spy);
111 Vertex pserver1 = graph.addVertex("aai-node-type", "pserver", "hostname",
112 "testSearchVertexByIdentityMap-pserver-hostname-01");
113 Vertex pserver2 = graph.addVertex("aai-node-type", "pserver", "hostname",
114 "testSearchVertexByIdentityMap-pserver-hostname-02");
115 Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "key1", "vnf-name", "vnfName1");
116 Vertex complex1 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id1");
117 GraphTraversalSource g = graph.traversal();
118 when(spy.asAdmin()).thenReturn(adminSpy);
119 when(adminSpy.getTraversalSource()).thenReturn(g);
120 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
124 public void testSearchVertexByIdentityMap() throws Exception {
125 String type = "pserver";
126 Map<String, Object> map = new HashMap<>();
127 map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
129 Optional<Vertex> optionalVertex;
131 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
132 } catch (Exception e) {
133 throw new Exception(e);
135 Assert.assertEquals("vp[hostname->testSearchVertexById]", optionalVertex.get().property("hostname").toString());
138 @Test(expected = AmbiguousMapAAIException.class)
139 public void testSearchVertexByIdentityMap_throwAmbiguousMapAAIException()
140 throws AmbiguousMapAAIException, Exception {
141 String type = "pserver";
142 Map<String, Object> map = new HashMap<>();
143 map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
144 map.put("complex.physical-location-id", "id1");
146 Optional<Vertex> optionalVertex;
148 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
149 } catch (AmbiguousMapAAIException e) {
150 throw new AmbiguousMapAAIException(e);
155 public void testLocateUniqueVertex() throws Exception {
156 String type = "complex";
157 Map<String, Object> map = new HashMap<>();
158 Vertex complex2 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id2");
159 map.put("physical-location-id", "id2");
160 Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
161 Assert.assertEquals("vp[physical-location-id->id2]",
162 optionalVertex.get().property("physical-location-id").toString());
165 @Test(expected = AAIException.class)
166 public void testLocateUniqueVertex_throwsException() throws AAIException, Exception {
167 String type = "Pserver";
168 Map<String, Object> map = new HashMap<>();
170 loader.unmarshal("relationship", this.getJsonString("related-link-and-relationship-data.json"));
171 map.put("hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
172 dbMethHelper.locateUniqueVertex(type, map);
176 public void testLocateUniqueVertex_returnNull() throws Exception {
177 String type = "complex";
178 Map<String, Object> map = new HashMap<>();
179 map.put("physical-location-id", "bogusId");
180 Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
181 Assert.assertEquals(optionalVertex, Optional.empty());
185 public void testGetVertexProperties() throws Exception {
187 graph.addVertex("aai-node-type", "pserver", "hostname", "testGetVertexProperties-pserver-hostname-01",
188 "ptnii-equip-name", "testGetVertexProperties-pserver-ptnii-equip-name-01");
189 String type = "pserver";
190 Map<String, Object> map = new HashMap<>();
191 map.put("pserver.hostname", "testGetVertexProperties-pserver-hostname-01");
193 Optional<Vertex> optionalVertex;
195 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
196 } catch (Exception e) {
197 throw new Exception(e);
200 Vertex v = optionalVertex.get();
201 List<String> vertexProperties = dbMethHelper.getVertexProperties(v);
202 Assert.assertTrue(vertexProperties.contains("Prop: [aai-node-type], val = [pserver] "));
204 vertexProperties.contains("Prop: [hostname], val = [testGetVertexProperties-pserver-hostname-01] "));
205 Assert.assertTrue(vertexProperties
206 .contains("Prop: [ptnii-equip-name], val = [testGetVertexProperties-pserver-ptnii-equip-name-01] "));
210 public void testGetVertexProperties_nullParameter() {
211 List<String> vertexProperties = dbMethHelper.getVertexProperties(null);
212 Assert.assertTrue(vertexProperties.contains("null Node object passed to showPropertiesForNode()\n"));
213 Assert.assertTrue(vertexProperties.size() == 1);
216 private String getJsonString(String filename) throws IOException {
217 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
218 String s = IOUtils.toString(is, "UTF-8");
219 IOUtils.closeQuietly(is);
224 public void tearDown() throws Exception {
230 public static void destroy() throws Exception {