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