Updating PAP deployment API to reflect actual status
[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 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.assertTrue;
26
27 import java.util.List;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.core.GenericType;
30 import javax.ws.rs.core.Response;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.models.pap.concepts.PolicyStatus;
34 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
35 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
36
37 public class PolicyStatusTest extends End2EndBase {
38     private static final String POLICY_NAME = "onap.restart.tca";
39     private static final String POLICY_TYPE_NAME = "onap.policies.monitoring.cdap.tca.hi.lo.app";
40     private static final String VERSION = "1.0.0";
41     private static final String POLICY_STATUS_ENDPOINT = "policies/deployed";
42     private static final String POLICY_DEPLOYMENT_STATUS_ENDPOINT = "policies/status";
43
44     /**
45      * Starts Main and adds policies to the DB.
46      *
47      * @throws Exception if an error occurs
48      */
49     @BeforeClass
50     public static void setUpBeforeClass() throws Exception {
51         End2EndBase.setUpBeforeClass();
52         addPdpPolicyStatus("policyStatus.json");
53     }
54
55     @Test
56     public void testQueryAllDeployedPolicies() throws Exception {
57         String uri = POLICY_STATUS_ENDPOINT;
58
59         Invocation.Builder invocationBuilder = sendRequest(uri);
60         Response rawresp = invocationBuilder.get();
61         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
62
63         List<PolicyStatus> resp = rawresp.readEntity(new GenericType<List<PolicyStatus>>() {});
64         assertEquals(1, resp.size());
65         checkAssertions(resp.get(0));
66     }
67
68     @Test
69     public void testQueryDeployedPolicies() throws Exception {
70         String uri = POLICY_STATUS_ENDPOINT + "/onap.restart.tca";
71
72         Invocation.Builder invocationBuilder = sendRequest(uri);
73         Response rawresp = invocationBuilder.get();
74         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
75
76         List<PolicyStatus> resp = rawresp.readEntity(new GenericType<List<PolicyStatus>>() {});
77         assertEquals(1, resp.size());
78         checkAssertions(resp.get(0));
79     }
80
81     @Test
82     public void testQueryDeployedPolicy() throws Exception {
83         String uri = POLICY_STATUS_ENDPOINT + "/onap.restart.tca/1.0.0";
84
85         Invocation.Builder invocationBuilder = sendRequest(uri);
86         Response rawresp = invocationBuilder.get();
87         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
88
89         PolicyStatus status = rawresp.readEntity(PolicyStatus.class);
90         checkAssertions(status);
91     }
92
93     @Test
94     public void testGetStatusOfAllDeployedPolicies() throws Exception {
95         String uri = POLICY_DEPLOYMENT_STATUS_ENDPOINT;
96
97         Invocation.Builder invocationBuilder = sendRequest(uri);
98         Response rawresp = invocationBuilder.get();
99         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
100
101         List<PdpPolicyStatus> resp = rawresp.readEntity(new GenericType<List<PdpPolicyStatus>>() {});
102         assertEquals(1, resp.size());
103         checkAssertionsForDeploymentStatus(resp.get(0));
104     }
105
106     @Test
107     public void testGetStatusOfDeployedPolicies() throws Exception {
108         String uri = POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/policyStatus/onap.restart.tca";
109
110         Invocation.Builder invocationBuilder = sendRequest(uri);
111         Response rawresp = invocationBuilder.get();
112         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
113
114         List<PdpPolicyStatus> resp = rawresp.readEntity(new GenericType<List<PdpPolicyStatus>>() {});
115         assertEquals(1, resp.size());
116         checkAssertionsForDeploymentStatus(resp.get(0));
117     }
118
119     @Test
120     public void testGetStatusOfDeployedPolicy() throws Exception {
121         String uri = POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/policyStatus/onap.restart.tca/1.0.0";
122
123         Invocation.Builder invocationBuilder = sendRequest(uri);
124         Response rawresp = invocationBuilder.get();
125         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
126
127         PdpPolicyStatus status = rawresp.readEntity(PdpPolicyStatus.class);
128         checkAssertionsForDeploymentStatus(status);
129     }
130
131
132     private void checkAssertions(PolicyStatus status) {
133         assertEquals(POLICY_NAME, status.getPolicyId());
134         assertEquals(VERSION, status.getPolicyVersion());
135         assertEquals(POLICY_TYPE_NAME, status.getPolicyTypeId());
136         assertEquals(VERSION, status.getPolicyTypeVersion());
137         assertEquals(0, status.getFailureCount());
138         assertEquals(1, status.getIncompleteCount());
139         assertEquals(0, status.getSuccessCount());
140     }
141
142     private void checkAssertionsForDeploymentStatus(PdpPolicyStatus status) {
143         assertEquals(POLICY_NAME, status.getPolicy().getName());
144         assertEquals(VERSION, status.getPolicy().getVersion());
145         assertEquals(POLICY_TYPE_NAME, status.getPolicyType().getName());
146         assertEquals(VERSION, status.getPolicyType().getVersion());
147         assertEquals("policyStatus", status.getPdpGroup());
148         assertEquals("pdpB_1", status.getPdpId());
149         assertEquals("pdpTypeB", status.getPdpType());
150         assertEquals(State.WAITING, status.getState());
151         assertTrue(status.isDeploy());
152     }
153 }