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 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.*;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.stream.Collectors;
33 import javax.ws.rs.core.Response;
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;
57 @RunWith(value = Parameterized.class)
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class ImpliedDeleteIntegrationTest extends AAISetup {
61 private static final Logger LOGGER = LoggerFactory.getLogger(ImpliedDeleteIntegrationTest.class);
63 @Parameterized.Parameter(value = 0)
64 public QueryStyle queryStyle;
66 @Parameterized.Parameters(name = "QueryStyle.{0}")
67 public static Collection<Object[]> data() {
68 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
72 public void testPutPserverWithMultiplePInterfaceChildrenAndDoPutWithZeroChildren() throws Exception {
74 String uri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver-implied-delete";
76 UEBNotification notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
77 HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
79 String resource = PayloadUtil.getResourcePayload("pserver-implied-delete.json");
81 Response response = httpTestUtil.doGet(uri);
82 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
84 response = httpTestUtil.doPut(uri, resource);
85 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
87 response = httpTestUtil.doGet(uri);
88 assertEquals("Expecting the pserver to be found", 200, response.getStatus());
90 JSONObject jsonObject = new JSONObject(response.getEntity().toString());
91 JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
92 jsonObject.getJSONObject("p-interfaces").remove("p-interface");
94 notification = Mockito.spy(new UEBNotification(ModelType.MOXY, loaderFactory, schemaVersions));
95 httpTestUtil = new HttpTestUtil(queryStyle, notification, AAIProperties.MINIMUM_DEPTH);
97 response = httpTestUtil.doPut(uri, jsonObject.toString());
98 assertEquals("Expecting the pserver to be updated and delete children", 200, response.getStatus());
100 List<NotificationEvent> notificationEvents = notification.getEvents();
101 assertThat(notificationEvents.size(), is(5));
103 List<String> notificationEventHeaders = notification.getEvents().stream()
104 .map(event -> event.getEventHeader().marshal(false)).collect(Collectors.toList());
106 Long deletedEventsCount = notificationEventHeaders.stream().filter(e -> e.contains("\"DELETE\"")).count();
108 assertThat(deletedEventsCount, is(4L));
110 response = httpTestUtil.doGet(uri);
111 assertThat(response.getEntity().toString(), not(containsString("p-interface")));
115 public void testPutGenericVnf() throws Exception {
117 String uri = "/aai/v12/network/generic-vnfs/generic-vnf/generic-vnf-implied-delete";
118 HttpTestUtil httpTestUtil = new HttpTestUtil(queryStyle);
120 String resource = PayloadUtil.getResourcePayload("generic-vnf-implied-delete.json");
122 Response response = httpTestUtil.doGet(uri);
123 assertEquals("Expecting the generic-vnf to be not found", 404, response.getStatus());
125 response = httpTestUtil.doPut(uri, resource);
126 assertEquals("Expecting the generic-vnf to be created", 201, response.getStatus());
128 response = httpTestUtil.doGet(uri);
129 assertEquals("Expecting the generic-vnf to be found", 200, response.getStatus());
131 JSONObject jsonObject = new JSONObject(response.getEntity().toString());
132 JSONAssert.assertEquals(resource, response.getEntity().toString(), false);
133 jsonObject.getJSONObject("vf-modules").remove("vf-module");
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"));
141 public void tearDown() {
143 JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
144 boolean success = true;
148 GraphTraversalSource g = transaction.traversal();
150 g.V().has("source-of-truth", "JUNIT").toList().forEach(v -> v.remove());
152 } catch (Exception ex) {
154 LOGGER.error("Unable to remove the vertexes", ex);
157 transaction.commit();
159 transaction.rollback();
160 fail("Unable to teardown the graph");