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 com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.Multimap;
25 import org.apache.commons.io.IOUtils;
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.apache.tinkerpop.gremlin.structure.VertexProperty;
30 import org.janusgraph.core.JanusGraph;
31 import org.janusgraph.core.JanusGraphFactory;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.DataLinkSetup;
40 import org.onap.aai.domain.model.AAIResource;
41 import org.onap.aai.edges.EdgeIngestor;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.Introspector;
44 import org.onap.aai.introspection.Loader;
45 import org.onap.aai.introspection.ModelType;
46 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
47 import org.onap.aai.parsers.query.QueryParser;
48 import org.onap.aai.serialization.db.DBSerializer;
49 import org.onap.aai.serialization.db.EdgeSerializer;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.setup.SchemaVersion;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.test.annotation.DirtiesContext;
57 import java.io.FileInputStream;
58 import java.io.IOException;
61 import static org.mockito.Mockito.spy;
62 import static org.mockito.Mockito.when;
64 @RunWith(value = Parameterized.class)
65 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
66 public class DbMethHelperTest extends AAISetup {
67 private DbMethHelper dbMethHelper = new DbMethHelper();
70 public ExpectedException thrown = ExpectedException.none();
71 protected static Graph graph;
74 protected EdgeSerializer edgeSer;
76 private SchemaVersion version;
77 private final ModelType introspectorFactoryType = ModelType.MOXY;
78 private Loader loader;
79 private TransactionalGraphEngine dbEngine;
80 private TransactionalGraphEngine engine; // for tests that aren't mocking the engine
81 private DBSerializer dbser;
82 private TransactionalGraphEngine spy;
83 private TransactionalGraphEngine.Admin adminSpy;
85 @Parameterized.Parameter
86 public QueryStyle queryStyle;
88 @Parameterized.Parameters(name = "QueryStyle.{0}")
89 public static Collection<Object[]> data() {
90 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}});
94 public static void init() {
95 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
99 public void setUp() throws Exception {
100 version = schemaVersions.getDefaultVersion();
101 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
102 dbEngine = new JanusGraphDBEngine(queryStyle, loader);
104 adminSpy = spy(dbEngine.asAdmin());
106 engine = new JanusGraphDBEngine(queryStyle, loader);
107 dbser = new DBSerializer(version, dbEngine, introspectorFactoryType, "AAI-TEST");
108 dbMethHelper = new DbMethHelper(loader, spy);
110 Vertex pserver1 = graph.addVertex("aai-node-type", "pserver", "hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
111 Vertex pserver2 = graph.addVertex("aai-node-type", "pserver", "hostname", "testSearchVertexByIdentityMap-pserver-hostname-02");
112 Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "key1", "vnf-name", "vnfName1");
113 Vertex complex1 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id1");
114 GraphTraversalSource g = graph.traversal();
115 when(spy.asAdmin()).thenReturn(adminSpy);
116 when(adminSpy.getTraversalSource()).thenReturn(g);
117 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
121 public void testSearchVertexByIdentityMap() throws Exception{
122 String type = "pserver";
123 Map<String, Object> map = new HashMap<>();
124 map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
126 Optional<Vertex> optionalVertex;
128 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
129 } catch (Exception e) {
130 throw new Exception(e);
132 Assert.assertEquals("vp[hostname->testSearchVertexById]", optionalVertex.get().property("hostname").toString());
135 @Test(expected = AmbiguousMapAAIException.class)
136 public void testSearchVertexByIdentityMap_throwAmbiguousMapAAIException() throws AmbiguousMapAAIException, Exception {
137 String type = "pserver";
138 Map<String, Object> map = new HashMap<>();
139 map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
140 map.put("complex.physical-location-id", "id1");
142 Optional<Vertex> optionalVertex;
144 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
145 } catch (AmbiguousMapAAIException e) {
146 throw new AmbiguousMapAAIException(e);
151 public void testLocateUniqueVertex() throws Exception {
152 String type = "complex";
153 Map<String, Object> map = new HashMap<>();
154 Vertex complex2 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id2");
155 map.put("physical-location-id", "id2");
156 Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
157 Assert.assertEquals("vp[physical-location-id->id2]", optionalVertex.get().property("physical-location-id").toString());
160 @Test(expected = AAIException.class)
161 public void testLocateUniqueVertex_throwsException() throws AAIException, Exception {
162 String type = "Pserver";
163 Map<String, Object> map = new HashMap<>();
164 Introspector obj = loader.unmarshal("relationship", this.getJsonString("related-link-and-relationship-data.json"));
165 map.put("hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
166 dbMethHelper.locateUniqueVertex(type, map);
170 public void testLocateUniqueVertex_returnNull() throws Exception {
171 String type = "complex";
172 Map<String, Object> map = new HashMap<>();
173 map.put("physical-location-id", "bogusId");
174 Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
175 Assert.assertEquals(optionalVertex, Optional.empty());
179 public void testGetVertexProperties() throws Exception {
180 Vertex pserver3 = graph.addVertex("aai-node-type", "pserver", "hostname", "testGetVertexProperties-pserver-hostname-01",
181 "ptnii-equip-name", "testGetVertexProperties-pserver-ptnii-equip-name-01");
182 String type = "pserver";
183 Map<String, Object> map = new HashMap<>();
184 map.put("pserver.hostname", "testGetVertexProperties-pserver-hostname-01");
186 Optional<Vertex> optionalVertex;
188 optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
189 } catch (Exception e) {
190 throw new Exception(e);
193 Vertex v = optionalVertex.get();
194 List<String> vertexProperties = dbMethHelper.getVertexProperties(v);
195 Assert.assertTrue(vertexProperties.contains("Prop: [aai-node-type], val = [pserver] "));
196 Assert.assertTrue(vertexProperties.contains("Prop: [hostname], val = [testGetVertexProperties-pserver-hostname-01] "));
197 Assert.assertTrue(vertexProperties.contains("Prop: [ptnii-equip-name], val = [testGetVertexProperties-pserver-ptnii-equip-name-01] "));
201 public void testGetVertexProperties_nullParameter() {
202 List<String> vertexProperties = dbMethHelper.getVertexProperties(null);
203 Assert.assertTrue(vertexProperties.contains("null Node object passed to showPropertiesForNode()\n"));
204 Assert.assertTrue(vertexProperties.size() == 1);
207 private String getJsonString(String filename) throws IOException {
208 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
209 String s = IOUtils.toString(is, "UTF-8");
210 IOUtils.closeQuietly(is);
215 public void tearDown() throws Exception {
221 public static void destroy() throws Exception {