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 ******************************************************************************/
19 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.AboutHttpServlet;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.StringWriter;
35 import java.nio.file.Files;
36 import javax.servlet.ServletException;
37 import javax.servlet.ServletOutputStream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
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";
46 public static void before() throws IOException {
47 //create temporary odl folder structure in tmp
48 Files.createDirectories(new File(REPO_MDSAL_DIR).toPath() );
49 Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath() );
52 public static void after() throws IOException {
54 delete(new File("system/"));
56 private static void delete(File file) throws IOException {
58 for (File childFile : file.listFiles()) {
60 if (childFile.isDirectory()) {
63 if (!childFile.delete()) {
64 throw new IOException();
70 throw new IOException();
74 public void testReadmeRequest() throws IOException, ServletException {
75 AboutHelperServlet servlet =new AboutHelperServlet();
76 HttpServletRequest request = mock(HttpServletRequest.class);
77 HttpServletResponse response = mock(HttpServletResponse.class);
78 when(request.getRequestURI()).thenReturn("/about");
79 StringWriter out = new StringWriter();
80 ServletOutputStream printOut = new ServletOutputStream() {
83 public void write(int arg0) throws IOException {
87 when(response.getOutputStream()).thenReturn(printOut);
88 servlet.doGet(request,response);
89 verify(response).setStatus(200);
90 verify(response).setContentType("text/plain");
91 System.out.println(out.getBuffer().toString());
92 assertTrue(out.getBuffer().length()>0);
96 public void testReadmeResourceRequest() throws IOException, ServletException {
97 AboutHelperServlet servlet =new AboutHelperServlet();
98 HttpServletRequest request = mock(HttpServletRequest.class);
99 HttpServletResponse response = mock(HttpServletResponse.class);
100 when(request.getRequestURI()).thenReturn("/about/test.bmp");
101 ByteArrayOutputStream out = new ByteArrayOutputStream();
102 ServletOutputStream printOut = new ServletOutputStream() {
105 public void write(int arg0) throws IOException {
109 when(response.getOutputStream()).thenReturn(printOut);
110 servlet.doGet(request,response);
111 verify(response).setStatus(200);
112 verify(response).setContentType("image/bmp");
113 assertTrue(out.size()>0);
118 private class AboutHelperServlet extends AboutHttpServlet{
123 private static final long serialVersionUID = 1L;
126 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
127 super.doGet(req, resp);