217735c40b2f928f281c7277c9a64808d8661e01
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.odlux.test;
19
20 import static org.junit.Assert.*;
21
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.net.HttpURLConnection;
25
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;
38
39 public class TestResFileServlet {
40
41     PublicResFilesServlet servlet;
42
43     @Test
44     public void test() throws ServletException {
45         servlet = new PublicResFilesServlet();
46                 servlet.init();
47
48         OdluxBundleLoader loader = OdluxBundleLoaderImpl.getInstance();
49         OdluxBundle b = new OdluxBundle();
50         b.setBundleName("b1");
51         b.setIndex(9);
52         b.setLoader(loader);
53         b.initialize();
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");
59     }
60
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() {
67
68             @Override
69             public void write(int arg0) throws IOException {
70                 out.write(arg0);
71             }
72
73             @Override
74             public boolean isReady() {
75                 return false;
76             }
77
78             @Override
79             public void setWriteListener(WriteListener writeListener) {
80
81             }
82         };
83         try {
84             when(resp.getOutputStream()).thenReturn(printOut);
85             servlet.doGet(req, resp);
86         } catch (ServletException | IOException e) {
87             fail(e.getMessage());
88         }
89         verify(resp).setStatus(responseCode);
90     }
91
92
93     @SuppressWarnings("serial")
94     private class PublicResFilesServlet extends ResFilesServlet {
95         @Override
96         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
97             super.doGet(req, resp);
98         }
99         @Override
100         public String getMimeType(String fileName) {
101                 return "mimetype";
102         }
103     }
104 }