2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.assertNotNull;
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.DeploymentGroup;
36 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
37 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
41 * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
43 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
45 private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch";
46 private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
49 public void testSwagger() throws Exception {
50 super.testSwagger(DEPLOY_GROUP_ENDPOINT);
51 super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
55 public void testUpdateGroupPolicies() throws Exception {
56 Entity<DeploymentGroups> entgrp = makeDeploymentGroupsEntity();
58 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
59 Response rawresp = invocationBuilder.post(entgrp);
60 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
61 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
62 assertNotNull(resp.getErrorDetails());
64 rawresp = invocationBuilder.post(entgrp);
65 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
66 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
67 assertNotNull(resp.getErrorDetails());
69 // verify it fails when no authorization info is included
70 checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
74 public void testDeployPolicies() throws Exception {
75 Entity<PdpDeployPolicies> entgrp = makePdpPoliciesEntity();
77 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
78 Response rawresp = invocationBuilder.post(entgrp);
79 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
80 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
81 assertNotNull(resp.getErrorDetails());
83 rawresp = invocationBuilder.post(entgrp);
84 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
85 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
86 assertNotNull(resp.getErrorDetails());
88 // verify it fails when no authorization info is included
89 checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
92 private Entity<DeploymentGroups> makeDeploymentGroupsEntity() {
93 DeploymentSubGroup subgrp = new DeploymentSubGroup();
94 subgrp.setPdpType("drools");
96 DeploymentGroup group = new DeploymentGroup();
97 group.setName("drools-group");
98 group.setDeploymentSubgroups(Arrays.asList(subgrp));
100 DeploymentGroups groups = new DeploymentGroups();
101 groups.setGroups(Arrays.asList(group));
103 return Entity.entity(groups, MediaType.APPLICATION_JSON);
106 private Entity<PdpDeployPolicies> makePdpPoliciesEntity() {
107 ToscaConceptIdentifierOptVersion pol1 = new ToscaConceptIdentifierOptVersion("policy-a", "1");
108 ToscaConceptIdentifierOptVersion pol2 = new ToscaConceptIdentifierOptVersion("policy-b", null);
110 PdpDeployPolicies policies = new PdpDeployPolicies();
111 policies.setPolicies(Arrays.asList(pol1, pol2));
113 return Entity.entity(policies, MediaType.APPLICATION_JSON);