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