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.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToByteArrayOutputStream;
39 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToStringWriter;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.AboutHttpServlet;
42 public class TestAbout {
44 private static final String REPO_MDSAL_DIR = "system/org/opendaylight/mdsal/mdsal-binding-api/3.0.9/";
45 private static final String REPO_YANGTOOLS_DIR = "system/org/opendaylight/yangtools/odl-yangtools-common/2.1.11";
48 public static void before() throws IOException {
49 //create temporary odl folder structure in tmp
50 Files.createDirectories(new File(REPO_MDSAL_DIR).toPath());
51 Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath());
55 public static void after() throws IOException {
57 delete(new File("system/"));
60 private static void delete(File file) throws IOException {
62 for (File childFile : file.listFiles()) {
64 if (childFile.isDirectory()) {
67 if (!childFile.delete()) {
68 throw new IOException();
74 throw new IOException();
79 public void testReadmeRequest() throws IOException, ServletException {
80 AboutHelperServlet servlet = new AboutHelperServlet();
81 HttpServletRequest request = mock(HttpServletRequest.class);
82 HttpServletResponse response = mock(HttpServletResponse.class);
83 when(request.getRequestURI()).thenReturn("/about");
84 ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
85 when(response.getOutputStream()).thenReturn(printOut);
86 servlet.doGet(request, response);
87 verify(response).setStatus(200);
88 verify(response).setContentType("text/plain");
89 System.out.println(printOut.getStringWriter().getBuffer().toString());
90 assertTrue(printOut.getStringWriter().getBuffer().length() > 0);
94 public void testReadmeResourceRequest() throws IOException, ServletException {
95 AboutHelperServlet servlet = new AboutHelperServlet();
96 HttpServletRequest request = mock(HttpServletRequest.class);
97 HttpServletResponse response = mock(HttpServletResponse.class);
98 when(request.getRequestURI()).thenReturn("/about/test.bmp");
99 ServletOutputStreamToByteArrayOutputStream printOut = new ServletOutputStreamToByteArrayOutputStream();
100 when(response.getOutputStream()).thenReturn(printOut);
101 servlet.doGet(request, response);
102 verify(response).setStatus(200);
103 verify(response).setContentType("image/bmp");
104 assertTrue(printOut.getByteArrayOutputStream().size() > 0);
108 public void testGetGroupId() {
109 AboutHelperServlet sv = new AboutHelperServlet();
110 assertNotNull(sv.getGroupIdOrDefault(null));
114 private class AboutHelperServlet extends AboutHttpServlet {
119 private static final long serialVersionUID = 1L;
122 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
123 super.doGet(req, resp);
126 public String getGroupIdOrDefault(String def) {
127 return super.getGroupIdOrDefault(def);
130 protected String getManifestValue(String key) {
131 if(key == "Bundle-SymbolicName") {
132 return "org.onap.ccsdk.features.sdnr.wt.sdnr-wt-data-provider-provider";
134 return super.getManifestValue(key);