21640bf9847c2b22ec31a1bfea283342ab7b82c7
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupDeleteControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
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  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25
26 import javax.ws.rs.client.Invocation;
27 import javax.ws.rs.client.SyncInvoker;
28 import javax.ws.rs.core.Response;
29 import org.junit.Test;
30 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
31 import org.springframework.test.context.ActiveProfiles;
32
33 /**
34  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
35  */
36 @ActiveProfiles({ "test", "default" })
37 public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
38
39     private static final String GROUP_NOT_FOUND = "group not found";
40     private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
41     private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
42
43     @Test
44     public void testSwagger() throws Exception {
45         super.testSwagger(DELETE_GROUP_ENDPOINT + "/{name}");
46
47         super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}");
48         super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}/versions/{version}");
49     }
50
51     @Test
52     public void testDeleteGroup() throws Exception {
53         String uri = DELETE_GROUP_ENDPOINT + "/my-name";
54
55         Invocation.Builder invocationBuilder = sendRequest(uri);
56         Response rawresp = invocationBuilder.delete();
57         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
58         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
59         assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails());
60
61         rawresp = invocationBuilder.delete();
62         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
63         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
64         assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails());
65
66         // verify it fails when no authorization info is included
67         checkUnauthRequest(uri, SyncInvoker::delete);
68     }
69
70     @Test
71     public void testDeletePolicy() throws Exception {
72         String uri = DELETE_POLICIES_ENDPOINT + "/my-name";
73
74         Invocation.Builder invocationBuilder = sendRequest(uri);
75         Response rawresp = invocationBuilder.delete();
76         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
77         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
78         assertEquals("cannot find policy: my-name null", resp.getErrorDetails());
79
80         rawresp = invocationBuilder.delete();
81         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
82         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
83         assertEquals("cannot find policy: my-name null", resp.getErrorDetails());
84
85         // verify it fails when no authorization info is included
86         checkUnauthRequest(uri, SyncInvoker::delete);
87     }
88
89     @Test
90     public void testDeletePolicyVersion() throws Exception {
91         String uri = DELETE_POLICIES_ENDPOINT + "/my-name/versions/3";
92
93         Invocation.Builder invocationBuilder = sendRequest(uri);
94         Response rawresp = invocationBuilder.delete();
95         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
96         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
97         assertEquals("cannot find policy: my-name 3", resp.getErrorDetails());
98
99         rawresp = invocationBuilder.delete();
100         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
101         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
102         assertEquals("cannot find policy: my-name 3", resp.getErrorDetails());
103
104         // verify it fails when no authorization info is included
105         checkUnauthRequest(uri, SyncInvoker::delete);
106     }
107 }