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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.persistence;
import com.yahoo.vespa.hosted.controller.api.integration.LogEntry;
import com.yahoo.vespa.hosted.controller.deployment.Step;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deployReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deployTester;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author jonmv
*/
public class LogSerializerTest {
private static final LogSerializer serializer = new LogSerializer();
private static final Path logsFile = Paths.get("src/test/java/com/yahoo/vespa/hosted/controller/persistence/testdata/logs.json");
@Test
void testSerialization() throws IOException {
for (LogEntry.Type type : LogEntry.Type.values())
assertEquals(type, LogSerializer.typeOf(LogSerializer.valueOf(type)));
byte[] logJson = Files.readAllBytes(logsFile);
LogEntry first = new LogEntry(0, Instant.ofEpochMilli(0), LogEntry.Type.info, "First");
LogEntry second = new LogEntry(1, Instant.ofEpochMilli(0), LogEntry.Type.info, "Second");
LogEntry third = new LogEntry(2, Instant.ofEpochMilli(1000), LogEntry.Type.debug, "Third");
LogEntry fourth = new LogEntry(3, Instant.ofEpochMilli(2000), LogEntry.Type.warning, "Fourth");
Map<Step, List<LogEntry>> expected = new HashMap<>();
expected.put(deployReal, new ArrayList<>());
expected.get(deployReal).add(third);
expected.put(deployTester, new ArrayList<>());
expected.get(deployTester).add(fourth);
assertEquals(expected, serializer.fromJson(logJson, 1));
expected.get(deployReal).add(0, first);
expected.get(deployTester).add(0, second);
assertEquals(expected, serializer.fromJson(logJson, -1));
assertEquals(expected, serializer.fromJson(serializer.toJson(expected), -1));
expected.get(deployReal).add(first);
expected.get(deployReal).add(third);
expected.get(deployTester).add(second);
expected.get(deployTester).add(fourth);
assertEquals(expected, serializer.fromJson(List.of(logJson, logJson), -1));
}
}
|