Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PolicyStatusTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
7  * Modifications Copyright (C) 2022-2023 Nordix Foundation.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest.e2e;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import jakarta.ws.rs.client.Invocation;
29 import jakarta.ws.rs.core.GenericType;
30 import jakarta.ws.rs.core.Response;
31 import java.util.List;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.models.pap.concepts.PolicyStatus;
35 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
36 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
37
38 class PolicyStatusTest extends End2EndBase {
39     private static final String POLICY_NAME = "onap.restart.tca";
40     private static final String POLICY_TYPE_NAME = "onap.policies.monitoring.cdap.tca.hi.lo.app";
41     private static final String VERSION = "1.0.0";
42     private static final String POLICY_STATUS_ENDPOINT = "policies/deployed";
43     private static final String POLICY_DEPLOYMENT_STATUS_ENDPOINT = "policies/status";
44
45     @Override
46     @BeforeEach
47     public void setUp() throws Exception {
48         addPdpPolicyStatus("policyStatus.json");
49         super.setUp();
50     }
51
52     @Test
53     void testQueryAllDeployedPolicies() throws Exception {
54         Invocation.Builder invocationBuilder = sendRequest(POLICY_STATUS_ENDPOINT);
55         Response rawresp = invocationBuilder.get();
56         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
57
58         List<PolicyStatus> resp = rawresp.readEntity(new GenericType<>() {});
59         assertEquals(1, resp.size());
60         checkAssertions(resp.get(0));
61     }
62
63     @Test
64     void testQueryDeployedPolicies() throws Exception {
65         String uri = POLICY_STATUS_ENDPOINT + "/onap.restart.tca";
66
67         Invocation.Builder invocationBuilder = sendRequest(uri);
68         Response rawresp = invocationBuilder.get();
69         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
70
71         List<PolicyStatus> resp = rawresp.readEntity(new GenericType<>() {});
72         assertEquals(1, resp.size());
73         checkAssertions(resp.get(0));
74     }
75
76     @Test
77     void testQueryDeployedPolicy() throws Exception {
78         String uri = POLICY_STATUS_ENDPOINT + "/onap.restart.tca/1.0.0";
79
80         Invocation.Builder invocationBuilder = sendRequest(uri);
81         Response rawresp = invocationBuilder.get();
82         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
83
84         PolicyStatus status = rawresp.readEntity(PolicyStatus.class);
85         checkAssertions(status);
86     }
87
88     @Test
89     void testGetStatusOfAllDeployedPolicies() throws Exception {
90         Invocation.Builder invocationBuilder = sendRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT);
91         Response rawresp = invocationBuilder.get();
92         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
93
94         List<PdpPolicyStatus> resp = rawresp.readEntity(new GenericType<>() {});
95         assertEquals(1, resp.size());
96         checkAssertionsForDeploymentStatus(resp.get(0));
97     }
98
99     @Test
100     void testGetStatusOfDeployedPolicies() throws Exception {
101         String uri = POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/policyStatus/onap.restart.tca";
102
103         Invocation.Builder invocationBuilder = sendRequest(uri);
104         Response rawresp = invocationBuilder.get();
105         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
106
107         List<PdpPolicyStatus> resp = rawresp.readEntity(new GenericType<>() {});
108         assertEquals(1, resp.size());
109         checkAssertionsForDeploymentStatus(resp.get(0));
110     }
111
112     @Test
113     void testGetStatusOfDeployedPolicy() throws Exception {
114         String uri = POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/policyStatus/onap.restart.tca/1.0.0";
115
116         Invocation.Builder invocationBuilder = sendRequest(uri);
117         Response rawresp = invocationBuilder.get();
118         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
119
120         PdpPolicyStatus status = rawresp.readEntity(PdpPolicyStatus.class);
121         checkAssertionsForDeploymentStatus(status);
122     }
123
124
125     private void checkAssertions(PolicyStatus status) {
126         assertEquals(POLICY_NAME, status.getPolicyId());
127         assertEquals(VERSION, status.getPolicyVersion());
128         assertEquals(POLICY_TYPE_NAME, status.getPolicyTypeId());
129         assertEquals(VERSION, status.getPolicyTypeVersion());
130         assertEquals(0, status.getFailureCount());
131         assertEquals(1, status.getIncompleteCount());
132         assertEquals(0, status.getSuccessCount());
133     }
134
135     private void checkAssertionsForDeploymentStatus(PdpPolicyStatus status) {
136         assertEquals(POLICY_NAME, status.getPolicy().getName());
137         assertEquals(VERSION, status.getPolicy().getVersion());
138         assertEquals(POLICY_TYPE_NAME, status.getPolicyType().getName());
139         assertEquals(VERSION, status.getPolicyType().getVersion());
140         assertEquals("policyStatus", status.getPdpGroup());
141         assertEquals("pdpB_1", status.getPdpId());
142         assertEquals("pdpTypeB", status.getPdpType());
143         assertEquals(State.WAITING, status.getState());
144         assertTrue(status.isDeploy());
145     }
146 }