01dc8fcaf1f9a3a72ffa6da527124afe391ac532
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18
19 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
20
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;
25
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;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
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;
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         @BeforeClass
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() );
50         }
51         @AfterClass
52         public static void after() throws IOException {
53                 //delete created dirs
54                 delete(new File("system/"));
55         }
56         private static void delete(File file) throws IOException {
57  
58                 for (File childFile : file.listFiles()) {
59  
60                         if (childFile.isDirectory()) {
61                                 delete(childFile);
62                         } else {
63                                 if (!childFile.delete()) {
64                                         throw new IOException();
65                                 }
66                         }
67                 }
68  
69                 if (!file.delete()) {
70                         throw new IOException();
71                 }
72         }
73         @Test
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() {
81
82                         @Override
83                         public void write(int arg0) throws IOException {
84                                 out.write(arg0);
85                         }
86                 };
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);
93         }
94
95         @Test
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() {
103
104                         @Override
105                         public void write(int arg0) throws IOException {
106                                 out.write(arg0);
107                         }
108                 };
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);
114         }
115
116         
117
118         private class AboutHelperServlet extends AboutHttpServlet{
119
120                 /**
121                  *
122                  */
123                 private static final long serialVersionUID = 1L;
124
125                 @Override
126                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
127                         super.doGet(req, resp);
128                 }
129
130         }
131 }