Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PdpGroupDeployTest.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-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
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.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;
30
31 import jakarta.ws.rs.client.Entity;
32 import jakarta.ws.rs.client.Invocation;
33 import jakarta.ws.rs.core.MediaType;
34 import jakarta.ws.rs.core.Response;
35 import java.util.List;
36 import java.util.concurrent.LinkedBlockingQueue;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
41 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
44 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
45 import org.onap.policy.models.pap.concepts.PolicyNotification;
46 import org.onap.policy.models.pap.concepts.PolicyStatus;
47 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
48 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
49 import org.onap.policy.models.pdp.concepts.PdpStatus;
50 import org.onap.policy.models.pdp.enums.PdpState;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;
53
54 class PdpGroupDeployTest extends End2EndBase {
55     private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch";
56     private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
57     private static final String DEPLOY_SUBGROUP = "pdpTypeA";
58
59     /**
60      * Sets up.
61      */
62     @Override
63     @BeforeEach
64     public void setUp() throws Exception {
65         addToscaPolicyTypes("monitoring.policy-type.yaml");
66         addToscaPolicies("monitoring.policy.yaml");
67         super.setUp();
68
69         context = new End2EndContext();
70     }
71
72     @Test
73     void testUpdateGroupPolicies() throws Exception {
74
75         addGroups("deployGroups.json");
76
77         PdpStatus status11 = new PdpStatus();
78         status11.setName("pdpAA_1");
79         status11.setState(PdpState.ACTIVE);
80         status11.setPdpGroup("deployGroups");
81         status11.setPdpType(DEPLOY_SUBGROUP);
82         status11.setPdpSubgroup(DEPLOY_SUBGROUP);
83
84         List<ToscaConceptIdentifier> idents = List.of(new ToscaConceptIdentifier("onap.restart.tca", "1.0.0"));
85         status11.setPolicies(idents);
86
87         PdpStatus status12 = new PdpStatus();
88         status12.setName("pdpAA_2");
89         status12.setState(PdpState.ACTIVE);
90         status12.setPdpGroup("deployGroups");
91         status12.setPdpType(DEPLOY_SUBGROUP);
92         status12.setPdpSubgroup(DEPLOY_SUBGROUP);
93         status12.setPolicies(idents);
94
95         context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
96         context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
97         context.addPdp("pdpAB_1", "pdpTypeA");
98
99         context.startThreads();
100
101         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
102
103         DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class);
104         Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
105         Response rawresp = invocationBuilder.post(entity);
106         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
107         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
108         assertNull(resp.getErrorDetails());
109
110         context.await();
111
112         // one of the PDPs should not have handled any requests
113         assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
114
115         // repeat - should be OK
116         rawresp = invocationBuilder.post(entity);
117         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
118         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
119         assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
120         assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
121         assertNull(resp.getErrorDetails());
122
123         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
124
125         // repeat with unknown group - should fail
126         DeploymentGroup group = groups.getGroups().get(0);
127         group.setName("unknown-group");
128         rawresp = invocationBuilder.post(entity);
129         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
130         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
131         assertTrue(resp.getErrorDetails().contains("unknown group"));
132     }
133
134     @Test
135     void testDeployPolicies() throws Exception {
136         addGroups("deployPolicies.json");
137
138         PdpStatus status11 = new PdpStatus();
139         status11.setName("pdpBA_1");
140         status11.setState(PdpState.ACTIVE);
141         status11.setPdpGroup("deployPolicies");
142         status11.setPdpType(DEPLOY_SUBGROUP);
143         status11.setPdpSubgroup(DEPLOY_SUBGROUP);
144
145         final ToscaConceptIdentifier identifier = new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0");
146
147         List<ToscaConceptIdentifier> identifiers = List.of(identifier);
148         status11.setPolicies(identifiers);
149
150         PdpStatus status12 = new PdpStatus();
151         status12.setName("pdpBA_2");
152         status12.setState(PdpState.ACTIVE);
153         status12.setPdpGroup("deployPolicies");
154         status12.setPdpType(DEPLOY_SUBGROUP);
155         status12.setPdpSubgroup(DEPLOY_SUBGROUP);
156         status12.setPolicies(identifiers);
157
158         context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
159         context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
160         context.addPdp("pdpBB_1", "pdpTypeB");
161
162         context.startThreads();
163
164         // arrange to catch notifications
165         LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
166         NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(getTopicPolicyNotification());
167         notifier.register((infra, topic, msg) -> notifications.add(msg));
168
169         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isZero();
170
171         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
172
173         PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
174         Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
175         Response rawresp = invocationBuilder.post(entity);
176         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
177         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
178         assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
179         assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
180         assertNull(resp.getErrorDetails());
181
182         context.await();
183
184         // wait for the notification
185         String json = notifications.poll(5, TimeUnit.SECONDS);
186         PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
187         assertNotNull(notify.getAdded());
188         assertNotNull(notify.getDeleted());
189         assertTrue(notify.getDeleted().isEmpty());
190         assertEquals(1, notify.getAdded().size());
191
192         PolicyStatus added = notify.getAdded().get(0);
193         assertEquals(2, added.getSuccessCount());
194         assertEquals(0, added.getFailureCount());
195         assertEquals(0, added.getIncompleteCount());
196         assertEquals(identifier, added.getPolicy());
197
198         // one of the PDPs should not have handled any requests
199         assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
200
201         // repeat - should be OK
202         rawresp = invocationBuilder.post(entity);
203         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
204         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
205         assertNull(resp.getErrorDetails());
206
207         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
208     }
209 }