2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest.e2e;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Collections;
31 import java.util.concurrent.LinkedBlockingQueue;
32 import java.util.concurrent.TimeUnit;
33 import javax.ws.rs.client.Invocation;
34 import javax.ws.rs.core.Response;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
39 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
42 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
43 import org.onap.policy.models.pap.concepts.PolicyNotification;
44 import org.onap.policy.models.pap.concepts.PolicyStatus;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.pap.main.PapConstants;
48 import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;
50 public class PdpGroupDeleteTest extends End2EndBase {
51 private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
52 private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
55 * Starts Main and adds policies to the DB.
57 * @throws Exception if an error occurs
60 public static void setUpBeforeClass() throws Exception {
61 End2EndBase.setUpBeforeClass();
63 addToscaPolicyTypes("monitoring.policy-type.yaml");
64 addToscaPolicies("monitoring.policy.yaml");
72 public void setUp() throws Exception {
75 context = new End2EndContext();
79 public void testDeleteGroup() throws Exception {
80 addGroups("deleteGroup.json");
82 context.addPdp("pdpAA_1", "pdpTypeA");
83 context.addPdp("pdpAA_2", "pdpTypeA");
84 context.addPdp("pdpAB_1", "pdpTypeB");
86 context.startThreads();
88 String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";
90 Invocation.Builder invocationBuilder = sendRequest(uri);
91 Response rawresp = invocationBuilder.delete();
92 PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
93 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
94 assertNull(resp.getErrorDetails());
98 // none of the PDPs should have handled any requests
99 assertEquals(context.getPdps().size(),
100 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
102 // repeat - should fail
103 rawresp = invocationBuilder.delete();
104 resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
105 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
106 assertEquals("group not found", resp.getErrorDetails());
110 public void testDeletePolicy() throws Exception {
111 addGroups("undeployPolicy.json");
113 PdpStatus status1 = new PdpStatus();
114 status1.setName("pdpBA_1");
115 status1.setPdpGroup("undeployPolicy");
116 status1.setPdpSubgroup("pdpTypeA");
117 status1.setPolicies(Collections.emptyList());
119 PdpStatus status2 = new PdpStatus();
120 status2.setName("pdpBA_2");
121 status2.setPdpGroup("undeployPolicy");
122 status2.setPdpSubgroup("pdpTypeA");
123 status2.setPolicies(Collections.emptyList());
125 context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
126 context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
127 context.addPdp("pdpBB_1", "pdpTypeB");
129 context.startThreads();
131 // arrange to catch notifications
132 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
133 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(PapConstants.TOPIC_POLICY_NOTIFICATION);
134 notifier.register((infra, topic, msg) -> {
135 notifications.add(msg);
138 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";
140 Invocation.Builder invocationBuilder = sendRequest(uri);
141 Response rawresp = invocationBuilder.delete();
142 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
143 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
144 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
145 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
146 assertNull(resp.getErrorDetails());
150 // wait for the notification
151 String json = notifications.poll(5, TimeUnit.SECONDS);
152 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
153 assertNotNull(notify.getAdded());
154 assertNotNull(notify.getDeleted());
155 assertTrue(notify.getAdded().isEmpty());
156 assertEquals(1, notify.getDeleted().size());
158 PolicyStatus deleted = notify.getDeleted().get(0);
159 assertEquals(2, deleted.getSuccessCount());
160 assertEquals(0, deleted.getFailureCount());
161 assertEquals(0, deleted.getIncompleteCount());
162 assertEquals(new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0"), deleted.getPolicy());
164 rawresp = invocationBuilder.delete();
165 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
166 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
167 assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails());
171 public void testDeletePolicyVersion() throws Exception {
172 addGroups("undeployPolicyVersion.json");
174 PdpStatus status1 = new PdpStatus();
175 status1.setName("pdpCA_1");
176 status1.setPdpGroup("undeployPolicyVersion");
177 status1.setPdpSubgroup("pdpTypeA");
178 status1.setPolicies(Collections.emptyList());
180 PdpStatus status2 = new PdpStatus();
181 status2.setName("pdpCA_2");
182 status2.setPdpGroup("undeployPolicyVersion");
183 status2.setPdpSubgroup("pdpTypeA");
184 status2.setPolicies(Collections.emptyList());
186 context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
187 context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
188 context.addPdp("pdpCB_1", "pdpTypeB");
190 context.startThreads();
192 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";
194 Invocation.Builder invocationBuilder = sendRequest(uri);
195 Response rawresp = invocationBuilder.delete();
196 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
197 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
198 assertNull(resp.getErrorDetails());
199 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
200 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
203 rawresp = invocationBuilder.delete();
204 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
205 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
206 assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails());