Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / file / FileUtilsTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.utilities.file;
18
19 import static junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.function.Function;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38 /**
39  * @author EVITALIY
40  * @since 22 Oct 17
41  */
42 public class FileUtilsTest {
43
44     private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
45             .replace('.', '/') + "/test-resource.txt";
46
47     private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
48
49         try {
50             return s.available();
51         } catch (IOException e) {
52             throw new RuntimeException(e);
53         }
54     };
55
56     @Test
57     public void testReadViaInputStreamWithSlash() {
58         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
59     }
60
61     @Test
62     public void testReadViaInputStreamWithoutSlash() {
63         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
64     }
65
66     @Test(expected = NullPointerException.class)
67     public void testReadViaInputStreamNull() {
68         FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testReadViaInputStreamNotFound() {
73         FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
74     }
75
76     @Test
77     public void testWriteFilesFromFileContentHandler() throws IOException {
78         Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
79         try {
80             byte[] uploadedFileData = IOUtils.toByteArray(
81                 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
82                     ".csar"));
83             FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
84             Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
85                 dir);
86
87             assertFalse(filePaths.isEmpty());
88             assertEquals(filePaths.size(), 18);
89             for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
90                 File f = new File(fileEntry.getValue());
91                 assertTrue(f.exists());
92             }
93         }
94         finally {
95             org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
96         }
97     }
98
99     @Test
100     public void testIsValidYamlExtension() {
101         assertTrue(FileUtils.isValidYamlExtension("yaml"));
102         assertTrue(FileUtils.isValidYamlExtension("yml"));
103         assertFalse(FileUtils.isValidYamlExtension("yml1"));
104         assertFalse(FileUtils.isValidYamlExtension("zip"));
105     }
106
107     @Test
108     public void testGetFileWithoutExtention() {
109         Assert.assertEquals(FileUtils.getFileWithoutExtention("test.txt"), "test");
110     }
111
112     @Test
113     public void testGetFileWithoutExtentionContainsNoExtension() {
114         Assert.assertEquals(FileUtils.getFileWithoutExtention("test"), "test");
115     }
116
117     @Test
118     public void testGetFileExtention() {
119         Assert.assertEquals(FileUtils.getFileExtension("test.txt"), "txt");
120     }
121
122     @Test
123     public void testGetNetworkPackageName() {
124         Assert.assertEquals(FileUtils.getNetworkPackageName("heat.zip"), "heat");
125     }
126
127     @Test
128     public void testGetNetworkPackageNameWithoutExtension() {
129         Assert.assertNull(FileUtils.getNetworkPackageName("heat"));
130     }
131
132     @Test
133     public void testToByteArrayNullStream() {
134         Assert.assertNotNull(FileUtils.toByteArray(null));
135     }
136
137     @Test
138     public void testGetAllLocations() {
139         List<URL> urlList = FileUtils.getAllLocations("org/openecomp/core/utilities/file/testFileUtils.txt");
140         Assert.assertNotNull(urlList);
141         Assert.assertEquals(urlList.size(), 1);
142     }
143
144     @Test
145     public void testConvertToBytesNullObject() {
146         Assert.assertNotNull(FileUtils.convertToBytes(null, null));
147     }
148
149     @Test
150     public void testConvertToBytes() {
151         byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
152                                 FileUtils.FileExtension.YAML);
153
154         Assert.assertNotNull(bytesArray);
155     }
156
157     @Test
158     public void testConvertToBytesNotYaml() {
159         byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
160                 FileUtils.FileExtension.JSON);
161
162         Assert.assertNotNull(bytesArray);
163     }
164
165     @Test
166     public void testConvertToInputStreamNullObject() {
167         Assert.assertNull(FileUtils.convertToInputStream(null, null));
168     }
169
170     @Test
171     public void testConvertToInputStream() {
172         InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test")
173                         .collect(Collectors.toList()), FileUtils.FileExtension.YAML);
174
175         Assert.assertNotNull(inputStream);
176     }
177
178     @Test(expected = RuntimeException.class)
179     public void testLoadFileToInputStreamIncorrectFilePath() {
180         FileUtils.loadFileToInputStream("invalidfilepath");
181     }
182
183     @Test
184     public void testLoadFileToInputStream() throws IOException{
185         int i;
186         StringBuilder builder = new StringBuilder(20);
187         InputStream inputStream = FileUtils.loadFileToInputStream(
188                 "org/openecomp/core/utilities/file/testFileUtils.txt");
189         while((i = inputStream.read())!=-1) {
190             builder.append((char)i);
191         }
192
193         Assert.assertNotNull(inputStream);
194         Assert.assertEquals(builder.toString(), "hello-test");
195     }
196 }