|
| 1 | +package com.google.cloud.functions.invoker.runner; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static com.google.common.truth.Truth8.assertThat; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.junit.runners.JUnit4; |
| 16 | + |
| 17 | +@RunWith(JUnit4.class) |
| 18 | +public class InvokerTest { |
| 19 | + @Test |
| 20 | + public void help() throws IOException { |
| 21 | + String help = captureOutput(() -> { |
| 22 | + Optional<Invoker> invoker = Invoker.makeInvoker("--help"); |
| 23 | + assertThat(invoker).isEmpty(); |
| 24 | + }); |
| 25 | + assertThat(help).contains("Usage:"); |
| 26 | + assertThat(help).contains("--target"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void defaultPort() { |
| 31 | + Optional<Invoker> invoker = Invoker.makeInvoker(); |
| 32 | + assertThat(invoker.get().getPort()).isEqualTo(8080); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void explicitPort() { |
| 37 | + Optional<Invoker> invoker = Invoker.makeInvoker("--port", "1234"); |
| 38 | + assertThat(invoker.get().getPort()).isEqualTo(1234); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void defaultTarget() { |
| 43 | + Optional<Invoker> invoker = Invoker.makeInvoker(); |
| 44 | + assertThat(invoker.get().getFunctionTarget()).isEqualTo("TestFunction.function"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void explicitTarget() { |
| 49 | + Optional<Invoker> invoker = Invoker.makeInvoker("--target", "com.example.MyFunction"); |
| 50 | + assertThat(invoker.get().getFunctionTarget()).isEqualTo("com.example.MyFunction"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void defaultSignatureType() { |
| 55 | + Optional<Invoker> invoker = Invoker.makeInvoker(); |
| 56 | + assertThat(invoker.get().getFunctionSignatureType()).isNull(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void explicitSignatureType() { |
| 61 | + Map<String, String> env = Collections.singletonMap("FUNCTION_SIGNATURE_TYPE", "http"); |
| 62 | + Optional<Invoker> invoker = Invoker.makeInvoker(env); |
| 63 | + assertThat(invoker.get().getFunctionSignatureType()).isEqualTo("http"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void defaultJar() { |
| 68 | + Optional<Invoker> invoker = Invoker.makeInvoker(); |
| 69 | + assertThat(invoker.get().getFunctionJarPath()).isEmpty(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void explicitJarViaEnvironment() { |
| 74 | + Map<String, String> env = Collections.singletonMap("FUNCTION_JAR", "/foo/bar/baz.jar"); |
| 75 | + Optional<Invoker> invoker = Invoker.makeInvoker(env); |
| 76 | + assertThat(invoker.get().getFunctionJarPath()).hasValue("/foo/bar/baz.jar"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void explicitJarViaOption() { |
| 81 | + Optional<Invoker> invoker = Invoker.makeInvoker("--jar", "/foo/bar/baz.jar"); |
| 82 | + assertThat(invoker.get().getFunctionJarPath()).hasValue("/foo/bar/baz.jar"); |
| 83 | + } |
| 84 | + |
| 85 | + private static String captureOutput(Runnable operation) throws IOException { |
| 86 | + PrintStream originalOut = System.out; |
| 87 | + PrintStream originalErr = System.err; |
| 88 | + ByteArrayOutputStream byteCapture = new ByteArrayOutputStream(); |
| 89 | + try (PrintStream capture = new PrintStream(byteCapture)) { |
| 90 | + System.setOut(capture); |
| 91 | + System.setErr(capture); |
| 92 | + operation.run(); |
| 93 | + } finally { |
| 94 | + System.setOut(originalOut); |
| 95 | + System.setErr(originalErr); |
| 96 | + } |
| 97 | + return new String(byteCapture.toByteArray(), StandardCharsets.UTF_8); |
| 98 | + } |
| 99 | +} |
0 commit comments