1c1c0fbbdfa8207fe44ba217f969b0589dcd40ce
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PDPControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.policy.controller;
23
24 import static org.junit.Assert.assertTrue;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import javax.servlet.http.HttpServletRequest;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.model.Roles;
37 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
38 import org.onap.policy.xacml.std.pap.StdPDPGroup;
39 import org.onap.policy.xacml.std.pap.StdPDPGroupStatus;
40 import org.springframework.mock.web.MockHttpServletResponse;
41
42 public class PDPControllerTest extends Mockito {
43
44     private static Logger logger = FlexLogger.getLogger(PDPControllerTest.class);
45     private Set<OnapPDPGroup> groupsData;
46     private Set<StdPDPGroup> groups;
47     private static List<Object> rolesdata;
48
49     @Before
50     public void setUp() throws Exception {
51         logger.info("setUp: Entering");
52         rolesdata = new ArrayList<>();
53         Roles roles = new Roles();
54         roles.setLoginId("Test");
55         roles.setRole("super-admin");
56         Roles roles1 = new Roles();
57         roles1.setLoginId("Test");
58         roles1.setRole("admin");
59         roles1.setScope("['com','Test']");
60         rolesdata.add(roles);
61         rolesdata.add(roles1);
62
63         groups = new HashSet<>();
64         StdPDPGroup group = new StdPDPGroup();
65         group.setId("default");
66         group.setDefault(true);
67         group.setName("default");
68         group.setDescription("The default group where new PDP's are put.");
69         group.setStatus(new StdPDPGroupStatus());
70         groups.add(group);
71         groupsData = new HashSet<>();
72         for (OnapPDPGroup g : this.groups) {
73             groupsData.add(g);
74         }
75     }
76
77     @Test
78     public void testPDPGroupData() {
79         HttpServletRequest request = mock(HttpServletRequest.class);
80         MockHttpServletResponse response = new MockHttpServletResponse();
81         PolicyController controller = mock(PolicyController.class);
82         PDPController pdpController = new PDPController();
83         pdpController.setJunit(true);;
84         pdpController.setPolicyController(controller);
85         pdpController.setGroupsData(groupsData);
86         when(controller.getRoles("Test")).thenReturn(rolesdata);
87         pdpController.getPDPGroupEntityData(request, response);
88         try {
89             assertTrue(response.getContentAsString() != null
90                     && response.getContentAsString().contains("data"));
91         } catch (UnsupportedEncodingException e) {
92             logger.error("Exception Occured" + e);
93         }
94     }
95 }