Reduce the number of problems in aai-common by removing unused imports
[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 static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.core.Is.is;
25 import static org.hamcrest.core.IsNot.not;
26 import static org.junit.Assert.*;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.stream.Collectors;
32
33 import javax.ws.rs.core.Response;
34
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.janusgraph.core.JanusGraphTransaction;
37 import org.json.JSONObject;
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.mockito.Mockito;
43 import org.onap.aai.AAISetup;
44 import org.onap.aai.HttpTestUtil;
45 import org.onap.aai.PayloadUtil;
46 import org.onap.aai.db.props.AAIProperties;
47 import org.onap.aai.dbmap.AAIGraph;
48 import org.onap.aai.introspection.ModelType;
49 import org.onap.aai.rest.ueb.NotificationEvent;
50 import org.onap.aai.rest.ueb.UEBNotification;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.skyscreamer.jsonassert.JSONAssert;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.test.annotation.DirtiesContext;
56
57 @RunWith(value = Parameterized.class)
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class ImpliedDeleteIntegrationTest extends AAISetup {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(ImpliedDeleteIntegrationTest.class);
62
63     @Parameterized.Parameter(value = 0)
64     public QueryStyle queryStyle;
65
66     @Parameterized.Parameters(name = "QueryStyle.{0}")
67     public static Collection<Object[]> data() {
68         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
69     }
70
71     @Test
72     public void testPutPserverWithMultiplePInterfaceChildrenAndDoPutWithZeroChildren() throws Exception {
73
74         String uri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-implied-delete";
75
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-implied-delete.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         response = httpTestUtil.doGet(uri);
88         assertEquals("Expecting the pserver to be found", 200, response.getStatus());
89
90         JSONObject jsonObject = new JSONObject(response.getEntity().toString());
91         JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
92         jsonObject.getJSONObject("p-interfaces").remove("p-interface");
93
94         notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
95         httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
96
97         response = httpTestUtil.doPut(uri, jsonObject.toString());
98         assertEquals("Expecting the pserver to be updated and delete children", 200, response.getStatus());
99
100         List<NotificationEvent> notificationEvents = notification.getEvents();
101         assertThat(notificationEvents.size(), is(5));
102
103         List<String> notificationEventHeaders = notification.getEvents().stream()
104                 .map(event -> event.getEventHeader().marshal(false)).collect(Collectors.toList());
105
106         Long deletedEventsCount = notificationEventHeaders.stream().filter(e -> e.contains("\"DELETE\"")).count();
107
108         assertThat(deletedEventsCount, is(4L));
109
110         response = httpTestUtil.doGet(uri);
111         assertThat(response.getEntity().toString(), not(containsString("p-interface")));
112     }
113
114     @Test
115     public void testPutGenericVnf() throws Exception {
116
117         String uri = "/aai/v12/network/generic-vnfs/generic-vnf/generic-vnf-implied-delete";
118         HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
119
120         String resource = PayloadUtil.getResourcePayload("generic-vnf-implied-delete.json");
121
122         Response response = httpTestUtil.doGet(uri);
123         assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
124
125         response = httpTestUtil.doPut(uri, resource);
126         assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
127
128         response = httpTestUtil.doGet(uri);
129         assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
130
131         JSONObject jsonObject = new JSONObject(response.getEntity().toString());
132         JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
133         jsonObject.getJSONObject("vf-modules").remove("vf-module");
134
135         response = httpTestUtil.doPut(uri, jsonObject.toString());
136         assertEquals("Expecting the generic-vnf to be not deleted and fail with 403", 403, response.getStatus());
137         assertThat(response.getEntity().toString(), containsString("User is not allowed to perform implicit delete"));
138     }
139
140     @After
141     public void tearDown() {
142
143         JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
144         boolean success = true;
145
146         try {
147
148             GraphTraversalSource g = transaction.traversal();
149
150             g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
151
152         } catch (Exception ex) {
153             success = false;
154             LOGGER.error("Unable to remove the vertexes", ex);
155         } finally {
156             if (success) {
157                 transaction.commit();
158             } else {
159                 transaction.rollback();
160                 fail("Unable to teardown the graph");
161             }
162         }
163     }
164
165 }