2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.rest.e2e;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
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;
47 public class PdpGroupDeployTest extends End2EndBase {
48 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployTest.class);
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";
56 * Starts Main and adds policies to the DB.
58 * @throws Exception if an error occurs
61 public static void setUpBeforeClass() throws Exception {
62 End2EndBase.setUpBeforeClass();
64 addToscaPolicyTypes("monitoring.policy-type.yaml");
65 addToscaPolicies("monitoring.policy.yaml");
72 public void setUp() throws Exception {
75 context = new End2EndContext();
79 * Deletes the deployed group.
82 public void tearDown() {
83 // delete the group that was inserted
85 sendRequest(DELETE_GROUP_ENDPOINT + "/deployGroups").delete();
86 } catch (Exception e) {
87 logger.warn("cannot delete group: deployGroups", e);
94 public void testUpdateGroupPolicies() throws Exception {
96 addGroups("deployGroups.json");
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);
105 List<ToscaPolicyIdentifier> idents = Arrays.asList(new ToscaPolicyIdentifier("onap.restart.tca", "1.0.0"));
106 status11.setPolicies(idents);
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);
116 context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
117 context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
118 context.addPdp("pdpAB_1", "pdpTypeA");
120 context.startThreads();
122 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
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());
133 // one of the PDPs should not have handled any requests
134 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
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());
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"));
152 public void testDeployPolicies() throws Exception {
153 addGroups("deployPolicies.json");
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);
162 List<ToscaPolicyIdentifier> idents = Arrays.asList(new ToscaPolicyIdentifier("onap.restart.tca", "1.0.0"));
163 status11.setPolicies(idents);
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);
173 context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
174 context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
175 context.addPdp("pdpBB_1", "pdpTypeB");
177 context.startThreads();
179 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
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());
191 // one of the PDPs should not have handled any requests
192 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
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());