5d9011ab0f42692714c320609ec005cfcd3eee61
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
23
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.AboutHttpServlet;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.StringWriter;
37 import java.nio.file.Files;
38 import javax.servlet.ServletException;
39 import javax.servlet.ServletOutputStream;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 public class TestAbout {
44
45     private static final String REPO_MDSAL_DIR = "system/org/opendaylight/mdsal/mdsal-binding-api/3.0.9/";
46     private static final String REPO_YANGTOOLS_DIR = "system/org/opendaylight/yangtools/odl-yangtools-common/2.1.11";
47
48     @BeforeClass
49     public static void before() throws IOException {
50         //create temporary odl folder structure in tmp
51         Files.createDirectories(new File(REPO_MDSAL_DIR).toPath());
52         Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath());
53     }
54
55     @AfterClass
56     public static void after() throws IOException {
57         //delete created dirs
58         delete(new File("system/"));
59     }
60
61     private static void delete(File file) throws IOException {
62
63         for (File childFile : file.listFiles()) {
64
65             if (childFile.isDirectory()) {
66                 delete(childFile);
67             } else {
68                 if (!childFile.delete()) {
69                     throw new IOException();
70                 }
71             }
72         }
73
74         if (!file.delete()) {
75             throw new IOException();
76         }
77     }
78
79     @Test
80     public void testReadmeRequest() throws IOException, ServletException {
81         AboutHelperServlet servlet = new AboutHelperServlet();
82         HttpServletRequest request = mock(HttpServletRequest.class);
83         HttpServletResponse response = mock(HttpServletResponse.class);
84         when(request.getRequestURI()).thenReturn("/about");
85         StringWriter out = new StringWriter();
86         ServletOutputStream printOut = new ServletOutputStream() {
87
88             @Override
89             public void write(int arg0) throws IOException {
90                 out.write(arg0);
91             }
92         };
93         when(response.getOutputStream()).thenReturn(printOut);
94         servlet.doGet(request, response);
95         verify(response).setStatus(200);
96         verify(response).setContentType("text/plain");
97         System.out.println(out.getBuffer().toString());
98         assertTrue(out.getBuffer().length() > 0);
99     }
100
101     @Test
102     public void testReadmeResourceRequest() throws IOException, ServletException {
103         AboutHelperServlet servlet = new AboutHelperServlet();
104         HttpServletRequest request = mock(HttpServletRequest.class);
105         HttpServletResponse response = mock(HttpServletResponse.class);
106         when(request.getRequestURI()).thenReturn("/about/test.bmp");
107         ByteArrayOutputStream out = new ByteArrayOutputStream();
108         ServletOutputStream printOut = new ServletOutputStream() {
109
110             @Override
111             public void write(int arg0) throws IOException {
112                 out.write(arg0);
113             }
114         };
115         when(response.getOutputStream()).thenReturn(printOut);
116         servlet.doGet(request, response);
117         verify(response).setStatus(200);
118         verify(response).setContentType("image/bmp");
119         assertTrue(out.size() > 0);
120     }
121
122
123
124     private class AboutHelperServlet extends AboutHttpServlet {
125
126         /**
127          *
128          */
129         private static final long serialVersionUID = 1L;
130
131         @Override
132         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
133             super.doGet(req, resp);
134         }
135
136     }
137 }