aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution/src/test/java/com/yahoo/vespa/filedistribution/FileReferenceDataTest.java
blob: 301441e00714b37cfe939928ff43d42511efa01e (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.filedistribution;

import com.yahoo.config.FileReference;
import com.yahoo.io.IOUtils;
import com.yahoo.text.Utf8;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

import static com.yahoo.vespa.filedistribution.FileReferenceData.CompressionType;
import static com.yahoo.vespa.filedistribution.FileReferenceData.CompressionType.gzip;
import static com.yahoo.vespa.filedistribution.FileReferenceData.Type;
import static com.yahoo.vespa.filedistribution.FileReferenceData.Type.compressed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class FileReferenceDataTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testFileReferenceDataWithTempFile() throws IOException {
        String content = "blob";
        File tempFile = writeTempFile(content);
        FileReferenceData fileReferenceData =
                new LazyTemporaryStorageFileReferenceData(new FileReference("ref"), "foo", compressed, tempFile, gzip);
        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
        assertEquals(4, fileReferenceData.nextContent(byteBuffer));
        assertEquals(content, Utf8.toString(Arrays.copyOfRange(byteBuffer.array(), 0, 4)));

        // nextContent() will always return everything for FileReferenceDataBlob, so nothing more should be read
        assertEquals(-1, fileReferenceData.nextContent(byteBuffer));
        assertTrue(tempFile.exists());
        fileReferenceData.close();
        assertFalse(tempFile.exists()); // temp file should be removed when closing LazyTemporaryStorageFileReferenceData
    }

    @Test
    public void testFileReferenceData() throws IOException {
        String content = "blobbblubbblabb";
        File file = writeTempFile(content);
        FileReferenceData fileReferenceData =
                new LazyFileReferenceData(new FileReference("ref"), "foo", Type.compressed, file, CompressionType.gzip);
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        assertEquals(10, fileReferenceData.nextContent(byteBuffer));
        assertEquals(content.substring(0,10), Utf8.toString(Arrays.copyOfRange(byteBuffer.array(), 0, 10)));
        byteBuffer.flip();
        assertEquals(5, fileReferenceData.nextContent(byteBuffer));
        assertEquals(content.substring(10,15), Utf8.toString(Arrays.copyOfRange(byteBuffer.array(), 0, 5)));

        // nextContent() will always return everything for FileReferenceDataBlob, so nothing more should be read
        assertEquals(-1, fileReferenceData.nextContent(byteBuffer));
        assertTrue(file.exists());
        fileReferenceData.close();
        assertTrue(file.exists()); // file should not be removed
    }

    private File writeTempFile(String content) throws IOException {
        File file = temporaryFolder.newFile();
        IOUtils.writeFile(file, Utf8.toBytes(content));
        return file;
    }

}