Consolidate PolicyRestAdapter setup
[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-2019 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
23 package org.onap.policy.controller;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28
29 import java.io.UnsupportedEncodingException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.dao.SystemLogDbDao;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.xacml.std.pap.StdEngine;
43 import org.springframework.mock.web.MockHttpServletResponse;
44
45 public class DashboardControllerTest {
46     private static Logger logger = FlexLogger.getLogger(DashboardControllerTest.class);
47
48     private static CommonClassDao commonClassDao;
49     private static SystemLogDbDao systemDAO;
50     private static PolicyController ctrl = null;
51     private HttpServletRequest request = null;
52     private DashboardController controller = null;
53     private MockHttpServletResponse response = null;
54     private Path repo;
55     StdEngine engine = null;
56
57     /**
58      * setUp.
59      *
60      * @throws Exception should not get one
61      */
62     @Before
63     public void setUp() throws Exception {
64         logger.info("setUp: Entering");
65         controller = new DashboardController();
66         commonClassDao = Mockito.mock(CommonClassDao.class);
67         systemDAO = Mockito.mock(SystemLogDbDao.class);
68         controller.setSystemLogDbDao(systemDAO);
69         controller.setCommonClassDao(commonClassDao);
70         request = mock(HttpServletRequest.class);
71         response = new MockHttpServletResponse();
72         repo = Paths.get("src/test/resources/pdps");
73         engine = new StdEngine(repo);
74         ctrl = new PolicyController();
75         PolicyController.setPapEngine(engine);
76         controller.setPolicyController(ctrl);
77         logger.info("setUp: exit");
78     }
79
80     @Test
81     public void testGetData() {
82         try {
83             controller.getData(request, response);
84             assertTrue(response.getContentAsString() != null
85                     && response.getContentAsString().contains("availableLoggingDatas"));
86         } catch (UnsupportedEncodingException e) {
87             logger.error("Exception Occured" + e);
88             fail();
89         }
90     }
91
92     @Test
93     public void testGetPAPStatusData() {
94         try {
95             controller.getPAPStatusData(request, response);
96             assertTrue(
97                     response.getContentAsString() != null && response.getContentAsString().contains("papTableDatas"));
98         } catch (UnsupportedEncodingException e) {
99             logger.error("Exception Occured" + e);
100             fail();
101         }
102     }
103
104     @Test
105     public void testGetPDPStatusData() {
106         try {
107             controller.getPDPStatusData(request, response);
108             assertTrue(
109                     response.getContentAsString() != null && response.getContentAsString().contains("pdpTableDatas"));
110         } catch (UnsupportedEncodingException e) {
111             logger.error("Exception Occured" + e);
112             fail();
113         }
114     }
115
116     @Test
117     public void testGetPolicyActivityData() {
118         try {
119             controller.getPolicyActivityData(request, response);
120             assertTrue(response.getContentAsString() != null
121                     && response.getContentAsString().contains("policyActivityTableDatas"));
122         } catch (UnsupportedEncodingException e) {
123             logger.error("Exception Occured" + e);
124             fail();
125         }
126     }
127
128     @Test
129     public void testGetSystemAlertData() {
130         try {
131             controller.getSystemAlertData(request, response);
132             assertTrue(response.getContentAsString() != null
133                     && response.getContentAsString().contains("systemAlertsTableDatas"));
134         } catch (UnsupportedEncodingException e) {
135             logger.error("Exception Occured" + e);
136             fail();
137         }
138     }
139 }