diff --git a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml new file mode 100644 index 000000000000..702d00f85212 --- /dev/null +++ b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml @@ -0,0 +1,58 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +on: + push: + branches: + - main + pull_request: +name: java-pubsub integration-tests-against-emulator +jobs: + filter: + runs-on: ubuntu-latest + outputs: + library: ${{ steps.filter.outputs.library }} + steps: + - uses: actions/checkout@v6 + - uses: dorny/paths-filter@v4 + id: filter + with: + filters: | + library: + - 'java-pubsub/**' + integration-tests: + needs: filter + if: ${{ needs.filter.outputs.library == 'true' }} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-java@v5 + with: + distribution: temurin + java-version: 17 + - name: Start Pub/Sub Emulator + run: | + docker run -d -p 8085:8085 --name pubsub-emulator gcr.io/google.com/cloudsdktool/cloud-sdk:567.0.0-emulators gcloud beta emulators pubsub start --project=abc --host-port=0.0.0.0:8085 + echo "Waiting for emulator to start..." + sleep 10 + + - name: Running tests + run: .kokoro/build.sh + env: + JOB_TYPE: test + BUILD_SUBDIR: java-pubsub + PUBSUB_EMULATOR_HOST: localhost:8085 + GOOGLE_CLOUD_PROJECT: abc + SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT -Dsurefire.failIfNoSpecifiedTests=false' diff --git a/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java new file mode 100644 index 000000000000..f19f9ae2979e --- /dev/null +++ b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java @@ -0,0 +1,65 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pubsub; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UsePubSubEmulatorExampleIT { + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalOut; + + private static void requireEnvVar(String varName) { + assertWithMessage("Environment variable " + varName + " is required to perform these tests.") + .that(System.getenv(varName)) + .isNotNull(); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("PUBSUB_EMULATOR_HOST"); + } + + @Before + public void setUp() { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalOut); + } + + @Test + public void testUsePubSubEmulatorExample() throws Exception { + UsePubSubEmulatorExample.main(); + String output = bout.toString(); + assertThat(output).contains("Created topic: "); + assertThat(output).contains("Published message ID: "); + } +}