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.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;
43 public class TestMyServlet extends Mockito {
45 private static final String GETHELPDIRECTORYBASE = "data";
46 private static final String CONTENT = "abbccdfkamaosie aksdmais";
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()) {
58 Files.write(file.toPath(), content.getBytes(),
59 new OpenOption[] {WRITE, CREATE_NEW, CREATE, TRUNCATE_EXISTING});
60 } catch (IOException e1) {
61 fail(e1.getMessage());
68 ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE));
69 } catch (IOException e) {
75 public void deinit() {
80 public void testServlet() throws Exception {
82 System.out.println("Test get");
84 HttpServletRequest request = mock(HttpServletRequest.class);
85 HttpServletResponse response = mock(HttpServletResponse.class);
87 when(request.getRequestURI()).thenReturn("help/");
88 when(request.getQueryString()).thenReturn("?meta");
90 StringWriter stringWriter = new StringWriter();
91 ServletOutputStream out = new ServletOutputStream() {
94 public void write(int arg0) throws IOException {
95 stringWriter.write(arg0);
98 when(response.getOutputStream()).thenReturn(out);
100 HelpServlet helpServlet = null;
102 helpServlet = new HelpServlet();
103 System.out.println("Server created");
104 createHelpFile("/meta.json", CONTENT);
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());
113 if (helpServlet != null) {
117 String result = stringWriter.toString().trim();
118 System.out.println("Result: '" + result + "'");
119 assertEquals(CONTENT, result);
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");
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);
136 when(request.getRequestURI()).thenReturn("help/" + fn);
137 StringWriter sw = new StringWriter();
138 ServletOutputStream out = new ServletOutputStream() {
141 public void write(int arg0) throws IOException {
146 when(response.getOutputStream()).thenReturn(out);
147 helpServlet.doGet(request, response);
148 } catch (ServletException | IOException e) {
149 fail(e.getMessage());
153 } catch (Exception e) {
157 } catch (Exception e) {
160 assertEquals("compare content for " + fn, CONTENT, sw.toString().trim());