2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2023 Nordix Foundation.
7 * Modifications Copyright (C) 2021-2022 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.assertj.core.api.Assertions.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import jakarta.ws.rs.client.Invocation;
32 import jakarta.ws.rs.core.Response;
33 import java.util.Collections;
34 import java.util.concurrent.LinkedBlockingQueue;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.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.rest.PdpGroupDeployControllerV1;
49 class PdpGroupDeleteTest extends End2EndBase {
50 private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
51 private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
58 public void setUp() throws Exception {
59 addToscaPolicyTypes("monitoring.policy-type.yaml");
60 addToscaPolicies("monitoring.policy.yaml");
63 context = new End2EndContext();
67 void testDeleteGroup() throws Exception {
68 addGroups("deleteGroup.json");
70 context.addPdp("pdpAA_1", "pdpTypeA");
71 context.addPdp("pdpAA_2", "pdpTypeA");
72 context.addPdp("pdpAB_1", "pdpTypeB");
74 context.startThreads();
76 String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";
78 Invocation.Builder invocationBuilder = sendRequest(uri);
79 Response rawresp = invocationBuilder.delete();
80 PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
81 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
82 assertNull(resp.getErrorDetails());
86 // none of the PDPs should have handled any requests
87 assertEquals(context.getPdps().size(),
88 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
90 // repeat - should fail
91 rawresp = invocationBuilder.delete();
92 resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
93 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
94 assertEquals("group not found", resp.getErrorDetails());
98 void testDeletePolicy() throws Exception {
99 addGroups("undeployPolicy.json");
101 PdpStatus status1 = new PdpStatus();
102 status1.setName("pdpBA_1");
103 status1.setPdpGroup("undeployPolicy");
104 status1.setPdpSubgroup("pdpTypeA");
105 status1.setPolicies(Collections.emptyList());
107 PdpStatus status2 = new PdpStatus();
108 status2.setName("pdpBA_2");
109 status2.setPdpGroup("undeployPolicy");
110 status2.setPdpSubgroup("pdpTypeA");
111 status2.setPolicies(Collections.emptyList());
113 context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
114 context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
115 context.addPdp("pdpBB_1", "pdpTypeB");
117 context.startThreads();
119 // arrange to catch notifications
120 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
121 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(getTopicPolicyNotification());
122 notifier.register((infra, topic, msg) -> notifications.add(msg));
124 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";
126 Invocation.Builder invocationBuilder = sendRequest(uri);
127 Response rawresp = invocationBuilder.delete();
128 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
129 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
130 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
131 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
132 assertNull(resp.getErrorDetails());
136 // wait for the notification
137 String json = notifications.poll(5, TimeUnit.SECONDS);
138 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
139 assertNotNull(notify.getAdded());
140 assertNotNull(notify.getDeleted());
141 assertTrue(notify.getAdded().isEmpty());
142 assertEquals(1, notify.getDeleted().size());
144 PolicyStatus deleted = notify.getDeleted().get(0);
145 assertEquals(2, deleted.getSuccessCount());
146 assertEquals(0, deleted.getFailureCount());
147 assertEquals(0, deleted.getIncompleteCount());
148 assertEquals(new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0"), deleted.getPolicy());
150 assertThat(meterRegistry.counter(deploymentsCounterName, unDeploymentSuccessTag).count()).isEqualTo(2);
152 rawresp = invocationBuilder.delete();
153 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
154 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
155 assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails());
159 void testDeletePolicyVersion() throws Exception {
160 addGroups("undeployPolicyVersion.json");
162 PdpStatus status1 = new PdpStatus();
163 status1.setName("pdpCA_1");
164 status1.setPdpGroup("undeployPolicyVersion");
165 status1.setPdpSubgroup("pdpTypeA");
166 status1.setPolicies(Collections.emptyList());
168 PdpStatus status2 = new PdpStatus();
169 status2.setName("pdpCA_2");
170 status2.setPdpGroup("undeployPolicyVersion");
171 status2.setPdpSubgroup("pdpTypeA");
172 status2.setPolicies(Collections.emptyList());
174 context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
175 context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
176 context.addPdp("pdpCB_1", "pdpTypeB");
178 context.startThreads();
180 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";
182 Invocation.Builder invocationBuilder = sendRequest(uri);
183 Response rawresp = invocationBuilder.delete();
184 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
185 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
186 assertNull(resp.getErrorDetails());
187 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
188 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
191 rawresp = invocationBuilder.delete();
192 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
193 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
194 assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails());