650dd41aaacf2038470c05a5c587b3206d453041
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PdpGroupCreateOrUpdateTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest.e2e;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
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;
42
43 public class PdpGroupCreateOrUpdateTest extends End2EndBase {
44     private static final Logger logger = LoggerFactory.getLogger(PdpGroupCreateOrUpdateTest.class);
45
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";
50
51     /**
52      * Sets up.
53      */
54     @Override
55     @Before
56     public void setUp() throws Exception {
57         addToscaPolicyTypes("monitoring.policy-type.yaml");
58         super.setUp();
59
60         context = new End2EndContext();
61     }
62
63     /**
64      * Deletes the deployed group.
65      */
66     @Override
67     @After
68     public void tearDown() {
69         // delete the group that was inserted
70         try {
71             sendRequest(DELETE_GROUP_ENDPOINT + "createGroups").delete();
72         } catch (Exception e) {
73             logger.warn("cannot delete group: createGroups", e);
74         }
75
76         super.tearDown();
77     }
78
79     @Test
80     public void testCreateGroupsJson() throws Exception {
81
82         createPdpGroups("createGroups.json", MediaType.APPLICATION_JSON);
83     }
84
85     @Test
86     public void testCreateGroupsYaml() throws Exception {
87
88         createPdpGroups("createGroups.yaml", "application/yaml");
89     }
90
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");
95
96         context.startThreads();
97
98         Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT, mediaType);
99
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());
108
109         context.await();
110
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());
114
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());
120
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"));
127     }
128
129     @Test
130     public void testCreateAndUpdate_MultipleGroups() throws Exception {
131
132         Invocation.Builder invocationBuilderForGroupUpdate = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
133
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());
141
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());
149
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());
157
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());
163
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());
171
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());
180     }
181 }