2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nordix Foundation.
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.Optional;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.models.pap.concepts.PdpGroupUpdateResponse;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroups;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class PdpGroupCreateOrUpdateTest extends End2EndBase {
44 private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateTest.class);
46 private static final String CREATEORUPDATE_GROUPS_ENDPOINT = "pdps/groups/batch";
47 private static final String DELETE_GROUP_ENDPOINT = "pdps/groups/";
48 private static final String CREATE_SUBGROUP = "pdpTypeA";
49 private static final String GROUP_ENDPOINT = "pdps";
52 * Starts Main and adds policies to the DB.
54 * @throws Exception if an error occurs
57 public static void setUpBeforeClass() throws Exception {
58 End2EndBase.setUpBeforeClass();
60 addToscaPolicyTypes("monitoring.policy-type.yaml");
68 public void setUp() throws Exception {
71 context = new End2EndContext();
75 * Deletes the deployed group.
79 public void tearDown() {
80 // delete the group that was inserted
82 sendRequest(DELETE_GROUP_ENDPOINT + "createGroups").delete();
83 } catch (Exception e) {
84 logger.warn("cannot delete group: createGroups", e);
91 public void testCreateGroups() throws Exception {
93 context.addPdp("pdpAA_1", CREATE_SUBGROUP);
94 context.addPdp("pdpAA_2", CREATE_SUBGROUP);
95 context.addPdp("pdpAB_1", "pdpTypeB");
97 context.startThreads();
99 Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
101 PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
102 Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
103 Response rawresp = invocationBuilder.post(entity);
104 PdpGroupUpdateResponse resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
105 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
106 assertNull(resp.getErrorDetails());
110 // none of the PDPs should have handled any requests
111 assertEquals(context.getPdps().size(),
112 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
114 // repeat - should be OK
115 rawresp = invocationBuilder.post(entity);
116 resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
117 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
118 assertNull(resp.getErrorDetails());
120 // repeat with different properties - should fail
121 groups.getGroups().get(0).setProperties(null);
122 rawresp = invocationBuilder.post(entity);
123 resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
124 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
125 assertTrue(resp.getErrorDetails().contains("cannot change properties"));
129 public void testCreateAndUpdate_MultipleGroups() throws Exception {
131 Invocation.Builder invocationBuilderForGroupUpdate = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
133 PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
134 Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
135 Response rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
136 PdpGroupUpdateResponse pdpGroupUpdateResponse =
137 rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
138 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
139 assertNull(pdpGroupUpdateResponse.getErrorDetails());
141 Invocation.Builder invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
142 Response pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
143 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
144 PdpGroups pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
145 assertEquals(1, pdpGroupsInDb.getGroups().size());
146 assertEquals("createGroups", pdpGroupsInDb.getGroups().get(0).getName());
147 assertEquals(PdpState.PASSIVE, pdpGroupsInDb.getGroups().get(0).getPdpGroupState());
149 // creating 2 new groups
150 PdpGroups newGroups = loadJsonFile("createNewGroups.json", PdpGroups.class);
151 entity = Entity.entity(newGroups, MediaType.APPLICATION_JSON);
152 rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
153 pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
154 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
155 assertNull(pdpGroupUpdateResponse.getErrorDetails());
157 invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
158 pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
159 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
160 pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
161 assertEquals(3, pdpGroupsInDb.getGroups().size());
163 // updating a group, changing state of old group to active
164 groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE);
165 entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
166 rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
167 pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
168 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
169 assertNull(pdpGroupUpdateResponse.getErrorDetails());
171 invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
172 pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
173 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
174 pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
175 assertEquals(3, pdpGroupsInDb.getGroups().size());
176 Optional<PdpGroup> oldGroupInDb =
177 pdpGroupsInDb.getGroups().stream().filter(group -> group.getName().equals("createGroups")).findAny();
178 assertEquals(PdpState.ACTIVE, oldGroupInDb.get().getPdpGroupState());