624451ca76ab6194ac875c10951115a3d04020cd
[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
36 import org.jline.utils.Log;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToByteArrayOutputStream;
41 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToStringWriter;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.AboutHttpServlet;
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
49     @BeforeClass
50     public static void before() throws IOException {
51         //create temporary odl folder structure in tmp
52         Files.createDirectories(new File(REPO_MDSAL_DIR).toPath());
53         Files.createDirectories(new File(REPO_YANGTOOLS_DIR).toPath());
54     }
55
56     @AfterClass
57     public static void after() throws IOException {
58         //delete created dirs
59         delete(new File("system/"));
60     }
61
62     private static void delete(File file) throws IOException {
63
64         for (File childFile : file.listFiles()) {
65
66             if (childFile.isDirectory()) {
67                 delete(childFile);
68             } else {
69                 if (!childFile.delete()) {
70                     throw new IOException();
71                 }
72             }
73         }
74
75         if (!file.delete()) {
76             throw new IOException();
77         }
78     }
79
80     @Test
81     public void testReadmeRequest() throws IOException, ServletException {
82         AboutHelperServlet servlet = new AboutHelperServlet();
83         HttpServletRequest request = mock(HttpServletRequest.class);
84         HttpServletResponse response = mock(HttpServletResponse.class);
85         when(request.getRequestURI()).thenReturn("/about");
86         ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
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(printOut.getStringWriter().getBuffer().toString());
92         assertTrue(printOut.getStringWriter().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         ServletOutputStreamToByteArrayOutputStream printOut = new ServletOutputStreamToByteArrayOutputStream();
102         when(response.getOutputStream()).thenReturn(printOut);
103         servlet.doGet(request, response);
104         verify(response).setStatus(200);
105         verify(response).setContentType("image/bmp");
106         assertTrue(printOut.getByteArrayOutputStream().size() > 0);
107     }
108
109     @Test
110     public void testGetGroupId() {
111         AboutHelperServlet sv = new AboutHelperServlet();
112         assertNotNull(sv.getGroupIdOrDefault(null));
113     }
114     
115
116     private class AboutHelperServlet extends AboutHttpServlet {
117
118         /**
119          *
120          */
121         private static final long serialVersionUID = 1L;
122
123         @Override
124         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
125             super.doGet(req, resp);
126         }
127         @Override
128         public String getGroupIdOrDefault(String def) {
129                 return super.getGroupIdOrDefault(def);
130         }
131         @Override
132         protected String getManifestValue(String key) {
133                 if(key == "Bundle-SymbolicName") {
134                         return "org.onap.ccsdk.features.sdnr.wt.sdnr-wt-data-provider-provider";
135                 }
136                 return super.getManifestValue(key);
137         }
138     }
139 }