aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/filedistribution/MockFileRegistry.java
blob: fc594d4f0ccc7559860515247c82b4bc44108cf0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;

import com.yahoo.config.FileReference;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.path.Path;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * A file registry for config server tests
 *
 * @author hmusum
 */
public class MockFileRegistry implements FileRegistry {

    private final List<Entry> entries = new ArrayList<>();
    private final AddFileInterface addFileInterface;

    public MockFileRegistry(File applicationDir, FileDirectory fileDirectory) {
        this.addFileInterface = new ApplicationFileManager(applicationDir, fileDirectory, false);
    }

    public FileReference addFile(String relativePath) {
        if (relativePath.isEmpty()) relativePath = "./";
        try {
            addFileInterface.addFile(Path.fromString(relativePath));
            FileReference fileReference = new FileReference(relativePath);
            entries.add(new Entry(relativePath, fileReference));
            return fileReference;
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public List<Entry> export() { return entries; }

    @Override
    public FileReference addUri(String uri) {
        throw new IllegalArgumentException("FileReference addUri(String uri) is not implemented for " + getClass().getCanonicalName());
    }

    @Override
    public FileReference addBlob(String name, ByteBuffer blob) {
        name = "./" + name;
        FileReference fileReference = addFileInterface.addBlob(blob, Path.fromString(name));

        entries.add(new Entry(name, fileReference));
        return fileReference;
    }

}