re base code
[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 org.testng.Assert;
4 import org.testng.annotations.Test;
5
6 import java.io.IOException;
7 import java.util.Arrays;
8 import java.util.Optional;
9
10 import static org.testng.Assert.*;
11
12 /**
13  * @author EVITALIY
14  * @since 24 Oct 17
15  */
16 public class FileContentHandlerTest {
17
18     private static final String FILE_NAME = "test-file.txt";
19
20     @Test
21     public void testProcessFileContent() throws Exception {
22
23         final int size = 13;
24         FileContentHandler contentHandler = new FileContentHandler();
25         final byte[] content = new byte[size];
26         Arrays.fill(content, (byte) 44);
27         contentHandler.addFile(FILE_NAME, content);
28
29         byte[] actualContent = contentHandler.processFileContent(FILE_NAME, optional -> {
30
31             try {
32                 byte[] buffer = new byte[size];
33                 assertTrue(optional.isPresent());
34                 assertEquals(size, optional.get().read(buffer));
35                 return buffer;
36             } catch (IOException e) {
37                 throw new RuntimeException("Unexpected error", e);
38             }
39
40         });
41         Assert.assertTrue(Arrays.equals(actualContent, content));
42     }
43
44     @Test
45     public void testProcessEmptyFileContent() throws Exception {
46         FileContentHandler contentHandler = new FileContentHandler();
47         contentHandler.addFile(FILE_NAME, new byte[0]);
48         assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent));
49     }
50
51     @Test
52     public void testProcessNoFileContent() throws Exception {
53         FileContentHandler contentHandler = new FileContentHandler();
54         assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
55     }
56 }