Add simple deploy/undeploy REST API
[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.assertNull;
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.PdpGroup;
34 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
35 import org.onap.policy.models.pap.concepts.PdpPolicies;
36 import org.onap.policy.models.pap.concepts.PdpSubGroup;
37 import org.onap.policy.pdp.common.models.Policy;
38
39 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
40
41     private static final String DEPLOY_GROUP_ENDPOINT = "pdps";
42     private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
43
44     @Test
45     public void testSwagger() throws Exception {
46         super.testSwagger(DEPLOY_GROUP_ENDPOINT);
47         super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
48     }
49
50     @Test
51     public void testDeployGroup() throws Exception {
52         Entity<PdpGroup> entgrp = makePdpGroupEntity();
53
54         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
55         Response rawresp = invocationBuilder.post(entgrp);
56         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
57         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
58         assertNull(resp.getErrorDetails());
59
60         rawresp = invocationBuilder.post(entgrp);
61         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
62         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
63         assertNull(resp.getErrorDetails());
64
65         // verify it fails when no authorization info is included
66         checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
67     }
68
69     @Test
70     public void testDeployPolicies() throws Exception {
71         Entity<PdpPolicies> entgrp = makePdpPoliciesEntity();
72
73         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
74         Response rawresp = invocationBuilder.post(entgrp);
75         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
76         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
77         assertNull(resp.getErrorDetails());
78
79         rawresp = invocationBuilder.post(entgrp);
80         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
81         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
82         assertNull(resp.getErrorDetails());
83
84         // verify it fails when no authorization info is included
85         checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
86     }
87
88     private Entity<PdpGroup> makePdpGroupEntity() {
89         PdpSubGroup subgrp = new PdpSubGroup();
90         subgrp.setPdpType("drools");
91
92         PdpGroup group = new PdpGroup();
93         group.setName("drools-group");
94         group.setDescription("my description");
95         group.setVersion("my-version");
96         group.setPdpSubgroups(Arrays.asList(subgrp));
97
98         return Entity.entity(group, MediaType.APPLICATION_JSON);
99     }
100
101     private Entity<PdpPolicies> makePdpPoliciesEntity() {
102         Policy pol1 = new Policy();
103         pol1.setName("policy-a");
104         pol1.setPolicyVersion("1");
105
106         Policy pol2 = new Policy();
107         pol2.setName("policy-b");
108
109         PdpPolicies policies = new PdpPolicies();
110         policies.setPolicies(Arrays.asList(pol1, pol2));
111
112         return Entity.entity(policies, MediaType.APPLICATION_JSON);
113     }
114 }