aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java
blob: 5e2ca38faa3616e3967c2f651d5ffc89111eb0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021-2022 Nordix Foundation.
 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.rest.e2e;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pap.concepts.PolicyStatus;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;

public class PdpGroupDeleteTest extends End2EndBase {
    private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
    private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";

    /**
     * Sets up.
     */
    @Override
    @Before
    public void setUp() throws Exception {
        addToscaPolicyTypes("monitoring.policy-type.yaml");
        addToscaPolicies("monitoring.policy.yaml");
        super.setUp();

        context = new End2EndContext();
    }

    @Test
    public void testDeleteGroup() throws Exception {
        addGroups("deleteGroup.json");

        context.addPdp("pdpAA_1", "pdpTypeA");
        context.addPdp("pdpAA_2", "pdpTypeA");
        context.addPdp("pdpAB_1", "pdpTypeB");

        context.startThreads();

        String uri = DELETE_GROUP_ENDPOINT + "/deleteGroup";

        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.delete();
        PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
        assertNull(resp.getErrorDetails());

        context.await();

        // none of the PDPs should have handled any requests
        assertEquals(context.getPdps().size(),
                        context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());

        // repeat - should fail
        rawresp = invocationBuilder.delete();
        resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
        assertEquals("group not found", resp.getErrorDetails());
    }

    @Test
    public void testDeletePolicy() throws Exception {
        addGroups("undeployPolicy.json");

        PdpStatus status1 = new PdpStatus();
        status1.setName("pdpBA_1");
        status1.setPdpGroup("undeployPolicy");
        status1.setPdpSubgroup("pdpTypeA");
        status1.setPolicies(Collections.emptyList());

        PdpStatus status2 = new PdpStatus();
        status2.setName("pdpBA_2");
        status2.setPdpGroup("undeployPolicy");
        status2.setPdpSubgroup("pdpTypeA");
        status2.setPolicies(Collections.emptyList());

        context.addPdp("pdpBA_1", "pdpTypeA").addReply(status1);
        context.addPdp("pdpBA_2", "pdpTypeA").addReply(status2);
        context.addPdp("pdpBB_1", "pdpTypeB");

        context.startThreads();

        // arrange to catch notifications
        LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
        NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(getTopicPolicyNotification());
        notifier.register((infra, topic, msg) -> notifications.add(msg));

        String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaB";

        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.delete();
        PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
        assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
        assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
        assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
        assertNull(resp.getErrorDetails());

        context.await();

        // wait for the notification
        String json = notifications.poll(5, TimeUnit.SECONDS);
        PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
        assertNotNull(notify.getAdded());
        assertNotNull(notify.getDeleted());
        assertTrue(notify.getAdded().isEmpty());
        assertEquals(1, notify.getDeleted().size());

        PolicyStatus deleted = notify.getDeleted().get(0);
        assertEquals(2, deleted.getSuccessCount());
        assertEquals(0, deleted.getFailureCount());
        assertEquals(0, deleted.getIncompleteCount());
        assertEquals(new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0"), deleted.getPolicy());

        assertThat(meterRegistry.counter(deploymentsCounterName, unDeploymentSuccessTag).count()).isEqualTo(2);

        rawresp = invocationBuilder.delete();
        resp = rawresp.readEntity(PdpGroupDeployResponse.class);
        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
        assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails());
    }

    @Test
    public void testDeletePolicyVersion() throws Exception {
        addGroups("undeployPolicyVersion.json");

        PdpStatus status1 = new PdpStatus();
        status1.setName("pdpCA_1");
        status1.setPdpGroup("undeployPolicyVersion");
        status1.setPdpSubgroup("pdpTypeA");
        status1.setPolicies(Collections.emptyList());

        PdpStatus status2 = new PdpStatus();
        status2.setName("pdpCA_2");
        status2.setPdpGroup("undeployPolicyVersion");
        status2.setPdpSubgroup("pdpTypeA");
        status2.setPolicies(Collections.emptyList());

        context.addPdp("pdpCA_1", "pdpTypeA").addReply(status1);
        context.addPdp("pdpCA_2", "pdpTypeA").addReply(status2);
        context.addPdp("pdpCB_1", "pdpTypeB");

        context.startThreads();

        String uri = DELETE_POLICIES_ENDPOINT + "/onap.restart.tcaC/versions/1.0.0";

        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.delete();
        PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
        assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
        assertNull(resp.getErrorDetails());
        assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
        assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
        context.await();

        rawresp = invocationBuilder.delete();
        resp = rawresp.readEntity(PdpGroupDeployResponse.class);
        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
        assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails());
    }
}