aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/processing/handler/ProcessingTestDriver.java
blob: cd22dc80a4ce45d1b80d7b0340d6be6384527a06 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.handler;

import com.yahoo.api.annotations.Beta;
import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.Chain;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.container.logging.RequestLogHandler;
import com.yahoo.processing.Processor;
import com.yahoo.processing.execution.chain.ChainRegistry;
import com.yahoo.processing.rendering.Renderer;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * A helper for making processing requests and rendering their responses.
 * Create an instance of this to test making processing requests and get the response or response data.
 *
 * @author bratseth
 */
@Beta
public class ProcessingTestDriver extends RequestHandlerTestDriver {

    private final ProcessingHandler processingHandler;

    public ProcessingTestDriver(Collection<Chain<Processor>> chains) {
        this(chains, new ComponentRegistry<>());
    }
    public ProcessingTestDriver(String binding, Collection<Chain<Processor>> chains) {
        this(chains, new ComponentRegistry<>());
    }
    @SafeVarargs
    @SuppressWarnings("varargs")
    public ProcessingTestDriver(Chain<Processor> ... chains) {
        this(Arrays.asList(chains), new ComponentRegistry<>());
    }
    @SafeVarargs
    @SuppressWarnings("varargs")
    public ProcessingTestDriver(String binding, Chain<Processor> ... chains) {
        this(binding, Arrays.asList(chains), new ComponentRegistry<>());
    }
    public ProcessingTestDriver(Collection<Chain<Processor>> chains, ComponentRegistry<Renderer> renderers) {
        this(createProcessingHandler(chains, renderers, AccessLog.voidAccessLog()));
    }
    public ProcessingTestDriver(String binding, Collection<Chain<Processor>> chains, ComponentRegistry<Renderer> renderers) {
        this(binding, createProcessingHandler(chains, renderers, AccessLog.voidAccessLog()));
    }
    public ProcessingTestDriver(ProcessingHandler processingHandler) {
        super(processingHandler);
        this.processingHandler = processingHandler;
    }
    public ProcessingTestDriver(String binding, ProcessingHandler processingHandler) {
        super(binding, processingHandler);
        this.processingHandler = processingHandler;
    }

    public ProcessingTestDriver(Chain<Processor> chain, RequestLogHandler accessLogInterface) {
        this(createProcessingHandler(
                Collections.singleton(chain),
                new ComponentRegistry<>(),
                createAccessLog(accessLogInterface)));
    }

    private static AccessLog createAccessLog(RequestLogHandler accessLogInterface) {
        ComponentRegistry<RequestLogHandler> componentRegistry = new ComponentRegistry<>();
        componentRegistry.register(ComponentId.createAnonymousComponentId("access-log"), accessLogInterface);
        componentRegistry.freeze();

        return new AccessLog(componentRegistry);
    }

    private static ProcessingHandler createProcessingHandler(Collection<Chain<Processor>> chains,
                                                             ComponentRegistry<Renderer> renderers,
                                                             AccessLog accessLog) {
        Executor executor = Executors.newSingleThreadExecutor();

        ChainRegistry<Processor> registry = new ChainRegistry<>();
        for (Chain<Processor> chain : chains)
            registry.register(chain.getId(), chain);
        return new ProcessingHandler(registry, renderers, executor, accessLog);
    }

    /** Returns the processing handler of this */
    public ProcessingHandler processingHandler() { return processingHandler; }

}