Move PAP database provider to spring boot default
[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 testCreateGroups() throws Exception {
81
82         context.addPdp("pdpAA_1", CREATE_SUBGROUP);
83         context.addPdp("pdpAA_2", CREATE_SUBGROUP);
84         context.addPdp("pdpAB_1", "pdpTypeB");
85
86         context.startThreads();
87
88         Invocation.Builder invocationBuilder = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
89
90         PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
91         Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
92         Response rawresp = invocationBuilder.post(entity);
93         PdpGroupUpdateResponse resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
94         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
95         assertNull(resp.getErrorDetails());
96
97         context.await();
98
99         // none of the PDPs should have handled any requests
100         assertEquals(context.getPdps().size(),
101             context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
102
103         // repeat - should be OK
104         rawresp = invocationBuilder.post(entity);
105         resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
106         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
107         assertNull(resp.getErrorDetails());
108
109         // repeat with different properties - should fail
110         groups.getGroups().get(0).setProperties(null);
111         rawresp = invocationBuilder.post(entity);
112         resp = rawresp.readEntity(PdpGroupUpdateResponse.class);
113         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
114         assertTrue(resp.getErrorDetails().contains("cannot change properties"));
115     }
116
117     @Test
118     public void testCreateAndUpdate_MultipleGroups() throws Exception {
119
120         Invocation.Builder invocationBuilderForGroupUpdate = sendRequest(CREATEORUPDATE_GROUPS_ENDPOINT);
121
122         PdpGroups groups = loadJsonFile("createGroups.json", PdpGroups.class);
123         Entity<PdpGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
124         Response rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
125         PdpGroupUpdateResponse pdpGroupUpdateResponse =
126             rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
127         assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
128         assertNull(pdpGroupUpdateResponse.getErrorDetails());
129
130         Invocation.Builder invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
131         Response pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
132         assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
133         PdpGroups pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
134         assertEquals(1, pdpGroupsInDb.getGroups().size());
135         assertEquals("createGroups", pdpGroupsInDb.getGroups().get(0).getName());
136         assertEquals(PdpState.PASSIVE, pdpGroupsInDb.getGroups().get(0).getPdpGroupState());
137
138         // creating 2 new groups
139         PdpGroups newGroups = loadJsonFile("createNewGroups.json", PdpGroups.class);
140         entity = Entity.entity(newGroups, MediaType.APPLICATION_JSON);
141         rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
142         pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
143         assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
144         assertNull(pdpGroupUpdateResponse.getErrorDetails());
145
146         invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
147         pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
148         assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
149         pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
150         assertEquals(3, pdpGroupsInDb.getGroups().size());
151
152         // updating a group, changing state of old group to active
153         groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE);
154         entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
155         rawPdpGroupUpdateResponse = invocationBuilderForGroupUpdate.post(entity);
156         pdpGroupUpdateResponse = rawPdpGroupUpdateResponse.readEntity(PdpGroupUpdateResponse.class);
157         assertEquals(Response.Status.OK.getStatusCode(), rawPdpGroupUpdateResponse.getStatus());
158         assertNull(pdpGroupUpdateResponse.getErrorDetails());
159
160         invocationBuilderForGroupQuery = sendRequest(GROUP_ENDPOINT);
161         pdpGroupQueryResponse = invocationBuilderForGroupQuery.get();
162         assertEquals(Response.Status.OK.getStatusCode(), pdpGroupQueryResponse.getStatus());
163         pdpGroupsInDb = pdpGroupQueryResponse.readEntity(PdpGroups.class);
164         assertEquals(3, pdpGroupsInDb.getGroups().size());
165         Optional<PdpGroup> oldGroupInDb =
166             pdpGroupsInDb.getGroups().stream().filter(group -> group.getName().equals("createGroups")).findAny();
167         assertEquals(PdpState.ACTIVE, oldGroupInDb.get().getPdpGroupState());
168     }
169 }