c650023075f25c4e29481a214524f54050198b32
[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.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;
41
42 public class TestMyServlet extends Mockito {
43
44     private static final String GETHELPDIRECTORYBASE = "data";
45     private static final String CONTENT = "abbccdfkamaosie aksdmais";
46
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()) {
51             folder.mkdirs();
52         }
53         try {
54             if (file.exists()) {
55                 file.delete();
56             }
57             Files.write(file.toPath(), content.getBytes(),
58                     new OpenOption[] {WRITE, CREATE_NEW, CREATE, TRUNCATE_EXISTING});
59         } catch (IOException e1) {
60             fail(e1.getMessage());
61         }
62     }
63
64     @Before
65     public void init() {
66         try {
67             ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE));
68         } catch (IOException e) {
69             e.printStackTrace();
70         }
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         ServletOutputStreamToStringWriter out = new ServletOutputStreamToStringWriter();
90         when(response.getOutputStream()).thenReturn(out);
91
92         HelpServlet helpServlet = null;
93         try {
94             helpServlet = new HelpServlet();
95             System.out.println("Server created");
96             createHelpFile("/meta.json", CONTENT);
97
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());
104         }
105         if (helpServlet != null) {
106             helpServlet.close();
107         }
108
109         String result = out.getStringWriter().toString().trim();
110         System.out.println("Result: '" + result + "'");
111         assertEquals(CONTENT, result);
112     }
113
114     @Test
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");
120     }
121
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);
127
128         when(request.getRequestURI()).thenReturn("help/" + fn);
129         ServletOutputStreamToStringWriter out = new ServletOutputStreamToStringWriter();
130         try {
131             when(response.getOutputStream()).thenReturn(out);
132             helpServlet.doGet(request, response);
133         } catch (ServletException | IOException e) {
134             fail(e.getMessage());
135         }
136         try {
137             out.close();
138         } catch (Exception e) {
139         }
140         try {
141             helpServlet.close();
142         } catch (Exception e) {
143         }
144
145         assertEquals("compare content for " + fn, CONTENT, out.getStringWriter().toString().trim());
146     }
147 }