aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClientTest.java
blob: 0191c29dd66506c78274df649aa7d6899474e5cf (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl;
import junit.framework.TestCase;

import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingAsyncHttpClientTest extends TestCase {
    class HttpClient implements AsyncHttpClient<HttpResult> {
        AsyncOperationImpl<HttpResult> lastOp;
        @Override
        public AsyncOperation<HttpResult> execute(HttpRequest r) {
            return lastOp = new AsyncOperationImpl<>("test");
        }
        @Override
        public void close() {
        }
    }

    public void testWithoutDebugLog() throws Exception {
        doRequests();
    }

    public void testWithDebugLog() throws Exception {
        Logger log = Logger.getLogger(LoggingAsyncHttpClient.class.getName());
        log.setLevel(Level.FINE);
        doRequests();
    }

    private void doRequests() {
        {
            HttpClient client = new HttpClient();
            LoggingAsyncHttpClient<HttpResult> loggingClient = new LoggingAsyncHttpClient<>(client);
            AsyncOperation<HttpResult> op = loggingClient.execute(new HttpRequest());
            client.lastOp.setResult(new HttpResult().setContent("foo"));
            assertEquals("foo", op.getResult().getContent());
        }
        {
            HttpClient client = new HttpClient();
            LoggingAsyncHttpClient<HttpResult> loggingClient = new LoggingAsyncHttpClient<>(client);
            AsyncOperation<HttpResult> op = loggingClient.execute(new HttpRequest());
            client.lastOp.setFailure(new Exception("foo"));
            assertEquals("foo", op.getCause().getMessage());
        }

    }
}