2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.policy.pap.main.rest.e2e;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
28 import java.util.Collections;
29 import java.util.concurrent.LinkedBlockingQueue;
30 import java.util.concurrent.TimeUnit;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.core.Response;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
37 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
40 import org.onap.policy.models.pap.concepts.PolicyNotification;
41 import org.onap.policy.models.pap.concepts.PolicyStatus;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
44 import org.onap.policy.pap.main.PapConstants;
46 public class PdpGroupDeleteTest extends End2EndBase {
47 private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
48 private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
51 * Starts Main and adds policies to the DB.
53 * @throws Exception if an error occurs
56 public static void setUpBeforeClass() throws Exception {
57 End2EndBase.setUpBeforeClass();
59 addToscaPolicyTypes("monitoring.policy-type.yaml");
60 addToscaPolicies("monitoring.policy.yaml");
67 public void setUp() throws Exception {
70 context = new End2EndContext();
74 public void testDeleteGroup() throws Exception {
75 addGroups("deleteGroup.json");
77 context.addPdp("pdpAA_1", "pdpTypeA");
78 context.addPdp("pdpAA_2", "pdpTypeA");
79 context.addPdp("pdpAB_1", "pdpTypeB");
81 context.startThreads();
83 String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";
85 Invocation.Builder invocationBuilder = sendRequest(uri);
86 Response rawresp = invocationBuilder.delete();
87 PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
88 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
89 assertNull(resp.getErrorDetails());
93 // none of the PDPs should have handled any requests
94 assertEquals(context.getPdps().size(),
95 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
97 // repeat - should fail
98 rawresp = invocationBuilder.delete();
99 resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
100 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
101 assertEquals("group not found", resp.getErrorDetails());
105 public void testDeletePolicy() throws Exception {
106 addGroups("undeployPolicy.json");
108 PdpStatus status1 = new PdpStatus();
109 status1.setName("pdpBA_1");
110 status1.setPdpGroup("undeployPolicy");
111 status1.setPdpSubgroup("pdpTypeA");
112 status1.setPolicies(Collections.emptyList());
114 PdpStatus status2 = new PdpStatus();
115 status2.setName("pdpBA_2");
116 status2.setPdpGroup("undeployPolicy");
117 status2.setPdpSubgroup("pdpTypeA");
118 status2.setPolicies(Collections.emptyList());
120 context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
121 context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
122 context.addPdp("pdpBB_1", "pdpTypeB");
124 context.startThreads();
126 // arrange to catch notifications
127 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
128 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(PapConstants.TOPIC_POLICY_NOTIFICATION);
129 notifier.register((infra, topic, msg) -> {
130 notifications.add(msg);
133 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";
135 Invocation.Builder invocationBuilder = sendRequest(uri);
136 Response rawresp = invocationBuilder.delete();
137 PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
138 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
139 assertNull(resp.getErrorDetails());
143 // wait for the notification
144 String json = notifications.poll(5, TimeUnit.SECONDS);
145 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
146 assertNotNull(notify.getAdded());
147 assertNotNull(notify.getDeleted());
148 assertTrue(notify.getAdded().isEmpty());
149 assertEquals(1, notify.getDeleted().size());
151 PolicyStatus deleted = notify.getDeleted().get(0);
152 assertEquals(2, deleted.getSuccessCount());
153 assertEquals(0, deleted.getFailureCount());
154 assertEquals(0, deleted.getIncompleteCount());
155 assertEquals(new ToscaPolicyIdentifier("onap.restart.tcaB", "1.0.0"), deleted.getPolicy());
157 rawresp = invocationBuilder.delete();
158 resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
159 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
160 assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails());
164 public void testDeletePolicyVersion() throws Exception {
165 addGroups("undeployPolicyVersion.json");
167 PdpStatus status1 = new PdpStatus();
168 status1.setName("pdpCA_1");
169 status1.setPdpGroup("undeployPolicyVersion");
170 status1.setPdpSubgroup("pdpTypeA");
171 status1.setPolicies(Collections.emptyList());
173 PdpStatus status2 = new PdpStatus();
174 status2.setName("pdpCA_2");
175 status2.setPdpGroup("undeployPolicyVersion");
176 status2.setPdpSubgroup("pdpTypeA");
177 status2.setPolicies(Collections.emptyList());
179 context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
180 context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
181 context.addPdp("pdpCB_1", "pdpTypeB");
183 context.startThreads();
185 String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";
187 Invocation.Builder invocationBuilder = sendRequest(uri);
188 Response rawresp = invocationBuilder.delete();
189 PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
190 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
191 assertNull(resp.getErrorDetails());
195 rawresp = invocationBuilder.delete();
196 resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
197 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
198 assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails());