summaryrefslogtreecommitdiffstats
path: root/vespa-testrunner-components/src/test/java/com/yahoo/vespa/hosted/testrunner/TestRunnerHandlerTest.java
blob: fdc6b633630660f9f31d909cbb49c7d7cf43a0b9 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.testrunner;

import com.yahoo.slime.SlimeUtils;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.time.Instant;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static org.junit.Assert.assertEquals;

/**
 * @author jvenstad
 */
public class TestRunnerHandlerTest {

    @Test
    public void logSerialization() throws IOException {
        Log log = new Log();
        LogRecord record = log.getLogRecord();
        String trace = log.getTrace();
        assertEquals("{\"logRecords\":[{\"id\":1,\"at\":2,\"type\":\"info\",\"message\":\"Hello.\\n" + trace + "\"}]}",
                     new String(SlimeUtils.toJsonBytes(TestRunnerHandler.logToSlime(Collections.singletonList(record)))));
    }

    private static class Log {

        private final LogRecord record;
        private final String trace;

        public Log() {
            Exception exception = new RuntimeException();
            record = createRecord(exception);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            exception.printStackTrace(new PrintStream(buffer));
            trace = buffer.toString()
                    .replaceAll("\n", "\\\\n")
                    .replaceAll("\t", "\\\\t");
        }

        LogRecord getLogRecord() {
            return record;
        }

        String getTrace() {
            return trace;
        }

        private static LogRecord createRecord(Exception exception) {
            LogRecord record = new LogRecord(Level.INFO, "Hello.");
            record.setSequenceNumber(1);
            record.setInstant(Instant.ofEpochMilli(2));
            record.setThrown(exception);
            return record;
        }
    }

}