Allow semantic versioning in all templates in pap
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyStatusControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2021-2023 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.jupiter.api.Assertions.assertEquals;
27
28 import jakarta.ws.rs.client.Invocation;
29 import jakarta.ws.rs.client.SyncInvoker;
30 import jakarta.ws.rs.core.Response;
31 import org.junit.jupiter.api.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", "default" })
38 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     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     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     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     void testQueryDeployedPolicies() throws Exception {
71         checkRequest(POLICY_STATUS_ENDPOINT + "/my-name");
72         checkRequest(POLICY_STATUS_ENDPOINT + "/my-name/1.2.3");
73         checkRequest(POLICY_STATUS_ENDPOINT + "/my-name/1.2.3+425");
74     }
75
76     @Test
77     void testGetStatusOfAllPolicies() throws Exception {
78         // verify it fails when no authorization info is included
79         checkUnauthRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT, SyncInvoker::get);
80     }
81
82     @Test
83     void testGetStatusOfPolicies() throws Exception {
84         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name");
85         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name");
86         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name/1.2.3");
87         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name/1.2.3-2");
88     }
89
90     @Test
91     void testGetStatusOfPoliciesWithRegex() throws Exception {
92         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmn%5Da.%7B1%7De");
93         checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1)name");
94         // my-?[mna.{1}e
95         checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmna.%7B1%7De");
96         checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1name");
97         checkEmptyRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=");
98     }
99
100     private void checkRequest(String uri) throws Exception {
101         Invocation.Builder invocationBuilder = sendRequest(uri);
102         Response rawresp = invocationBuilder.get();
103         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
104
105         // verify it fails when no authorization info is included
106         checkUnauthRequest(uri, SyncInvoker::get);
107     }
108
109     private void checkInvalidRegexRequest(String uri) throws Exception {
110         Invocation.Builder invocationBuilder = sendRequest(uri);
111         Response rawresp = invocationBuilder.get();
112         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
113         final String entity = rawresp.readEntity(String.class);
114         assertThat(entity).contains("error parsing regexp");
115
116         // verify it fails when no authorization info is included
117         checkUnauthRequest(uri, SyncInvoker::get);
118     }
119
120     private void checkEmptyRegexRequest(String uri) throws Exception {
121         Invocation.Builder invocationBuilder = sendRequest(uri);
122         Response rawresp = invocationBuilder.get();
123         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
124         final String entity = rawresp.readEntity(String.class);
125         assertThat(entity).contains("empty string passed as a regex");
126
127         // verify it fails when no authorization info is included
128         checkUnauthRequest(uri, SyncInvoker::get);
129     }
130 }