7b79c58cb0a72f501f42606aca3b5d790e432c97
[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.portalsdk.analytics.system.fusion.controller;
39
40 import static org.junit.Assert.assertNull;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import javax.servlet.ServletOutputStream;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.InjectMocks;
55 import org.mockito.Matchers;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalsdk.analytics.util.XSSFilter;
59 import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite;
60 import org.onap.portalsdk.core.service.DataAccessService;
61 import org.owasp.esapi.ESAPI;
62 import org.owasp.esapi.Encoder;
63 import org.powermock.api.mockito.PowerMockito;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ ESAPI.class, XSSFilter.class})
69 public class FileServletControllerTest {
70         
71         @InjectMocks
72         FileServletController fileServletController = new FileServletController();
73
74         @Before
75         public void setup() {
76                 MockitoAnnotations.initMocks(this);
77         }
78
79         @Mock
80         DataAccessService dataAccessService;
81         
82         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
83         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
84         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
85         
86         @Test
87         public void handleRequestInternalTest() throws Exception {
88                 List<Object> objs = new ArrayList<>();
89                 byte [] byteVal = "test123".getBytes();
90                 objs.add(byteVal);
91                 PowerMockito.mockStatic(ESAPI.class);
92                 Encoder encoder = PowerMockito.mock(Encoder.class);
93                 when(ESAPI.encoder()).thenReturn(encoder);
94                 when(encoder.canonicalize(Matchers.anyString())).thenReturn("test");
95                 when(mockedRequest.getParameter(Matchers.anyString())).thenReturn("test");
96                 ServletOutputStream sos = mock(ServletOutputStream.class);
97                 when(mockedResponse.getOutputStream()).thenReturn(sos);
98                 when(fileServletController.getDataAccessService().executeNamedQuery(Matchers.anyString(), Matchers.anyMap(), Matchers.anyMap())).thenReturn(objs);
99                 assertNull(fileServletController.handleRequestInternal(mockedRequest, mockedResponse));
100         }
101 }
102
103