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