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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
27 import java.util.Arrays;
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;
34 import org.junit.Test;
35 import org.onap.policy.models.pap.concepts.PdpGroup;
36 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
37 import org.onap.policy.models.pap.concepts.PdpPolicies;
38 import org.onap.policy.models.pap.concepts.PdpSubGroup;
39 import org.onap.policy.models.pap.concepts.Policy;
41 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
43 private static final String DEPLOY_GROUP_ENDPOINT = "pdps";
44 private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
47 public void testSwagger() throws Exception {
48 super.testSwagger(DEPLOY_GROUP_ENDPOINT);
49 super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
53 public void testDeployGroup() throws Exception {
54 final Entity<PdpGroup> entgrp = makePdpGroupEntity();
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());
62 rawresp = invocationBuilder.post(entgrp);
63 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
64 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
65 assertNull(resp.getErrorDetails());
67 // verify it fails when no authorization info is included
68 checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
72 public void testDeployPolicies() throws Exception {
73 final Entity<PdpPolicies> entgrp = makePdpPoliciesEntity();
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());
81 rawresp = invocationBuilder.post(entgrp);
82 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
83 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
84 assertNull(resp.getErrorDetails());
86 // verify it fails when no authorization info is included
87 checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
90 private Entity<PdpGroup> makePdpGroupEntity() {
91 final PdpSubGroup subgrp = new PdpSubGroup();
92 subgrp.setPdpType("drools");
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));
100 return Entity.entity(group, MediaType.APPLICATION_JSON);
103 private Entity<PdpPolicies> makePdpPoliciesEntity() {
104 final Policy pol1 = new Policy();
105 pol1.setName("policy-a");
106 pol1.setPolicyVersion("1");
108 final Policy pol2 = new Policy();
109 pol2.setName("policy-b");
111 final PdpPolicies policies = new PdpPolicies();
112 policies.setPolicies(Arrays.asList(pol1, pol2));
114 return Entity.entity(policies, MediaType.APPLICATION_JSON);