2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nordix Foundation.
6 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest.e2e;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
28 import java.util.Optional;
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.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";
56 public void setUp() throws Exception {
57 addToscaPolicyTypes("monitoring.policy-type.yaml");
60 context = new End2EndContext();
64 * Deletes the deployed group.
68 public void tearDown() {
69 // delete the group that was inserted
71 sendRequest(DELETE_GROUP_ENDPOINT + "createGroups").delete();
72 } catch (Exception e) {
73 logger.warn("cannot delete group: createGroups", e);
80 public void testCreateGroupsJson() throws Exception {
82 createPdpGroups("createGroups.json", MediaType.APPLICATION_JSON);
86 public void testCreateGroupsYaml() throws Exception {
88 createPdpGroups("createGroups.yaml", "application/yaml");
91 private void createPdpGroups(String fileName, String mediaType) throws Exception, InterruptedException {
92 context.addPdp("pdpAA_1", CREATE_SUBGROUP);
93 context.addPdp("pdpAA_2", CREATE_SUBGROUP);
94 context.addPdp("pdpAB_1", "pdpTypeB");
96 context.startThreads();
98 Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT, mediaType);
100 PdpGroups groups = (mediaType.equalsIgnoreCase(MediaType.APPLICATION_JSON)
101 ? loadJsonFile(fileName, PdpGroups.class)
102 : loadYamlFile(fileName, PdpGroups.class));
103 Entity<PdpGroups> entity = Entity.entity(groups, mediaType);
104 Response rawresp = invocationBuilder.post(entity);
105 PdpGroupUpdateResponse resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
106 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
107 assertNull(resp.getErrorDetails());
111 // none of the PDPs should have handled any requests
112 assertEquals(context.getPdps().size(),
113 context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
115 // repeat - should be OK
116 rawresp = invocationBuilder.post(entity);
117 resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
118 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
119 assertNull(resp.getErrorDetails());
121 // repeat with different properties - should fail
122 groups.getGroups().get(0).setProperties(null);
123 rawresp = invocationBuilder.post(entity);
124 resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
125 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
126 assertTrue(resp.getErrorDetails().contains("cannot change properties"));
130 public void testCreateAndUpdate_MultipleGroups() throws Exception {
132 Invocation.Builder invocationBuilderForGroupUpdate = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
134 PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
135 Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
136 Response rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
137 PdpGroupUpdateResponse pdpGroupUpdateResponse =
138 rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
139 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
140 assertNull(pdpGroupUpdateResponse.getErrorDetails());
142 Invocation.Builder invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
143 Response pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
144 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
145 PdpGroups pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
146 assertEquals(1, pdpGroupsInDb.getGroups().size());
147 assertEquals("createGroups", pdpGroupsInDb.getGroups().get(0).getName());
148 assertEquals(PdpState.PASSIVE, pdpGroupsInDb.getGroups().get(0).getPdpGroupState());
150 // creating 2 new groups
151 PdpGroups newGroups = loadJsonFile("createNewGroups.json", PdpGroups.class);
152 entity = Entity.entity(newGroups, MediaType.APPLICATION_JSON);
153 rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
154 pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
155 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
156 assertNull(pdpGroupUpdateResponse.getErrorDetails());
158 invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
159 pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
160 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
161 pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
162 assertEquals(3, pdpGroupsInDb.getGroups().size());
164 // updating a group, changing state of old group to active
165 groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE);
166 entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
167 rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
168 pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
169 assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
170 assertNull(pdpGroupUpdateResponse.getErrorDetails());
172 invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
173 pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
174 assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
175 pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
176 assertEquals(3, pdpGroupsInDb.getGroups().size());
177 Optional<PdpGroup> oldGroupInDb =
178 pdpGroupsInDb.getGroups().stream().filter(group -> group.getName().equals("createGroups")).findAny();
179 assertEquals(PdpState.ACTIVE, oldGroupInDb.get().getPdpGroupState());