68e7028765f92fa7fe009ee4dbafaadcea2c2d72
[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 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest.e2e;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25
26 import java.util.Collections;
27 import javax.ws.rs.client.Invocation;
28 import javax.ws.rs.core.Response;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
33 import org.onap.policy.models.pdp.concepts.PdpStatus;
34
35 public class PdpGroupDeleteTest extends End2EndBase {
36     private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
37     private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
38
39     /**
40      * Starts Main and adds policies to the DB.
41      *
42      * @throws Exception if an error occurs
43      */
44     @BeforeClass
45     public static void setUpBeforeClass() throws Exception {
46         End2EndBase.setUpBeforeClass();
47
48         addToscaPolicyTypes("monitoring.policy-type.yaml");
49         addToscaPolicies("monitoring.policy.yaml");
50     }
51
52     /**
53      * Sets up.
54      */
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58
59         context = new End2EndContext();
60     }
61
62     @Test
63     public void testDeleteGroup() throws Exception {
64         addGroups("deleteGroup.json");
65
66         context.addPdp("pdpAA_1", "pdpTypeA");
67         context.addPdp("pdpAA_2", "pdpTypeA");
68         context.addPdp("pdpAB_1", "pdpTypeB");
69
70         context.startThreads();
71
72         String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";
73
74         Invocation.Builder invocationBuilder = sendRequest(uri);
75         Response rawresp = invocationBuilder.delete();
76         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
77         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
78         assertNull(resp.getErrorDetails());
79
80         context.await();
81
82         // none of the PDPs should have handled any requests
83         assertEquals(context.getPdps().size(),
84                         context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
85
86         // repeat - should fail
87         rawresp = invocationBuilder.delete();
88         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
89         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
90         assertEquals("group not found", resp.getErrorDetails());
91     }
92
93     @Test
94     public void testDeletePolicy() throws Exception {
95         addGroups("undeployPolicy.json");
96
97         PdpStatus status1 = new PdpStatus();
98         status1.setName("pdpBA_1");
99         status1.setPdpGroup("undeployPolicy");
100         status1.setPdpSubgroup("pdpTypeA");
101         status1.setPolicies(Collections.emptyList());
102
103         PdpStatus status2 = new PdpStatus();
104         status2.setName("pdpBA_2");
105         status2.setPdpGroup("undeployPolicy");
106         status2.setPdpSubgroup("pdpTypeA");
107         status2.setPolicies(Collections.emptyList());
108
109         context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
110         context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
111         context.addPdp("pdpBB_1", "pdpTypeB");
112
113         context.startThreads();
114
115         String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";
116
117         Invocation.Builder invocationBuilder = sendRequest(uri);
118         Response rawresp = invocationBuilder.delete();
119         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
120         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
121         assertNull(resp.getErrorDetails());
122
123         context.await();
124
125         rawresp = invocationBuilder.delete();
126         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
127         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
128         assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails());
129     }
130
131     @Test
132     public void testDeletePolicyVersion() throws Exception {
133         addGroups("undeployPolicyVersion.json");
134
135         PdpStatus status1 = new PdpStatus();
136         status1.setName("pdpCA_1");
137         status1.setPdpGroup("undeployPolicyVersion");
138         status1.setPdpSubgroup("pdpTypeA");
139         status1.setPolicies(Collections.emptyList());
140
141         PdpStatus status2 = new PdpStatus();
142         status2.setName("pdpCA_2");
143         status2.setPdpGroup("undeployPolicyVersion");
144         status2.setPdpSubgroup("pdpTypeA");
145         status2.setPolicies(Collections.emptyList());
146
147         context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
148         context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
149         context.addPdp("pdpCB_1", "pdpTypeB");
150
151         context.startThreads();
152
153         String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";
154
155         Invocation.Builder invocationBuilder = sendRequest(uri);
156         Response rawresp = invocationBuilder.delete();
157         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
158         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
159         assertNull(resp.getErrorDetails());
160
161         context.await();
162
163         rawresp = invocationBuilder.delete();
164         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
165         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
166         assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails());
167     }
168 }