adcb523c0126fe82513ff9966fcd5530df49dea7
[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.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import java.io.File;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.attribute.FileAttribute;
36 import java.nio.file.attribute.PosixFilePermission;
37 import java.nio.file.attribute.PosixFilePermissions;
38 import java.text.ParseException;
39 import java.text.SimpleDateFormat;
40 import java.util.Comparator;
41 import java.util.EnumSet;
42 import java.util.Set;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.ccsdk.features.sdnr.wt.common.test.ServletOutputStreamToStringWriter;
50 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema.YangFileProvider;
51 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema.YangFilename;
52 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema.YangSchemaHttpServlet;
53
54 public class TestYangProvider {
55
56     private static final String TESTPATH = "cache/schema/";
57     private static final String TESTPATH_BASE = "cache/";
58     private static final String TESTPATH_SEPERATE_1 = TESTPATH_BASE+"abc1/";
59
60
61     @BeforeClass
62     public static void init() {
63         Set<PosixFilePermission> perms;
64         FileAttribute<?> attr;
65         perms = EnumSet.noneOf(PosixFilePermission.class);
66
67         perms.add(PosixFilePermission.OWNER_READ);
68         perms.add(PosixFilePermission.OWNER_WRITE);
69         perms.add(PosixFilePermission.OWNER_EXECUTE);
70
71         attr = PosixFilePermissions.asFileAttribute(perms);
72         try {
73             Files.createDirectories(new File(TESTPATH).toPath(), attr);
74             new File(TESTPATH + new YangFilename("module1", "2010-01-01").getFilename()).createNewFile();
75             new File(TESTPATH + new YangFilename("module2", "2010-01-01").getFilename()).createNewFile();
76             new File(TESTPATH + new YangFilename("module2", "2010-04-01").getFilename()).createNewFile();
77             new File(TESTPATH + new YangFilename("module3", "2010-01-01").getFilename()).createNewFile();
78             new File(TESTPATH + new YangFilename("module4", "2010-05-01").getFilename()).createNewFile();
79             new File(TESTPATH + new YangFilename("module5", "2010-01-11").getFilename()).createNewFile();
80             new File(TESTPATH + new YangFilename("module6", "2010-01-01").getFilename()).createNewFile();
81             Files.createDirectories(new File(TESTPATH_SEPERATE_1).toPath(), attr);
82             new File(TESTPATH_SEPERATE_1 + new YangFilename("module7", "2011-01-01").getFilename()).createNewFile();
83             } catch (IOException | ParseException e) {
84
85         }
86     }
87
88     @AfterClass
89     public static void deinit() {
90         try {
91             Files.walk(new File("cache").toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile)
92                     .forEach(File::delete);
93         } catch (IOException e) {
94             System.err.println(e);
95         }
96     }
97
98     @Test
99     public void testExisting() {
100         YangFileProvider provider = new YangFileProvider(TESTPATH_BASE, TESTPATH);
101         assertTrue(provider.hasFileForModule("module1", "2010-01-01"));
102         assertTrue(provider.hasFileForModule("module2"));
103         assertTrue(provider.hasFileForModule("module3"));
104         assertFalse(provider.hasFileForModule("module5", "2010-01-01"));
105         assertTrue(provider.hasFileForModule("module7"));
106     }
107
108     @Test
109     public void testRevision() throws ParseException {
110         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
111         YangFileProvider provider = new YangFileProvider(TESTPATH_BASE, TESTPATH);
112         YangFilename f1 = provider.getFileForModule("module1", "2010-01-01");
113         assertEquals("module1", f1.getModule());
114         assertEquals(sdf.parse("2010-01-01"), f1.getRevision());
115         YangFilename f2 = provider.getFileForModule("module2");
116         assertEquals("module2", f2.getModule());
117         assertEquals(sdf.parse("2010-04-01"), f2.getRevision());
118         f2 = provider.getFileForModule("module2", "2010-02-01");
119         assertEquals("module2", f2.getModule());
120         assertEquals(sdf.parse("2010-04-01"), f2.getRevision());
121         YangFilename f3 = provider.getFileForModule("module3");
122         assertEquals("module3", f3.getModule());
123         assertEquals(sdf.parse("2010-01-01"), f3.getRevision());
124         f3 = provider.getFileForModule("module3", "2010-04-01");
125         assertNull(f3);
126     }
127
128     @Test
129     public void testServlet() throws IOException, ServletException {
130         HelpYangSchemaHttpServlet servlet = new HelpYangSchemaHttpServlet();
131         HttpServletRequest req = mock(HttpServletRequest.class);
132         HttpServletResponse resp = mock(HttpServletResponse.class);
133
134         when(req.getRequestURI()).thenReturn("/yang-schema/module1");
135         ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
136         when(resp.getOutputStream()).thenReturn(printOut);
137         servlet.doGet(req, resp);
138         verify(resp).setStatus(200);
139         verify(resp).setContentType("text/plain");
140
141     }
142
143     @Test
144     public void testServletBad() throws IOException, ServletException {
145         HelpYangSchemaHttpServlet servlet = new HelpYangSchemaHttpServlet();
146         HttpServletRequest req = mock(HttpServletRequest.class);
147         HttpServletResponse resp = mock(HttpServletResponse.class);
148
149         when(req.getRequestURI()).thenReturn("/yang-schema/module1/2020-01-01");
150         ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
151         when(resp.getOutputStream()).thenReturn(printOut);
152         servlet.doGet(req, resp);
153         verify(resp).sendError(HttpServletResponse.SC_NOT_FOUND);
154
155     }
156
157     @Test
158     public void testServletNear() throws IOException, ServletException {
159         HelpYangSchemaHttpServlet servlet = new HelpYangSchemaHttpServlet();
160         HttpServletRequest req = mock(HttpServletRequest.class);
161         HttpServletResponse resp = mock(HttpServletResponse.class);
162
163         when(req.getRequestURI()).thenReturn("/yang-schema/module2/2010-03-01");
164         ServletOutputStreamToStringWriter printOut = new ServletOutputStreamToStringWriter();
165         when(resp.getOutputStream()).thenReturn(printOut);
166         servlet.doGet(req, resp);
167         verify(resp).setStatus(200);
168         verify(resp).setContentType("text/plain");
169
170     }
171
172     private static class HelpYangSchemaHttpServlet extends YangSchemaHttpServlet {
173
174         /**
175          *
176          */
177         private static final long serialVersionUID = 1L;
178
179         @Override
180         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
181             super.doGet(req, resp);
182         }
183     }
184
185
186 }