aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/migration/VertexMergeTest.java
blob: 1484eb4b44c2e882dfb7f6ee81a43e4cd11303e6 (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
167
168
169
170
171
172
173
174
175
176
/**
 * ============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.migration;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.Cardinality;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.onap.aai.AAISetup;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.introspection.Loader;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.serialization.db.DBSerializer;
import org.onap.aai.serialization.engines.JanusGraphDBEngine;
import org.onap.aai.serialization.engines.QueryStyle;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.setup.SchemaVersion;

import java.io.UnsupportedEncodingException;
import java.util.*;

import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@Ignore
public class VertexMergeTest extends AAISetup {
	
	
	private final static SchemaVersion version = new SchemaVersion("v10");
	private final static ModelType introspectorFactoryType = ModelType.MOXY;
	private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;

	private Loader loader;
	private TransactionalGraphEngine dbEngine;
	private JanusGraph graph;
	private GraphTraversalSource g;
	private Graph tx;

	@Before
	public void setUp() throws Exception {
		graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
		tx = graph.newTransaction();
		g = tx.traversal();
		loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
		dbEngine = new JanusGraphDBEngine(
				queryStyle,
				loader);

		JanusGraphManagement mgmt = graph.openManagement();
		mgmt.makePropertyKey("test-list").dataType(String.class).cardinality(Cardinality.SET).make();
		mgmt.commit();
		Vertex pserverSkeleton = g.addV().property("aai-node-type", "pserver").property("hostname", "TEST1")
				.property("source-of-truth", "AAI-EXTENSIONS").property("fqdn", "test1.com").property("test-list", "value1").next();

		Vertex pInterface1 = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface1")
				.property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface1").next();
		
		Vertex pInterface2 = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface2")
				.property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2").next();
		
		Vertex pInterface2Secondary = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface2").property("special-prop", "value")
				.property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2").next();
		
		Vertex lInterface1 = g.addV().property("aai-node-type", "l-interface").property("interface-name", "l-interface1").property("special-prop", "value")
				.property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2/l-interfaces/l-interface/l-interface1").next();
		
		Vertex lInterface1Canopi = g.addV().property("aai-node-type", "l-interface").property("interface-name", "l-interface1")
				.property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2/l-interfaces/l-interface/l-interface1").next();
		
		Vertex logicalLink = g.addV().property("aai-node-type", "logical-link").property("link-name", "logical-link1")
				.property(AAIProperties.AAI_URI, "/network/logical-links/logical-link/logical-link1").next();
		Vertex pserverCanopi = g.addV().property("aai-node-type", "pserver").property("hostname",  "TEST1")
				.property("source-of-truth", "CANOPI-WS").property("fqdn", "test2.com").property("test-list", "value2").next();
		
		Vertex complex1 = g.addV().property("aai-node-type", "complex").property("physical-location-id", "complex1")
				.property("source-of-truth", "RO").next();
		
		Vertex complex2 = g.addV().property("aai-node-type", "complex").property("physical-location-id", "complex2")
				.property("source-of-truth", "RCT").next();
		
		Vertex vserver1 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver1")
				.property("source-of-truth", "RO").next();
		
		Vertex vserver2 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver2")
				.property("source-of-truth", "RCT").next();
		Vertex vserver3 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver3")
				.property("source-of-truth", "RCT").next();
		Vertex vserver4 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver4")
				.property("source-of-truth", "RCT").next();
		Vertex vserver5 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver5")
				.property("source-of-truth", "RCT").next();
		
		
		edgeSerializer.addEdge(g, pserverSkeleton, complex1);
		edgeSerializer.addEdge(g, pserverSkeleton, vserver1);
		edgeSerializer.addEdge(g, pserverSkeleton, vserver2);
		edgeSerializer.addTreeEdge(g, pserverSkeleton, pInterface1);
		edgeSerializer.addTreeEdge(g, pserverSkeleton, pInterface2Secondary);
		edgeSerializer.addTreeEdge(g, pInterface2Secondary, lInterface1);
		edgeSerializer.addEdge(g, lInterface1, logicalLink);
		edgeSerializer.addEdge(g, pserverCanopi, complex2);
		edgeSerializer.addEdge(g, pserverCanopi, vserver3);
		edgeSerializer.addEdge(g, pserverCanopi, vserver4);
		edgeSerializer.addEdge(g, pserverCanopi, vserver5);
		edgeSerializer.addTreeEdge(g, pserverCanopi, pInterface2);
		edgeSerializer.addTreeEdge(g, pInterface2, lInterface1Canopi);

		Map<String, Set<String>> forceCopy = new HashMap<>();
		Set<String> forceSet = new HashSet<>();
		forceSet.add("fqdn");
		forceCopy.put("pserver", forceSet);
		
		TransactionalGraphEngine spy = spy(dbEngine);
		TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
		GraphTraversalSource traversal = g;
		GraphTraversalSource readOnly = g;
		when(spy.asAdmin()).thenReturn(adminSpy);
		when(adminSpy.getTraversalSource()).thenReturn(traversal);
		when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
		DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "Merge test");

		VertexMerge merge = new VertexMerge.Builder(loader, spy, serializer).edgeSerializer(edgeSerializer).build();
		merge.performMerge(pserverCanopi, pserverSkeleton, forceCopy, basePath);
	}

	@After
	public void cleanUp() {
		tx.tx().rollback();
		graph.close();
	}

	@Test
	public void run() throws UnsupportedEncodingException {

		assertEquals("pserver merged", false, g.V().has("hostname", "TEST1").has("source-of-truth", "AAI-EXTENSIONS").hasNext());
		assertThat("pserver list merge", Arrays.asList("value1", "value2"), containsInAnyOrder(g.V().has("hostname", "TEST1").values("test-list").toList().toArray()));
		assertEquals("canopi pserver has one edge to vserver2", 1, g.V().has("hostname", "TEST1").both().has("vserver-id", "vserver2").toList().size());
		assertEquals("canopi pserver has one edge to vserver1", 1, g.V().has("hostname", "TEST1").both().has("vserver-id", "vserver1").toList().size());
		assertEquals("canopi pserver retained edge to complex2", true, g.V().has("hostname", "TEST1").both().has("physical-location-id", "complex2").hasNext());
		assertEquals("canopi pserver received forced prop", "test1.com", g.V().has("hostname", "TEST1").values("fqdn").next());
		assertEquals("pserver skeleton child copied", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface1").hasNext());
		assertEquals("pserver skeleton child merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").has("special-prop", "value").hasNext());
		assertEquals("l-interface child merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").both().has("interface-name", "l-interface1").has("special-prop", "value").hasNext());
		assertEquals("l-interface child cousin edge merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").both().has("interface-name", "l-interface1").both().has("link-name", "logical-link1").hasNext());
		assertEquals("one l-interface1 found", new Long(1), g.V().has("interface-name", "l-interface1").count().next());
		assertEquals("one p-interface2 found", new Long(1), g.V().has("interface-name", "p-interface2").count().next());

	}
}