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.PdpGroups;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.enums.PdpState;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class PdpGroupDeployTest extends End2EndBase {
47 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployTest.class);
49 private static final String DEPLOY_GROUP_ENDPOINT = "pdps";
50 private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
51 private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
52 private static final String CREATE_SUBGROUP = "pdpTypeA";
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 + "/createGroups").delete();
86 } catch (Exception e) {
87 logger.warn("cannot delete group: createGroups", e);
94 public void testCreateGroups() throws Exception {
96 context.addPdp("pdpAA_1", CREATE_SUBGROUP);
97 context.addPdp("pdpAA_2", CREATE_SUBGROUP);
98 context.addPdp("pdpAB_1", "pdpTypeB");
100 context.startThreads();
102 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
104 PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
105 Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
106 Response rawresp = invocationBuilder.post(entity);
107 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
108 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
109 assertNull(resp.getErrorDetails());
113 // none of the PDPs should have handled any requests
114 assertEquals(context.getPdps().size(),
115 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
117 // repeat - should be OK
118 rawresp = invocationBuilder.post(entity);
119 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
120 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
121 assertNull(resp.getErrorDetails());
123 // repeat with different properties - should fail
124 groups.getGroups().get(0).setProperties(null);
125 rawresp = invocationBuilder.post(entity);
126 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
127 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
128 assertTrue(resp.getErrorDetails().contains("cannot change properties"));
132 public void testDeployPolicies() throws Exception {
133 addGroups("deployPolicies.json");
135 PdpStatus status11 = new PdpStatus();
136 status11.setName("pdpBA_1");
137 status11.setState(PdpState.ACTIVE);
138 status11.setPdpGroup("deployPolicies");
139 status11.setPdpType(DEPLOY_SUBGROUP);
140 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
142 List<ToscaPolicyIdentifier> idents = Arrays.asList(new ToscaPolicyIdentifier("onap.restart.tca", "1.0.0"));
143 status11.setPolicies(idents);
145 PdpStatus status12 = new PdpStatus();
146 status12.setName("pdpBA_2");
147 status12.setState(PdpState.ACTIVE);
148 status12.setPdpGroup("deployPolicies");
149 status12.setPdpType(DEPLOY_SUBGROUP);
150 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
151 status12.setPolicies(idents);
153 context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
154 context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
155 context.addPdp("pdpBB_1", "pdpTypeB");
157 context.startThreads();
159 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
161 PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq.json", PdpDeployPolicies.class);
162 Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
163 Response rawresp = invocationBuilder.post(entity);
164 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
165 System.out.println(resp.getErrorDetails());
166 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
167 assertNull(resp.getErrorDetails());
171 // one of the PDPs should not have handled any requests
172 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
174 // repeat - should be OK
175 rawresp = invocationBuilder.post(entity);
176 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
177 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
178 assertNull(resp.getErrorDetails());