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