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

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

public class CommandLineOptionsTest {

    private ClientParameters getParsedOptions(String... args) {
        CommandLineOptions parser = new CommandLineOptions();
        return parser.parseCommandLineArguments(args);
    }

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

    @Test
    void testMultipleOptions() {
        ClientParameters params = getParsedOptions("--dump", "--route", "dummyroute", "--user", "userid");
        assertTrue(params.dumpData);
        assertEquals("dummyroute", params.route);
        assertEquals(ClientParameters.SelectionType.USER, params.selectionType);
        assertEquals("userid", params.id);
    }

    @Test
    void testSelectionTypes() {
        assertEquals(ClientParameters.SelectionType.USER, getParsedOptions("--user", "id").selectionType);
        assertEquals(ClientParameters.SelectionType.DOCUMENT, getParsedOptions("--document", "id").selectionType);
        assertEquals(ClientParameters.SelectionType.BUCKET, getParsedOptions("--bucket", "id").selectionType);
        assertEquals(ClientParameters.SelectionType.GROUP, getParsedOptions("--group", "id").selectionType);
        assertEquals(ClientParameters.SelectionType.GID, getParsedOptions("--gid", "id").selectionType);
    }

    @Test
    void testMissingSelectionType() {
        assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions();
        });
    }

    @Test
    void testFailOnMultipleDumpTypes() {
        assertThrows(IllegalArgumentException.class, () -> {
            getParsedOptions("--user", "id", "--document", "id", "--group", "id", "--gid", "id");
        });
    }

    @Test
    void testForceDumpOnDocumentOrGid() {
        assertTrue(getParsedOptions("--document", "docid").dumpData);
        assertTrue(getParsedOptions("--gid", "gid").dumpData);
    }

    @Test
    void testPrintHelp() {
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        PrintStream oldOut = System.out;
        System.setOut(new PrintStream(outContent));
        try {
            CommandLineOptions options = new CommandLineOptions();
            options.printHelp();
            String output = outContent.toString();
            assertTrue(output.contains("vespa-stat [options]"));
        } finally {
            System.setOut(oldOut);
            outContent.reset();
        }
    }

    @Test
    void bucket_space_is_default_unless_specified() {
        assertEquals("default", getParsedOptions("--user", "id").bucketSpace);
    }

    @Test
    void can_specify_explicit_bucket_space() {
        assertEquals("global", getParsedOptions("--user", "id", "--bucketspace", "global").bucketSpace);
    }

    @Test
    void testDefaultRoute() {
        assertEquals("default", getParsedOptions("--user", "dummyuser").route);
    }

}