907e6c5a2fedf66b3de40e5fd294dc016e3867f6
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyStatusControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2021-2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.client.SyncInvoker;
30 import javax.ws.rs.core.Response;
31 import org.junit.Test;
32 import org.springframework.test.context.ActiveProfiles;
33
34 /**
35  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
36  */
37 @ActiveProfiles("test")
38 public class TestPolicyStatusControllerV1 extends CommonPapRestServer {
39
40     private static final String POLICY_STATUS_ENDPOINT = "policies/deployed";
41     private static final String POLICY_DEPLOYMENT_STATUS_ENDPOINT = "policies/status";
42
43     @Test
44     public void testSwagger() throws Exception {
45         super.testSwagger(POLICY_STATUS_ENDPOINT);
46         super.testSwagger(POLICY_STATUS_ENDPOINT + "/{name}");
47         super.testSwagger(POLICY_STATUS_ENDPOINT + "/{name}/{version}");
48
49         super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT);
50         super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}");
51         super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}/{policyName}");
52         super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}/{policyName}/{policyVersion}");
53     }
54
55     @Test
56     public void testQueryAllDeployedPolicies() throws Exception {
57         // verify it fails when no authorization info is included
58         checkUnauthRequest(POLICY_STATUS_ENDPOINT, SyncInvoker::get);
59         checkRequest(POLICY_STATUS_ENDPOINT);
60     }
61
62     @Test
63     public void testQueryAllDeployedPoliciesWithRegex() throws Exception {
64         checkRequest(POLICY_STATUS_ENDPOINT + "?regex=my.(1)name");
65         checkEmptyRegexRequest(POLICY_STATUS_ENDPOINT + "?regex=");
66         checkInvalidRegexRequest(POLICY_STATUS_ENDPOINT + "?regex=my-(name");
67     }
68
69     @Test
70     public void testQueryDeployedPolicies() throws Exception {
71         checkRequest(POLICY_STATUS_ENDPOINT + "/my-name");
72         checkRequest(POLICY_STATUS_ENDPOINT + "/my-name/1.2.3");
73     }
74
75     @Test
76     public void testGetStatusOfAllPolicies() throws Exception {
77         // verify it fails when no authorization info is included
78         checkUnauthRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT, SyncInvoker::get);
79     }
80
81     @Test
82     public void testGetStatusOfPolicies() throws Exception {
83         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name");
84         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name");
85         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name/1.2.3");
86     }
87
88     @Test
89     public void testGetStatusOfPoliciesWithRegex() throws Exception {
90         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmn%5Da.%7B1%7De");
91         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1)name");
92         // my-?[mna.{1}e
93         checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmna.%7B1%7De");
94         checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1name");
95         checkEmptyRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=");
96     }
97
98     private void checkRequest(String uri) throws Exception {
99         Invocation.Builder invocationBuilder = sendRequest(uri);
100         Response rawresp = invocationBuilder.get();
101         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
102
103         // verify it fails when no authorization info is included
104         checkUnauthRequest(uri, SyncInvoker::get);
105     }
106
107     private void checkInvalidRegexRequest(String uri) throws Exception {
108         Invocation.Builder invocationBuilder = sendRequest(uri);
109         Response rawresp = invocationBuilder.get();
110         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
111         final String entity = rawresp.readEntity(String.class);
112         assertThat(entity).contains("error parsing regexp");
113
114         // verify it fails when no authorization info is included
115         checkUnauthRequest(uri, SyncInvoker::get);
116     }
117
118     private void checkEmptyRegexRequest(String uri) throws Exception {
119         Invocation.Builder invocationBuilder = sendRequest(uri);
120         Response rawresp = invocationBuilder.get();
121         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
122         final String entity = rawresp.readEntity(String.class);
123         assertThat(entity).contains("empty string passed as a regex");
124
125         // verify it fails when no authorization info is included
126         checkUnauthRequest(uri, SyncInvoker::get);
127     }
128 }