summaryrefslogtreecommitdiffstats
path: root/configserver-client/src/main/java/ai/vespa/hosted/client/MockConfigServerClient.java
blob: 3bd93f7fac78f6d72708165fcdb18f376cc2ed20 (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.client;

import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.HttpEntities;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Function;

/**
 * @author jonmv
 */
public class MockConfigServerClient extends AbstractConfigServerClient {

    private final Deque<Expectation> expectations = new ArrayDeque<>();

    @Override
    protected ClassicHttpResponse execute(ClassicHttpRequest request, HttpClientContext context) throws IOException {
        Expectation expectation = expectations.poll();
        if (expectation == null)
            throw new AssertionError("No further requests expected, but got " + request);

        return expectation.handle(request);
    }

    @Override
    public void close() {
        if ( ! expectations.isEmpty())
            throw new AssertionError(expectations.size() + " more requests were expected");
    }

    public void expect(Expectation expectation) {
        expectations.add(expectation);
    }

    public void expect(int status, Function<ClassicHttpRequest, String> mapper) {
        expect(request -> {
            BasicClassicHttpResponse response = new BasicClassicHttpResponse(status);
            response.setEntity(HttpEntities.create(mapper.apply(request), ContentType.APPLICATION_JSON));
            return response;
        });
    }

    @FunctionalInterface
    public interface Expectation {

        ClassicHttpResponse handle(ClassicHttpRequest request) throws IOException;

    }

}