Server Stubs PAP
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupDeployControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2022 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 java.util.List;
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 import org.junit.Test;
34 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
35 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
36 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
37 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
38 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
40 import org.springframework.test.context.ActiveProfiles;
41
42 /**
43  * Note: this tests failure cases; success cases are tested by tests in the "e2e" package.
44  */
45 @ActiveProfiles({ "test", "default" })
46 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
47
48     private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch";
49     private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
50
51     @Test
52     public void testSwagger() throws Exception {
53         super.testSwagger(DEPLOY_GROUP_ENDPOINT);
54         super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
55     }
56
57     @Test
58     public void testUpdateGroupPolicies() throws Exception {
59         Entity<DeploymentGroups> entgrp = makeDeploymentGroupsEntity();
60
61         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
62         Response rawresp = invocationBuilder.post(entgrp);
63         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
64         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
65         assertNotNull(resp.getErrorDetails());
66
67         rawresp = invocationBuilder.post(entgrp);
68         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
69         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
70         assertNotNull(resp.getErrorDetails());
71
72         // verify it fails when no authorization info is included
73         checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
74     }
75
76     @Test
77     public void testDeployPolicies() throws Exception {
78         Entity<PdpDeployPolicies> entgrp = makePdpPoliciesEntity();
79
80         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
81         Response rawresp = invocationBuilder.post(entgrp);
82         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
83         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
84         assertNotNull(resp.getErrorDetails());
85
86         rawresp = invocationBuilder.post(entgrp);
87         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
88         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus());
89         assertNotNull(resp.getErrorDetails());
90
91         // verify it fails when no authorization info is included
92         checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
93     }
94
95     private Entity<DeploymentGroups> makeDeploymentGroupsEntity() {
96         DeploymentSubGroup subgrp = new DeploymentSubGroup();
97         subgrp.setPdpType("drools");
98
99         DeploymentGroup group = new DeploymentGroup();
100         group.setName("drools-group");
101         group.setDeploymentSubgroups(List.of(subgrp));
102
103         DeploymentGroups groups = new DeploymentGroups();
104         groups.setGroups(List.of(group));
105
106         return Entity.entity(groups, MediaType.APPLICATION_JSON);
107     }
108
109     private Entity<PdpDeployPolicies> makePdpPoliciesEntity() {
110         ToscaConceptIdentifierOptVersion pol1 = new ToscaConceptIdentifierOptVersion("policy-a", "1");
111         ToscaConceptIdentifierOptVersion pol2 = new ToscaConceptIdentifierOptVersion("policy-b", null);
112
113         PdpDeployPolicies policies = new PdpDeployPolicies();
114         policies.setPolicies(Arrays.asList(pol1, pol2));
115
116         return Entity.entity(policies, MediaType.APPLICATION_JSON);
117     }
118 }