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==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.test;
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;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.OpenOption;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToStringWriter;
38 import org.onap.ccsdk.features.sdnr.wt.helpserver.HelpServlet;
39 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource;
40 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
42 public class TestMyServlet extends Mockito {
44 private static final String GETHELPDIRECTORYBASE = "data";
45 private static final String CONTENT = "abbccdfkamaosie aksdmais";
47 public static void createHelpFile(String filename, String content) {
48 File file = new File(HelpInfrastructureObject.getHelpDirectoryBase() + filename);
49 File folder = file.getParentFile();
50 if (!folder.exists()) {
57 Files.write(file.toPath(), content.getBytes(),
58 new OpenOption[] {WRITE, CREATE_NEW, CREATE, TRUNCATE_EXISTING});
59 } catch (IOException e1) {
60 fail(e1.getMessage());
67 ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE));
68 } catch (IOException e) {
74 public void deinit() {
79 public void testServlet() throws Exception {
81 System.out.println("Test get");
83 HttpServletRequest request = mock(HttpServletRequest.class);
84 HttpServletResponse response = mock(HttpServletResponse.class);
86 when(request.getRequestURI()).thenReturn("help/");
87 when(request.getQueryString()).thenReturn("?meta");
89 ServletOutputStreamToStringWriter out = new ServletOutputStreamToStringWriter();
90 when(response.getOutputStream()).thenReturn(out);
92 HelpServlet helpServlet = null;
94 helpServlet = new HelpServlet();
95 System.out.println("Server created");
96 createHelpFile("/meta.json", CONTENT);
98 helpServlet.doOptions(request, response);
99 System.out.println("Get calling");
100 helpServlet.doGet(request, response);
101 System.out.println("Get called");
102 } catch (Exception e) {
103 fail(e.getMessage());
105 if (helpServlet != null) {
109 String result = out.getStringWriter().toString().trim();
110 System.out.println("Result: '" + result + "'");
111 assertEquals(CONTENT, result);
115 public void testServlet2() {
116 this.testGetRequest("test/test.txt");
117 this.testGetRequest("test.css");
118 this.testGetRequest("test.eps");
119 this.testGetRequest("test.pdf");
122 private void testGetRequest(String fn) {
123 HelpServlet helpServlet = new HelpServlet();
124 createHelpFile("/" + fn, CONTENT);
125 HttpServletRequest request = mock(HttpServletRequest.class);
126 HttpServletResponse response = mock(HttpServletResponse.class);
128 when(request.getRequestURI()).thenReturn("help/" + fn);
129 ServletOutputStreamToStringWriter out = new ServletOutputStreamToStringWriter();
131 when(response.getOutputStream()).thenReturn(out);
132 helpServlet.doGet(request, response);
133 } catch (ServletException | IOException e) {
134 fail(e.getMessage());
138 } catch (Exception e) {
142 } catch (Exception e) {
145 assertEquals("compare content for " + fn, CONTENT, out.getStringWriter().toString().trim());