1 package org.openecomp.core.utilities.file;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertTrue;
7 import java.io.ByteArrayInputStream;
8 import java.io.IOException;
9 import java.util.AbstractMap;
10 import java.util.Arrays;
12 import java.util.Optional;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
16 import org.testng.Assert;
17 import org.testng.annotations.Test;
23 public class FileContentHandlerTest {
25 private static final String FILE_NAME = "test-file.txt";
28 public void testProcessFileContent() {
31 FileContentHandler contentHandler = new FileContentHandler();
32 final byte[] content = new byte[size];
33 Arrays.fill(content, (byte) 44);
34 contentHandler.addFile(FILE_NAME, content);
36 byte[] actualContent = contentHandler.processFileContent(FILE_NAME, optional -> {
39 byte[] buffer = new byte[size];
40 assertTrue(optional.isPresent());
41 assertEquals(size, optional.get().read(buffer));
43 } catch (IOException e) {
44 throw new RuntimeException("Unexpected error", e);
48 Assert.assertTrue(Arrays.equals(actualContent, content));
52 public void testProcessEmptyFileContent() {
53 FileContentHandler contentHandler = new FileContentHandler();
54 contentHandler.addFile(FILE_NAME, new byte[0]);
55 assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent));
59 public void testProcessNoFileContent() {
60 FileContentHandler contentHandler = new FileContentHandler();
61 assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
65 public void testAddFiles() {
66 FileContentHandler contentHandler = new FileContentHandler();
67 contentHandler.addFile("org/openecomp/core/utilities/file/testFileUtils.txt",
68 new ByteArrayInputStream(new byte[100]));
70 Assert.assertNotNull(contentHandler.getFiles());
71 Assert.assertTrue(contentHandler.getFiles().containsKey("org/openecomp/core/utilities/file/testFileUtils.txt"));
75 public void testSetFiles() {
76 FileContentHandler contentHandler = new FileContentHandler();
77 Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]),
78 new AbstractMap.SimpleEntry<>("file2", new byte[0]))
79 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
81 contentHandler.setFiles(fileMap);
83 Assert.assertEquals(contentHandler.getFiles().size(), 2);
84 Assert.assertEquals(contentHandler.getFileList().size(), 2);
85 Assert.assertFalse(contentHandler.isEmpty());
86 contentHandler.remove("file1");
87 Assert.assertFalse(contentHandler.containsFile("file1"));
91 public void testAddAll() {
92 FileContentHandler contentHandler = new FileContentHandler();
93 FileContentHandler contentHandler1 = createFileHandlerContent();
95 contentHandler.addAll(contentHandler1);
97 Assert.assertTrue(contentHandler1.containsFile("file1"));
98 Assert.assertEquals(contentHandler.getFiles().size(), 2);
102 public void testSetFilesUsingFIleContentHandlerObject() {
103 FileContentHandler contentHandler1 = createFileHandlerContent();
105 FileContentHandler contentHandler = new FileContentHandler();
106 contentHandler.setFiles(contentHandler1);
108 Assert.assertEquals(contentHandler.getFiles().size(), 2);
111 private FileContentHandler createFileHandlerContent() {
112 FileContentHandler contentHandler1 = new FileContentHandler();
113 Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]),
114 new AbstractMap.SimpleEntry<>("file2", new byte[0]))
115 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
116 contentHandler1.putAll(fileMap);
117 return contentHandler1;