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.datasnapshot;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Property;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
29 import org.janusgraph.core.JanusGraph;
30 import org.janusgraph.core.JanusGraphTransaction;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.onap.aai.AAISetup;
36 import org.onap.aai.datagrooming.DataGrooming;
37 import org.onap.aai.dbmap.AAIGraph;
38 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.logging.LogFormatTools;
41 import org.onap.aai.util.AAISystemExitUtil;
42 import org.springframework.boot.test.rule.OutputCapture;
44 import com.beust.jcommander.ParameterException;
46 import java.lang.NumberFormatException;
48 import java.io.IOException;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.HashMap;
54 import java.util.Iterator;
55 import java.util.List;
58 import java.util.stream.Collectors;
60 import static org.hamcrest.CoreMatchers.is;
61 import static org.hamcrest.Matchers.containsString;
62 import static org.junit.Assert.*;
65 public class DataSnapshotTest4HistInit extends AAISetup {
67 private GraphTraversalSource g;
69 private JanusGraphTransaction currentTransaction;
71 private List<Vertex> vertexes;
73 private DataSnapshot4HistInit dataSnapshot4HistInit;
76 public OutputCapture outputCapture = new OutputCapture();
79 public void setup() throws AAIException {
80 dataSnapshot4HistInit = new DataSnapshot4HistInit(loaderFactory, schemaVersions);
82 JanusGraph graph = AAIGraph.getInstance().getGraph();
83 currentTransaction = graph.newTransaction();
84 g = currentTransaction.traversal();
86 // Setup the graph so it has one pserver vertex
87 vertexes = setupPserverData(g);
88 currentTransaction.commit();
92 public void tearDown(){
94 JanusGraph graph = AAIGraph.getInstance().getGraph();
95 currentTransaction = graph.newTransaction();
96 g = currentTransaction.traversal();
98 vertexes.stream().forEach((v) -> g.V(v).next().remove());
99 currentTransaction.commit();
103 public void testClearEntireDatabaseAndVerifyDataIsRemoved() throws IOException {
105 // Copy the pserver.graphson file from src/test/resoures to ${AJSC_HOME}/logs/data/dataSnapshots/ folder
106 String sourceFileName = "src/test/resources/pserver.graphson";
107 String destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver.graphson";
108 copySnapshotFile(sourceFileName,destFileName);
111 // Run the dataSnapshot to clear the graph
112 String [] args = {"-c", "CLEAR_ENTIRE_DATABASE", "-f", "pserver.graphson"};
113 dataSnapshot4HistInit.executeCommand(args);
115 // Since the code doesn't clear the graph using AAIGraph.getInstance().getGraph(), its creating a second inmemory graph
116 // so we can't verify this with by counting the vertexes and edges in the graph
117 // In the future we could do that but for now we will depend on the following string "All done clearing DB"
119 // Capture the standard output and see if the following text is there
120 assertThat(outputCapture.toString(), containsString("All done clearing DB"));
125 public void testClearEntireDatabaseWithEmptyGraphSONFileAndItShouldNotClearDatabase() throws IOException {
127 // Create a empty file called empty.graphson in src/test/resources/
129 // Copy that file to ${AJSC_HOME}/logs/data/dataSnapshots/
130 String sourceFileName = "src/test/resources/empty.graphson";
131 String destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/empty.graphson";
132 copySnapshotFile(sourceFileName,destFileName);
134 // Run the clear dataSnapshot and this time it should fail
135 String [] args = {"-c","CLEAR_ENTIRE_DATABASE", "-f","empty.graphson"};
136 dataSnapshot4HistInit.executeCommand(args);
138 // Capture the standard output and see if the following text had no data is there
139 // Since the graphson is empty it should output that and not clear the graph
140 // Uncomment the following line after the test changes are done
141 assertThat(outputCapture.toString(), containsString("graphson had no data."));
146 public void testTakeSnapshotAndItShouldCreateASnapshotFileWithOneVertex() throws IOException, InterruptedException {
148 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
150 Set<Path> preSnapshotFiles = Files.walk(Paths.get(logsFolder)).collect(Collectors.toSet());
152 // Run the clear dataSnapshot and this time it should fail
153 //String [] args = {"JUST_TAKE_SNAPSHOT"}; >> default behavior is now to use 15 threads
154 // To just get one file, you have to tell it to just use one.
155 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount" ,"1"};
157 dataSnapshot4HistInit.executeCommand(args);
159 // Add sleep so the file actually gets created with the data
161 Set<Path> postSnapshotFiles = Files.walk(Paths.get(logsFolder)).collect(Collectors.toSet());
163 assertThat(postSnapshotFiles.size(), is(preSnapshotFiles.size()+1));
164 postSnapshotFiles.removeAll(preSnapshotFiles);
165 List<Path> snapshotPathList = postSnapshotFiles.stream().collect(Collectors.toList());
167 assertThat(snapshotPathList.size(), is(1));
169 List<String> fileContents = Files.readAllLines(snapshotPathList.get(0));
170 assertThat(fileContents.get(0), containsString("id"));
175 public void testTakeSnapshotMultiAndItShouldCreateMultipleSnapshotFiles() throws IOException {
177 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
179 // Run the clear dataSnapshot and this time it should fail
180 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount","2"};
182 dataSnapshot4HistInit.executeCommand(args);
184 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
185 // would need to add more data to the janusgraph
190 public void testTakeSnapshotMultiWithDebugAndItShouldCreateMultipleSnapshotFiles() throws IOException {
192 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
194 // Run the clear dataSnapshot and this time it should fail
195 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount","2", "-debugFlag","DEBUG"};
197 dataSnapshot4HistInit.executeCommand(args);
199 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
200 // would need to add more data to the janusgraph
205 public void testTakeSnapshotMultiWithDebugAndInvalidNumberAndItShouldFail() throws IOException {
207 boolean thrown = false;
208 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
210 // Run the clear dataSnapshot and this time it should fail
211 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount","foo","-debugFlag", "DEBUG"};
213 dataSnapshot4HistInit.executeCommand(args);
215 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
216 // would need to add more data to the janusgraph
220 public void testTakeSnapshotMultiWithDebugAndTimeDelayAndInvalidNumberAndItShouldFail() throws IOException {
222 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
224 // Run the clear dataSnapshot and this time it should fail
225 String [] args = {"-c","THREADED_SNAPSHOT","-threadCount", "foo", "-debugFlag","DEBUG","-debugAddDelayTime", "100"};
227 dataSnapshot4HistInit.executeCommand(args);
229 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
230 // would need to add more data to the janusgraph
234 public void testTakeSnapshotMultiWithDebugAndTimeDelayAndZeroThreadsAndItShouldFail() throws IOException {
236 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
238 // Run the clear dataSnapshot and this time it should fail
239 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount","0", "-debugFlag","DEBUG", "-debugAddDelayTime","100"};
241 dataSnapshot4HistInit.executeCommand(args);
243 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
244 // would need to add more data to the janusgraph
248 public void testTakeSnapshotMultiWithDebugAndTimeDelayIsInvalidNumberAndItShouldFail() throws IOException {
250 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
252 // Run the clear dataSnapshot and this time it should fail
253 String [] args = {"-c","THREADED_SNAPSHOT","-threadCount", "0","-debugFlag","DEBUG", "-debugAddDelayTime","foo"};
255 dataSnapshot4HistInit.executeCommand(args);
257 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
258 // would need to add more data to the janusgraph
262 public void testTakeSnapshotMultiWithMoreParametersThanAllowedAndItShouldFail() throws IOException {
264 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
266 // Run the clear dataSnapshot and this time it should fail
267 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount", "0", "-debugFlag","DEBUG", "-debugAddDelayTime","foo", "bar"};
269 dataSnapshot4HistInit.executeCommand(args);
271 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
272 // would need to add more data to the janusgraph
276 public void testTakeSnapshotMultiWithZeroThreadsAndItShouldFail(){
278 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
279 // would need to add more data to the janusgraph
280 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
282 // Run the clear dataSnapshot and this time it should fail
283 String [] args = {"-c","THREADED_SNAPSHOT", "-threadCount","0"};
285 dataSnapshot4HistInit.executeCommand(args);
289 public void testTakeSnapshotMultiWithInvalidNumberForThreadsAndItShouldFail(){
291 // For this test if there is only one vertex in the graph, not sure if it will create multiple files
292 // would need to add more data to the janusgraph
293 String logsFolder = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/";
295 // Run the clear dataSnapshot and this time it should fail
296 String [] args = {"-c","THREADED_SNAPSHOT","-threadCount", "foo"};
298 dataSnapshot4HistInit.executeCommand(args);
303 public void testReloadDataAndVerifyDataInGraphMatchesGraphson() throws IOException {
305 // Create a graphson file that contains a couple of vertexes in src/test/resources
306 // Copy that file to ${AJSC_HOME}/logs/data/dataSnasphots/
307 // Run the reload arguments and ensure that the graph was recreated by checking vertexes in graph
309 // After reload remove the added vertexes in the graph
310 // The reason for this so each test is independent
311 // as there shouldn't be dependencies and cause weird issues
312 String sourceFileName = "src/test/resources/pserver.graphson";
313 String destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver.graphson";
314 copySnapshotFile(sourceFileName,destFileName);
316 String [] args = {"-c","RELOAD_DATA", "-f","pserver.graphson"};
318 dataSnapshot4HistInit.executeCommand(args);
323 public void testMultiReloadDataAndVerifyDataInGraphMatchesGraphson() throws IOException, AAIException {
325 // Create multiple graphson files that contains a couple of vertexes in src/test/resources
326 // Copy those files to ${AJSC_HOME}/logs/data/dataSnasphots/
327 // Run the reload arguments and ensure that the graph was recreated by checking vertexes in graph
328 String sourceFileName = "src/test/resources/pserver2.graphson.P0";
329 String destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver2.graphson.P0";
330 copySnapshotFile(sourceFileName,destFileName);
332 sourceFileName = "src/test/resources/pserver2.graphson.P1";
333 destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver2.graphson.P1";
334 copySnapshotFile(sourceFileName,destFileName);
336 // After reload remove the added vertexes in the graph
337 // The reason for this so each test is independent
338 // as there shouldn't be dependencies and cause weird issues
340 String [] args = {"-c","MULTITHREAD_RELOAD","-f", "pserver2.graphson"};
341 dataSnapshot4HistInit.executeCommand(args);
346 public void testMultiReloadDataWithNonExistentFilesAndItShouldFail() throws IOException {
348 // After reload remove the added vertexes in the graph
349 // The reason for this so each test is independent
350 // as there shouldn't be dependencies and cause weird issues
351 String [] args = {"-c","MULTITHREAD_RELOAD", "-f","emptyfoo2.graphson"};
353 dataSnapshot4HistInit.executeCommand(args);
357 public void testReloadMultiDataAndVerifyDataInGraphMatchesGraphson() throws IOException {
359 // Create multiple graphson files that contains a couple of vertexes in src/test/resources
360 // Copy those files to ${AJSC_HOME}/logs/data/dataSnasphots/
361 // Run the reload arguments and ensure that the graph was recreated by checking vertexes in graph
362 String sourceFileName = "src/test/resources/pserver2.graphson.P0";
363 String destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver2.graphson.P0";
364 copySnapshotFile(sourceFileName,destFileName);
366 sourceFileName = "src/test/resources/pserver2.graphson.P1";
367 destFileName = System.getProperty("AJSC_HOME") + "/logs/data/dataSnapshots/pserver2.graphson.P1";
368 copySnapshotFile(sourceFileName,destFileName);
370 // After reload remove the added vertexes in the graph
371 // The reason for this so each test is independent
372 // as there shouldn't be dependencies and cause weird issues
373 String [] args = {"-c","RELOAD_DATA_MULTI","-f", "pserver2.graphson"};
375 dataSnapshot4HistInit.executeCommand(args);
379 public void testCanRetrieveNamesOfKeyProps() throws IOException {
381 // Make sure we can get the key names without failing
382 HashMap <String,ArrayList<String>> keyNamesHash = dataSnapshot4HistInit.getNodeKeyNames();
383 Iterator keyItr = keyNamesHash.entrySet().iterator();
384 while( keyItr.hasNext() ) {
385 Map.Entry entry = (Map.Entry) keyItr.next();
386 String nodeType = (String)entry.getKey();
387 ArrayList<String> keyNames = (ArrayList<String>)entry.getValue();
388 keyNamesHash.put(nodeType,keyNames);
389 System.out.println("DEBUGjojo === for nType " + nodeType + ", got keys = [" + keyNames + "]");
392 assertTrue(keyNamesHash != null );
393 assertFalse(keyNamesHash.isEmpty());
397 private void showVertProperties(String propKey, String propVal) {
399 Vertex v1 = g.V().has(propKey, propVal).next();
400 Iterator<VertexProperty<Object>> pI = v1.properties();
401 while( pI.hasNext() ){
402 VertexProperty<Object> tp = pI.next();
403 String infStr = " [" + tp.key() + "][" + tp.value() + "] ";
404 System.out.println("Regular ole properties are: " + infStr );
405 Iterator<Property<Object>> fullPropI = tp.properties();
406 while( fullPropI.hasNext() ){
407 // Note - the 'real' key/value of a property are not part of this list, just the
408 // extra stuff beyond those two.
409 Property<Object> propOfProp = fullPropI.next();
410 String infStr2 = " [" + propOfProp.key() + "][" + propOfProp.value() + "] ";
411 System.out.println("For " + infStr + ", got sub-property:" + infStr2 );
417 private List<Vertex> setupOneHistoryNode(GraphTraversalSource g) throws AAIException {
419 Vertex v1 = g.addV().property("aai-node-type", "pserver","start-ts", 9988707,"source-of-truth","N/A")
420 .property("hostname", "historyHOstGuy--8","start-ts", 9988707,"source-of-truth","N/A")
421 .property("equip-vendor", "historyVendor","start-ts", 9988707,"source-of-truth","N/A")
422 .property("role", "historyRole","start-ts", 9988707,"source-of-truth","N/A")
424 List<Vertex> list = new ArrayList<>();
427 Iterator<VertexProperty<Object>> pI = v1.properties();
428 while( pI.hasNext() ){
429 VertexProperty<Object> tp = pI.next();
430 String infStr = " [" + tp.key() + "|" + tp.value() + "] ";
431 System.out.println("Regular ole properties are: " + infStr );
432 Iterator<Property<Object>> fullPropI = tp.properties();
433 while( fullPropI.hasNext() ){
434 // Note - the 'real' key/value of a property are not part of this list, just the
435 // extra stuff beyond those two.
436 Property<Object> propOfProp = fullPropI.next();
437 String infStr2 = " [" + propOfProp.key() + "|" + propOfProp.value() + "] ";
438 System.out.println("For " + infStr + ", got sub-property:" + infStr2 );
444 private List<Vertex> setupPserverData(GraphTraversalSource g) throws AAIException {
445 Vertex v1 = g.addV().property("aai-node-type", "pserver")
446 .property("hostname", "somerandomhostname")
448 List<Vertex> list = new ArrayList<>();
450 Vertex v2 = g.addV().property("aai-node-type", "pserver")
451 .property("hostname", "somerandomhostname2")
453 Vertex pinterface = g.addV()
454 .property("aai-node-type", "p-interface")
455 .property("interface-name", "p-interface-name")
456 .property("in-maint", false)
457 .property("source-of-truth", "JUNIT")
459 edgeSerializer.addTreeEdge(g, v2, pinterface);
464 private void copySnapshotFile(String sourceFileName, String destFileName) throws IOException {
466 File inputFile = new File(sourceFileName);
467 File outputFile = new File(destFileName);
469 FileUtils.copyFile(inputFile, outputFile);