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
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==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.test;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.nio.file.Files;
27 import java.nio.file.OpenOption;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.ccsdk.features.sdnr.wt.helpserver.HelpServlet;
37 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource;
38 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject;
39 import static java.nio.file.StandardOpenOption.CREATE_NEW;
40 import static java.nio.file.StandardOpenOption.WRITE;
41 import static java.nio.file.StandardOpenOption.CREATE;
42 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
44 public class TestMyServlet extends Mockito {
46 private static final String GETHELPDIRECTORYBASE = "data";
47 private static final String CONTENT = "abbccdfkamaosie aksdmais";
49 public static void createHelpFile(String filename,String content) {
50 File file=new File(HelpInfrastructureObject.getHelpDirectoryBase() + filename);
51 File folder = file.getParentFile();
52 if(!folder.exists()) {
59 Files.write( file.toPath(),content.getBytes(),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) {
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 StringWriter stringWriter = new StringWriter();
90 ServletOutputStream out=new ServletOutputStream() {
93 public void write(int arg0) throws IOException {
94 stringWriter.write(arg0);
97 when(response.getOutputStream()).thenReturn(out);
99 HelpServlet helpServlet=null;
101 helpServlet = new HelpServlet();
102 System.out.println("Server created");
103 createHelpFile("/meta.json",CONTENT);
105 helpServlet.doOptions(request, response);
106 System.out.println("Get calling");
107 helpServlet.doGet(request, response);
108 System.out.println("Get called");
109 } catch (Exception e) {
110 fail(e.getMessage());
112 if (helpServlet != null) {
116 String result = stringWriter.toString().trim();
117 System.out.println("Result: '" + result + "'");
118 assertEquals(CONTENT,result);
122 public void testServlet2() {
123 this.testGetRequest("test/test.txt");
124 this.testGetRequest("test.css");
125 this.testGetRequest("test.eps");
126 this.testGetRequest("test.pdf");
129 private void testGetRequest(String fn) {
130 HelpServlet helpServlet = new HelpServlet();
131 createHelpFile("/"+fn,CONTENT);
132 HttpServletRequest request = mock(HttpServletRequest.class);
133 HttpServletResponse response = mock(HttpServletResponse.class);
135 when(request.getRequestURI()).thenReturn("help/"+fn);
136 StringWriter sw = new StringWriter();
137 ServletOutputStream out = new ServletOutputStream() {
140 public void write(int arg0) throws IOException {
145 when(response.getOutputStream()).thenReturn(out);
146 helpServlet.doGet(request, response);
147 } catch (ServletException | IOException e) {
148 fail(e.getMessage());
152 } catch (Exception e) {
156 } catch (Exception e) {
159 assertEquals("compare content for "+fn,CONTENT,sw.toString().trim());