cd40e122be3b0b40a9975139a6e9b1d723a119ff
[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.wt.helpserver.test;
19
20 import static java.nio.file.StandardOpenOption.CREATE;
21 import static java.nio.file.StandardOpenOption.CREATE_NEW;
22 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
23 import static java.nio.file.StandardOpenOption.WRITE;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.StringWriter;
29 import java.nio.file.Files;
30 import java.nio.file.OpenOption;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletOutputStream;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.ccsdk.features.sdnr.wt.helpserver.HelpServlet;
40 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource;
41 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
42
43 public class TestMyServlet extends Mockito {
44
45     private static final String GETHELPDIRECTORYBASE = "data";
46     private static final String CONTENT = "abbccdfkamaosie aksdmais";
47
48     public static void createHelpFile(String filename, String content) {
49         File file = new File(HelpInfrastructureObject.getHelpDirectoryBase() + filename);
50         File folder = file.getParentFile();
51         if (!folder.exists()) {
52             folder.mkdirs();
53         }
54         try {
55             if (file.exists()) {
56                 file.delete();
57             }
58             Files.write(file.toPath(), content.getBytes(),
59                     new OpenOption[] {WRITE, CREATE_NEW, CREATE, TRUNCATE_EXISTING});
60         } catch (IOException e1) {
61             fail(e1.getMessage());
62         }
63     }
64
65     @Before
66     public void init() {
67         try {
68             ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE));
69         } catch (IOException e) {
70             e.printStackTrace();
71         }
72     }
73
74     @After
75     public void deinit() {
76         this.init();
77     }
78
79     @Test
80     public void testServlet() throws Exception {
81
82         System.out.println("Test get");
83
84         HttpServletRequest request = mock(HttpServletRequest.class);
85         HttpServletResponse response = mock(HttpServletResponse.class);
86
87         when(request.getRequestURI()).thenReturn("help/");
88         when(request.getQueryString()).thenReturn("?meta");
89
90         StringWriter stringWriter = new StringWriter();
91         ServletOutputStream out = new ServletOutputStream() {
92
93             @Override
94             public void write(int arg0) throws IOException {
95                 stringWriter.write(arg0);
96             }
97         };
98         when(response.getOutputStream()).thenReturn(out);
99
100         HelpServlet helpServlet = null;
101         try {
102             helpServlet = new HelpServlet();
103             System.out.println("Server created");
104             createHelpFile("/meta.json", CONTENT);
105
106             helpServlet.doOptions(request, response);
107             System.out.println("Get calling");
108             helpServlet.doGet(request, response);
109             System.out.println("Get called");
110         } catch (Exception e) {
111             fail(e.getMessage());
112         }
113         if (helpServlet != null) {
114             helpServlet.close();
115         }
116
117         String result = stringWriter.toString().trim();
118         System.out.println("Result: '" + result + "'");
119         assertEquals(CONTENT, result);
120     }
121
122     @Test
123     public void testServlet2() {
124         this.testGetRequest("test/test.txt");
125         this.testGetRequest("test.css");
126         this.testGetRequest("test.eps");
127         this.testGetRequest("test.pdf");
128     }
129
130     private void testGetRequest(String fn) {
131         HelpServlet helpServlet = new HelpServlet();
132         createHelpFile("/" + fn, CONTENT);
133         HttpServletRequest request = mock(HttpServletRequest.class);
134         HttpServletResponse response = mock(HttpServletResponse.class);
135
136         when(request.getRequestURI()).thenReturn("help/" + fn);
137         StringWriter sw = new StringWriter();
138         ServletOutputStream out = new ServletOutputStream() {
139
140             @Override
141             public void write(int arg0) throws IOException {
142                 sw.write(arg0);
143             }
144         };
145         try {
146             when(response.getOutputStream()).thenReturn(out);
147             helpServlet.doGet(request, response);
148         } catch (ServletException | IOException e) {
149             fail(e.getMessage());
150         }
151         try {
152             out.close();
153         } catch (Exception e) {
154         }
155         try {
156             helpServlet.close();
157         } catch (Exception e) {
158         }
159
160         assertEquals("compare content for " + fn, CONTENT, sw.toString().trim());
161     }
162 }