aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client-cli/src/test/java/ai/vespa/feed/client/impl/CliArgumentsTest.java
blob: 19b93c3172b0a5692e8a9943beab2592eadf9675 (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
// 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.impl.CliArguments;
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.*;

/**
 * @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", "--show-errors", "--show-all"});
        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());
        assertTrue(args.verboseSpecified());
        assertTrue(args.showErrors());
        assertTrue(args.showSuccesses());
        assertFalse(args.showProgress());
    }

    @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() {
        CliArguments.CliArgumentsException exception = assertThrows(
                CliArguments.CliArgumentsException.class,
                () -> CliArguments.fromRawArgs(new String[] {"--endpoint=https://endpoint", "--file=/path/to/file", "--stdin"}));
        assertEquals("Either option 'file' or 'stdin' must be specified", exception.getMessage());

        exception = assertThrows(
                CliArguments.CliArgumentsException.class,
                () -> CliArguments.fromRawArgs(new String[] {"--endpoint=https://endpoint"}));
        assertEquals("Either option 'file' or 'stdin' must be specified", exception.getMessage());
    }

    @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);
    }

}