5e2ca38faa3616e3967c2f651d5ffc89111eb0e8
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PdpGroupDeleteTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021-2022 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest.e2e;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.Collections;
32 import java.util.concurrent.LinkedBlockingQueue;
33 import java.util.concurrent.TimeUnit;
34 import javax.ws.rs.client.Invocation;
35 import javax.ws.rs.core.Response;
36 import org.junit.Before;
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.rest.PdpGroupDeployControllerV1;
48
49 public class PdpGroupDeleteTest extends End2EndBase {
50     private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
51     private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
52
53     /**
54      * Sets up.
55      */
56     @Override
57     @Before
58     public void setUp() throws Exception {
59         addToscaPolicyTypes("monitoring.policy-type.yaml");
60         addToscaPolicies("monitoring.policy.yaml");
61         super.setUp();
62
63         context = new End2EndContext();
64     }
65
66     @Test
67     public void testDeleteGroup() throws Exception {
68         addGroups("deleteGroup.json");
69
70         context.addPdp("pdpAA_1", "pdpTypeA");
71         context.addPdp("pdpAA_2", "pdpTypeA");
72         context.addPdp("pdpAB_1", "pdpTypeB");
73
74         context.startThreads();
75
76         String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";
77
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());
83
84         context.await();
85
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());
89
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());
95     }
96
97     @Test
98     public void testDeletePolicy() throws Exception {
99         addGroups("undeployPolicy.json");
100
101         PdpStatus status1 = new PdpStatus();
102         status1.setName("pdpBA_1");
103         status1.setPdpGroup("undeployPolicy");
104         status1.setPdpSubgroup("pdpTypeA");
105         status1.setPolicies(Collections.emptyList());
106
107         PdpStatus status2 = new PdpStatus();
108         status2.setName("pdpBA_2");
109         status2.setPdpGroup("undeployPolicy");
110         status2.setPdpSubgroup("pdpTypeA");
111         status2.setPolicies(Collections.emptyList());
112
113         context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
114         context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
115         context.addPdp("pdpBB_1", "pdpTypeB");
116
117         context.startThreads();
118
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));
123
124         String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";
125
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());
133
134         context.await();
135
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());
143
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());
149
150         assertThat(meterRegistry.counter(deploymentsCounterName, unDeploymentSuccessTag).count()).isEqualTo(2);
151
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());
156     }
157
158     @Test
159     public void testDeletePolicyVersion() throws Exception {
160         addGroups("undeployPolicyVersion.json");
161
162         PdpStatus status1 = new PdpStatus();
163         status1.setName("pdpCA_1");
164         status1.setPdpGroup("undeployPolicyVersion");
165         status1.setPdpSubgroup("pdpTypeA");
166         status1.setPolicies(Collections.emptyList());
167
168         PdpStatus status2 = new PdpStatus();
169         status2.setName("pdpCA_2");
170         status2.setPdpGroup("undeployPolicyVersion");
171         status2.setPdpSubgroup("pdpTypeA");
172         status2.setPolicies(Collections.emptyList());
173
174         context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
175         context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
176         context.addPdp("pdpCB_1", "pdpTypeB");
177
178         context.startThreads();
179
180         String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";
181
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());
189         context.await();
190
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());
195     }
196 }