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.ArrayList;
48 import java.util.List;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.mockito.InjectMocks;
56 import org.mockito.Matchers;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.controller.sample.PostDroolsController;
61 import org.onap.portalapp.framework.MockitoTestSuite;
62 import org.onap.portalsdk.core.command.PostDroolsBean;
63 import org.onap.portalsdk.core.service.PostDroolsService;
64 import org.springframework.web.servlet.ModelAndView;
66 public class PostDroolsControllerTest {
69 PostDroolsController postDroolsController = new PostDroolsController();
73 MockitoAnnotations.initMocks(this);
76 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
77 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
78 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
81 PostDroolsService postDroolsService;
84 public void droolsTest() {
85 ModelAndView expectedResluts = postDroolsController.drools(mockedRequest);
86 assertNull(expectedResluts.getViewName());
90 public void getDroolsTest() throws IOException {
91 List<PostDroolsBean> beanList = new ArrayList<>();
92 Mockito.when(postDroolsService.fetchDroolBeans()).thenReturn(beanList);
93 StringWriter sw = new StringWriter();
94 PrintWriter writer = new PrintWriter(sw);
95 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
96 postDroolsController.getDrools(mockedRequest,mockedResponse);
100 public void getDroolsExceptionTest() throws IOException {
101 Mockito.when(postDroolsService.fetchDroolBeans()).thenThrow(new NullPointerException());
102 postDroolsController.getDrools(mockedRequest,mockedResponse);
107 public void getDroolDetailsTest() throws IOException {
108 Mockito.when(mockedRequest.getParameter("selectedFile")).thenReturn("test");
109 Mockito.when(postDroolsService.retrieveClass("test")).thenReturn("result");
110 StringWriter sw = new StringWriter();
111 PrintWriter writer = new PrintWriter(sw);
112 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
113 postDroolsController.getDroolDetails(mockedRequest,mockedResponse);
116 public void getDroolDetailsExceptionTest() throws IOException {
117 Mockito.when(mockedRequest.getParameter("selectedFile")).thenReturn("test");
118 Mockito.when(postDroolsService.retrieveClass("test")).thenThrow(new NullPointerException());
119 StringWriter sw = new StringWriter();
120 PrintWriter writer = new PrintWriter(sw);
121 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
122 postDroolsController.getDroolDetails(mockedRequest,mockedResponse);
126 public void searchTest() throws Exception {
127 String json = "{\"postDroolsBean\": {\"droolsFile\":\"test\",\"className\":null,\"selectedRules\":null}}";
128 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
129 Mockito.when(postDroolsService.execute(Matchers.anyString(),
130 Matchers.anyString(), Matchers.anyString())).thenReturn("result");
131 StringWriter sw = new StringWriter();
132 PrintWriter writer = new PrintWriter(sw);
133 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
134 assertNull(postDroolsController.search(mockedRequest,mockedResponse));
138 public void searchExceptionTest() throws Exception {
139 assertNull(postDroolsController.search(mockedRequest,mockedResponse));