aboutsummaryrefslogtreecommitdiffstats
path: root/fileacquirer/src/test/java/MockFileAcquirerTest.java
blob: dc8908249e09ebca5b8a3e493936ad53c4c22ee8 (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
72
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

import com.yahoo.config.FileReference;
import com.yahoo.filedistribution.fileacquirer.FileAcquirer;
import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer;
import com.yahoo.filedistribution.fileacquirer.TimeoutException;
import com.yahoo.vespa.config.FileReferenceDoesNotExistException;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;


/**
 * Test of public API of MockFileAcquirer, since it is intended to be used by 3rd parties.
 * Do not place it in the same package as MockFileAcquirer.
 * @author Tony Vaagenes
 */
public class MockFileAcquirerTest {
    @Test
    public void testReturnFile() throws Exception {
        File file = new File("/test");
        assertEquals(file, waitFor(MockFileAcquirer.returnFile(file)));
    }

    @Test
    public void testReturnFiles() throws Exception {
        File file1 = new File("/test1");
        File file2 = new File("/test2");

        HashMap<String, File> map = new HashMap<String, File>();
        map.put("1", file1);
        map.put("2", file2);

        FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(map);

        assertEquals(file1, waitFor(fileAcquirer, createFileReference("1")));
        assertEquals(file2, waitFor(fileAcquirer, createFileReference("2")));
    }

    @Test(expected = TimeoutException.class)
    public void testThrowTimeoutException() throws Exception {
        waitFor(MockFileAcquirer.throwTimeoutException());
    }

    @Test(expected = FileReferenceDoesNotExistException.class)
    public void testThrowFileReferenceDoesNotExistException() throws Exception {
        waitFor(MockFileAcquirer.throwFileReferenceDoesNotExistException());
    }

    private File waitFor(FileAcquirer fileAcquirer) throws InterruptedException {
        return waitFor(fileAcquirer, null);
    }

    private File waitFor(FileAcquirer fileAcquirer, FileReference reference)
            throws InterruptedException {
        return fileAcquirer.waitFor(reference, 100, TimeUnit.SECONDS);
    }

    private FileReference createFileReference(String value) {
        try {
            Constructor<FileReference> constructors = FileReference.class.getDeclaredConstructor(String.class);
            constructors.setAccessible(true);
            return constructors.newInstance(value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}