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