Add collaboration feature
[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.annotations.Test;
4
5 import java.io.IOException;
6 import java.util.Arrays;
7 import java.util.Optional;
8
9 import static org.testng.Assert.assertEquals;
10 import static org.testng.Assert.assertFalse;
11 import static org.testng.Assert.assertTrue;
12
13 /**
14  * @author EVITALIY
15  * @since 24 Oct 17
16  */
17 public class FileContentHandlerTest {
18
19     private static final String FILE_NAME = "test-file.txt";
20
21     @Test
22     public void testProcessFileContent() throws Exception {
23
24         final int size = 13;
25         FileContentHandler contentHandler = new FileContentHandler();
26         final byte[] content = new byte[size];
27         Arrays.fill(content, (byte) 44);
28         contentHandler.addFile(FILE_NAME, content);
29         assertEquals(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         }), content);
41     }
42
43     @Test
44     public void testProcessEmptyFileContent() throws Exception {
45         FileContentHandler contentHandler = new FileContentHandler();
46         contentHandler.addFile(FILE_NAME, new byte[0]);
47         assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent));
48     }
49
50     @Test
51     public void testProcessNoFileContent() throws Exception {
52         FileContentHandler contentHandler = new FileContentHandler();
53         assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
54     }
55 }