07fa3890a0fd18a582914145ef0ecc7390f95a9e
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / db / DbMethHelperTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.db;
22
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;
32 import org.junit.*;
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;
56
57 import java.io.FileInputStream;
58 import java.io.IOException;
59 import java.util.*;
60
61 import static org.mockito.Mockito.spy;
62 import static org.mockito.Mockito.when;
63
64 @RunWith(value = Parameterized.class)
65 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
66 public class DbMethHelperTest extends AAISetup {
67     private DbMethHelper dbMethHelper = new DbMethHelper();
68
69     @Rule
70     public ExpectedException thrown = ExpectedException.none();
71     protected static Graph graph;
72
73     @Autowired
74     protected EdgeSerializer edgeSer;
75
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;
84
85     @Parameterized.Parameter
86     public QueryStyle queryStyle;
87
88     @Parameterized.Parameters(name = "QueryStyle.{0}")
89     public static Collection<Object[]> data() {
90         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}});
91     }
92
93     @BeforeClass
94     public static void init() {
95         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
96     }
97
98     @Before
99     public void setUp() throws Exception {
100         version = schemaVersions.getDefaultVersion();
101         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
102         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
103         spy = spy(dbEngine);
104         adminSpy = spy(dbEngine.asAdmin());
105
106         engine = new JanusGraphDBEngine(queryStyle, loader);
107         dbser = new DBSerializer(version, dbEngine, introspectorFactoryType, "AAI-TEST");
108         dbMethHelper = new DbMethHelper(loader, spy);
109
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);
118     }
119
120     @Test
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");
125
126         Optional<Vertex> optionalVertex;
127         try {
128             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
129         } catch (Exception e) {
130             throw new Exception(e);
131         }
132         Assert.assertEquals("vp[hostname->testSearchVertexById]", optionalVertex.get().property("hostname").toString());
133     }
134
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");
141
142         Optional<Vertex> optionalVertex;
143         try {
144             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
145         } catch (AmbiguousMapAAIException e) {
146             throw new AmbiguousMapAAIException(e);
147         }
148     }
149
150     @Test
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());
158     }
159
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);
167     }
168
169     @Test
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());
176     }
177
178     @Test
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");
185
186         Optional<Vertex> optionalVertex;
187         try {
188             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
189         } catch (Exception e) {
190             throw new Exception(e);
191         }
192
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] "));
198     }
199
200     @Test
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);
205     }
206
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);
211         return s;
212     }
213
214     @After
215     public void tearDown() throws Exception {
216         engine.rollback();
217         dbEngine.rollback();
218     }
219
220     @AfterClass
221     public static void destroy() throws Exception {
222         graph.close();
223     }
224 }