Merge "Added Junits for POLICY-SDK-APP controllers"
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / DashboardControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * POLICY-SDK-APP
4  * ================================================================================
5  * Copyright (C) 2018 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 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25
26 import java.io.UnsupportedEncodingException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
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.dao.SystemLogDbDao;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.xacml.std.pap.StdEngine;
40 import org.springframework.mock.web.MockHttpServletResponse;
41
42 public class DashboardControllerTest{
43         private static Logger logger = FlexLogger.getLogger(DashboardControllerTest.class);
44         
45         private static CommonClassDao commonClassDao;
46         private static SystemLogDbDao systemDAO;
47         private static PolicyController ctrl = null;
48         private HttpServletRequest request = null;
49         private DashboardController controller = null;
50         private MockHttpServletResponse response = null;
51         private Path repo;
52         StdEngine engine = null;
53         
54         @Before
55         public void setUp() throws Exception {
56                 logger.info("setUp: Entering");
57                 controller = new DashboardController();
58                 commonClassDao = Mockito.mock(CommonClassDao.class);
59                 systemDAO = Mockito.mock(SystemLogDbDao.class); 
60                 controller.setSystemLogDbDao(systemDAO);
61                 controller.setCommonClassDao(commonClassDao);
62                 request = mock(HttpServletRequest.class);       
63                 response =  new MockHttpServletResponse();
64                 repo = Paths.get("src/test/resources/pdps");
65                 engine = new StdEngine(repo);
66                 ctrl = new PolicyController();
67                 PolicyController.setPapEngine(engine);
68                 controller.setPolicyController(ctrl);
69                 logger.info("setUp: exit");
70         }
71         
72         @Test
73         public void testGetData() {
74                 try {
75                         controller.getData(request, response);
76                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("availableLoggingDatas"));
77                 } catch (UnsupportedEncodingException e) {
78                         logger.error("Exception Occured"+e);
79                         fail();
80                 }
81         }
82
83         @Test
84         public void testGetPAPStatusData() {
85                 try {
86                         controller.getPAPStatusData(request, response);
87                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("papTableDatas"));
88                 } catch (UnsupportedEncodingException e) {
89                         logger.error("Exception Occured"+e);
90                         fail();
91                 }
92         }
93
94         @Test
95         public void testGetPDPStatusData() {
96                 try {
97                         controller.getPDPStatusData(request, response);
98                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("pdpTableDatas"));
99                 } catch (UnsupportedEncodingException e) {
100                         logger.error("Exception Occured"+e);
101                         fail();
102                 }
103         }
104
105         @Test
106         public void testGetPolicyActivityData() {
107                 try {
108                         controller.getPolicyActivityData(request, response);
109                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("policyActivityTableDatas"));
110                 } catch (UnsupportedEncodingException e) {
111                         logger.error("Exception Occured"+e);
112                         fail();
113                 }
114         }
115
116         @Test
117         public void testGetSystemAlertData() {
118                 try {
119                         controller.getSystemAlertData(request, response);
120                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("systemAlertsTableDatas"));
121                 } catch (UnsupportedEncodingException e) {
122                         logger.error("Exception Occured"+e);
123                         fail();
124                 }
125         }
126 }
127