summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/filedistribution/MockFileRegistry.java
blob: 723adc1400b10e3d707b6fb61109dfe51e2cb56b (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
// Copyright Verizon Media. 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.net.HostName;
import net.jpountz.xxhash.XXHashFactory;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Path;
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, Path rootPath) {
        FileDirectory fileDirectory = new FileDirectory(rootPath.toFile());
        this.addFileInterface = new ApplicationFileManager(applicationDir, fileDirectory);
    }

    public FileReference addFile(String relativePath) {
        if (relativePath.isEmpty())
            relativePath = "./";
        addFileInterface.addFile(relativePath);

        FileReference fileReference = new FileReference(relativePath);
        entries.add(new Entry(relativePath, fileReference));
        return fileReference;
    }

    @Override
    public String fileSourceHost() { return HostName.getLocalhost(); }

    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(ByteBuffer blob) {
        long blobHash = XXHashFactory.fastestJavaInstance().hash64().hash(blob, 0);
        String relativePath = "./" + Long.toHexString(blobHash) + ".blob";
        FileReference fileReference = addFileInterface.addBlob(blob, relativePath);

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

}