87d33f32f943f9050e484bf8500982a48c561185
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.test;
19
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import java.io.File;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.List;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.ccsdk.features.sdnr.wt.helpserver.data.ExtactBundleResource;
32 import org.osgi.framework.Bundle;
33
34 public class TestExtract extends Mockito {
35
36     private boolean called = false;
37     private String testFile;
38
39     @Test
40     public void test() {
41
42         Bundle myBundle = mock(Bundle.class);
43
44         final ClassLoader loader = this.getClass().getClassLoader();
45         try {
46             when(myBundle.getEntryPaths(anyString())).thenAnswer(invocation -> {
47                 if (!called) {
48                     Object[] args = invocation.getArguments();
49                     System.out.println("Get files from: " + args[0]);
50                     Enumeration<URL> e = loader.getResources((String) args[0]);
51                     List<String> res = new ArrayList<>();
52                     while (e.hasMoreElements()) {
53                         String resourceFileName = e.nextElement().toString();
54                         System.out.println("is file: " + resourceFileName);
55                         res.add(resourceFileName);
56                     }
57                     called = true;
58                     return Collections.enumeration(res);
59                 } else {
60                     return null;
61                 }
62             });
63             when(myBundle.getEntry(anyString())).thenAnswer(invocation -> {
64                 Object[] args = invocation.getArguments();
65                 System.out.println("GetEntrye input: " + args[0]);
66                 return new URL(testFile = (String) args[0]);
67             });
68
69             String TMPDATAFOLDER = "tmpData";
70
71             ExtactBundleResource.copyBundleResoucesRecursively(myBundle, TMPDATAFOLDER, "help/meta.json");
72
73             assertTrue("Test file not found: " + testFile, new File(TMPDATAFOLDER + testFile).exists());
74
75             ExtactBundleResource.deleteRecursively(new File(TMPDATAFOLDER + "file:"));
76
77             assertFalse("Test not deleted: " + testFile, new File(TMPDATAFOLDER + "file:").exists());
78
79         } catch (Exception e) {
80             e.printStackTrace();
81             fail("Exception" + e);
82         }
83
84
85
86     }
87
88 }