aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/util/SendDeleteMigrationNotificationsTest.java
blob: 5df4a758945fe5eae8591c07e3a2b187f386e9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.util;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphTransaction;
import org.junit.*;
import org.junit.runners.MethodSorters;
import org.mockito.Mockito;
import org.onap.aai.AAISetup;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dbmap.AAIGraph;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.migration.EventAction;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SendDeleteMigrationNotificationsTest extends AAISetup {

	private final static String FILE = "./test.txt";

	private static AtomicBoolean graphCreated = new AtomicBoolean(false);

	private JanusGraph graph;
	private JanusGraphTransaction tx;
	private GraphTraversalSource g;

	private static final String REALTIME_CONFIG = "./src/main/resources/etc/appprops/janusgraph-realtime.properties";

	@Before
	public void setUp() throws Exception {
		System.setProperty("realtime.db.config", REALTIME_CONFIG);
		AAIGraph.getInstance();
		graph = AAIGraph.getInstance().getGraph();
		tx = graph.newTransaction();
		g = tx.traversal();

		createFile();
	}

	public void createFile() throws AAIException, IOException {
		
		/*String str = "pserver#@#/cloud-infrastructure/pservers/pserver/mtunj102sd9#@#{\"hostname\":\"mtunj102sd9\",\"ptnii-equip-name\":\"mtunj102sd9\",\"equip-type\":\"SERVER\",\"equip-vendor\":\"HP\",\"equip-model\":\"DL380p9-nd\",\"fqdn\":\"mtunjrsv102.mtunj.sbcglobal.net\",\"ipv4-oam-address\":\"10.64.220.7\",\"resource-version\":\"1523039038578\",\"purpose\":\"LCPA-3.0\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"complex\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"related-link\":\"/aai/v14/cloud-infrastructure/complexes/complex/MDTWNJ21A5\",\"relationship-data\":[{\"relationship-key\":\"complex.physical-location-id\",\"relationship-value\":\"MDTWNJ21A5\"}]}]}}";
		Files.write(Paths.get(FILE), str.getBytes());
		graphCreated.compareAndSet(false, true);
		*/
	    if(!graphCreated.get()){
			Vertex pserver1 = g.addV()
					.property("aai-node-type", "pserver")
					.property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1")
					.property(AAIProperties.RESOURCE_VERSION, "333")
					.next();
			
			Vertex pserver2 = g.addV()
					.property("aai-node-type", "pserver")
					.property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2")
					.property(AAIProperties.RESOURCE_VERSION, "334")
					.next();
			
			Vertex pserver3 = g.addV()
					.property("aai-node-type", "pserver")
					.property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3")
					.property(AAIProperties.RESOURCE_VERSION, "335")
					.next();
			
			Vertex pserver4 = g.addV()
					.property("aai-node-type", "pserver")
					.property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4")
					.property(AAIProperties.RESOURCE_VERSION, "336")
					.next();

			tx.commit();
			
			try{
				Files.createFile(Paths.get(FILE));
			}catch(Exception e) {
				e.printStackTrace();
			}
			String finalStr = "";
			finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1\",\"resource-version\":\"333\"}" + "\n";
			Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND);
			finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2\",\"resource-version\":\"334\"}" + "\n";
			Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND);
			finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3\",\"resource-version\":\"335\"}" + "\n";
			Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND);
			finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4\",\"resource-version\":\"336\"}" + "\n";
			Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND);
			graphCreated.compareAndSet(false, true);
		}
	}
	@AfterClass
	public static void cleanUp() throws IOException {
		Files.delete(Paths.get(FILE));
	}

	@After
	public void tearDown() throws IOException {
		if (tx.isOpen()) {
			tx.tx().rollback();
		}
	}

	@Test
	public void processEverything() throws Exception {
		SendDeleteMigrationNotifications s  = spy(new SendDeleteMigrationNotifications(
				loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 0, "test", EventAction.DELETE, "DMAAP-LOAD"));
		doNothing().when(s).trigger();
		doNothing().when(s).cleanup();
		s.process("/aai/");
		assertEquals("1 events are created ", 4, s.notificationHelper.getNotifications().getEvents().size());

	}

	@Test
	public void processEverythingBatched2() throws Exception {
		SendDeleteMigrationNotifications s  = spy(new SendDeleteMigrationNotifications(
				loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 2, "test", EventAction.DELETE, "DMAAP-LOAD"));
		doNothing().when(s).trigger();
		doNothing().when(s).cleanup();
		s.process("/aai/");
		Mockito.verify(s, times(2)).trigger();

	}

	@Test
	public void processEverythingBatched3() throws Exception {
		SendDeleteMigrationNotifications s  = spy(new SendDeleteMigrationNotifications(
				loaderFactory, schemaVersions,  REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 3, "test", EventAction.DELETE, "DMAAP-LOAD"));
		doNothing().when(s).trigger();
		doNothing().when(s).cleanup();
		s.process("/aai/");
		Mockito.verify(s, times(2)).trigger();

	}

}