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