aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/test/java/ai/vespa/metricsproxy/metric/model/json/YamasJsonModelTest.java
blob: b20217cdca067d734d1b72059355d9b00bf679d6 (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
// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric.model.json;

import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;

import static ai.vespa.metricsproxy.metric.model.ConsumerId.toConsumerId;
import static ai.vespa.metricsproxy.metric.model.MetricId.toMetricId;
import static ai.vespa.metricsproxy.metric.model.ServiceId.toServiceId;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * Tests for YamasJsonModel and YamasArrayJsonModel
 *
 * @author smorgrav
 * @author gjoranv
 */
public class YamasJsonModelTest {

    private static final String EXPECTED_JSON = "{\"metrics\":[{\"status_code\":0,\"timestamp\":1400047900,\"application\":\"vespa.searchnode\",\"metrics\":{\"cpu\":55.5555555555555,\"memory_virt\":22222222222,\"memory_rss\":5555555555},\"dimensions\":{\"applicationName\":\"app\",\"tenantName\":\"tenant\",\"metrictype\":\"system\",\"instance\":\"searchnode\",\"applicationInstance\":\"default\",\"clustername\":\"cluster\"},\"routing\":{\"yamas\":{\"namespaces\":[\"Vespa\"]}},\"status_msg\":\"Data collected successfully\"}]}";

    @Test
    public void array_definition_creates_correct_json() throws IOException {
        YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");

        YamasArrayJsonModel yamasData = new YamasArrayJsonModel();
        yamasData.add(jsonModel);

        assertEquals(EXPECTED_JSON, yamasData.serialize());
    }

    @Test
    public void deserialize_serialize_roundtrip() throws IOException {
        YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");

        // Do some sanity checking
        assertEquals("vespa.searchnode", jsonModel.application);
        assertEquals("Vespa", jsonModel.routing.get("yamas").namespaces.get(0));
        assertEquals(5.555555555E9, jsonModel.metrics.get("memory_rss"), 0.1d); //Not using custom double renderer

        // Serialize and verify
        YamasArrayJsonModel yamasArray = new YamasArrayJsonModel();
        yamasArray.add(jsonModel);
        String string = yamasArray.serialize();
        assertEquals(EXPECTED_JSON, string);
    }

    @Test
    public void deserialize_serialize_roundtrip_with_metrics_packet() throws IOException {
        YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");
        MetricsPacket metricsPacket = YamasJsonUtil.toMetricsPacketBuilder(jsonModel).build();

        // Do some sanity checking
        assertEquals(toServiceId("vespa.searchnode"), metricsPacket.service);
        assertEquals(toConsumerId("Vespa"), metricsPacket.consumers().get(0));
        assertEquals(5.555555555E9, metricsPacket.metrics().get(toMetricId("memory_rss")).doubleValue(), 0.1d); //Not using custom double rendrer

        // Serialize and verify
        YamasArrayJsonModel yamasArray = YamasJsonUtil.toYamasArray(Collections.singleton(metricsPacket), true);
        String string = yamasArray.serialize();
        assertEquals(EXPECTED_JSON, string);
    }

    @Test
    public void missing_routing_object_makes_it_null() throws IOException {
        // Read file that was taken from production (real -life example that is)
        String filename = getClass().getClassLoader().getResource("yamas-array-no-routing.json").getFile();
        BufferedReader reader = Files.newBufferedReader(Paths.get(filename));
        ObjectMapper mapper = new ObjectMapper();
        YamasJsonModel jsonModel = mapper.readValue(reader, YamasJsonModel.class);

        // Do some sanity checking
        assertNull(jsonModel.routing);
    }

    private YamasJsonModel getYamasJsonModel(String testFile) throws IOException {
        String filename = getClass().getClassLoader().getResource(testFile).getFile();
        BufferedReader reader = Files.newBufferedReader(Paths.get(filename));
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(reader, YamasJsonModel.class);
    }

}