aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-java/src/test/java/com/yahoo/vespaget/CommandLineOptionsTest.java
blob: 8848b6f1e7f2ff01176646399c64a3d95cb12825 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaget;

import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.*;
import java.util.Iterator;

import static org.junit.Assert.*;

/**
 * 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;
        }
    };

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    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
    public void testDefaultOptions() {
        ClientParameters params = getParsedOptions();
        assertFalse(params.help);
        assertFalse(params.documentIds.hasNext());
        assertFalse(params.printIdsOnly);
        assertEquals("[all]", params.fieldSet);
        assertEquals("default", 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);
        assertTrue(params.loadTypeName.isEmpty());
    }

    @Test
    public 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()),
                "--loadtype", "dummyloadtype",
                "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);
        assertEquals("dummyloadtype", params.loadTypeName);

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

    @Test
    public void testInvalidCombination3() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Field set option can not be used in combination with print ids option.");
        getParsedOptions("--printids", "--fieldset", "[header]");
    }

    @Test
    public void testInvalidCombination4() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Cluster and route options are mutually exclusive.");
        getParsedOptions("--route", "dummyroute", "--cluster", "dummycluster");
    }

    @Test
    public void testInvalidPriority() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Invalid priority: 16");
        getParsedOptions("--priority", "16");
    }

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

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

    @Test
    public void testInvalidTraceLevel1() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Invalid tracelevel: -1");
        getParsedOptions("--trace", "-1");
    }

    @Test
    public void testInvalidTraceLevel2() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Invalid tracelevel: 10");
        getParsedOptions("--trace", "10");
    }

    @Test
    public void testPrintids() {
        ClientParameters params = getParsedOptions("--printids");
        assertEquals("[id]", params.fieldSet);
    }

    @Test
    public void testHeadersOnly() {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Headers only option has been removed.");
        ClientParameters params = getParsedOptions("--headersonly");
    }

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

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

    @Test
    public 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
    public 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();

        }
    }
}