Merge "Handle null policy lists"
[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.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;
45
46 public class PdpGroupDeployTest extends End2EndBase {
47     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployTest.class);
48
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";
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 + "/createGroups").delete();
86         } catch (Exception e) {
87             logger.warn("cannot delete group: createGroups", e);
88         }
89
90         super.tearDown();
91     }
92
93     @Test
94     public void testCreateGroups() throws Exception {
95
96         context.addPdp("pdpAA_1", CREATE_SUBGROUP);
97         context.addPdp("pdpAA_2", CREATE_SUBGROUP);
98         context.addPdp("pdpAB_1", "pdpTypeB");
99
100         context.startThreads();
101
102         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
103
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());
110
111         context.await();
112
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());
116
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());
122
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"));
129     }
130
131     @Test
132     public void testDeployPolicies() throws Exception {
133         addGroups("deployPolicies.json");
134
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);
141
142         List<ToscaPolicyIdentifier> idents = Arrays.asList(new ToscaPolicyIdentifier("onap.restart.tca", "1.0.0"));
143         status11.setPolicies(idents);
144
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);
152
153         context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
154         context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
155         context.addPdp("pdpBB_1", "pdpTypeB");
156
157         context.startThreads();
158
159         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
160
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());
168
169         context.await();
170
171         // one of the PDPs should not have handled any requests
172         assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
173
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());
179     }
180 }