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 static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToByteArrayOutputStream;
38 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToStringWriter;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.AboutHttpServlet;
41 public class TestAbout {
43 private static final String REPO_MDSAL_DIR = "system/org/opendaylight/mdsal/mdsal-binding-api/3.0.9/";
44 private static final String REPO_YANGTOOLS_DIR = "system/org/opendaylight/yangtools/odl-yangtools-common/2.1.11";
47 public static void before() throws IOException {
48 //create temporary odl folder structure in tmp
49 Files.createDirectories(new File(REPO_MDSAL_DIR).toPath());
50 Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath());
54 public static void after() throws IOException {
56 delete(new File("system/"));
59 private static void delete(File file) throws IOException {
61 for (File childFile : file.listFiles()) {
63 if (childFile.isDirectory()) {
66 if (!childFile.delete()) {
67 throw new IOException();
73 throw new IOException();
78 public void testReadmeRequest() throws IOException, ServletException {
79 AboutHelperServlet servlet = new AboutHelperServlet();
80 HttpServletRequest request = mock(HttpServletRequest.class);
81 HttpServletResponse response = mock(HttpServletResponse.class);
82 when(request.getRequestURI()).thenReturn("/about");
83 ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
84 when(response.getOutputStream()).thenReturn(printOut);
85 servlet.doGet(request, response);
86 verify(response).setStatus(200);
87 verify(response).setContentType("text/plain");
88 System.out.println(printOut.getStringWriter().getBuffer().toString());
89 assertTrue(printOut.getStringWriter().getBuffer().length() > 0);
93 public void testReadmeResourceRequest() throws IOException, ServletException {
94 AboutHelperServlet servlet = new AboutHelperServlet();
95 HttpServletRequest request = mock(HttpServletRequest.class);
96 HttpServletResponse response = mock(HttpServletResponse.class);
97 when(request.getRequestURI()).thenReturn("/about/test.bmp");
98 ServletOutputStreamToByteArrayOutputStream printOut = new ServletOutputStreamToByteArrayOutputStream();
99 when(response.getOutputStream()).thenReturn(printOut);
100 servlet.doGet(request, response);
101 verify(response).setStatus(200);
102 verify(response).setContentType("image/bmp");
103 assertTrue(printOut.getByteArrayOutputStream().size() > 0);
108 private class AboutHelperServlet extends AboutHttpServlet {
113 private static final long serialVersionUID = 1L;
116 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
117 super.doGet(req, resp);