48e512ad9782520a6ecfccaf394413149163c229
[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.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;
29 import java.io.File;
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;
41
42 public class TestAbout {
43
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";
46
47     @BeforeClass
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());
52     }
53
54     @AfterClass
55     public static void after() throws IOException {
56         //delete created dirs
57         delete(new File("system/"));
58     }
59
60     private static void delete(File file) throws IOException {
61
62         for (File childFile : file.listFiles()) {
63
64             if (childFile.isDirectory()) {
65                 delete(childFile);
66             } else {
67                 if (!childFile.delete()) {
68                     throw new IOException();
69                 }
70             }
71         }
72
73         if (!file.delete()) {
74             throw new IOException();
75         }
76     }
77
78     @Test
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);
91     }
92
93     @Test
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);
105     }
106
107     @Test
108     public void testGetGroupId() {
109         AboutHelperServlet sv = new AboutHelperServlet();
110         assertNotNull(sv.getGroupIdOrDefault(null));
111     }
112
113
114     private class AboutHelperServlet extends AboutHttpServlet {
115
116         /**
117          *
118          */
119         private static final long serialVersionUID = 1L;
120
121         @Override
122         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
123             super.doGet(req, resp);
124         }
125         @Override
126         public String getGroupIdOrDefault(String def) {
127                 return super.getGroupIdOrDefault(def);
128         }
129         @Override
130         protected String getManifestValue(String key) {
131                 if(key == "Bundle-SymbolicName") {
132                         return "org.onap.ccsdk.features.sdnr.wt.sdnr-wt-data-provider-provider";
133                 }
134                 return super.getManifestValue(key);
135         }
136     }
137 }