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