[AAI] Fix doc config files
[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 static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25
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;
32 import java.util.Map;
33 import java.util.Optional;
34
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;
64
65 @RunWith(value = Parameterized.class)
66 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
67 public class DbMethHelperTest extends AAISetup {
68     private DbMethHelper dbMethHelper = new DbMethHelper();
69
70     @Rule
71     public ExpectedException thrown = ExpectedException.none();
72     protected static Graph graph;
73
74     @Autowired
75     protected EdgeSerializer edgeSer;
76
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;
85
86     @Parameterized.Parameter
87     public QueryStyle queryStyle;
88
89     @Parameterized.Parameters(name = "QueryStyle.{0}")
90     public static Collection<Object[]> data() {
91         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}});
92     }
93
94     @BeforeClass
95     public static void init() {
96         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
97     }
98
99     @Before
100     public void setUp() throws Exception {
101         version = schemaVersions.getDefaultVersion();
102         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
103         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
104         spy = spy(dbEngine);
105         adminSpy = spy(dbEngine.asAdmin());
106
107         engine = new JanusGraphDBEngine(queryStyle, loader);
108         dbser = new DBSerializer(version, dbEngine, introspectorFactoryType, "AAI-TEST");
109         dbMethHelper = new DbMethHelper(loader, spy);
110
111         Vertex pserver1 = graph.addVertex("aai-node-type", "pserver", "hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
112         Vertex pserver2 = graph.addVertex("aai-node-type", "pserver", "hostname", "testSearchVertexByIdentityMap-pserver-hostname-02");
113         Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "key1", "vnf-name", "vnfName1");
114         Vertex complex1 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id1");
115         GraphTraversalSource g = graph.traversal();
116         when(spy.asAdmin()).thenReturn(adminSpy);
117         when(adminSpy.getTraversalSource()).thenReturn(g);
118         when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
119     }
120
121     @Test
122     public void testSearchVertexByIdentityMap() throws Exception{
123         String type = "pserver";
124         Map<String, Object> map = new HashMap<>();
125         map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
126
127         Optional<Vertex> optionalVertex;
128         try {
129             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
130         } catch (Exception e) {
131             throw new Exception(e);
132         }
133         Assert.assertEquals("vp[hostname->testSearchVertexById]", optionalVertex.get().property("hostname").toString());
134     }
135
136     @Test(expected = AmbiguousMapAAIException.class)
137     public void testSearchVertexByIdentityMap_throwAmbiguousMapAAIException() throws AmbiguousMapAAIException, Exception {
138         String type = "pserver";
139         Map<String, Object> map = new HashMap<>();
140         map.put("pserver.hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
141         map.put("complex.physical-location-id", "id1");
142
143         Optional<Vertex> optionalVertex;
144         try {
145             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
146         } catch (AmbiguousMapAAIException e) {
147             throw new AmbiguousMapAAIException(e);
148         }
149     }
150
151     @Test
152     public void testLocateUniqueVertex() throws Exception {
153         String type = "complex";
154         Map<String, Object> map = new HashMap<>();
155         Vertex complex2 = graph.addVertex("aai-node-type", "complex", "physical-location-id", "id2");
156         map.put("physical-location-id", "id2");
157         Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
158         Assert.assertEquals("vp[physical-location-id->id2]", optionalVertex.get().property("physical-location-id").toString());
159     }
160
161     @Test(expected = AAIException.class)
162     public void testLocateUniqueVertex_throwsException() throws AAIException, Exception {
163         String type = "Pserver";
164         Map<String, Object> map = new HashMap<>();
165         Introspector obj = loader.unmarshal("relationship", this.getJsonString("related-link-and-relationship-data.json"));
166         map.put("hostname", "testSearchVertexByIdentityMap-pserver-hostname-01");
167         dbMethHelper.locateUniqueVertex(type, map);
168     }
169
170     @Test
171     public void testLocateUniqueVertex_returnNull() throws Exception {
172         String type = "complex";
173         Map<String, Object> map = new HashMap<>();
174         map.put("physical-location-id", "bogusId");
175         Optional<Vertex> optionalVertex = dbMethHelper.locateUniqueVertex(type, map);
176         Assert.assertEquals(optionalVertex, Optional.empty());
177     }
178
179     @Test
180     public void testGetVertexProperties() throws Exception {
181         Vertex pserver3 = graph.addVertex("aai-node-type", "pserver", "hostname", "testGetVertexProperties-pserver-hostname-01",
182             "ptnii-equip-name", "testGetVertexProperties-pserver-ptnii-equip-name-01");
183         String type = "pserver";
184         Map<String, Object> map = new HashMap<>();
185         map.put("pserver.hostname", "testGetVertexProperties-pserver-hostname-01");
186
187         Optional<Vertex> optionalVertex;
188         try {
189             optionalVertex = dbMethHelper.searchVertexByIdentityMap(type, map);
190         } catch (Exception e) {
191             throw new Exception(e);
192         }
193
194         Vertex v = optionalVertex.get();
195         List<String> vertexProperties = dbMethHelper.getVertexProperties(v);
196         Assert.assertTrue(vertexProperties.contains("Prop: [aai-node-type], val = [pserver] "));
197         Assert.assertTrue(vertexProperties.contains("Prop: [hostname], val = [testGetVertexProperties-pserver-hostname-01] "));
198         Assert.assertTrue(vertexProperties.contains("Prop: [ptnii-equip-name], val = [testGetVertexProperties-pserver-ptnii-equip-name-01] "));
199     }
200
201     @Test
202     public void testGetVertexProperties_nullParameter() {
203         List<String> vertexProperties = dbMethHelper.getVertexProperties(null);
204         Assert.assertTrue(vertexProperties.contains("null Node object passed to showPropertiesForNode()\n"));
205         Assert.assertTrue(vertexProperties.size() == 1);
206     }
207
208     private String getJsonString(String filename) throws IOException {
209         FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
210         String s = IOUtils.toString(is, "UTF-8");
211         IOUtils.closeQuietly(is);
212         return s;
213     }
214
215     @After
216     public void tearDown() throws Exception {
217         engine.rollback();
218         dbEngine.rollback();
219     }
220
221     @AfterClass
222     public static void destroy() throws Exception {
223         graph.close();
224     }
225 }