2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller;
40 import static org.junit.Assert.assertNull;
42 import java.io.BufferedReader;
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringReader;
46 import java.io.StringWriter;
47 import java.util.HashMap;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.mockito.InjectMocks;
55 import org.mockito.Mock;
56 import org.mockito.Mockito;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.controller.sample.BroadcastListController;
59 import org.onap.portalapp.framework.MockitoTestSuite;
60 import org.onap.portalsdk.core.service.BroadcastService;
61 import org.springframework.web.servlet.ModelAndView;
63 public class BroadcastListControllerTest {
66 BroadcastListController broadcastListController = new BroadcastListController();
70 MockitoAnnotations.initMocks(this);
74 BroadcastService broadcastService;
76 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
77 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
78 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
79 NullPointerException nullPointerException = new NullPointerException();
83 public void broadcastListTest() {
84 Mockito.when(broadcastService.getBcModel(mockedRequest)).thenReturn(new HashMap<>());
85 ModelAndView expectedResult = broadcastListController.broadcastList(mockedRequest);
86 assertNull(expectedResult.getViewName());
90 public void getBroadcastTest() throws IOException {
91 Mockito.when(broadcastService.getBcModel(mockedRequest)).thenReturn(new HashMap<>());
92 Mockito.when(broadcastService.getBcModel(mockedRequest).get("messagesList")).thenReturn(new HashMap<>());
93 Mockito.when(broadcastService.getBcModel(mockedRequest).get("messageLocations")).thenReturn(new HashMap<>());
94 StringWriter sw = new StringWriter();
95 PrintWriter writer = new PrintWriter(sw);
96 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
97 broadcastListController.getBroadcast(mockedRequest, mockedResponse);
101 public void getBroadcastExceptionTest() throws IOException {
102 Mockito.when(broadcastService.getBcModel(mockedRequest)).thenThrow(nullPointerException);
103 broadcastListController.getBroadcast(mockedRequest, mockedResponse);
107 public void removeTest() throws Exception {
108 String json = "{\"broadcastMessage\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"messageText\":null,\"locationId\":null,\"startDate\":null,\"endDate\":null,\"sortOrder\":null,\"active\":true,\"siteCd\":null}}";
109 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
110 StringWriter sw = new StringWriter();
111 PrintWriter writer = new PrintWriter(sw);
112 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
113 assertNull(broadcastListController.remove(mockedRequest, mockedResponse));
117 public void removeExceptionTest() throws Exception {
118 StringWriter sw = new StringWriter();
119 PrintWriter writer = new PrintWriter(sw);
120 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
121 assertNull(broadcastListController.remove(mockedRequest, mockedResponse));
126 public void toggleActiveTest() throws Exception {
127 String json = "{\"broadcastMessage\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"messageText\":null,\"locationId\":null,\"startDate\":null,\"endDate\":null,\"sortOrder\":null,\"active\":true,\"siteCd\":null}}";
128 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
129 StringWriter sw = new StringWriter();
130 PrintWriter writer = new PrintWriter(sw);
131 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
132 assertNull(broadcastListController.toggleActive(mockedRequest, mockedResponse));
136 public void toggleActiveExceptionTest() throws Exception {
137 StringWriter sw = new StringWriter();
138 PrintWriter writer = new PrintWriter(sw);
139 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
140 assertNull(broadcastListController.toggleActive(mockedRequest, mockedResponse));