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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
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;
35 * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
37 @ActiveProfiles({ "test", "default" })
38 class TestPolicyStatusControllerV1 extends CommonPapRestServer {
40 private static final String POLICY_STATUS_ENDPOINT = "policies/deployed";
41 private static final String POLICY_DEPLOYMENT_STATUS_ENDPOINT = "policies/status";
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}");
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}");
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);
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");
70 void testQueryDeployedPolicies() throws Exception {
71 checkRequest(POLICY_STATUS_ENDPOINT + "/my-name");
72 checkRequest(POLICY_STATUS_ENDPOINT + "/my-name/1.2.3");
76 void testGetStatusOfAllPolicies() throws Exception {
77 // verify it fails when no authorization info is included
78 checkUnauthRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT, SyncInvoker::get);
82 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");
89 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");
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=");
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());
103 // verify it fails when no authorization info is included
104 checkUnauthRequest(uri, SyncInvoker::get);
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");
114 // verify it fails when no authorization info is included
115 checkUnauthRequest(uri, SyncInvoker::get);
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");
125 // verify it fails when no authorization info is included
126 checkUnauthRequest(uri, SyncInvoker::get);