92ac0470273c7c05bf27faaa94aa5cd90d105690
[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 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;
28 import java.io.File;
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;
40
41 public class TestAbout {
42
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";
45
46     @BeforeClass
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());
51     }
52
53     @AfterClass
54     public static void after() throws IOException {
55         //delete created dirs
56         delete(new File("system/"));
57     }
58
59     private static void delete(File file) throws IOException {
60
61         for (File childFile : file.listFiles()) {
62
63             if (childFile.isDirectory()) {
64                 delete(childFile);
65             } else {
66                 if (!childFile.delete()) {
67                     throw new IOException();
68                 }
69             }
70         }
71
72         if (!file.delete()) {
73             throw new IOException();
74         }
75     }
76
77     @Test
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);
90     }
91
92     @Test
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);
104     }
105
106
107
108     private class AboutHelperServlet extends AboutHttpServlet {
109
110         /**
111          *
112          */
113         private static final long serialVersionUID = 1L;
114
115         @Override
116         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
117             super.doGet(req, resp);
118         }
119
120     }
121 }