Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / NotificationDmaapEventTest.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.rest;
22
23 import static org.hamcrest.CoreMatchers.*;
24 import static org.hamcrest.junit.MatcherAssert.assertThat;
25 import static org.junit.Assert.assertEquals;
26
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.util.*;
33
34 import javax.ws.rs.core.Response;
35
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
37 import org.apache.tinkerpop.gremlin.structure.Vertex;
38 import org.janusgraph.core.JanusGraph;
39 import org.janusgraph.core.JanusGraphTransaction;
40 import org.javatuples.Pair;
41 import org.json.JSONObject;
42 import org.junit.After;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.Parameterized;
46 import org.mockito.Mockito;
47 import org.onap.aai.AAISetup;
48 import org.onap.aai.HttpTestUtil;
49 import org.onap.aai.PayloadUtil;
50 import org.onap.aai.db.props.AAIProperties;
51 import org.onap.aai.dbmap.AAIGraph;
52 import org.onap.aai.exceptions.AAIException;
53 import org.onap.aai.introspection.ModelType;
54 import org.onap.aai.rest.ueb.NotificationEvent;
55 import org.onap.aai.rest.ueb.UEBNotification;
56 import org.onap.aai.serialization.engines.QueryStyle;
57 import org.skyscreamer.jsonassert.JSONAssert;
58 import org.springframework.test.annotation.DirtiesContext;
59
60 @RunWith(value = Parameterized.class)
61 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
62 public class NotificationDmaapEventTest extends AAISetup {
63
64     @Parameterized.Parameter
65     public QueryStyle queryStyle;
66
67     @Parameterized.Parameters(name = "QueryStyle.{0}")
68     public static Collection<Object[]> data() {
69         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
70     }
71
72     @Test
73     public void testCreateWithPserverWithAllChildrenAndVerifyMultipleNotificationsWhenNotificationDepthIsZero()
74             throws IOException, AAIException {
75
76         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
77         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
78         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
79
80         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
81
82         Response response = httpTestUtil.doGet(uri);
83         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
84
85         response = httpTestUtil.doPut(uri, resource);
86         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
87
88         int expectedCreateEvents = 17;
89
90         assertThat(notification.getEvents().size(), is(expectedCreateEvents));
91
92         // Verify all the events are create since its a new PUT
93         notification.getEvents().forEach((event) -> {
94
95             String header = event.getEventHeader().marshal(false);
96
97             assertThat(event.getEventHeader().marshal(false), containsString("\"CREATE\""));
98
99             assertThat(header, containsString("\"top-entity-type\":\"pserver\""));
100
101         });
102
103         response = httpTestUtil.doGet(uri);
104         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
105     }
106
107     // Test existing pserver create new pinterface check dmaap event for pinterface is CREATE
108     @Test
109     public void testExistingPserverCreateNewChildPInterfaceAndCheckDmaapEventForPInterfaceIsCreateWhenNotificationDepthIsZero()
110             throws IOException, AAIException {
111
112         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
113         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
114         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
115
116         String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
117
118         Response response = httpTestUtil.doGet(uri);
119         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
120
121         response = httpTestUtil.doPut(uri, pserverResource);
122         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
123         notification.clearEvents();
124
125         response = httpTestUtil.doGet(uri, "all");
126         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
127
128         JSONObject pserverJson = new JSONObject(response.getEntity().toString());
129         JSONObject pInterfaceObject = new JSONObject();
130         pInterfaceObject.put("interface-name", "p-interface-1");
131
132         pserverJson.getJSONObject("p-interfaces").getJSONArray("p-interface").put(pInterfaceObject);
133
134         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
135         response = httpTestUtil.doPut(uri, pserverJson.toString());
136         assertEquals("Expecting the pserver to be updated with a new p-interface", 200, response.getStatus());
137
138         response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/p-interface-1", "0");
139         assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
140
141         List<NotificationEvent> events = notification.getEvents();
142         assertThat(events.size(), is(2));
143
144         String notificationEventHeader = events.get(1).getEventHeader().marshal(false);
145         String notificationEventBody = events.get(1).getObj().marshal(false);
146
147         assertThat(notificationEventHeader, containsString("\"action\":\"CREATE\""));
148         assertThat(notificationEventHeader, containsString("\"entity-type\":\"p-interface\""));
149         assertThat(notificationEventHeader, containsString("\"top-entity-type\":\"pserver\""));
150
151         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
152                 "notification-dmaap-events/depth-zero/expected-notification-header-create-child-on-existing-obj.json");
153         String expectedNotificationBody = PayloadUtil.getResourcePayload(
154                 "notification-dmaap-events/depth-zero/expected-notification-body-create-child-on-existing-obj.json");
155
156         JSONAssert.assertEquals(expectedNotificationHeader, notificationEventHeader, false);
157         JSONAssert.assertEquals(expectedNotificationBody, notificationEventBody, false);
158     }
159
160     @Test
161     public void testExistingPserverCreateNewChildPInterfaceAndCheckDmaapEventForPserverIsSentWithNewPInterfaceWhenNotificationDepthIsAll()
162             throws IOException, AAIException {
163
164         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
165         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
166         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
167
168         String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
169
170         Response response = httpTestUtil.doGet(uri);
171         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
172
173         response = httpTestUtil.doPut(uri, pserverResource);
174         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
175
176         response = httpTestUtil.doGet(uri, "all");
177         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
178
179         JSONObject pserverJson = new JSONObject(response.getEntity().toString());
180         String pserverResourceVersion = pserverJson.getString("resource-version");
181
182         JSONObject pInterfaceObject = new JSONObject();
183         pInterfaceObject.put("interface-name", "p-interface-1");
184
185         pserverJson.getJSONObject("p-interfaces").getJSONArray("p-interface").put(pInterfaceObject);
186
187         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
188         response = httpTestUtil.doPut(uri, pserverJson.toString());
189         assertEquals("Expecting the pserver to be updated with a new p-interface", 200, response.getStatus());
190
191         response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/p-interface-1", "0");
192         assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
193
194         List<NotificationEvent> events = notification.getEvents();
195         assertThat(events.size(), is(1));
196
197         String notificationEventHeader = events.get(0).getEventHeader().marshal(false);
198         String notificationEventBody = events.get(0).getObj().marshal(false);
199
200         assertThat(notificationEventHeader, containsString("\"action\":\"UPDATE\""));
201         assertThat(notificationEventHeader, containsString("\"entity-type\":\"pserver\""));
202         assertThat(notificationEventHeader, containsString("\"top-entity-type\":\"pserver\""));
203
204         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
205                 "notification-dmaap-events/depth-all/expected-notification-header-create-child-on-existing-obj.json");
206         String expectedNotificationBody = PayloadUtil.getResourcePayload(
207                 "notification-dmaap-events/depth-all/expected-notification-body-create-child-on-existing-obj.json");
208
209         JSONAssert.assertEquals(expectedNotificationHeader, notificationEventHeader, false);
210         JSONAssert.assertEquals(expectedNotificationBody, notificationEventBody, false);
211
212         response = httpTestUtil.doGet(uri, "0");
213         pserverJson = new JSONObject(response.getEntity().toString());
214         String newPserverResourceVersion = pserverJson.getString("resource-version");
215
216         // After an pserver's p-interface is updated on the pserver, even though
217         // the pserver nothing changed, expecting the pserver resource version to be changed
218         assertThat("Expecting the new pserver resource version and old resource version to be not same",
219                 newPserverResourceVersion, is(not(pserverResourceVersion)));
220         assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
221     }
222
223     // Test Bulk Scenario
224     @Test
225     public void testBulkScenarioWhereMultipleCreatesAndEnsureNoDuplicationInDmaapEventsWhenNotificationDepthIsZero()
226             throws UnsupportedEncodingException, AAIException {
227
228         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/random-pserver";
229         String cloudRegionUri =
230                 "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/random-cloud-region-owner/random-cloud-region-id";
231
232         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
233         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
234
235         Map<String, String> uriPayload = new LinkedHashMap<>();
236
237         uriPayload.put(pserverUri, "{}");
238         uriPayload.put(cloudRegionUri, "{}");
239
240         Response response = httpTestUtil.doPut(uriPayload);
241         assertThat(response.getStatus(), is(201));
242
243         int numberOfEventsActual = notification.getEvents().size();
244         int expectedEvents = 2;
245
246         assertThat("Expecting the number of dmaap events to be 2", numberOfEventsActual, is(expectedEvents));
247
248         notification.getEvents().forEach((event) -> {
249             String notificationEventHeader = event.getEventHeader().marshal(false);
250             assertThat(notificationEventHeader, containsString("\"CREATE\""));
251         });
252     }
253
254     @Test
255     public void testBulkScenarioWhereMultipleCreatesAndEnsureNoDuplicationInDmaapEventsWhenNotificationDepthIsAll()
256             throws UnsupportedEncodingException, AAIException {
257
258         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/random-pserver";
259         String cloudRegionUri =
260                 "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/random-cloud-region-owner/random-cloud-region-id";
261
262         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
263         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
264
265         Map<String, String> uriPayload = new LinkedHashMap<>();
266
267         uriPayload.put(pserverUri, "{}");
268         uriPayload.put(cloudRegionUri, "{}");
269
270         Response response = httpTestUtil.doPut(uriPayload);
271         assertThat(response.getStatus(), is(201));
272
273         int numberOfEventsActual = notification.getEvents().size();
274         int expectedEvents = 2;
275
276         assertThat("Expecting the number of dmaap events to be 2", numberOfEventsActual, is(expectedEvents));
277
278         notification.getEvents().forEach((event) -> {
279             String notificationEventHeader = event.getEventHeader().marshal(false);
280             assertThat(notificationEventHeader, containsString("\"CREATE\""));
281         });
282     }
283
284     @Test
285     public void testDeleteOnExistingPserverAndCheckIfNotificationDepthIsZeroThatAllEventsHaveDeleteAndThatDepthIsZeroOnEachNotificationEvent()
286             throws IOException, AAIException {
287         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
288         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
289         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
290
291         String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
292
293         Response response = httpTestUtil.doGet(uri);
294         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
295
296         response = httpTestUtil.doPut(uri, pserverResource);
297         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
298
299         response = httpTestUtil.doGet(uri, "all");
300         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
301
302         JSONObject pserverObject = new JSONObject(response.getEntity().toString());
303         String resourceVersion = pserverObject.getString("resource-version");
304
305         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
306         response = httpTestUtil.doDelete(uri, resourceVersion);
307         assertEquals("Expecting the pserver to be deleted", 204, response.getStatus());
308
309         List<NotificationEvent> notificationEvents = notification.getEvents();
310         assertThat(notificationEvents.size(), is(17));
311
312         notificationEvents.forEach((event) -> {
313
314             String header = event.getEventHeader().marshal(false);
315
316             assertThat(event.getEventHeader().marshal(false), containsString("\"DELETE\""));
317
318             assertThat(header, containsString("\"top-entity-type\":\"pserver\""));
319         });
320     }
321
322     @Test
323     public void testDeleteOnExistingResourceVersionMismatchNoEventGenerated() throws IOException, AAIException {
324         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
325         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
326         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
327
328         String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
329
330         Response response = httpTestUtil.doGet(uri);
331         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
332
333         response = httpTestUtil.doPut(uri, pserverResource);
334         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
335
336         response = httpTestUtil.doGet(uri, "all");
337         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
338
339         JSONObject pserverObject = new JSONObject(response.getEntity().toString());
340         String resourceVersion = pserverObject.getString("resource-version");
341
342         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
343         response = httpTestUtil.doDelete(uri, resourceVersion + "123");
344         assertEquals("Resource version mismatch exception", 412, response.getStatus());
345
346         List<NotificationEvent> notificationEvents = notification.getEvents();
347         assertThat(notificationEvents.size(), is(0));
348     }
349
350     // Test notification depth set to all
351     // Scenario for testing the creation of pserver with children, grandchildren
352     // Default behaviour is for one event to be sent out
353     // which includes all the children and grandchildren, etc
354     @Test
355     public void testCreateWithPserverWithAllChildrenAndVerifyOneNotificationWhenNotificationDepthIsAll()
356             throws IOException, AAIException {
357
358         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
359         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
360         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
361
362         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
363
364         Response response = httpTestUtil.doGet(uri);
365         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
366
367         response = httpTestUtil.doPut(uri, resource);
368         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
369
370         assertThat(notification.getEvents().size(), is(1));
371
372         NotificationEvent notificationEvent = notification.getEvents().get(0);
373
374         // Verify all the events are create since its a new PUT
375         String header = notificationEvent.getEventHeader().marshal(false);
376
377         assertThat(header, containsString("\"CREATE\""));
378
379         assertThat(header, containsString("\"entity-type\":\"pserver\""));
380
381         assertThat(header, containsString("\"top-entity-type\":\"pserver\""));
382
383         assertThat(header, containsString("\"entity-link\":\"" + uri + "\""));
384
385         response = httpTestUtil.doGet(uri);
386         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
387
388         JSONAssert.assertEquals(response.getEntity().toString(), notificationEvent.getObj().marshal(false), false);
389     }
390
391     @Test
392     public void testPatchExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventNoChildrenWhenNotificationDepthIsZero()
393             throws IOException, AAIException {
394
395         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
396         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
397         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
398
399         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
400
401         Response response = httpTestUtil.doGet(uri);
402         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
403
404         response = httpTestUtil.doPut(uri, resource);
405         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
406
407         response = httpTestUtil.doGet(uri);
408         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
409
410         JSONObject pserverObject = new JSONObject();
411         pserverObject.put("equip-type", "new-equip-patch-type");
412
413         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
414         response = httpTestUtil.doPatch(uri, pserverObject.toString());
415         assertThat(response.getStatus(), is(200));
416
417         response = httpTestUtil.doGet(uri, "0");
418         assertThat(response.getEntity().toString(), containsString("new-equip-patch-type"));
419
420         assertThat(notification.getEvents().size(), is(1));
421         String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);
422
423         // Check that everything in notification event is also response body
424         // Not comparing the other way as notification only includes parents main properties
425         JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
426     }
427
428     @Test
429     public void testPatchExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventIncludeChildrenWhenNotificationDepthIsAll()
430             throws IOException, AAIException {
431
432         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
433         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
434         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
435
436         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
437
438         Response response = httpTestUtil.doGet(uri);
439         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
440
441         response = httpTestUtil.doPut(uri, resource);
442         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
443
444         response = httpTestUtil.doGet(uri);
445         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
446
447         JSONObject pserverObject = new JSONObject();
448         pserverObject.put("equip-type", "new-equip-patch-type");
449
450         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
451         response = httpTestUtil.doPatch(uri, pserverObject.toString());
452         assertThat(response.getStatus(), is(200));
453
454         response = httpTestUtil.doGet(uri, "all");
455         assertThat(response.getEntity().toString(), containsString("new-equip-patch-type"));
456
457         assertThat(notification.getEvents().size(), is(1));
458         String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);
459
460         // Check that everything in notification event is also response body
461         // Not comparing the other way as notification only includes parents main properties
462         JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
463     }
464
465     // Test notification depth set to all
466     // Scenario where we are only updating one field in p-interface
467     // Make sure the parent and children are included
468     @Test
469     public void testUpdateExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventIncludingChildrenWhenNotificationDepthIsAll()
470             throws IOException, AAIException {
471
472         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
473         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
474         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
475
476         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
477
478         Response response = httpTestUtil.doGet(uri);
479         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
480
481         response = httpTestUtil.doPut(uri, resource);
482         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
483
484         response = httpTestUtil.doGet(uri);
485         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
486
487         response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/example-interface-name-val-46147", "0");
488         assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
489
490         JSONObject pInterfaceObject = new JSONObject(response.getEntity().toString());
491         pInterfaceObject.put("equipment-identifier", "new-equipment-identifier");
492
493         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
494         response = httpTestUtil.doPut(uri + "/p-interfaces/p-interface/example-interface-name-val-46147",
495                 pInterfaceObject.toString());
496         assertThat(response.getStatus(), is(200));
497
498         // Get the parent uri as the notification event json has parent structure it makes it easy to compare
499         response = httpTestUtil.doGet(uri);
500         assertThat(response.getEntity().toString(), containsString("new-equipment-identifier"));
501
502         assertThat(notification.getEvents().size(), is(1));
503         String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);
504
505         // Check that everything in notification event is also response body
506         // Not comparing the other way as notification only includes parents main properties
507         JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
508     }
509
510     // Test notification depth set to 0
511     // Scenario where we are only updating one field in p-interface
512     @Test
513     public void testUpdateExistingPserverWithChildrenAndModifyOnlyPInterfaceAndVerifyThatOnlyOneNotificationForPInterfaceIsCreatedWhenNotificationDepthIsZero()
514             throws IOException, AAIException {
515
516         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
517         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
518         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
519
520         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
521
522         Response response = httpTestUtil.doGet(uri);
523         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
524
525         response = httpTestUtil.doPut(uri, resource);
526         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
527
528         response = httpTestUtil.doGet(uri);
529         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
530
531         response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/example-interface-name-val-46147", "0");
532         assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
533
534         JSONObject pInterfaceObject = new JSONObject(response.getEntity().toString());
535         pInterfaceObject.put("equipment-identifier", "new-equipment-identifier");
536
537         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
538         response = httpTestUtil.doPut(uri + "/p-interfaces/p-interface/example-interface-name-val-46147",
539                 pInterfaceObject.toString());
540         assertThat(response.getStatus(), is(200));
541
542         response = httpTestUtil.doGet(uri);
543         assertThat(notification.getEvents().size(), is(1));
544         String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);
545         System.out.println("Update notification " + updateNotificationEvent);
546
547         // Check that everything in notification event is also response body
548         // Not comparing the other way as notification only includes parents main properties
549         JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
550     }
551
552     @Test
553     public void testExistingPserverWithChildAndGenericVnfAndCreateEdgeBetweenThemAndCheckNoChildWhenNotificationDepthIsZero()
554             throws IOException, AAIException {
555
556         String hostname = "example-hostname-val-85598";
557
558         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
559         String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";
560
561         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
562         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
563
564         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
565         String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");
566
567         Response response = httpTestUtil.doGet(pserverUri);
568         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
569
570         response = httpTestUtil.doPut(pserverUri, resource);
571         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
572
573         response = httpTestUtil.doGet(pserverUri);
574         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
575
576         response = httpTestUtil.doGet(genericVnfUri);
577         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
578
579         response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
580         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
581
582         response = httpTestUtil.doGet(genericVnfUri);
583         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
584         assertThat(response.getEntity().toString(), not(containsString(hostname)));
585
586         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
587
588         String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");
589
590         response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
591         assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());
592
593         List<NotificationEvent> notificationEvents = notification.getEvents();
594         assertThat(notificationEvents.size(), is(2));
595
596         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
597                 "notification-dmaap-events/depth-zero/expected-notification-header-create-edge-between-pserver-and-generic-vnf.json");
598         String expectedNotificationBody = PayloadUtil.getResourcePayload(
599                 "notification-dmaap-events/depth-zero/expected-notification-body-create-edge-between-pserver-and-generic-vnf.json");
600
601         JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
602                 false);
603         JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);
604
605         response = httpTestUtil.doGet(genericVnfUri);
606
607         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
608         assertThat(response.getEntity().toString(), containsString(hostname));
609     }
610
611     @Test
612     public void testExistingPserverWithChildAndGenericVnfAndCreateEdgeBetweenThemAndCheckChildrenIncludedWhenNotificationDepthIsAll()
613             throws IOException, AAIException {
614
615         String hostname = "example-hostname-val-85598";
616
617         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
618         String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";
619
620         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
621         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
622
623         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
624         String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");
625
626         Response response = httpTestUtil.doGet(pserverUri);
627         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
628
629         response = httpTestUtil.doPut(pserverUri, resource);
630         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
631
632         response = httpTestUtil.doGet(pserverUri);
633         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
634
635         response = httpTestUtil.doGet(genericVnfUri);
636         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
637
638         response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
639         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
640
641         response = httpTestUtil.doGet(genericVnfUri);
642         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
643         assertThat(response.getEntity().toString(), not(containsString(hostname)));
644
645         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
646
647         String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");
648
649         response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
650         assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());
651
652         List<NotificationEvent> notificationEvents = notification.getEvents();
653         assertThat(notificationEvents.size(), is(2));
654
655         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
656                 "notification-dmaap-events/depth-all/expected-notification-header-create-edge-between-pserver-and-generic-vnf.json");
657         String expectedNotificationBody = PayloadUtil.getResourcePayload(
658                 "notification-dmaap-events/depth-all/expected-notification-body-create-edge-between-pserver-and-generic-vnf.json");
659
660         System.out.println("Notification Body: " + notificationEvents.get(0).getObj().marshal(false));
661         JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
662                 false);
663         JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);
664
665         response = httpTestUtil.doGet(genericVnfUri);
666
667         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
668         assertThat(response.getEntity().toString(), containsString(hostname));
669     }
670
671     @Test
672     public void testExistingPserverWithChildAndGenericVnfAndExistingEdgeBetweenThemAndDeleteEdgeAndCheckNoChildWhenNotificationDepthIsZero()
673             throws IOException, AAIException {
674
675         String hostname = "example-hostname-val-85598";
676
677         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
678         String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";
679
680         String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");
681
682         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
683         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
684
685         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
686         String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");
687
688         Response response = httpTestUtil.doGet(pserverUri);
689         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
690
691         response = httpTestUtil.doPut(pserverUri, resource);
692         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
693
694         response = httpTestUtil.doGet(pserverUri);
695         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
696
697         response = httpTestUtil.doGet(genericVnfUri);
698         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
699
700         response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
701         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
702
703         response = httpTestUtil.doGet(genericVnfUri);
704         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
705         assertThat(response.getEntity().toString(), not(containsString(hostname)));
706
707         response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
708         assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());
709
710         response = httpTestUtil.doGet(genericVnfUri);
711
712         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
713         assertThat(response.getEntity().toString(), containsString(hostname));
714
715         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
716         assertThat(response.getEntity().toString(), containsString(hostname));
717
718         response = httpTestUtil.doGet(pserverUri);
719         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
720
721         JSONObject pserverJson = new JSONObject(response.getEntity().toString());
722         String resourceVersion = pserverJson.getString("resource-version");
723
724         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
725
726         response = httpTestUtil.doDelete(pserverUri + "/relationship-list/relationship", resourceVersion, relationship);
727         assertThat("Expected the pserver relationship to generic-vnf to be deleted", response.getStatus(), is(204));
728
729         List<NotificationEvent> notificationEvents = notification.getEvents();
730
731         assertThat(notificationEvents.size(), is(2));
732
733         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
734                 "notification-dmaap-events/depth-zero/expected-notification-header-delete-edge-between-pserver-and-generic-vnf.json");
735         String expectedNotificationBody = PayloadUtil.getResourcePayload(
736                 "notification-dmaap-events/depth-zero/expected-notification-body-delete-edge-between-pserver-and-generic-vnf.json");
737
738         JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
739                 false);
740         JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);
741
742     }
743
744     @Test
745     public void testExistingPserverWithChildAndGenericVnfAndExistingEdgeBetweenThemAndDeleteEdgeAndCheckChildrenWhenNotificationDepthIsAll()
746             throws IOException, AAIException {
747
748         String hostname = "example-hostname-val-85598";
749
750         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
751         String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";
752
753         String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");
754
755         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
756         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
757
758         String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
759         String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");
760
761         Response response = httpTestUtil.doGet(pserverUri);
762         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
763
764         response = httpTestUtil.doPut(pserverUri, resource);
765         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
766
767         response = httpTestUtil.doGet(pserverUri);
768         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
769
770         response = httpTestUtil.doGet(genericVnfUri);
771         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
772
773         response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
774         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
775
776         response = httpTestUtil.doGet(genericVnfUri);
777         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
778         assertThat(response.getEntity().toString(), not(containsString(hostname)));
779
780         response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
781         assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());
782
783         response = httpTestUtil.doGet(genericVnfUri);
784
785         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
786         assertThat(response.getEntity().toString(), containsString(hostname));
787
788         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
789         assertThat(response.getEntity().toString(), containsString(hostname));
790
791         response = httpTestUtil.doGet(pserverUri);
792         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
793
794         JSONObject pserverJson = new JSONObject(response.getEntity().toString());
795         String resourceVersion = pserverJson.getString("resource-version");
796
797         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
798
799         response = httpTestUtil.doDelete(pserverUri + "/relationship-list/relationship", resourceVersion, relationship);
800         assertThat("Expected the pserver relationship to generic-vnf to be deleted", response.getStatus(), is(204));
801
802         List<NotificationEvent> notificationEvents = notification.getEvents();
803         assertThat(notificationEvents.size(), is(2));
804
805         String expectedNotificationHeader = PayloadUtil.getResourcePayload(
806                 "notification-dmaap-events/depth-all/expected-notification-header-delete-edge-between-pserver-and-generic-vnf.json");
807         String expectedNotificationBody = PayloadUtil.getResourcePayload(
808                 "notification-dmaap-events/depth-all/expected-notification-body-delete-edge-between-pserver-and-generic-vnf.json");
809
810         JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
811                 false);
812         JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);
813
814     }
815
816     @Test
817     public void testDeleteOnExistingResourceVersionMismatchNoEventGeneratedFullDepth()
818             throws IOException, AAIException {
819         String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
820         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
821         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
822
823         String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
824
825         Response response = httpTestUtil.doGet(uri);
826         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
827
828         response = httpTestUtil.doPut(uri, pserverResource);
829         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
830
831         response = httpTestUtil.doGet(uri, "all");
832         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
833
834         JSONObject pserverObject = new JSONObject(response.getEntity().toString());
835         String resourceVersion = pserverObject.getString("resource-version");
836
837         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
838         response = httpTestUtil.doDelete(uri, resourceVersion + "123");
839         assertEquals("Resource version mismatch exception", 412, response.getStatus());
840
841         List<NotificationEvent> notificationEvents = notification.getEvents();
842         assertThat(notificationEvents.size(), is(0));
843     }
844
845     @Test
846     public void testCreateVnfWithChildrenCreateCustomerWithChildrenAndCousinBetweenVlanAndServiceInstanceThenDeleteCustomerVerifyingVlanRV()
847             throws IOException, AAIException {
848         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
849         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
850
851         JsonObject paylaods = new JsonParser().parse(PayloadUtil.getResourcePayload(
852                 "customer_with_children_and_generic-vnf_with_children_and_edge_between_service-instance_vlan.json"))
853                 .getAsJsonObject();
854         String gvnfPaylaod = paylaods.get("generic-vnf").toString();
855         String custPaylaod = paylaods.get("customer").toString();
856         String gvnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf";
857         String custUri = "/aai/v14/business/customers/customer/cust";
858         String vlanUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf/l-interfaces/l-interface/lint/vlans/vlan/vlan";
859
860         // Setup generic vnf
861         Response response = httpTestUtil.doGet(gvnfUri);
862         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
863         response = httpTestUtil.doPut(gvnfUri, gvnfPaylaod);
864         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
865         response = httpTestUtil.doGet(gvnfUri, "all");
866         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
867         response = httpTestUtil.doGet(vlanUri, "all");
868         assertEquals("Expecting the vlan to be found", 200, response.getStatus());
869         String vlanResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");
870
871         // Setup customer with service instance relation to vlan
872         response = httpTestUtil.doGet(custUri);
873         assertEquals("Expecting the customer to be not found", 404, response.getStatus());
874         response = httpTestUtil.doPut(custUri, custPaylaod);
875         assertEquals("Expecting the customer to be created", 201, response.getStatus());
876         response = httpTestUtil.doGet(custUri, "all");
877         assertEquals("Expecting the customer to be found", 200, response.getStatus());
878         String custResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");
879
880         // Verify vlan rv was updated
881         response = httpTestUtil.doGet(vlanUri, "all");
882         assertEquals("Expecting the vlan to be found", 200, response.getStatus());
883         String vlanResourceVersionAfterCustPut =
884                 new JSONObject(response.getEntity().toString()).getString("resource-version");
885         assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterCustPut,
886                 not(is(vlanResourceVersion)));
887
888         // Delete customer
889         notification.clearEvents();
890         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
891         response = httpTestUtil.doDelete(custUri, custResourceVersion);
892         assertEquals("Expecting customer to be deleted", 204, response.getStatus());
893
894         // Verify vlan rv was updated
895         response = httpTestUtil.doGet(vlanUri, "all");
896         assertEquals("Expecting the vlan to be found", 200, response.getStatus());
897         String vlanResourceVersionAfterDelete =
898                 new JSONObject(response.getEntity().toString()).getString("resource-version");
899         assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterDelete,
900                 not(is(vlanResourceVersionAfterCustPut)));
901
902         List<NotificationEvent> notificationEvents = notification.getEvents();
903         assertThat("Expect the delete to generate 4 events customer, its children and vlan", notificationEvents.size(),
904                 is(4));
905     }
906
907     @Test
908     public void testBulkCreateOfComplexAndPserverWithRelationshipThenBulkDeleteBoth() throws IOException, AAIException {
909         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
910         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
911
912         JsonObject paylaods = new JsonParser()
913                 .parse(PayloadUtil.getResourcePayload("complex_pserver_with_relation.json")).getAsJsonObject();
914         String complexPaylaod = paylaods.get("complex").toString();
915         String pserverPaylaod = paylaods.get("pserver").toString();
916         String complexUri = "/aai/v14/cloud-infrastructure/complexes/complex/complex-1";
917         String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/pserver-1";
918
919         Response response = httpTestUtil.doGet(complexUri);
920         assertEquals("Expecting the complex to be not found", 404, response.getStatus());
921         response = httpTestUtil.doGet(pserverUri);
922         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
923
924         Map<String, String> puts = new LinkedHashMap<>();
925         puts.put(complexUri, complexPaylaod);
926         puts.put(pserverUri, pserverPaylaod);
927
928         response = httpTestUtil.doPut(puts);
929         assertEquals("Expecting the puts request to succeed", 201, response.getStatus());
930         assertEquals("Expect 2 messages to be created", 2, notification.getEvents().size());
931         response = httpTestUtil.doGet(complexUri, "all");
932         assertEquals("Expecting the complex to be found", 200, response.getStatus());
933         String complexRV = new JSONObject(response.getEntity().toString()).getString("resource-version");
934         response = httpTestUtil.doGet(pserverUri, "all");
935         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
936         String pserverRv = new JSONObject(response.getEntity().toString()).getString("resource-version");
937         assertThat("Resource versions match", complexRV, is(pserverRv));
938
939         Map<String, Pair<String, String>> deletes = new LinkedHashMap<>();
940         deletes.put(pserverUri, new Pair<>(pserverRv, null));
941         deletes.put(complexUri, new Pair<>(complexRV, null));
942         notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
943         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
944         httpTestUtil.doDelete(deletes);
945
946         response = httpTestUtil.doGet(complexUri);
947         assertEquals("Expecting the complex to be not found", 404, response.getStatus());
948         response = httpTestUtil.doGet(pserverUri);
949         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
950     }
951
952     @Test
953     public void testCreateVnfWithChildrenCreateCustomerWithChildrenAndCousinBetweenVlanAndServiceInstanceThenImplicitDeleteVlanVerifyingServiceInstanceRV()
954             throws IOException, AAIException {
955         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
956         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
957
958         JsonObject paylaods = new JsonParser().parse(PayloadUtil.getResourcePayload(
959                 "customer_with_children_and_generic-vnf_with_children_and_edge_between_service-instance_vlan.json"))
960                 .getAsJsonObject();
961         String gvnfPaylaod = paylaods.get("generic-vnf").toString();
962         String custPaylaod = paylaods.get("customer").toString();
963         String custUri = "/aai/v14/business/customers/customer/cust";
964         String ssUri = custUri + "/service-subscriptions/service-subscription/ss";
965         String siUri = ssUri + "/service-instances/service-instance/si";
966         String gvnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf";
967         String lintUri = gvnfUri + "/l-interfaces/l-interface/lint";
968         String vlanUri = lintUri + "/vlans/vlan/vlan";
969
970         // Setup generic vnf
971         Response response = httpTestUtil.doGet(gvnfUri);
972         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
973         response = httpTestUtil.doPut(gvnfUri, gvnfPaylaod);
974         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
975         response = httpTestUtil.doGet(gvnfUri, "all");
976         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
977         response = httpTestUtil.doGet(vlanUri, "all");
978         assertEquals("Expecting the vlan to be found", 200, response.getStatus());
979         String vlanResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");
980
981         // Setup customer with service instance relation to vlan
982         response = httpTestUtil.doGet(custUri);
983         assertEquals("Expecting the customer to be not found", 404, response.getStatus());
984         response = httpTestUtil.doPut(custUri, custPaylaod);
985         assertEquals("Expecting the customer to be created", 201, response.getStatus());
986         response = httpTestUtil.doGet(custUri, "all");
987         assertEquals("Expecting the customer to be found", 200, response.getStatus());
988         response = httpTestUtil.doGet(siUri, "all");
989         assertEquals("Expecting the service-instance to be found", 200, response.getStatus());
990         String serviceInstanceResourceVersion =
991                 new JSONObject(response.getEntity().toString()).getString("resource-version");
992
993         // Verify vlan rv was updated
994         response = httpTestUtil.doGet(vlanUri, "all");
995         assertEquals("Expecting the vlan to be found", 200, response.getStatus());
996         String vlanResourceVersionAfterCustPut =
997                 new JSONObject(response.getEntity().toString()).getString("resource-version");
998         assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterCustPut,
999                 not(is(vlanResourceVersion)));
1000
1001         // Get linterface, replace vlans with empty json (implicit delete) and put triggering implicit delete
1002         response = httpTestUtil.doGet(lintUri, "all");
1003         assertEquals("Expecting the l-interface to be found", 200, response.getStatus());
1004         JSONObject lintJson = new JSONObject(response.getEntity().toString());
1005         lintJson.put("vlans", new JsonObject());
1006         notification.clearEvents();
1007         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
1008         response = httpTestUtil.doPut(lintUri, lintJson.toString());
1009         assertEquals("Expecting the l-interface to be updated", 200, response.getStatus());
1010
1011         List<NotificationEvent> notificationEvents = notification.getEvents();
1012         assertThat("Expect the implied delete to generate 2", notificationEvents.size(), is(2));
1013
1014         // Verify vlan is no longer there anf get service-instance and compare rv
1015         response = httpTestUtil.doGet(vlanUri, "all");
1016         assertEquals("Expecting the vlan not to be found", 404, response.getStatus());
1017         response = httpTestUtil.doGet(siUri, "all");
1018         assertEquals("Expecting the service-instance to be found", 200, response.getStatus());
1019         String serviceInstanceResourceVersionAfterImplicitDelete =
1020                 new JSONObject(response.getEntity().toString()).getString("resource-version");
1021         assertThat("Expecting the service-instance resource version to be updated after implicit delete of vlan",
1022                 serviceInstanceResourceVersionAfterImplicitDelete, not(is(serviceInstanceResourceVersion)));
1023     }
1024
1025     @After
1026     public void teardown() {
1027
1028         JanusGraph janusGraph = AAIGraph.getInstance().getGraph();
1029         JanusGraphTransaction transaction = janusGraph.newTransaction();
1030
1031         GraphTraversalSource g = transaction.traversal();
1032
1033         g.V().has(AAIProperties.SOURCE_OF_TRUTH, "JUNIT").forEachRemaining(Vertex::remove);
1034
1035         transaction.commit();
1036     }
1037 }