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