Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / ImpliedDeleteIntegrationTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.rest;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.janusgraph.core.JanusGraphTransaction;
27 import org.json.JSONObject;
28 import org.junit.After;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 import org.mockito.Mockito;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.HttpTestUtil;
35 import org.onap.aai.PayloadUtil;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.dbmap.AAIGraph;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.rest.ueb.NotificationEvent;
40 import org.onap.aai.rest.ueb.UEBNotification;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.skyscreamer.jsonassert.JSONAssert;
43 import org.springframework.test.annotation.DirtiesContext;
44
45 import javax.ws.rs.core.Response;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.List;
49 import java.util.stream.Collectors;
50
51 import static org.hamcrest.CoreMatchers.containsString;
52 import static org.hamcrest.core.Is.is;
53 import static org.hamcrest.core.IsNot.not;
54 import static org.junit.Assert.*;
55
56 @RunWith(value = Parameterized.class)
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class ImpliedDeleteIntegrationTest extends AAISetup {
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(ImpliedDeleteIntegrationTest.class);
61
62     @Parameterized.Parameter(value = 0)
63     public QueryStyle queryStyle;
64
65     @Parameterized.Parameters(name = "QueryStyle.{0}")
66     public static Collection<Object[]> data() {
67         return Arrays.asList(new Object[][] {
68             { QueryStyle.TRAVERSAL },
69             { QueryStyle.TRAVERSAL_URI }
70         });
71     }
72
73     @Test
74     public void testPutPserverWithMultiplePInterfaceChildrenAndDoPutWithZeroChildren() throws Exception {
75
76         String uri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-implied-delete";
77
78         UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
79         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
80
81         String resource = PayloadUtil.getResourcePayload("pserver-implied-delete.json");
82
83         Response response = httpTestUtil.doGet(uri);
84         assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
85
86         response = httpTestUtil.doPut(uri, resource);
87         assertEquals("Expecting the pserver to be created", 201, response.getStatus());
88
89         response = httpTestUtil.doGet(uri);
90         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
91
92         JSONObject jsonObject = new JSONObject(response.getEntity().toString());
93         JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
94         jsonObject.getJSONObject("p-interfaces").remove("p-interface");
95
96         notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
97         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
98
99         response = httpTestUtil.doPut(uri, jsonObject.toString());
100         assertEquals("Expecting the pserver to be updated and delete children", 200, response.getStatus());
101
102         List<NotificationEvent> notificationEvents = notification.getEvents();
103         assertThat(notificationEvents.size(), is(5));
104
105         List<String> notificationEventHeaders = notification.getEvents()
106             .stream()
107             .map(event -> event.getEventHeader().marshal(false))
108             .collect(Collectors.toList());
109
110         Long deletedEventsCount = notificationEventHeaders.stream().filter(e -> e.contains("\"DELETE\"")).count();
111
112         assertThat(deletedEventsCount, is(4L));
113
114         response = httpTestUtil.doGet(uri);
115         assertThat(response.getEntity().toString(), not(containsString("p-interface")));
116     }
117
118     @Test
119     public void testPutGenericVnf() throws Exception {
120
121         String uri = "/aai/v12/network/generic-vnfs/generic-vnf/generic-vnf-implied-delete";
122         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
123
124         String resource = PayloadUtil.getResourcePayload("generic-vnf-implied-delete.json");
125
126         Response response = httpTestUtil.doGet(uri);
127         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
128
129         response = httpTestUtil.doPut(uri, resource);
130         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
131
132         response = httpTestUtil.doGet(uri);
133         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
134
135         JSONObject jsonObject = new JSONObject(response.getEntity().toString());
136         JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
137         jsonObject.getJSONObject("vf-modules").remove("vf-module");
138
139         response = httpTestUtil.doPut(uri, jsonObject.toString());
140         assertEquals("Expecting the generic-vnf to be not deleted and fail with 403", 403, response.getStatus());
141         assertThat(response.getEntity().toString(), containsString("User is not allowed to perform implicit delete"));
142     }
143
144     @After
145     public void tearDown() {
146
147         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
148         boolean success = true;
149
150         try {
151
152             GraphTraversalSource g = transaction.traversal();
153
154             g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
155
156         } catch (Exception ex) {
157             success = false;
158             LOGGER.error("Unable to remove the vertexes", ex);
159         } finally {
160             if (success) {
161                 transaction.commit();
162             } else {
163                 transaction.rollback();
164                 fail("Unable to teardown the graph");
165             }
166         }
167     }
168
169 }