23fa803ad13b6cec985a6561d72c6ee94f33083f
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyAuditControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Bell Canada. All rights reserved.
4  * Modifications Copyright (C) 2022 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.pap.main.rest;
21
22 import static org.junit.Assert.assertEquals;
23
24 import javax.ws.rs.client.Invocation;
25 import javax.ws.rs.core.Response;
26 import org.junit.Test;
27 import org.springframework.test.context.ActiveProfiles;
28
29 /**
30  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
31  */
32 @ActiveProfiles("test")
33 public class TestPolicyAuditControllerV1 extends CommonPapRestServer {
34
35     private static final String POLICY_AUDIT_ENDPOINT = "policies/audit";
36
37     @Test
38     public void testSwagger() throws Exception {
39
40         super.testSwagger(POLICY_AUDIT_ENDPOINT);
41         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{pdpGroupName}");
42         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{pdpGroupName}/{policyName}/{policyVersion}");
43         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{policyName}/{policyVersion}");
44     }
45
46     @Test
47     public void testGetAllAuditRecords() throws Exception {
48         String uri = POLICY_AUDIT_ENDPOINT;
49
50         // verify it fails when no authorization info is included
51         checkUnauthRequest(uri, req -> req.get());
52     }
53
54     @Test
55     public void testGetAuditRecordsByGroup() throws Exception {
56         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-group-name");
57     }
58
59     @Test
60     public void testGetAuditRecordsOfPolicy() throws Exception {
61         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-group-name/my-name/1.2.3");
62         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-name/1.2.3");
63     }
64
65     private void checkRequest(String uri) throws Exception {
66         Invocation.Builder invocationBuilder = sendRequest(uri);
67         Response rawresp = invocationBuilder.get();
68         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
69
70         // verify it fails when no authorization info is included
71         checkUnauthRequest(uri, req -> req.get());
72     }
73 }