e51cc68437feb5b4f9903626c9f78ac8c933612f
[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 org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22
23 import java.io.File;
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;
43
44 public class TestMyServlet extends Mockito {
45
46     private static final String GETHELPDIRECTORYBASE = "data";
47     private static final String CONTENT = "abbccdfkamaosie aksdmais";
48
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()) {
53             folder.mkdirs();
54         }
55         try {
56             if(file.exists()) {
57                 file.delete();
58             }
59             Files.write( file.toPath(),content.getBytes(),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     @After
74     public void deinit() {
75         this.init();
76     }
77
78     @Test
79     public void testServlet() throws Exception {
80
81         System.out.println("Test get");
82
83         HttpServletRequest request = mock(HttpServletRequest.class);
84         HttpServletResponse response = mock(HttpServletResponse.class);
85
86         when(request.getRequestURI()).thenReturn("help/");
87         when(request.getQueryString()).thenReturn("?meta");
88
89         StringWriter stringWriter = new StringWriter();
90         ServletOutputStream out=new ServletOutputStream() {
91
92             @Override
93             public void write(int arg0) throws IOException {
94                 stringWriter.write(arg0);
95             }
96         };
97         when(response.getOutputStream()).thenReturn(out);
98
99         HelpServlet helpServlet=null;
100         try {
101             helpServlet = new HelpServlet();
102             System.out.println("Server created");
103             createHelpFile("/meta.json",CONTENT);
104
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());
111         }
112         if (helpServlet != null) {
113             helpServlet.close();
114         }
115
116         String result = stringWriter.toString().trim();
117         System.out.println("Result: '" + result + "'");
118         assertEquals(CONTENT,result);
119     }
120
121     @Test
122     public void testServlet2() {
123         this.testGetRequest("test/0.4.0/test.txt");
124         this.testGetRequest("test.css");
125         this.testGetRequest("test.eps");
126         this.testGetRequest("test.pdf");
127     }
128
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);
134
135         when(request.getRequestURI()).thenReturn("help/"+fn);
136         StringWriter sw = new StringWriter();
137         ServletOutputStream out = new ServletOutputStream() {
138
139             @Override
140             public void write(int arg0) throws IOException {
141                 sw.write(arg0);
142             }
143         };
144         try {
145             when(response.getOutputStream()).thenReturn(out);
146             helpServlet.doGet(request, response);
147         } catch (ServletException | IOException e) {
148             fail(e.getMessage());
149         }
150         try {
151             out.close();
152         } catch (Exception e) {
153         }
154         try {
155             helpServlet.close();
156         } catch (Exception e) {
157         }
158
159         assertEquals(CONTENT,sw.toString().trim());
160     }
161 }