summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java
blob: 77ada193b366894455932d8c2038685fa76928fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.core.utilities.file;

import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author EVITALIY
 * @since 24 Oct 17
 */
public class FileContentHandlerTest {

    private static final String FILE_NAME = "test-file.txt";

    @Test
    public void testProcessFileContent() {

        final int size = 13;
        FileContentHandler contentHandler = new FileContentHandler();
        final byte[] content = new byte[size];
        Arrays.fill(content, (byte) 44);
        contentHandler.addFile(FILE_NAME, content);

        byte[] actualContent = processFileContent(FILE_NAME, optional -> {

            try {
                byte[] buffer = new byte[size];
                assertTrue(optional.isPresent());
                assertEquals(size, optional.get().read(buffer));
                return buffer;
            } catch (IOException e) {
                throw new RuntimeException("Unexpected error", e);
            }

        }, contentHandler);
        Assert.assertTrue(Arrays.equals(actualContent, content));
    }

    @Test
    public void testProcessEmptyFileContent() {
        FileContentHandler contentHandler = new FileContentHandler();
        contentHandler.addFile(FILE_NAME, new byte[0]);
        assertFalse(processFileContent(FILE_NAME, Optional::isPresent, contentHandler));
    }

    @Test
    public void testProcessNoFileContent() {
        FileContentHandler contentHandler = new FileContentHandler();
        assertFalse(processFileContent("filename", Optional::isPresent, contentHandler));
    }

    @Test
    public void testAddFiles() {
        FileContentHandler contentHandler = new FileContentHandler();
        contentHandler.addFile("org/openecomp/core/utilities/file/testFileUtils.txt",
                new ByteArrayInputStream(new byte[100]));

        Assert.assertNotNull(contentHandler.getFiles());
        Assert.assertTrue(contentHandler.getFiles().containsKey("org/openecomp/core/utilities/file/testFileUtils.txt"));
    }

    @Test
    public void testSetFiles() {
        //given
        final FileContentHandler expectedFileContentHandler = createFileContentHandler();
        //when
        final FileContentHandler actualContentHandler = new FileContentHandler();
        actualContentHandler.setFiles(expectedFileContentHandler.getFiles());

        //then
        final Map<String, byte[]> actualFileMap = actualContentHandler.getFiles();
        assertThat("Should contain the expected number of folders", actualContentHandler.getFolderList(), hasSize(0));
        assertThat("Should contain the expected number of files", actualFileMap, aMapWithSize(2));
        expectedFileContentHandler.getFiles().keySet().forEach(filePath -> {
            assertThat("Should contain the expected file", actualFileMap.keySet(), hasItem(filePath));
        });
    }

    @Test
    public void testAddAllFromFileContentHandler() {
        //given
        final FileContentHandler expectedFileContentHandler = createFileContentHandler();
        //when
        final FileContentHandler actualContentHandler = new FileContentHandler();
        actualContentHandler.addAll(expectedFileContentHandler);
        //then
        final Map<String, byte[]> actualFileMap = actualContentHandler.getFiles();
        assertThat("Should contain the expected number of files", actualFileMap, aMapWithSize(2));
        final Set<String> actualFolderList = actualContentHandler.getFolderList();
        assertThat("Should contain the expected number of folders", actualFolderList, hasSize(3));
        expectedFileContentHandler.getFiles().keySet().forEach(filePath -> {
            assertThat("Should contain the expected file", actualFileMap.keySet(), hasItem(filePath));
        });
        expectedFileContentHandler.getFolderList().forEach(folderPath -> {
            assertThat("Should contain the expected file", actualFolderList, hasItem(folderPath));
        });
    }

    private FileContentHandler createFileContentHandler() {
        final FileContentHandler contentHandler = new FileContentHandler();
        final Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]),
                new AbstractMap.SimpleEntry<>("file2", new byte[0]))
                .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
        contentHandler.setFiles(fileMap);
        contentHandler.addFolder("folder1");
        contentHandler.addFolder("folder1/folder2");
        contentHandler.addFolder("folder3");
        return contentHandler;
    }

    /**
     * Applies a business logic to a file's content while taking care of all retrieval logic.
     *
     * @param fileName  name of a file inside this content handler.
     * @param processor the business logic to work on the file's input stream, which may not be set
     *                  (check the {@link Optional} if no such file can be found
     * @param <T>       return type, may be {@link java.lang.Void}
     * @return result produced by the processor
     */
    public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor, FileContentHandler contentHandler) {

        // do not throw IOException to mimic the existing uses of getFileContent()
        try (InputStream contentInputStream = contentHandler.getFileContentAsStream(fileName)) {
            return processor.apply(Optional.ofNullable(contentInputStream));
        } catch (IOException e) {
            throw new RuntimeException("Failed to process file: " + fileName, e);
        }
    }
}