diff options
author | Herbert Eiselt <herbert.eiselt@highstreet-technologies.com> | 2019-02-28 18:15:10 +0100 |
---|---|---|
committer | Herbert Eiselt <herbert.eiselt@highstreet-technologies.com> | 2019-02-28 18:15:40 +0100 |
commit | af992dbf68c75093d56b12f99ca410ce06fd7ad5 (patch) | |
tree | 16a96f642f3f6841be8904d6c77fc86e74ed5565 /sdnr/wt/helpserver/provider/src/test/java | |
parent | 49b155ec687cdf58fb51fe8245a2f5f4582b68f0 (diff) |
SDN-R helpserver show version info
SDN-R helpserver show version info
Change-Id: Ifa9d5a8c914f942151594c583c31184eef6f3296
Issue-ID: SDNC-681
Signed-off-by: Herbert Eiselt <herbert.eiselt@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/helpserver/provider/src/test/java')
-rw-r--r-- | sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestExtract.java | 88 | ||||
-rw-r--r-- | sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfObject.java | 69 | ||||
-rw-r--r-- | sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfrastructure.java (renamed from sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpServer.java) | 28 | ||||
-rw-r--r-- | sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestMyServlet.java | 161 |
4 files changed, 338 insertions, 8 deletions
diff --git a/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestExtract.java b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestExtract.java new file mode 100644 index 000000000..5f8558270 --- /dev/null +++ b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestExtract.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * ============LICENSE_END========================================================================== + ******************************************************************************/ +package org.onap.ccsdk.features.sdnr.wt.helpserver.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource; +import org.osgi.framework.Bundle; + +public class TestExtract extends Mockito { + + private boolean called = false; + private String testFile; + + @Test + public void test() { + + Bundle myBundle = mock(Bundle.class); + + final ClassLoader loader = this.getClass().getClassLoader(); + try { + when(myBundle.getEntryPaths(anyString())).thenAnswer(invocation -> { + if (!called) { + Object[] args = invocation.getArguments(); + System.out.println("Get files from: " + args[0]); + Enumeration<URL> e = loader.getResources((String) args[0]); + List<String> res = new ArrayList<>(); + while (e.hasMoreElements()) { + String resourceFileName = e.nextElement().toString(); + System.out.println("is file: " + resourceFileName); + res.add(resourceFileName); + } + called = true; + return Collections.enumeration(res); + } else { + return null; + } + }); + when(myBundle.getEntry(anyString())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + System.out.println("GetEntrye input: "+args[0]); + return new URL(testFile = (String) args[0]); + }); + + String TMPDATAFOLDER = "tmpData"; + + ExtactBundleResource.copyBundleResoucesRecursively(myBundle, TMPDATAFOLDER, "help/meta.json"); + + assertTrue("Test file not found: "+testFile, new File(TMPDATAFOLDER+testFile).exists()); + + ExtactBundleResource.deleteRecursively(new File(TMPDATAFOLDER+"file:")); + + assertFalse("Test not deleted: "+testFile, new File(TMPDATAFOLDER+"file:").exists()); + + } catch (Exception e) { + e.printStackTrace(); + fail("Exception" + e); + } + + + + } + +} diff --git a/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfObject.java b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfObject.java new file mode 100644 index 000000000..9d63da500 --- /dev/null +++ b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfObject.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * ============LICENSE_END========================================================================== + ******************************************************************************/ +package org.onap.ccsdk.features.sdnr.wt.helpserver.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject; + +public class TestHelpInfObject { + + private static final String GETHELPDIRECTORYBASE = "data"; + private static final String ROOT = "tmp"; + private static final String CONTENT = "abc"; + + @Before + public void init() { + try { + ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE)); + } catch (IOException e) { + e.printStackTrace(); + } + } + @After + public void deinit() { + this.init(); + } + @Test + public void test() { + File root=new File(HelpInfrastructureObject.getHelpDirectoryBase()+"/"+ROOT); + TestMyServlet.createHelpFile("/"+ROOT+"/test/0.4.0/README.md", CONTENT); + TestMyServlet.createHelpFile("/"+ROOT+"/test2/0.4.0/README.md", CONTENT); + TestMyServlet.createHelpFile("/"+ROOT+"/test3/abc/0.4.0/README.md", CONTENT); + TestMyServlet.createHelpFile("/"+ROOT+"/test3/abc1/0.4.0/README.md", CONTENT); + TestMyServlet.createHelpFile("/"+ROOT+"/test5/0.4.0/README.md", CONTENT); + + try { + new HelpInfrastructureObject(root.toPath()); + } catch (URISyntaxException e) { + fail(e.getMessage()); + } + + + } + + +} diff --git a/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpServer.java b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfrastructure.java index db6d783b9..dfe649a87 100644 --- a/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpServer.java +++ b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestHelpInfrastructure.java @@ -1,4 +1,3 @@ -package org.onap.ccsdk.features.sdnr.wt.helpserver.test; /******************************************************************************* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt @@ -16,20 +15,33 @@ package org.onap.ccsdk.features.sdnr.wt.helpserver.test; * the License. * ============LICENSE_END========================================================================== ******************************************************************************/ -import static org.junit.Assert.assertNotNull; +package org.onap.ccsdk.features.sdnr.wt.helpserver.test; + +import static org.junit.Assert.fail; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; import org.junit.Test; -import org.onap.ccsdk.features.sdnr.wt.helpserver.HelpServlet; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject; -public class TestHelpServer { +public class TestHelpInfrastructure { @Test - public void test() throws Exception { + public void test() { - HelpServlet helpServelet = new HelpServlet(); + final ClassLoader loader = this.getClass().getClassLoader(); + URL url = loader.getResource("help/meta.json"); + Path path; + try { + path = Paths.get(url.toURI()); + HelpInfrastructureObject helpInfrastuctureObject = new HelpInfrastructureObject(path); + System.out.println("Help: "+helpInfrastuctureObject); + } catch (URISyntaxException e) { + fail(e.getMessage()); + } - assertNotNull("Helpservelet created", helpServelet); - helpServelet.close(); } } diff --git a/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestMyServlet.java b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestMyServlet.java new file mode 100644 index 000000000..e51cc6843 --- /dev/null +++ b/sdnr/wt/helpserver/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/helpserver/test/TestMyServlet.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt + * ================================================================================================= + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * ============LICENSE_END========================================================================== + ******************************************************************************/ +package org.onap.ccsdk.features.sdnr.wt.helpserver.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.OpenOption; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.ccsdk.features.sdnr.wt.helpserver.HelpServlet; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource; +import org.onap.ccsdk.features.sdnr.wt.helpserver.data.HelpInfrastructureObject; +import static java.nio.file.StandardOpenOption.CREATE_NEW; +import static java.nio.file.StandardOpenOption.WRITE; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; + +public class TestMyServlet extends Mockito { + + private static final String GETHELPDIRECTORYBASE = "data"; + private static final String CONTENT = "abbccdfkamaosie aksdmais"; + + public static void createHelpFile(String filename,String content) { + File file=new File(HelpInfrastructureObject.getHelpDirectoryBase() + filename); + File folder = file.getParentFile(); + if(!folder.exists()) { + folder.mkdirs(); + } + try { + if(file.exists()) { + file.delete(); + } + Files.write( file.toPath(),content.getBytes(),new OpenOption[] { WRITE, CREATE_NEW , CREATE, TRUNCATE_EXISTING}); + } catch (IOException e1) { + fail(e1.getMessage()); + } + } + + @Before + public void init() { + try { + ExtactBundleResource.deleteRecursively(new File(GETHELPDIRECTORYBASE)); + } catch (IOException e) { + e.printStackTrace(); + } + } + @After + public void deinit() { + this.init(); + } + + @Test + public void testServlet() throws Exception { + + System.out.println("Test get"); + + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + + when(request.getRequestURI()).thenReturn("help/"); + when(request.getQueryString()).thenReturn("?meta"); + + StringWriter stringWriter = new StringWriter(); + ServletOutputStream out=new ServletOutputStream() { + + @Override + public void write(int arg0) throws IOException { + stringWriter.write(arg0); + } + }; + when(response.getOutputStream()).thenReturn(out); + + HelpServlet helpServlet=null; + try { + helpServlet = new HelpServlet(); + System.out.println("Server created"); + createHelpFile("/meta.json",CONTENT); + + helpServlet.doOptions(request, response); + System.out.println("Get calling"); + helpServlet.doGet(request, response); + System.out.println("Get called"); + } catch (Exception e) { + fail(e.getMessage()); + } + if (helpServlet != null) { + helpServlet.close(); + } + + String result = stringWriter.toString().trim(); + System.out.println("Result: '" + result + "'"); + assertEquals(CONTENT,result); + } + + @Test + public void testServlet2() { + this.testGetRequest("test/0.4.0/test.txt"); + this.testGetRequest("test.css"); + this.testGetRequest("test.eps"); + this.testGetRequest("test.pdf"); + } + + private void testGetRequest(String fn) { + HelpServlet helpServlet = new HelpServlet(); + createHelpFile("/"+fn,CONTENT); + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + + when(request.getRequestURI()).thenReturn("help/"+fn); + StringWriter sw = new StringWriter(); + ServletOutputStream out = new ServletOutputStream() { + + @Override + public void write(int arg0) throws IOException { + sw.write(arg0); + } + }; + try { + when(response.getOutputStream()).thenReturn(out); + helpServlet.doGet(request, response); + } catch (ServletException | IOException e) { + fail(e.getMessage()); + } + try { + out.close(); + } catch (Exception e) { + } + try { + helpServlet.close(); + } catch (Exception e) { + } + + assertEquals(CONTENT,sw.toString().trim()); + } +} |