37243534344eaa85098d39be43420cc40fd7c042
[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
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.StringWriter;
38 import java.nio.file.Files;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43
44 public class TestAbout {
45
46         private static final String REPO_MDSAL_DIR = "system/org/opendaylight/mdsal/mdsal-binding-api/3.0.9/";
47         private static final String REPO_YANGTOOLS_DIR = "system/org/opendaylight/yangtools/odl-yangtools-common/2.1.11";
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         @AfterClass
55         public static void after() throws IOException {
56                 //delete created dirs
57                 delete(new File("system/"));
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         @Test
77         public void testReadmeRequest() throws IOException, ServletException {
78                 AboutHelperServlet servlet =new AboutHelperServlet();
79                 HttpServletRequest request = mock(HttpServletRequest.class);
80                 HttpServletResponse response =  mock(HttpServletResponse.class);
81                 when(request.getRequestURI()).thenReturn("/about");
82                 StringWriter out = new StringWriter();
83                 ServletOutputStream printOut = new ServletOutputStream() {
84
85                         @Override
86                         public void write(int arg0) throws IOException {
87                                 out.write(arg0);
88                         }
89                 };
90                 when(response.getOutputStream()).thenReturn(printOut);
91                 servlet.doGet(request,response);
92                 verify(response).setStatus(200);
93                 verify(response).setContentType("text/plain");
94                 System.out.println(out.getBuffer().toString());
95                 assertTrue(out.getBuffer().length()>0);
96         }
97
98         @Test
99         public void testReadmeResourceRequest() throws IOException, ServletException {
100                 AboutHelperServlet servlet =new AboutHelperServlet();
101                 HttpServletRequest request = mock(HttpServletRequest.class);
102                 HttpServletResponse response =  mock(HttpServletResponse.class);
103                 when(request.getRequestURI()).thenReturn("/about/test.bmp");
104                 ByteArrayOutputStream out = new ByteArrayOutputStream();
105                 ServletOutputStream printOut = new ServletOutputStream() {
106
107                         @Override
108                         public void write(int arg0) throws IOException {
109                                 out.write(arg0);
110                         }
111                 };
112                 when(response.getOutputStream()).thenReturn(printOut);
113                 servlet.doGet(request,response);
114                 verify(response).setStatus(200);
115                 verify(response).setContentType("image/bmp");
116                 assertTrue(out.size()>0);
117         }
118
119         
120
121         private class AboutHelperServlet extends AboutHttpServlet{
122
123                 /**
124                  *
125                  */
126                 private static final long serialVersionUID = 1L;
127
128                 @Override
129                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
130                         super.doGet(req, resp);
131                 }
132
133         }
134 }