summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/ServerCacheTest.java
blob: fa69df9f62891a154ca128084879db3eecd1e418 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.vespa.config.ConfigCacheKey;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.config.buildergen.ConfigDefinition;
import com.yahoo.vespa.config.protocol.ConfigResponse;
import com.yahoo.vespa.config.protocol.SlimeConfigResponse;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 */
public class ServerCacheTest {
    private ServerCache cache;

    private static final String defMd5 = "595f44fec1e92a71d3e9e77456ba80d1";
    private static final String defMd5_2 = "a2f8edfc965802bf6d44826f9da7e2b0";
    private static final String configMd5 = "mymd5";
    private static final String configMd5_2 = "mymd5_2";
    private static final ConfigDefinition def = new ConfigDefinition("mypayload", new String[0]);

    private static final ConfigDefinitionKey fooBarDefKey = new ConfigDefinitionKey("foo", "bar");
    private static final ConfigDefinitionKey fooBazDefKey = new ConfigDefinitionKey("foo", "baz");
    private static final ConfigDefinitionKey fooBimDefKey = new ConfigDefinitionKey("foo", "bim");

    private static final ConfigKey<?> fooConfigKey = new ConfigKey<>("foo", "id", "bar");
    private static final ConfigKey<?> bazConfigKey = new ConfigKey<>("foo", "id2", "bar");

    private final ConfigCacheKey fooBarCacheKey = new ConfigCacheKey(fooConfigKey, defMd5);
    private final ConfigCacheKey bazQuuxCacheKey = new ConfigCacheKey(bazConfigKey, defMd5);
    private final ConfigCacheKey fooBarCacheKeyDifferentMd5 = new ConfigCacheKey(fooConfigKey, defMd5_2);

    @Before
    public void setup() {
        UserConfigDefinitionRepo userConfigDefinitionRepo = new UserConfigDefinitionRepo();
        userConfigDefinitionRepo.add(fooBarDefKey, def);
        userConfigDefinitionRepo.add(fooBazDefKey, new com.yahoo.vespa.config.buildergen.ConfigDefinition("baz", new String[0]));
        userConfigDefinitionRepo.add(fooBimDefKey, new ConfigDefinition("mynode", new String[0]));

        cache = new ServerCache(new TestConfigDefinitionRepo(), userConfigDefinitionRepo);

        cache.put(fooBarCacheKey, SlimeConfigResponse.fromConfigPayload(ConfigPayload.empty(), 2, false, false, configMd5), configMd5);
        cache.put(bazQuuxCacheKey, SlimeConfigResponse.fromConfigPayload(ConfigPayload.empty(), 2, false, false, configMd5), configMd5);
        cache.put(fooBarCacheKeyDifferentMd5, SlimeConfigResponse.fromConfigPayload(ConfigPayload.empty(), 2, false, false, configMd5_2), configMd5_2);
    }

    @Test
    public void testThatCacheWorks() {
        assertNotNull(cache.getDef(fooBazDefKey));
        assertThat(cache.getDef(fooBarDefKey), is(def));
        assertThat(cache.getDef(fooBimDefKey).getCNode().getName(), is("mynode"));
        ConfigResponse raw = cache.get(fooBarCacheKey);
        assertThat(raw.getConfigMd5(), is(configMd5));
    }

    @Test
    public void testThatCacheWorksWithSameKeyDifferentMd5() {
        assertThat(cache.getDef(fooBarDefKey), is(def));
        ConfigResponse raw = cache.get(fooBarCacheKey);
        assertThat(raw.getConfigMd5(), is(configMd5));
        raw = cache.get(fooBarCacheKeyDifferentMd5);
        assertThat(raw.getConfigMd5(), is(configMd5_2));
    }

    @Test
    public void testThatCacheWorksWithDifferentKeySameMd5() {
        assertTrue(cache.get(fooBarCacheKey) == cache.get(bazQuuxCacheKey));
    }
}