1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.odlux.test;
20 import static org.junit.Assert.*;
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.net.HttpURLConnection;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletOutputStream;
29 import javax.servlet.WriteListener;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import static org.mockito.Mockito.*;
33 import org.junit.Test;
34 import org.onap.ccsdk.features.sdnr.wt.odlux.OdluxBundleLoaderImpl;
35 import org.onap.ccsdk.features.sdnr.wt.odlux.ResFilesServlet;
36 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
37 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoader;
39 public class TestResFileServlet {
41 PublicResFilesServlet servlet;
44 public void test() throws ServletException {
45 servlet = new PublicResFilesServlet();
48 OdluxBundleLoader loader = OdluxBundleLoaderImpl.getInstance();
49 OdluxBundle b = new OdluxBundle();
50 b.setBundleName("b1");
54 System.out.println("Subtest1");
55 testResReq("odlux/index.html", HttpURLConnection.HTTP_OK);
56 System.out.println("Subtest2");
57 testResReq("odlux/fragmich.txt", HttpURLConnection.HTTP_NOT_FOUND);
58 System.out.println("Done");
61 private void testResReq(String res, int responseCode) {
62 HttpServletRequest req = mock(HttpServletRequest.class);
63 HttpServletResponse resp = mock(HttpServletResponse.class);
64 when(req.getRequestURI()).thenReturn(res);
65 StringWriter out = new StringWriter();
66 ServletOutputStream printOut = new ServletOutputStream() {
69 public void write(int arg0) throws IOException {
74 public boolean isReady() {
79 public void setWriteListener(WriteListener writeListener) {
84 when(resp.getOutputStream()).thenReturn(printOut);
85 servlet.doGet(req, resp);
86 } catch (ServletException | IOException e) {
89 verify(resp).setStatus(responseCode);
93 @SuppressWarnings("serial")
94 private class PublicResFilesServlet extends ResFilesServlet {
96 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
97 super.doGet(req, resp);
100 public String getMimeType(String fileName) {