8658b7d25c4d0a34ffb6ff50c6fc3d7c2460252b
[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.annotation.DirtiesContext;
28 import org.springframework.test.context.ActiveProfiles;
29
30 /**
31  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
32  */
33 @ActiveProfiles("test")
34 public class TestPolicyAuditControllerV1 extends CommonPapRestServer {
35
36     private static final String POLICY_AUDIT_ENDPOINT = "policies/audit";
37
38     @Test
39     public void testSwagger() throws Exception {
40
41         super.testSwagger(POLICY_AUDIT_ENDPOINT);
42         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{pdpGroupName}");
43         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{pdpGroupName}/{policyName}/{policyVersion}");
44         super.testSwagger(POLICY_AUDIT_ENDPOINT + "/{policyName}/{policyVersion}");
45     }
46
47     @Test
48     public void testGetAllAuditRecords() throws Exception {
49         String uri = POLICY_AUDIT_ENDPOINT;
50
51         // verify it fails when no authorization info is included
52         checkUnauthRequest(uri, req -> req.get());
53     }
54
55     @Test
56     public void testGetAuditRecordsByGroup() throws Exception {
57         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-group-name");
58     }
59
60     @Test
61     public void testGetAuditRecordsOfPolicy() throws Exception {
62         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-group-name/my-name/1.2.3");
63         checkRequest(POLICY_AUDIT_ENDPOINT + "/my-name/1.2.3");
64     }
65
66     private void checkRequest(String uri) throws Exception {
67         Invocation.Builder invocationBuilder = sendRequest(uri);
68         Response rawresp = invocationBuilder.get();
69         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
70
71         // verify it fails when no authorization info is included
72         checkUnauthRequest(uri, req -> req.get());
73     }
74 }