aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusControllerV1.java
blob: 08ec80a42f132b3bbceb8a646e6cb7f1de35b6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019, 2021-2023 Nordix Foundation.
 *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
 *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.SyncInvoker;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ActiveProfiles;

/**
 * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
 */
@ActiveProfiles({ "test", "default" })
class TestPolicyStatusControllerV1 extends CommonPapRestServer {

    private static final String POLICY_STATUS_ENDPOINT = "policies/deployed";
    private static final String POLICY_DEPLOYMENT_STATUS_ENDPOINT = "policies/status";

    @Test
    void testSwagger() throws Exception {
        super.testSwagger(POLICY_STATUS_ENDPOINT);
        super.testSwagger(POLICY_STATUS_ENDPOINT + "/{name}");
        super.testSwagger(POLICY_STATUS_ENDPOINT + "/{name}/{version}");

        super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT);
        super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}");
        super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}/{policyName}");
        super.testSwagger(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/{pdpGroupName}/{policyName}/{policyVersion}");
    }

    @Test
    void testQueryAllDeployedPolicies() throws Exception {
        // verify it fails when no authorization info is included
        checkUnauthRequest(POLICY_STATUS_ENDPOINT, SyncInvoker::get);
        checkRequest(POLICY_STATUS_ENDPOINT);
    }

    @Test
    void testQueryAllDeployedPoliciesWithRegex() throws Exception {
        checkRequest(POLICY_STATUS_ENDPOINT + "?regex=my.(1)name");
        checkEmptyRegexRequest(POLICY_STATUS_ENDPOINT + "?regex=");
        checkInvalidRegexRequest(POLICY_STATUS_ENDPOINT + "?regex=my-(name");
    }

    @Test
    void testQueryDeployedPolicies() throws Exception {
        checkRequest(POLICY_STATUS_ENDPOINT + "/my-name");
        checkRequest(POLICY_STATUS_ENDPOINT + "/my-name/1.2.3");
    }

    @Test
    void testGetStatusOfAllPolicies() throws Exception {
        // verify it fails when no authorization info is included
        checkUnauthRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT, SyncInvoker::get);
    }

    @Test
    void testGetStatusOfPolicies() throws Exception {
        checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name");
        checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name");
        checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name/my-name/1.2.3");
    }

    @Test
    void testGetStatusOfPoliciesWithRegex() throws Exception {
        checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmn%5Da.%7B1%7De");
        checkRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1)name");
        // my-?[mna.{1}e
        checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my-%3F%5Bmna.%7B1%7De");
        checkInvalidRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=my.(1name");
        checkEmptyRegexRequest(POLICY_DEPLOYMENT_STATUS_ENDPOINT + "/my-group-name?regex=");
    }

    private void checkRequest(String uri) throws Exception {
        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.get();
        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());

        // verify it fails when no authorization info is included
        checkUnauthRequest(uri, SyncInvoker::get);
    }

    private void checkInvalidRegexRequest(String uri) throws Exception {
        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.get();
        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
        final String entity = rawresp.readEntity(String.class);
        assertThat(entity).contains("error parsing regexp");

        // verify it fails when no authorization info is included
        checkUnauthRequest(uri, SyncInvoker::get);
    }

    private void checkEmptyRegexRequest(String uri) throws Exception {
        Invocation.Builder invocationBuilder = sendRequest(uri);
        Response rawresp = invocationBuilder.get();
        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
        final String entity = rawresp.readEntity(String.class);
        assertThat(entity).contains("empty string passed as a regex");

        // verify it fails when no authorization info is included
        checkUnauthRequest(uri, SyncInvoker::get);
    }
}