aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java
blob: 399ef7e0fc6d4643fa52583ef203111f321e6393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.rest;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.junit.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphTransaction;
import org.javatuples.Pair;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mockito;
import org.onap.aai.AAISetup;
import org.onap.aai.HttpTestUtil;
import org.onap.aai.PayloadUtil;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dbmap.AAIGraph;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.rest.ueb.NotificationEvent;
import org.onap.aai.rest.ueb.UEBNotification;
import org.onap.aai.serialization.engines.QueryStyle;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.test.annotation.DirtiesContext;

@RunWith(value = Parameterized.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class NotificationDmaapEventTest extends AAISetup {

    @Parameterized.Parameter
    public QueryStyle queryStyle;

    @Parameterized.Parameters(name = "QueryStyle.{0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
    }

    @Test
    public void testCreateWithPserverWithAllChildrenAndVerifyMultipleNotificationsWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        int expectedCreateEvents = 17;

        assertThat(notification.getEvents().size(), is(expectedCreateEvents));

        // Verify all the events are create since its a new PUT
        notification.getEvents().forEach((event) -> {

            String header = event.getEventHeader().marshal(false);

            assertThat(event.getEventHeader().marshal(false), containsString("\"CREATE\""));

            assertThat(header, containsString("\"top-entity-type\":\"pserver\""));

        });

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());
    }

    // Test existing pserver create new pinterface check dmaap event for pinterface is CREATE
    @Test
    public void testExistingPserverCreateNewChildPInterfaceAndCheckDmaapEventForPInterfaceIsCreateWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, pserverResource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());
        notification.clearEvents();

        response = httpTestUtil.doGet(uri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverJson = new JSONObject(response.getEntity().toString());
        JSONObject pInterfaceObject = new JSONObject();
        pInterfaceObject.put("interface-name", "p-interface-1");

        pserverJson.getJSONObject("p-interfaces").getJSONArray("p-interface").put(pInterfaceObject);

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
        response = httpTestUtil.doPut(uri, pserverJson.toString());
        assertEquals("Expecting the pserver to be updated with a new p-interface", 200, response.getStatus());

        response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/p-interface-1", "0");
        assertEquals("Expecting the p-interface to be found", 200, response.getStatus());

        List<NotificationEvent> events = notification.getEvents();
        assertThat(events.size(), is(2));

        String notificationEventHeader = events.get(1).getEventHeader().marshal(false);
        String notificationEventBody = events.get(1).getObj().marshal(false);

        assertThat(notificationEventHeader, containsString("\"action\":\"CREATE\""));
        assertThat(notificationEventHeader, containsString("\"entity-type\":\"p-interface\""));
        assertThat(notificationEventHeader, containsString("\"top-entity-type\":\"pserver\""));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-header-create-child-on-existing-obj.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-body-create-child-on-existing-obj.json");

        JSONAssert.assertEquals(expectedNotificationHeader, notificationEventHeader, false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEventBody, false);
    }

    @Test
    public void testExistingPserverCreateNewChildPInterfaceAndCheckDmaapEventForPserverIsSentWithNewPInterfaceWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, pserverResource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverJson = new JSONObject(response.getEntity().toString());
        String pserverResourceVersion = pserverJson.getString("resource-version");

        JSONObject pInterfaceObject = new JSONObject();
        pInterfaceObject.put("interface-name", "p-interface-1");

        pserverJson.getJSONObject("p-interfaces").getJSONArray("p-interface").put(pInterfaceObject);

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doPut(uri, pserverJson.toString());
        assertEquals("Expecting the pserver to be updated with a new p-interface", 200, response.getStatus());

        response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/p-interface-1", "0");
        assertEquals("Expecting the p-interface to be found", 200, response.getStatus());

        List<NotificationEvent> events = notification.getEvents();
        assertThat(events.size(), is(1));

        String notificationEventHeader = events.get(0).getEventHeader().marshal(false);
        String notificationEventBody = events.get(0).getObj().marshal(false);

        assertThat(notificationEventHeader, containsString("\"action\":\"UPDATE\""));
        assertThat(notificationEventHeader, containsString("\"entity-type\":\"pserver\""));
        assertThat(notificationEventHeader, containsString("\"top-entity-type\":\"pserver\""));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-header-create-child-on-existing-obj.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-body-create-child-on-existing-obj.json");

        JSONAssert.assertEquals(expectedNotificationHeader, notificationEventHeader, false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEventBody, false);

        response = httpTestUtil.doGet(uri, "0");
        pserverJson = new JSONObject(response.getEntity().toString());
        String newPserverResourceVersion = pserverJson.getString("resource-version");

        // After an pserver's p-interface is updated on the pserver, even though
        // the pserver nothing changed, expecting the pserver resource version to be changed
        assertThat("Expecting the new pserver resource version and old resource version to be not same",
                newPserverResourceVersion, is(not(pserverResourceVersion)));
        assertEquals("Expecting the p-interface to be found", 200, response.getStatus());
    }

    // Test Bulk Scenario
    @Test
    public void testBulkScenarioWhereMultipleCreatesAndEnsureNoDuplicationInDmaapEventsWhenNotificationDepthIsZero()
            throws UnsupportedEncodingException, AAIException {

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/random-pserver";
        String cloudRegionUri =
                "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/random-cloud-region-owner/random-cloud-region-id";

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);

        Map<String, String> uriPayload = new LinkedHashMap<>();

        uriPayload.put(pserverUri, "{}");
        uriPayload.put(cloudRegionUri, "{}");

        Response response = httpTestUtil.doPut(uriPayload);
        assertThat(response.getStatus(), is(201));

        int numberOfEventsActual = notification.getEvents().size();
        int expectedEvents = 2;

        assertThat("Expecting the number of dmaap events to be 2", numberOfEventsActual, is(expectedEvents));

        notification.getEvents().forEach((event) -> {
            String notificationEventHeader = event.getEventHeader().marshal(false);
            assertThat(notificationEventHeader, containsString("\"CREATE\""));
        });
    }

    @Test
    public void testBulkScenarioWhereMultipleCreatesAndEnsureNoDuplicationInDmaapEventsWhenNotificationDepthIsAll()
            throws UnsupportedEncodingException, AAIException {

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/random-pserver";
        String cloudRegionUri =
                "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/random-cloud-region-owner/random-cloud-region-id";

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);

        Map<String, String> uriPayload = new LinkedHashMap<>();

        uriPayload.put(pserverUri, "{}");
        uriPayload.put(cloudRegionUri, "{}");

        Response response = httpTestUtil.doPut(uriPayload);
        assertThat(response.getStatus(), is(201));

        int numberOfEventsActual = notification.getEvents().size();
        int expectedEvents = 2;

        assertThat("Expecting the number of dmaap events to be 2", numberOfEventsActual, is(expectedEvents));

        notification.getEvents().forEach((event) -> {
            String notificationEventHeader = event.getEventHeader().marshal(false);
            assertThat(notificationEventHeader, containsString("\"CREATE\""));
        });
    }

    @Test
    public void testDeleteOnExistingPserverAndCheckIfNotificationDepthIsZeroThatAllEventsHaveDeleteAndThatDepthIsZeroOnEachNotificationEvent()
            throws IOException, AAIException {
        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, pserverResource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverObject = new JSONObject(response.getEntity().toString());
        String resourceVersion = pserverObject.getString("resource-version");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
        response = httpTestUtil.doDelete(uri, resourceVersion);
        assertEquals("Expecting the pserver to be deleted", 204, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(17));

        notificationEvents.forEach((event) -> {

            String header = event.getEventHeader().marshal(false);

            assertThat(event.getEventHeader().marshal(false), containsString("\"DELETE\""));

            assertThat(header, containsString("\"top-entity-type\":\"pserver\""));
        });
    }

    @Test
    public void testDeleteOnExistingResourceVersionMismatchNoEventGenerated() throws IOException, AAIException {
        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, pserverResource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverObject = new JSONObject(response.getEntity().toString());
        String resourceVersion = pserverObject.getString("resource-version");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
        response = httpTestUtil.doDelete(uri, resourceVersion + "123");
        assertEquals("Resource version mismatch exception", 412, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(0));
    }

    // Test notification depth set to all
    // Scenario for testing the creation of pserver with children, grandchildren
    // Default behaviour is for one event to be sent out
    // which includes all the children and grandchildren, etc
    @Test
    public void testCreateWithPserverWithAllChildrenAndVerifyOneNotificationWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        assertThat(notification.getEvents().size(), is(1));

        NotificationEvent notificationEvent = notification.getEvents().get(0);

        // Verify all the events are create since its a new PUT
        String header = notificationEvent.getEventHeader().marshal(false);

        assertThat(header, containsString("\"CREATE\""));

        assertThat(header, containsString("\"entity-type\":\"pserver\""));

        assertThat(header, containsString("\"top-entity-type\":\"pserver\""));

        assertThat(header, containsString("\"entity-link\":\"" + uri + "\""));

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONAssert.assertEquals(response.getEntity().toString(), notificationEvent.getObj().marshal(false), false);
    }

    @Test
    public void testPatchExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventNoChildrenWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverObject = new JSONObject();
        pserverObject.put("equip-type", "new-equip-patch-type");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
        response = httpTestUtil.doPatch(uri, pserverObject.toString());
        assertThat(response.getStatus(), is(200));

        response = httpTestUtil.doGet(uri, "0");
        assertThat(response.getEntity().toString(), containsString("new-equip-patch-type"));

        assertThat(notification.getEvents().size(), is(1));
        String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);

        // Check that everything in notification event is also response body
        // Not comparing the other way as notification only includes parents main properties
        JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
    }

    @Test
    public void testPatchExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventIncludeChildrenWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverObject = new JSONObject();
        pserverObject.put("equip-type", "new-equip-patch-type");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doPatch(uri, pserverObject.toString());
        assertThat(response.getStatus(), is(200));

        response = httpTestUtil.doGet(uri, "all");
        assertThat(response.getEntity().toString(), containsString("new-equip-patch-type"));

        assertThat(notification.getEvents().size(), is(1));
        String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);

        // Check that everything in notification event is also response body
        // Not comparing the other way as notification only includes parents main properties
        JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
    }

    // Test notification depth set to all
    // Scenario where we are only updating one field in p-interface
    // Make sure the parent and children are included
    @Test
    public void testUpdateExistingPserverWithChildrenAndModifyOnlyOneObjectAndVerifyThatOnlyOneNotificationEventIncludingChildrenWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/example-interface-name-val-46147", "0");
        assertEquals("Expecting the p-interface to be found", 200, response.getStatus());

        JSONObject pInterfaceObject = new JSONObject(response.getEntity().toString());
        pInterfaceObject.put("equipment-identifier", "new-equipment-identifier");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doPut(uri + "/p-interfaces/p-interface/example-interface-name-val-46147",
                pInterfaceObject.toString());
        assertThat(response.getStatus(), is(200));

        // Get the parent uri as the notification event json has parent structure it makes it easy to compare
        response = httpTestUtil.doGet(uri);
        assertThat(response.getEntity().toString(), containsString("new-equipment-identifier"));

        assertThat(notification.getEvents().size(), is(1));
        String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);

        // Check that everything in notification event is also response body
        // Not comparing the other way as notification only includes parents main properties
        JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
    }

    // Test notification depth set to 0
    // Scenario where we are only updating one field in p-interface
    @Test
    public void testUpdateExistingPserverWithChildrenAndModifyOnlyPInterfaceAndVerifyThatOnlyOneNotificationForPInterfaceIsCreatedWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(uri + "/p-interfaces/p-interface/example-interface-name-val-46147", "0");
        assertEquals("Expecting the p-interface to be found", 200, response.getStatus());

        JSONObject pInterfaceObject = new JSONObject(response.getEntity().toString());
        pInterfaceObject.put("equipment-identifier", "new-equipment-identifier");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
        response = httpTestUtil.doPut(uri + "/p-interfaces/p-interface/example-interface-name-val-46147",
                pInterfaceObject.toString());
        assertThat(response.getStatus(), is(200));

        response = httpTestUtil.doGet(uri);
        assertThat(notification.getEvents().size(), is(1));
        String updateNotificationEvent = notification.getEvents().get(0).getObj().marshal(true);
        System.out.println("Update notification " + updateNotificationEvent);

        // Check that everything in notification event is also response body
        // Not comparing the other way as notification only includes parents main properties
        JSONAssert.assertEquals(updateNotificationEvent, response.getEntity().toString(), false);
    }

    @Test
    public void testExistingPserverWithChildAndGenericVnfAndCreateEdgeBetweenThemAndCheckNoChildWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String hostname = "example-hostname-val-85598";

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
        String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
        String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");

        Response response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(pserverUri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), not(containsString(hostname)));

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);

        String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");

        response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
        assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(2));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-header-create-edge-between-pserver-and-generic-vnf.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-body-create-edge-between-pserver-and-generic-vnf.json");

        JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
                false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);

        response = httpTestUtil.doGet(genericVnfUri);

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));
    }

    @Test
    public void testExistingPserverWithChildAndGenericVnfAndCreateEdgeBetweenThemAndCheckChildrenIncludedWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String hostname = "example-hostname-val-85598";

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
        String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
        String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");

        Response response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(pserverUri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), not(containsString(hostname)));

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);

        String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");

        response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
        assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(2));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-header-create-edge-between-pserver-and-generic-vnf.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-body-create-edge-between-pserver-and-generic-vnf.json");

        System.out.println("Notification Body: " + notificationEvents.get(0).getObj().marshal(false));
        JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
                false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);

        response = httpTestUtil.doGet(genericVnfUri);

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));
    }

    @Test
    public void testExistingPserverWithChildAndGenericVnfAndExistingEdgeBetweenThemAndDeleteEdgeAndCheckNoChildWhenNotificationDepthIsZero()
            throws IOException, AAIException {

        String hostname = "example-hostname-val-85598";

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
        String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";

        String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
        String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");

        Response response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(pserverUri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), not(containsString(hostname)));

        response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
        assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverJson = new JSONObject(response.getEntity().toString());
        String resourceVersion = pserverJson.getString("resource-version");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);

        response = httpTestUtil.doDelete(pserverUri + "/relationship-list/relationship", resourceVersion, relationship);
        assertThat("Expected the pserver relationship to generic-vnf to be deleted", response.getStatus(), is(204));

        List<NotificationEvent> notificationEvents = notification.getEvents();

        assertThat(notificationEvents.size(), is(2));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-header-delete-edge-between-pserver-and-generic-vnf.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-zero/expected-notification-body-delete-edge-between-pserver-and-generic-vnf.json");

        JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
                false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);

    }

    @Test
    public void testExistingPserverWithChildAndGenericVnfAndExistingEdgeBetweenThemAndDeleteEdgeAndCheckChildrenWhenNotificationDepthIsAll()
            throws IOException, AAIException {

        String hostname = "example-hostname-val-85598";

        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/" + hostname;
        String genericVnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/generic-vnf-notification";

        String relationship = PayloadUtil.getResourcePayload("pserver-to-gvnf-relationship-notification.json");

        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String resource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");
        String genericVnfResource = PayloadUtil.getResourcePayload("generic-vnf-notification.json");

        Response response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(pserverUri, resource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(genericVnfUri, genericVnfResource);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), not(containsString(hostname)));

        response = httpTestUtil.doPut(pserverUri + "/relationship-list/relationship", relationship);
        assertEquals("Expecting the pserver to generic-vnf relationship to be created", 200, response.getStatus());

        response = httpTestUtil.doGet(genericVnfUri);

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));

        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        assertThat(response.getEntity().toString(), containsString(hostname));

        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverJson = new JSONObject(response.getEntity().toString());
        String resourceVersion = pserverJson.getString("resource-version");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);

        response = httpTestUtil.doDelete(pserverUri + "/relationship-list/relationship", resourceVersion, relationship);
        assertThat("Expected the pserver relationship to generic-vnf to be deleted", response.getStatus(), is(204));

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(2));

        String expectedNotificationHeader = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-header-delete-edge-between-pserver-and-generic-vnf.json");
        String expectedNotificationBody = PayloadUtil.getResourcePayload(
                "notification-dmaap-events/depth-all/expected-notification-body-delete-edge-between-pserver-and-generic-vnf.json");

        JSONAssert.assertEquals(expectedNotificationHeader, notificationEvents.get(0).getEventHeader().marshal(false),
                false);
        JSONAssert.assertEquals(expectedNotificationBody, notificationEvents.get(0).getObj().marshal(false), false);

    }

    @Test
    public void testDeleteOnExistingResourceVersionMismatchNoEventGeneratedFullDepth()
            throws IOException, AAIException {
        String uri = "/aai/v14/cloud-infrastructure/pservers/pserver/example-hostname-val-85598";
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String pserverResource = PayloadUtil.getResourcePayload("pserver-with-children-for-notification.json");

        Response response = httpTestUtil.doGet(uri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        response = httpTestUtil.doPut(uri, pserverResource);
        assertEquals("Expecting the pserver to be created", 201, response.getStatus());

        response = httpTestUtil.doGet(uri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());

        JSONObject pserverObject = new JSONObject(response.getEntity().toString());
        String resourceVersion = pserverObject.getString("resource-version");

        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doDelete(uri, resourceVersion + "123");
        assertEquals("Resource version mismatch exception", 412, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat(notificationEvents.size(), is(0));
    }

    @Test
    public void testCreateVnfWithChildrenCreateCustomerWithChildrenAndCousinBetweenVlanAndServiceInstanceThenDeleteCustomerVerifyingVlanRV()
            throws IOException, AAIException {
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String json = PayloadUtil.getResourcePayload(
                "customer_with_children_and_generic-vnf_with_children_and_edge_between_service-instance_vlan.json");
        JsonObject payloads = JsonParser.parseString(json).getAsJsonObject();
        String gvnfPaylaod = payloads.get("generic-vnf").toString();
        String custPaylaod = payloads.get("customer").toString();
        String gvnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf";
        String custUri = "/aai/v14/business/customers/customer/cust";
        String vlanUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf/l-interfaces/l-interface/lint/vlans/vlan/vlan";

        // Setup generic vnf
        Response response = httpTestUtil.doGet(gvnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
        response = httpTestUtil.doPut(gvnfUri, gvnfPaylaod);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
        response = httpTestUtil.doGet(gvnfUri, "all");
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan to be found", 200, response.getStatus());
        String vlanResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");

        // Setup customer with service instance relation to vlan
        response = httpTestUtil.doGet(custUri);
        assertEquals("Expecting the customer to be not found", 404, response.getStatus());
        response = httpTestUtil.doPut(custUri, custPaylaod);
        assertEquals("Expecting the customer to be created", 201, response.getStatus());
        response = httpTestUtil.doGet(custUri, "all");
        assertEquals("Expecting the customer to be found", 200, response.getStatus());
        String custResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");

        // Verify vlan rv was updated
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan to be found", 200, response.getStatus());
        String vlanResourceVersionAfterCustPut =
                new JSONObject(response.getEntity().toString()).getString("resource-version");
        assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterCustPut,
                not(is(vlanResourceVersion)));

        // Delete customer
        notification.clearEvents();
        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doDelete(custUri, custResourceVersion);
        assertEquals("Expecting customer to be deleted", 204, response.getStatus());

        // Verify vlan rv was updated
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan to be found", 200, response.getStatus());
        String vlanResourceVersionAfterDelete =
                new JSONObject(response.getEntity().toString()).getString("resource-version");
        assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterDelete,
                not(is(vlanResourceVersionAfterCustPut)));

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat("Expect the delete to generate 4 events customer, its children and vlan", notificationEvents.size(),
                is(4));
    }

    @Test
    public void testBulkCreateOfComplexAndPserverWithRelationshipThenBulkDeleteBoth() throws IOException, AAIException {
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);

        JsonObject payloads = JsonParser
                .parseString(PayloadUtil.getResourcePayload("complex_pserver_with_relation.json")).getAsJsonObject();
        String complexPaylaod = payloads.get("complex").toString();
        String pserverPaylaod = payloads.get("pserver").toString();
        String complexUri = "/aai/v14/cloud-infrastructure/complexes/complex/complex-1";
        String pserverUri = "/aai/v14/cloud-infrastructure/pservers/pserver/pserver-1";

        Response response = httpTestUtil.doGet(complexUri);
        assertEquals("Expecting the complex to be not found", 404, response.getStatus());
        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());

        Map<String, String> puts = new LinkedHashMap<>();
        puts.put(complexUri, complexPaylaod);
        puts.put(pserverUri, pserverPaylaod);

        response = httpTestUtil.doPut(puts);
        assertEquals("Expecting the puts request to succeed", 201, response.getStatus());
        assertEquals("Expect 2 messages to be created", 2, notification.getEvents().size());
        response = httpTestUtil.doGet(complexUri, "all");
        assertEquals("Expecting the complex to be found", 200, response.getStatus());
        String complexRV = new JSONObject(response.getEntity().toString()).getString("resource-version");
        response = httpTestUtil.doGet(pserverUri, "all");
        assertEquals("Expecting the pserver to be found", 200, response.getStatus());
        String pserverRv = new JSONObject(response.getEntity().toString()).getString("resource-version");
        assertThat("Resource versions match", complexRV, is(pserverRv));

        Map<String, Pair<String, String>> deletes = new LinkedHashMap<>();
        deletes.put(pserverUri, new Pair<>(pserverRv, null));
        deletes.put(complexUri, new Pair<>(complexRV, null));
        notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        httpTestUtil.doDelete(deletes);

        response = httpTestUtil.doGet(complexUri);
        assertEquals("Expecting the complex to be not found", 404, response.getStatus());
        response = httpTestUtil.doGet(pserverUri);
        assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
    }

    @Test
    public void testCreateVnfWithChildrenCreateCustomerWithChildrenAndCousinBetweenVlanAndServiceInstanceThenImplicitDeleteVlanVerifyingServiceInstanceRV()
            throws IOException, AAIException {
        UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
        HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);

        String json = PayloadUtil.getResourcePayload(
                "customer_with_children_and_generic-vnf_with_children_and_edge_between_service-instance_vlan.json");
        JsonObject payloads = JsonParser.parseString(json).getAsJsonObject();
        String gvnfPayload = payloads.get("generic-vnf").toString();
        String custPayload = payloads.get("customer").toString();
        String custUri = "/aai/v14/business/customers/customer/cust";
        String ssUri = custUri + "/service-subscriptions/service-subscription/ss";
        String siUri = ssUri + "/service-instances/service-instance/si";
        String gvnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/gvnf";
        String lintUri = gvnfUri + "/l-interfaces/l-interface/lint";
        String vlanUri = lintUri + "/vlans/vlan/vlan";

        // Setup generic vnf
        Response response = httpTestUtil.doGet(gvnfUri);
        assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
        response = httpTestUtil.doPut(gvnfUri, gvnfPayload);
        assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
        response = httpTestUtil.doGet(gvnfUri, "all");
        assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan to be found", 200, response.getStatus());
        String vlanResourceVersion = new JSONObject(response.getEntity().toString()).getString("resource-version");

        // Setup customer with service instance relation to vlan
        response = httpTestUtil.doGet(custUri);
        assertEquals("Expecting the customer to be not found", 404, response.getStatus());
        response = httpTestUtil.doPut(custUri, custPayload);
        assertEquals("Expecting the customer to be created", 201, response.getStatus());
        response = httpTestUtil.doGet(custUri, "all");
        assertEquals("Expecting the customer to be found", 200, response.getStatus());
        response = httpTestUtil.doGet(siUri, "all");
        assertEquals("Expecting the service-instance to be found", 200, response.getStatus());
        String serviceInstanceResourceVersion =
                new JSONObject(response.getEntity().toString()).getString("resource-version");

        // Verify vlan rv was updated
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan to be found", 200, response.getStatus());
        String vlanResourceVersionAfterCustPut =
                new JSONObject(response.getEntity().toString()).getString("resource-version");
        assertThat("Expecting the vlan resource version to be updated", vlanResourceVersionAfterCustPut,
                not(is(vlanResourceVersion)));

        // Get linterface, replace vlans with empty json (implicit delete) and put triggering implicit delete
        response = httpTestUtil.doGet(lintUri, "all");
        assertEquals("Expecting the l-interface to be found", 200, response.getStatus());
        JSONObject lintJson = new JSONObject(response.getEntity().toString());
        lintJson.put("vlans", new JsonObject());
        notification.clearEvents();
        httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MAXIMUM_DEPTH);
        response = httpTestUtil.doPut(lintUri, lintJson.toString());
        assertEquals("Expecting the l-interface to be updated", 200, response.getStatus());

        List<NotificationEvent> notificationEvents = notification.getEvents();
        assertThat("Expect the implied delete to generate 2", notificationEvents.size(), is(2));

        // Verify vlan is no longer there anf get service-instance and compare rv
        response = httpTestUtil.doGet(vlanUri, "all");
        assertEquals("Expecting the vlan not to be found", 404, response.getStatus());
        response = httpTestUtil.doGet(siUri, "all");
        assertEquals("Expecting the service-instance to be found", 200, response.getStatus());
        String serviceInstanceResourceVersionAfterImplicitDelete =
                new JSONObject(response.getEntity().toString()).getString("resource-version");
        assertThat("Expecting the service-instance resource version to be updated after implicit delete of vlan",
                serviceInstanceResourceVersionAfterImplicitDelete, not(is(serviceInstanceResourceVersion)));
    }

    @After
    public void teardown() {

        JanusGraph janusGraph = AAIGraph.getInstance().getGraph();
        JanusGraphTransaction transaction = janusGraph.newTransaction();

        GraphTraversalSource g = transaction.traversal();

        g.V().has(AAIProperties.SOURCE_OF_TRUTH, "JUNIT").forEachRemaining(Vertex::remove);

        transaction.commit();
    }
}