Test coverage
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / file / FileContentHandlerTest.java
1 package org.openecomp.core.utilities.file;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertTrue;
6
7 import java.io.ByteArrayInputStream;
8 import java.io.IOException;
9 import java.util.AbstractMap;
10 import java.util.Arrays;
11 import java.util.Map;
12 import java.util.Optional;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
15
16 import org.testng.Assert;
17 import org.testng.annotations.Test;
18
19 /**
20  * @author EVITALIY
21  * @since 24 Oct 17
22  */
23 public class FileContentHandlerTest {
24
25     private static final String FILE_NAME = "test-file.txt";
26
27     @Test
28     public void testProcessFileContent() {
29
30         final int size = 13;
31         FileContentHandler contentHandler = new FileContentHandler();
32         final byte[] content = new byte[size];
33         Arrays.fill(content, (byte) 44);
34         contentHandler.addFile(FILE_NAME, content);
35
36         byte[] actualContent = contentHandler.processFileContent(FILE_NAME, optional -> {
37
38             try {
39                 byte[] buffer = new byte[size];
40                 assertTrue(optional.isPresent());
41                 assertEquals(size, optional.get().read(buffer));
42                 return buffer;
43             } catch (IOException e) {
44                 throw new RuntimeException("Unexpected error", e);
45             }
46
47         });
48         Assert.assertTrue(Arrays.equals(actualContent, content));
49     }
50
51     @Test
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));
56     }
57
58     @Test
59     public void testProcessNoFileContent() {
60         FileContentHandler contentHandler = new FileContentHandler();
61         assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
62     }
63
64     @Test
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]));
69
70         Assert.assertNotNull(contentHandler.getFiles());
71         Assert.assertTrue(contentHandler.getFiles().containsKey("org/openecomp/core/utilities/file/testFileUtils.txt"));
72     }
73
74     @Test
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));
80
81         contentHandler.setFiles(fileMap);
82
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"));
88     }
89
90     @Test
91     public void testAddAll() {
92         FileContentHandler contentHandler = new FileContentHandler();
93         FileContentHandler contentHandler1 = createFileHandlerContent();
94
95         contentHandler.addAll(contentHandler1);
96
97         Assert.assertTrue(contentHandler1.containsFile("file1"));
98         Assert.assertEquals(contentHandler.getFiles().size(), 2);
99     }
100
101     @Test
102     public void testSetFilesUsingFIleContentHandlerObject() {
103         FileContentHandler contentHandler1 = createFileHandlerContent();
104
105         FileContentHandler contentHandler = new FileContentHandler();
106         contentHandler.setFiles(contentHandler1);
107
108         Assert.assertEquals(contentHandler.getFiles().size(), 2);
109     }
110
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;
118     }
119 }