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