Move deploy/undeploy REST classes
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupDeployControllerV1.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 import static org.junit.Assert.assertNotNull;
26
27 import java.util.Arrays;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.junit.Test;
33 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
34 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
35 import org.onap.policy.models.pdp.concepts.PdpGroup;
36 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
38
39 /**
40  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
41  */
42 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
43
44     private static final String DEPLOY_GROUP_ENDPOINT = "pdps";
45     private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
46
47     @Test
48     public void testSwagger() throws Exception {
49         super.testSwagger(DEPLOY_GROUP_ENDPOINT);
50         super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
51     }
52
53     @Test
54     public void testDeployGroup() throws Exception {
55         Entity<PdpGroup> entgrp = makePdpGroupEntity();
56
57         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
58         Response rawresp = invocationBuilder.post(entgrp);
59         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
60         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
61         assertNotNull(resp.getErrorDetails());
62
63         rawresp = invocationBuilder.post(entgrp);
64         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
65         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
66         assertNotNull(resp.getErrorDetails());
67
68         // verify it fails when no authorization info is included
69         checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
70     }
71
72     @Test
73     public void testDeployPolicies() throws Exception {
74         Entity<PdpDeployPolicies> entgrp = makePdpPoliciesEntity();
75
76         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
77         Response rawresp = invocationBuilder.post(entgrp);
78         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
79         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
80         assertNotNull(resp.getErrorDetails());
81
82         rawresp = invocationBuilder.post(entgrp);
83         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
84         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
85         assertNotNull(resp.getErrorDetails());
86
87         // verify it fails when no authorization info is included
88         checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
89     }
90
91     private Entity<PdpGroup> makePdpGroupEntity() {
92         PdpSubGroup subgrp = new PdpSubGroup();
93         subgrp.setPdpType("drools");
94
95         PdpGroup group = new PdpGroup();
96         group.setName("drools-group");
97         group.setDescription("my description");
98         group.setPdpSubgroups(Arrays.asList(subgrp));
99
100         return Entity.entity(group, MediaType.APPLICATION_JSON);
101     }
102
103     private Entity<PdpDeployPolicies> makePdpPoliciesEntity() {
104         ToscaPolicyIdentifierOptVersion pol1 = new ToscaPolicyIdentifierOptVersion("policy-a", "1");
105         ToscaPolicyIdentifierOptVersion pol2 = new ToscaPolicyIdentifierOptVersion("policy-b", null);
106
107         PdpDeployPolicies policies = new PdpDeployPolicies();
108         policies.setPolicies(Arrays.asList(pol1, pol2));
109
110         return Entity.entity(policies, MediaType.APPLICATION_JSON);
111     }
112 }