Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / file / FileContentHandlerTest.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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.util.AbstractMap;
26 import java.util.Arrays;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34 /**
35  * @author EVITALIY
36  * @since 24 Oct 17
37  */
38 public class FileContentHandlerTest {
39
40     private static final String FILE_NAME = "test-file.txt";
41
42     @Test
43     public void testProcessFileContent() {
44
45         final int size = 13;
46         FileContentHandler contentHandler = new FileContentHandler();
47         final byte[] content = new byte[size];
48         Arrays.fill(content, (byte) 44);
49         contentHandler.addFile(FILE_NAME, content);
50
51         byte[] actualContent = contentHandler.processFileContent(FILE_NAME, optional -> {
52
53             try {
54                 byte[] buffer = new byte[size];
55                 assertTrue(optional.isPresent());
56                 assertEquals(size, optional.get().read(buffer));
57                 return buffer;
58             } catch (IOException e) {
59                 throw new RuntimeException("Unexpected error", e);
60             }
61
62         });
63         Assert.assertTrue(Arrays.equals(actualContent, content));
64     }
65
66     @Test
67     public void testProcessEmptyFileContent() {
68         FileContentHandler contentHandler = new FileContentHandler();
69         contentHandler.addFile(FILE_NAME, new byte[0]);
70         assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent));
71     }
72
73     @Test
74     public void testProcessNoFileContent() {
75         FileContentHandler contentHandler = new FileContentHandler();
76         assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
77     }
78
79     @Test
80     public void testAddFiles() {
81         FileContentHandler contentHandler = new FileContentHandler();
82         contentHandler.addFile("org/openecomp/core/utilities/file/testFileUtils.txt",
83                 new ByteArrayInputStream(new byte[100]));
84
85         Assert.assertNotNull(contentHandler.getFiles());
86         Assert.assertTrue(contentHandler.getFiles().containsKey("org/openecomp/core/utilities/file/testFileUtils.txt"));
87     }
88
89     @Test
90     public void testSetFiles() {
91         FileContentHandler contentHandler = new FileContentHandler();
92         Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]),
93                     new AbstractMap.SimpleEntry<>("file2", new byte[0]))
94                 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
95
96         contentHandler.setFiles(fileMap);
97
98         Assert.assertEquals(contentHandler.getFiles().size(), 2);
99         Assert.assertEquals(contentHandler.getFileList().size(), 2);
100         assertFalse(contentHandler.isEmpty());
101         contentHandler.remove("file1");
102         assertFalse(contentHandler.containsFile("file1"));
103     }
104
105     @Test
106     public void testAddAll() {
107         FileContentHandler contentHandler = new FileContentHandler();
108         FileContentHandler contentHandler1 = createFileHandlerContent();
109
110         contentHandler.addAll(contentHandler1);
111
112         Assert.assertTrue(contentHandler1.containsFile("file1"));
113         Assert.assertEquals(contentHandler.getFiles().size(), 2);
114     }
115
116     @Test
117     public void testSetFilesUsingFIleContentHandlerObject() {
118         FileContentHandler contentHandler1 = createFileHandlerContent();
119
120         FileContentHandler contentHandler = new FileContentHandler();
121         contentHandler.setFiles(contentHandler1);
122
123         Assert.assertEquals(contentHandler.getFiles().size(), 2);
124     }
125
126     private FileContentHandler createFileHandlerContent() {
127         FileContentHandler contentHandler1 = new FileContentHandler();
128         Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]),
129                 new AbstractMap.SimpleEntry<>("file2", new byte[0]))
130                 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
131         contentHandler1.putAll(fileMap);
132         return contentHandler1;
133     }
134 }