Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / DbSerializerNotificationEventsTest.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.serialization.db;
22
23 import org.apache.tinkerpop.gremlin.structure.Graph;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.JanusGraphFactory;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.edges.EdgeIngestor;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.parsers.query.QueryParser;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.setup.SchemaVersion;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.test.annotation.DirtiesContext;
47
48 import java.io.UnsupportedEncodingException;
49 import java.net.URI;
50 import java.net.URISyntaxException;
51 import java.util.*;
52 import java.util.stream.Collectors;
53
54 import static org.hamcrest.CoreMatchers.is;
55 import static org.junit.Assert.*;
56
57 @RunWith(value = Parameterized.class)
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class DbSerializerNotificationEventsTest extends AAISetup {
60
61     // to use, set thrown.expect to whatever your test needs
62     // this line establishes default of expecting no exception to be thrown
63     @Rule
64     public ExpectedException thrown = ExpectedException.none();
65
66     protected static Graph graph;
67
68     @Autowired
69     protected EdgeSerializer edgeSer;
70     @Autowired
71     protected EdgeIngestor ei;
72
73     private SchemaVersion version;
74     private final ModelType introspectorFactoryType = ModelType.MOXY;
75     private Loader loader;
76     private TransactionalGraphEngine engine; // for tests that aren't mocking the engine
77
78     @Parameterized.Parameter(value = 0)
79     public QueryStyle queryStyle;
80
81     @Parameterized.Parameters(name = "QueryStyle.{0}")
82     public static Collection<Object[]> data() {
83         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
84     }
85
86     @BeforeClass
87     public static void init() throws Exception {
88         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
89
90     }
91
92     @Before
93     public void setup() throws Exception {
94         // createGraph();
95         version = schemaVersions.getDefaultVersion();
96         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
97         engine = new JanusGraphDBEngine(queryStyle, loader);
98     }
99
100     /*
101     Create Complex
102     Create Pserver with pinterface and relationship to complex
103     Update pserver removing relationship to complex
104     Update pserver adding a second p-interface
105     Add l-interface directly to the 2nd p-interface
106     Delete pserver
107      */
108     @Test
109     public void createComplexPserverWithRelUpdatePserverToDeleteRelAddPinterfaceThenDeleteComplexCheckingUpdatedListTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
110         engine.startTransaction();
111
112         System.out.println("Create Complex");
113         DBSerializer dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-complex", AAIProperties.MINIMUM_DEPTH);
114         Introspector complex = loader.introspectorFromName("complex");
115         Vertex complexV = dbserLocal.createNewVertex(complex);
116         final String complexUri = "/cloud-infrastructure/complexes/complex/c-id-b";
117         QueryParser uriQuery =
118             engine.getQueryBuilder().createQueryFromURI(new URI(complexUri));
119
120         complex.setValue("physical-location-id", "c-id-b");
121         complex.setValue("physical-location-type", "type");
122         complex.setValue("street1", "streetA");
123         complex.setValue("city", "cityA");
124         complex.setValue("postal-code", "11111");
125         complex.setValue("country", "abc");
126         complex.setValue("region", "ef");
127         dbserLocal.serializeToDb(complex, complexV, uriQuery, "complex", complex.marshal(false));
128
129         assertTrue("Complex created", engine.tx().traversal().V()
130             .has("aai-node-type", "complex")
131             .has("physical-location-id", "c-id-b")
132             .hasNext());
133         Map<Vertex, Boolean> updated = getUpdatedVertexes(dbserLocal);
134         assertEquals("Number of updated vertexes", 1, updated.size());
135         assertThat("Only modified vertexes are in the updated set",
136             updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
137             is(Collections.singleton(complexUri)));
138         List<String> didNotUpdateStandardVertexProps = updated.entrySet().stream()
139             .filter(e -> !e.getValue())
140             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
141         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
142
143
144
145
146         System.out.println("Create Pserver with pinterface and relationship to complex ");
147         dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-pserver", AAIProperties.MINIMUM_DEPTH);
148         Introspector pserver = loader.introspectorFromName("pserver");
149         Vertex pserverV = dbserLocal.createNewVertex(pserver);
150         final String pserverUri = "/cloud-infrastructure/pservers/pserver/ps-b";
151         uriQuery =
152             engine.getQueryBuilder().createQueryFromURI(new URI(pserverUri));
153
154         Introspector relationship = loader.introspectorFromName("relationship");
155         relationship.setValue("related-to", "complex");
156         relationship.setValue("related-link", complexUri);
157         Introspector relationshipList = loader.introspectorFromName("relationship-list");
158         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
159
160         pserver.setValue("relationship-list", relationshipList.getUnderlyingObject());
161         pserver.setValue("hostname", "ps-b");
162         pserver.setValue("number-of-cpus", 20);
163
164         Introspector pint = loader.introspectorFromName("p-interface");
165         pint.setValue("interface-name", "pint-1");
166         final String pintUri = pserverUri + "/p-interfaces/p-interface/pint-1";
167
168         Introspector pints = loader.introspectorFromName("p-interfaces");
169         pints.setValue("p-interface", Collections.singletonList(pint.getUnderlyingObject()));
170         pserver.setValue("p-interfaces", pints.getUnderlyingObject());
171         dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
172
173         assertTrue("Pserver created", engine.tx().traversal().V()
174             .has("aai-node-type", "pserver")
175             .has("hostname", "ps-b")
176             .hasNext());
177         assertTrue("Pserver has edge to complex", engine.tx().traversal().V()
178             .has("aai-node-type", "pserver")
179             .has("hostname", "ps-b")
180             .bothE()
181             .otherV()
182             .has("aai-node-type", "complex")
183             .hasNext());
184         assertTrue("Pserver has edge to pinterface", engine.tx().traversal().V()
185             .has("aai-node-type", "pserver")
186             .has("hostname", "ps-b")
187             .bothE()
188             .otherV()
189             .has("aai-node-type", "p-interface")
190             .hasNext());
191         updated = getUpdatedVertexes(dbserLocal);
192         assertEquals("Number of updated vertexes", 3, updated.size());
193         assertThat("Only modified vertexes are in the updated set",
194             updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
195             is(new HashSet<>(Arrays.asList(pserverUri, pintUri, complexUri))));
196         didNotUpdateStandardVertexProps = updated.entrySet().stream()
197             .filter(e -> !e.getValue())
198             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
199         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
200
201
202         System.out.println("Update pserver removing relationship to complex");
203         dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "update-pserver", AAIProperties.MINIMUM_DEPTH);
204         pserver = dbserLocal.getLatestVersionView(pserverV);
205         relationshipList = loader.introspectorFromName("relationship-list");
206         relationshipList.setValue("relationship", Collections.emptyList());
207         pserver.setValue("relationship-list", relationshipList.getUnderlyingObject());
208         pserver.setValue("equip-type", "server-a");
209         pserver.setValue("number-of-cpus", 99);
210         dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
211
212         assertFalse("Pserver no longer has edge to complex", engine.tx().traversal().V()
213             .has("aai-node-type", "pserver")
214             .has("hostname", "ps-b")
215             .bothE()
216             .otherV()
217             .has("aai-node-type", "complex")
218             .hasNext());
219         updated = getUpdatedVertexes(dbserLocal);
220         assertEquals("Number of updated vertexes", 2, updated.size());
221         assertThat("Only modified vertexes are in the updated set",
222             updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
223             is(new HashSet<>(Arrays.asList(pserverUri, complexUri))));
224         didNotUpdateStandardVertexProps = updated.entrySet().stream()
225             .filter(e -> !e.getValue())
226             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
227         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
228
229
230
231         System.out.println("Update pserver adding a second p-interface");
232         dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "update-pserver", AAIProperties.MINIMUM_DEPTH);
233         pserver = dbserLocal.getLatestVersionView(pserverV);
234         Introspector pint2 = loader.introspectorFromName("p-interface");
235         pint2.setValue("interface-name", "pint-2");
236         pints = pserver.getWrappedValue("p-interfaces");
237         List<Object> pintList = pserver.getWrappedValue("p-interfaces").getWrappedListValue("p-interface")
238             .stream().map(Introspector::getUnderlyingObject).collect(Collectors.toList());
239         pintList.add(pint2.getUnderlyingObject());
240         pints.setValue("p-interface", pintList);
241         pserver.setValue("p-interfaces", pints.getUnderlyingObject());
242         final String pint2Uri = pserverUri + "/p-interfaces/p-interface/pint-2";
243         dbserLocal.serializeToDb(pserver, pserverV, uriQuery, "pserver", pserver.marshal(false));
244
245         assertTrue("Pserver has edge to pinterface 2", engine.tx().traversal().V()
246             .has("aai-node-type", "pserver")
247             .has("hostname", "ps-b")
248             .in()
249             .has("aai-node-type", "p-interface")
250             .has("interface-name","pint-2")
251             .hasNext());
252         assertTrue("p-interface 2 created", engine.tx().traversal().V()
253             .has("aai-node-type", "p-interface")
254             .has("interface-name", "pint-2")
255             .has(AAIProperties.AAI_URI, pint2Uri)
256             .hasNext());
257         updated = getUpdatedVertexes(dbserLocal);
258         assertEquals("Number of updated vertexes", 1, updated.size());
259         assertThat("Only modified vertexes are in the updated set",
260             updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
261             is(Collections.singleton(pint2Uri)));
262         didNotUpdateStandardVertexProps = updated.entrySet().stream()
263             .filter(e -> !e.getValue())
264             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
265         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
266
267
268         System.out.println("Add l-interface directly to the 2nd p-interface");
269         dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "create-pserver", AAIProperties.MINIMUM_DEPTH);
270         Introspector lInt = loader.introspectorFromName("l-interface");
271         Vertex lIntV = dbserLocal.createNewVertex(lInt);
272         final String lIntUri = pint2Uri + "/l-interfaces/l-interface/lint-1";
273         uriQuery =
274             engine.getQueryBuilder().createQueryFromURI(new URI(lIntUri));
275         lInt.setValue("interface-name", "lint-1");
276         dbserLocal.serializeToDb(lInt, lIntV, uriQuery, "l-interface", lInt.marshal(false));
277
278         assertTrue("l-interface created", engine.tx().traversal().V()
279             .has("aai-node-type", "l-interface")
280             .has("interface-name", "lint-1")
281             .hasNext());
282
283         assertTrue("Pserver has edge to pinterface to l-interface", engine.tx().traversal().V()
284             .has("aai-node-type", "pserver")
285             .has("hostname", "ps-b")
286             .bothE()
287             .otherV()
288             .has("aai-node-type", "p-interface")
289             .bothE()
290             .otherV()
291             .has("aai-node-type", "l-interface")
292             .hasNext());
293         updated = getUpdatedVertexes(dbserLocal);
294         assertEquals("Number of updated vertexes", 1, updated.size());
295         assertThat("Only modified vertexes are in the updated set",
296             updated.keySet().stream().map(v -> v.<String>value(AAIProperties.AAI_URI)).collect(Collectors.toSet()),
297             is(new HashSet<>(Collections.singletonList(lIntUri))));
298         didNotUpdateStandardVertexProps = updated.entrySet().stream()
299             .filter(e -> !e.getValue())
300             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
301         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
302
303
304         System.out.println("Delete pserver");
305         dbserLocal = new DBSerializer(version, engine, introspectorFactoryType, "delete-pserver", AAIProperties.MINIMUM_DEPTH);
306         pserver = dbserLocal.getLatestVersionView(pserverV);
307         String rv = pserver.getValue(AAIProperties.RESOURCE_VERSION);
308         dbserLocal.delete(engine.tx().traversal().V(pserverV).next(), rv, true);
309
310         assertFalse("pserver no longer exists", engine.tx().traversal().V()
311             .has("aai-node-type", "pserver")
312             .hasNext());
313         updated = getUpdatedVertexes(dbserLocal);
314         assertEquals("Number of updated vertexes", 0, updated.size());
315         didNotUpdateStandardVertexProps = updated.entrySet().stream()
316             .filter(e -> !e.getValue())
317             .map(e -> e.getKey().<String>value(AAIProperties.AAI_URI)).collect(Collectors.toList());
318         assertThat("Vertexes should all have their standard props updated", didNotUpdateStandardVertexProps, is(Collections.emptyList()));
319
320
321     }
322
323     private Map<Vertex, Boolean> getUpdatedVertexes(DBSerializer dbserLocal) {
324         Map<Vertex, Boolean> updated = new LinkedHashMap<>(dbserLocal.getUpdatedVertexes());
325         dbserLocal.touchStandardVertexPropertiesForEdges().forEach(v -> updated.putIfAbsent(v, true));
326         return updated;
327     }
328
329
330 }