aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client-cli/src/test/java/ai/vespa/feed/client/impl/CliArgumentsTest.java
blob: 21e279b05845a8ebfa2d87c7cd120f2f5ab3d524 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client.impl;

import ai.vespa.feed.client.FeedClientBuilder.Compression;
import ai.vespa.feed.client.impl.CliArguments.CliArgumentsException;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bjorncs
 */
class CliArgumentsTest {

    @Test
    void parses_parameters_correctly() throws CliArguments.CliArgumentsException {
        CliArguments args = CliArguments.fromRawArgs(new String[]{
                "--endpoint", "https://vespa.ai:4443/",
                "--file", "feed.json",
                "--connections", "10",
                "--max-streams-per-connection", "128",
                "--certificate", "cert.pem",
                "--private-key", "key.pem",
                "--ca-certificates", "ca-certs.pem",
                "--disable-ssl-hostname-verification",
                "--header", "\"My-Header: my-value\"",
                "--header", "Another-Header: another-value",
                "--benchmark",
                "--route", "myroute",
                "--timeout", "0.125",
                "--trace", "9",
                "--verbose",
                "--silent",
                "--compression", "gzip",
                "--show-errors",
                "--show-all",
                "--max-failure-seconds", "30",
                "--proxy", "https://myproxy:1234"});
        assertEquals(URI.create("https://vespa.ai:4443/"), args.endpoint());
        assertEquals(Paths.get("feed.json"), args.inputFile().get());
        assertEquals(10, args.connections().getAsInt());
        assertEquals(128, args.maxStreamsPerConnection().getAsInt());
        assertEquals(Paths.get("cert.pem"), args.certificateAndKey().get().certificateFile);
        assertEquals(Paths.get("key.pem"), args.certificateAndKey().get().privateKeyFile);
        assertEquals(Paths.get("ca-certs.pem"), args.caCertificates().get());
        assertTrue(args.sslHostnameVerificationDisabled());
        assertFalse(args.helpSpecified());
        assertFalse(args.versionSpecified());
        assertEquals(2, args.headers().size());
        assertEquals("my-value", args.headers().get("My-Header"));
        assertEquals("another-value", args.headers().get("Another-Header"));
        assertTrue(args.benchmarkModeEnabled());
        assertEquals("myroute", args.route().get());
        assertEquals(Duration.ofMillis(125), args.timeout().get());
        assertEquals(9, args.traceLevel().getAsInt());
        assertEquals(30, args.doomSeconds().getAsInt());
        assertTrue(args.verboseSpecified());
        assertTrue(args.showErrors());
        assertTrue(args.showSuccesses());
        assertFalse(args.showProgress());
        assertEquals(Compression.gzip, args.compression());
        assertEquals(URI.create("https://myproxy:1234"), args.proxy().orElse(null));
    }

    @Test
    void fails_on_missing_parameters() {
        CliArguments.CliArgumentsException exception = assertThrows(
                CliArguments.CliArgumentsException.class,
                () -> CliArguments.fromRawArgs(new String[] {"--file", "/path/to/file", "--stdin"}));
        assertEquals("Endpoint must be specified", exception.getMessage());
    }

    @Test
    void fails_on_conflicting_parameters() throws CliArgumentsException {
        assertEquals("Exactly one of 'file' and 'stdin' must be specified",
                     assertThrows(CliArgumentsException.class,
                                  () -> CliArguments.fromRawArgs(new String[] {"--endpoint", "https://endpoint", "--file", "/path/to/file", "--stdin"}))
                             .getMessage());

        assertEquals("Exactly one of 'file' and 'stdin' must be specified",
                     assertThrows(CliArgumentsException.class,
                                  () -> CliArguments.fromRawArgs(new String[] {"--endpoint", "https://endpoint"}))
                             .getMessage());

        assertEquals("At most one of 'file', 'stdin' and 'test-payload-size' may be specified",
                     assertThrows(CliArgumentsException.class,
                                  () -> CliArguments.fromRawArgs(new String[] {"--endpoint", "https://endpoint", "--speed-test", "--test-payload-size", "123", "--file", "file"}))
                             .getMessage());

        CliArguments.fromRawArgs(new String[] {"--endpoint", "foo", "--speed-test"});
    }

    @Test
    void generated_help_page_contains_expected_description() throws CliArguments.CliArgumentsException, IOException {
        CliArguments args = CliArguments.fromRawArgs(new String[]{"--help"});
        assertTrue(args.helpSpecified());

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        args.printHelp(out);
        String text = out.toString();
        String expectedHelp = new String(Files.readAllBytes(Paths.get("src", "test", "resources", "help.txt")));
        assertEquals(expectedHelp, text);
    }

}