aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/horizon/TsdbQueryRewriterTest.java
blob: 7f826566ebc72ffbcd327a2c9785c82847a6d5ac (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.horizon;

import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;

import static com.yahoo.slime.SlimeUtils.jsonToSlimeOrThrow;
import static com.yahoo.slime.SlimeUtils.toJsonBytes;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author valerijf
 */
public class TsdbQueryRewriterTest {

    @Test
    void rewrites_query() throws IOException {
        assertRewrite("filters-complex.json", "filters-complex.expected.json", Set.of(TenantName.from("tenant2")), false);

        assertRewrite("filter-in-execution-graph.json",
                "filter-in-execution-graph.expected.json",
                Set.of(TenantName.from("tenant2"), TenantName.from("tenant3")), false);

        assertRewrite("filter-in-execution-graph.json",
                "filter-in-execution-graph.expected.operator.json",
                Set.of(TenantName.from("tenant2"), TenantName.from("tenant3")), true);

        assertRewrite("no-filters.json",
                "no-filters.expected.json",
                Set.of(TenantName.from("tenant2"), TenantName.from("tenant3")), false);

        assertRewrite("filters-meta-query.json",
                "filters-meta-query.expected.json",
                Set.of(TenantName.from("tenant2"), TenantName.from("tenant3")), false);
    }

    private static void assertRewrite(String initialFilename, String expectedFilename, Set<TenantName> tenants, boolean operator) throws IOException {
        byte[] data = Files.readAllBytes(Paths.get("src/test/resources/horizon", initialFilename));
        data = TsdbQueryRewriter.rewrite(data, tenants, operator, SystemName.Public);

        String actualJson = new String(toJsonBytes(jsonToSlimeOrThrow(data).get(), false), UTF_8);
        String expectedJson = new String(toJsonBytes(jsonToSlimeOrThrow(Files.readAllBytes(Paths.get("src/test/resources/horizon", expectedFilename))).get(), false), UTF_8);

        assertEquals(expectedJson, actualJson);
    }
}