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