aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-java/src/test/java/com/yahoo/vespaget/CommandLineOptionsTest.java
blob: f12dea7d54af636f703832a2fcb7549706852b23 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaget;

import com.yahoo.document.fieldset.AllFields;
import com.yahoo.document.fieldset.DocumentOnly;
import com.yahoo.document.fieldset.DocIdOnly;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Test class for {@link CommandLineOptions}
 *
 * @author bjorncs
 * @since 5.26
 */
public class CommandLineOptionsTest {

    private final InputStream emptyStream = new InputStream() {

        @Override
        public int read() throws IOException {
            return -1;
        }
    };

    private ClientParameters getParsedOptions(InputStream in, String... args) {
        CommandLineOptions options = new CommandLineOptions(in);
        return options.parseCommandLineArguments(args);
    }

    private ClientParameters getParsedOptions(String... args) {
        return getParsedOptions(emptyStream, args);
    }

    @Test
    void testDefaultOptions() {
        ClientParameters params = getParsedOptions();
        assertFalse(params.help);
        assertFalse(params.documentIds.hasNext());
        assertFalse(params.printIdsOnly);
        assertEquals(DocumentOnly.NAME, params.fieldSet);
        assertEquals("default-get", params.route);
        assertTrue(params.cluster.isEmpty());
        assertEquals("client", params.configId);
        assertFalse(params.showDocSize);
        assertEquals(0, params.timeout, 0);
        assertFalse(params.noRetry);
        assertEquals(0, params.traceLevel);
        assertEquals(DocumentProtocol.Priority.NORMAL_2, params.priority);
        assertFalse(params.tensorShortForm); // TODO Vespa 9: change default to true
    }

    @Test
    void testValidOptions() {
        ClientParameters params = getParsedOptions(
                "--fieldset", "[fieldset]",
                "--route", "dummyroute",
                "--configid", "dummyconfig",
                "--showdocsize",
                "--timeout", "0.25",
                "--noretry",
                "--trace", "1",
                "--priority", Integer.toString(DocumentProtocol.Priority.HIGH_3.getValue()),
                "--shorttensors",
                "id:1", "id:2"
        );

        assertEquals("[fieldset]", params.fieldSet);
        assertEquals("dummyroute", params.route);
        assertEquals("dummyconfig", params.configId);
        assertTrue(params.showDocSize);
        assertEquals(0.25, params.timeout, 0.0001);
        assertTrue(params.noRetry);
        assertEquals(1, params.traceLevel);
        assertEquals(DocumentProtocol.Priority.HIGH_3, params.priority);
        assertTrue(params.tensorShortForm);

        Iterator<String> documentsIds = params.documentIds;
        assertEquals("id:1", documentsIds.next());
        assertEquals("id:2", documentsIds.next());
        assertFalse(documentsIds.hasNext());
    }

    @Test
    void testInvalidCombination3() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--printids", "--fieldset", AllFields.NAME);
        });
        assertTrue(exception.getMessage().contains("Field set option can not be used in combination with print ids option."));
    }

    @Test
    void testInvalidCombination4() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--route", "dummyroute", "--cluster", "dummycluster");
        });
        assertTrue(exception.getMessage().contains("Cluster and route options are mutually exclusive."));
    }

    @Test
    void testInvalidPriority() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--priority", "16");
        });
        assertTrue(exception.getMessage().contains("Invalid priority: 16"));
    }

    @Test
    void TestHighestPriority() {
        ClientParameters params = getParsedOptions("--priority", "HIGHEST");
        assertEquals(DocumentProtocol.Priority.HIGHEST, params.priority);
    }

    @Test
    void TestHigh1PriorityAsNumber() {
        ClientParameters params = getParsedOptions("--priority", "2");
        assertEquals(DocumentProtocol.Priority.HIGH_1, params.priority);
    }

    @Test
    void testInvalidTraceLevel1() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--trace", "-1");
        });
        assertTrue(exception.getMessage().contains("Invalid tracelevel: -1"));
    }

    @Test
    void testInvalidTraceLevel2() {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--trace", "10");
        });
        assertTrue(exception.getMessage().contains("Invalid tracelevel: 10"));
    }

    @Test
    void testPrintids() {
        ClientParameters params = getParsedOptions("--printids");
        assertEquals(DocIdOnly.NAME, params.fieldSet);
    }

    @Test
    void testCluster() {
        ClientParameters params = getParsedOptions("--cluster", "dummycluster");
        assertEquals("dummycluster", params.cluster);
        assertTrue(params.route.isEmpty());
    }

    @Test
    void testHelp() {
        ClientParameters params = getParsedOptions("--help");
        assertTrue(params.help);
    }

    @Test
    void testDocumentIdsFromInputStream() throws UnsupportedEncodingException {
        InputStream in = new ByteArrayInputStream("id:1 id:2 id:3".getBytes("UTF-8"));
        ClientParameters params = getParsedOptions(in, "");

        Iterator<String> documentsIds = params.documentIds;
        assertEquals("id:1", documentsIds.next());
        assertEquals("id:2", documentsIds.next());
        assertEquals("id:3", documentsIds.next());
        assertFalse(documentsIds.hasNext());
    }

    @Test
    void testPrintHelp() {
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        PrintStream oldOut = System.out;
        System.setOut(new PrintStream(outContent));
        try {
            CommandLineOptions options = new CommandLineOptions(emptyStream);
            options.printHelp();

            String output = outContent.toString();
            assertTrue(output.contains("vespa-get <options> [documentid...]"));
            assertTrue(output.contains("Fetch a document from a Vespa Content cluster."));
        } finally {
            System.setOut(oldOut);
            outContent.reset();

        }
    }
}