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
|
package ai.vespa.feed.client;// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
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"});
assertEquals(URI.create("https://vespa.ai:4443/"), args.endpoint());
assertEquals(Paths.get("feed.json"), args.inputFile());
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());
}
@Test
void fails_on_missing_parameters() throws CliArguments.CliArgumentsException {
CliArguments cliArguments = CliArguments.fromRawArgs(new String[0]);
CliArguments.CliArgumentsException exception = assertThrows(CliArguments.CliArgumentsException.class, cliArguments::endpoint);
assertEquals("Endpoint must be specified", exception.getMessage());
exception = assertThrows(CliArguments.CliArgumentsException.class, cliArguments::inputFile);
assertEquals("Feed file 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);
}
}
|