aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/content/cluster/DomSearchCoverageBuilderTest.java
blob: d9db6234f1c39224abce73f41043e0204699f019 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.content.cluster;

import com.yahoo.vespa.model.builder.xml.dom.ModelElement;
import com.yahoo.vespa.model.content.SearchCoverage;
import org.apache.commons.io.input.CharSequenceInputStream;
import org.junit.Test;

import javax.xml.parsers.DocumentBuilderFactory;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * @author Simon Thoresen Hult
 */
public class DomSearchCoverageBuilderTest {

    @Test
    public void requireThatDefaultsAreNull() throws Exception {
        SearchCoverage coverage = newSearchCoverage(
                "<content/>");
        assertNull(coverage.getMinimum());
        assertNull(coverage.getMinWaitAfterCoverageFactor());
        assertNull(coverage.getMaxWaitAfterCoverageFactor());
    }

    @Test
    public void requireThatEmptySearchIsSafe() throws Exception {
        SearchCoverage coverage = newSearchCoverage(
                "<content>" +
                "  <search/>" +
                "</content>");
        assertNull(coverage.getMinimum());
        assertNull(coverage.getMinWaitAfterCoverageFactor());
        assertNull(coverage.getMaxWaitAfterCoverageFactor());
    }

    @Test
    public void requireThatEmptyCoverageIsSafe() throws Exception {
        SearchCoverage coverage = newSearchCoverage(
                "<content>" +
                "  <search>" +
                "    <coverage/>" +
                "  </search>" +
                "</content>");
        assertNull(coverage.getMinimum());
        assertNull(coverage.getMinWaitAfterCoverageFactor());
        assertNull(coverage.getMaxWaitAfterCoverageFactor());
    }

    @Test
    public void requireThatSearchCoverageCanBeBuilt() throws Exception {
        SearchCoverage coverage = newSearchCoverage(
                "<content>" +
                "  <search>" +
                "    <coverage>" +
                "      <minimum>0.11</minimum>" +
                "      <min-wait-after-coverage-factor>0.23</min-wait-after-coverage-factor>" +
                "      <max-wait-after-coverage-factor>0.58</max-wait-after-coverage-factor>" +
                "    </coverage>" +
                "  </search>" +
                "</content>");
        assertEquals(0.11, coverage.getMinimum(), 1E-6);
        assertEquals(0.23, coverage.getMinWaitAfterCoverageFactor(), 1E-6);
        assertEquals(0.58, coverage.getMaxWaitAfterCoverageFactor(), 1E-6);
    }

    private static SearchCoverage newSearchCoverage(String xml) throws Exception {
        return DomSearchCoverageBuilder.build(
                new ModelElement(DocumentBuilderFactory.newInstance()
                                                       .newDocumentBuilder()
                                                       .parse(new CharSequenceInputStream(xml, StandardCharsets.UTF_8))
                                                       .getDocumentElement()));
    }
}