14f4ab0a3f2648df37b791a3424ef96dcd56e9ef
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
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  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller;
39
40 import static org.junit.Assert.assertNull;
41
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;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
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;
62
63 public class BroadcastListControllerTest {
64
65         @InjectMocks
66         BroadcastListController broadcastListController = new BroadcastListController();
67
68         @Before
69         public void setup() {
70                 MockitoAnnotations.initMocks(this);
71         }
72
73         @Mock
74         BroadcastService broadcastService;
75         
76         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
77         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
78         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
79         NullPointerException nullPointerException = new NullPointerException();
80
81         
82         @Test
83         public void broadcastListTest() {
84                 Mockito.when(broadcastService.getBcModel(mockedRequest)).thenReturn(new HashMap<>());
85                 ModelAndView expectedResult = broadcastListController.broadcastList(mockedRequest);
86                 assertNull(expectedResult.getViewName());
87         }
88
89         @Test
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);
98         }
99
100         @Test
101         public void getBroadcastExceptionTest() throws IOException {
102                 Mockito.when(broadcastService.getBcModel(mockedRequest)).thenThrow(nullPointerException);
103                 broadcastListController.getBroadcast(mockedRequest, mockedResponse);
104         }
105
106         @Test
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));
114         }
115
116         @Test
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));
122         }
123         
124         
125         @Test
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));
133         }
134
135         @Test
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));
141         }
142 }