2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.AboutHttpServlet;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.io.StringWriter;
38 import java.nio.file.Files;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
44 public class TestAbout {
46 private static final String REPO_MDSAL_DIR = "system/org/opendaylight/mdsal/mdsal-binding-api/3.0.9/";
47 private static final String REPO_YANGTOOLS_DIR = "system/org/opendaylight/yangtools/odl-yangtools-common/2.1.11";
50 public static void before() throws IOException {
51 //create temporary odl folder structure in tmp
52 Files.createDirectories(new File(REPO_MDSAL_DIR).toPath());
53 Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath());
57 public static void after() throws IOException {
59 delete(new File("system/"));
62 private static void delete(File file) throws IOException {
64 for (File childFile : file.listFiles()) {
66 if (childFile.isDirectory()) {
69 if (!childFile.delete()) {
70 throw new IOException();
76 throw new IOException();
81 public void testReadmeRequest() throws IOException, ServletException {
82 AboutHelperServlet servlet = new AboutHelperServlet();
83 HttpServletRequest request = mock(HttpServletRequest.class);
84 HttpServletResponse response = mock(HttpServletResponse.class);
85 when(request.getRequestURI()).thenReturn("/about");
86 StringWriter out = new StringWriter();
87 ServletOutputStream printOut = new ServletOutputStream() {
90 public void write(int arg0) throws IOException {
94 when(response.getOutputStream()).thenReturn(printOut);
95 servlet.doGet(request, response);
96 verify(response).setStatus(200);
97 verify(response).setContentType("text/plain");
98 System.out.println(out.getBuffer().toString());
99 assertTrue(out.getBuffer().length() > 0);
103 public void testReadmeResourceRequest() throws IOException, ServletException {
104 AboutHelperServlet servlet = new AboutHelperServlet();
105 HttpServletRequest request = mock(HttpServletRequest.class);
106 HttpServletResponse response = mock(HttpServletResponse.class);
107 when(request.getRequestURI()).thenReturn("/about/test.bmp");
108 ByteArrayOutputStream out = new ByteArrayOutputStream();
109 ServletOutputStream printOut = new ServletOutputStream() {
112 public void write(int arg0) throws IOException {
116 when(response.getOutputStream()).thenReturn(printOut);
117 servlet.doGet(request, response);
118 verify(response).setStatus(200);
119 verify(response).setContentType("image/bmp");
120 assertTrue(out.size() > 0);
125 private class AboutHelperServlet extends AboutHttpServlet {
130 private static final long serialVersionUID = 1L;
133 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
134 super.doGet(req, resp);