6d537e38ba2c66d3107eba2756502585619527a8
[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 import static org.mockito.Mockito.*;
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.net.HttpURLConnection;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.WriteListener;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.odlux.OdluxBundleLoaderImpl;
33 import org.onap.ccsdk.features.sdnr.wt.odlux.ResFilesServlet;
34 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
35 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoader;
36
37 public class TestResFileServlet {
38
39     PublicResFilesServlet servlet;
40
41     @Test
42     public void test() throws ServletException {
43         servlet = new PublicResFilesServlet();
44         servlet.init();
45
46         OdluxBundleLoader loader = OdluxBundleLoaderImpl.getInstance();
47         OdluxBundle b = new OdluxBundle();
48         b.setBundleName("b1");
49         b.setIndex(9);
50         b.setLoader(loader);
51         b.initialize();
52         System.out.println("Subtest1");
53         testResReq("odlux/index.html", HttpURLConnection.HTTP_OK);
54         System.out.println("Subtest2");
55         testResReq("odlux/fragmich.txt", HttpURLConnection.HTTP_NOT_FOUND);
56         System.out.println("Done");
57     }
58
59     private void testResReq(String res, int responseCode) {
60         HttpServletRequest req = mock(HttpServletRequest.class);
61         HttpServletResponse resp = mock(HttpServletResponse.class);
62         when(req.getRequestURI()).thenReturn(res);
63         StringWriter out = new StringWriter();
64         ServletOutputStream printOut = new ServletOutputStream() {
65
66             @Override
67             public void write(int arg0) throws IOException {
68                 out.write(arg0);
69             }
70
71             @Override
72             public boolean isReady() {
73                 return false;
74             }
75
76             @Override
77             public void setWriteListener(WriteListener writeListener) {
78
79             }
80         };
81         try {
82             when(resp.getOutputStream()).thenReturn(printOut);
83             servlet.doGet(req, resp);
84         } catch (ServletException | IOException e) {
85             fail(e.getMessage());
86         }
87         verify(resp).setStatus(responseCode);
88     }
89
90
91     @SuppressWarnings("serial")
92     private class PublicResFilesServlet extends ResFilesServlet {
93         @Override
94         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
95             super.doGet(req, resp);
96         }
97
98         @Override
99         public String getMimeType(String fileName) {
100             return "mimetype";
101         }
102     }
103 }