2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.rest;
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;
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;
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.*;
56 @RunWith(value = Parameterized.class)
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class ImpliedDeleteIntegrationTest extends AAISetup {
60 private static final Logger LOGGER = LoggerFactory.getLogger(ImpliedDeleteIntegrationTest.class);
62 @Parameterized.Parameter(value = 0)
63 public QueryStyle queryStyle;
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 }
74 public void testPutPserverWithMultiplePInterfaceChildrenAndDoPutWithZeroChildren() throws Exception {
76 String uri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-implied-delete";
78 UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
79 HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
81 String resource = PayloadUtil.getResourcePayload("pserver-implied-delete.json");
83 Response response = httpTestUtil.doGet(uri);
84 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
86 response = httpTestUtil.doPut(uri, resource);
87 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
89 response = httpTestUtil.doGet(uri);
90 assertEquals("Expecting the pserver to be found", 200, response.getStatus());
92 JSONObject jsonObject = new JSONObject(response.getEntity().toString());
93 JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
94 jsonObject.getJSONObject("p-interfaces").remove("p-interface");
96 notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
97 httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
99 response = httpTestUtil.doPut(uri, jsonObject.toString());
100 assertEquals("Expecting the pserver to be updated and delete children", 200, response.getStatus());
102 List<NotificationEvent> notificationEvents = notification.getEvents();
103 assertThat(notificationEvents.size(), is(5));
105 List<String> notificationEventHeaders = notification.getEvents()
107 .map(event -> event.getEventHeader().marshal(false))
108 .collect(Collectors.toList());
110 Long deletedEventsCount = notificationEventHeaders.stream().filter(e -> e.contains("\"DELETE\"")).count();
112 assertThat(deletedEventsCount, is(4L));
114 response = httpTestUtil.doGet(uri);
115 assertThat(response.getEntity().toString(), not(containsString("p-interface")));
119 public void testPutGenericVnf() throws Exception {
121 String uri = "/aai/v12/network/generic-vnfs/generic-vnf/generic-vnf-implied-delete";
122 HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
124 String resource = PayloadUtil.getResourcePayload("generic-vnf-implied-delete.json");
126 Response response = httpTestUtil.doGet(uri);
127 assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
129 response = httpTestUtil.doPut(uri, resource);
130 assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
132 response = httpTestUtil.doGet(uri);
133 assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
135 JSONObject jsonObject = new JSONObject(response.getEntity().toString());
136 JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
137 jsonObject.getJSONObject("vf-modules").remove("vf-module");
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"));
145 public void tearDown() {
147 JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
148 boolean success = true;
152 GraphTraversalSource g = transaction.traversal();
154 g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
156 } catch (Exception ex) {
158 LOGGER.error("Unable to remove the vertexes", ex);
161 transaction.commit();
163 transaction.rollback();
164 fail("Unable to teardown the graph");