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