Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup auto validation of java examples #464

Merged
merged 3 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------------------------------

name: Auto Validate Examples

on:
workflow_dispatch:
artursouza marked this conversation as resolved.
Show resolved Hide resolved
push:
branches:
- master
- release-*
tags:
- v*

pull_request:
branches:
- master
- release-*
jobs:
validate:
runs-on: ubuntu-latest
env:
GOVER: 1.15.0
GOOS: linux
GOARCH: amd64
GOPROXY: https://proxy.golang.org
JDK_VER: 13.0.x
DAPR_CLI_VER: 1.0.0-rc.4
DAPR_RUNTIME_VER: 1.0.0-rc.3
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/3dacfb672d55f1436c249057aaebbe597e1066f3/install/install.sh
DAPR_CLI_REF:
DAPR_REF:
steps:
- uses: actions/checkout@v2
- name: Set up OpenJDK ${{ env.JDK_VER }}
uses: actions/setup-java@v1
with:
java-version: ${{ env.JDK_VER }}
- name: Set up Dapr CLI
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}
- name: Set up Go ${{ env.GOVER }}
if: env.DAPR_REF != '' || env.DAPR_CLI_REF != ''
uses: actions/setup-go@v2
with:
go-version: ${{ env.GOVER }}
- name: Checkout Dapr CLI repo to override dapr command.
uses: actions/checkout@v2
if: env.DAPR_CLI_REF != ''
with:
repository: dapr/cli
ref: ${{ env.DAPR_CLI_REF }}
path: cli
- name: Checkout Dapr repo to override daprd.
uses: actions/checkout@v2
if: env.DAPR_REF != ''
with:
repository: dapr/dapr
ref: ${{ env.DAPR_REF }}
path: dapr
- name: Build and override dapr cli with referenced commit.
if: env.DAPR_CLI_REF != ''
run: |
cd cli
make
sudo cp dist/linux_amd64/release/dapr /usr/local/bin/dapr
cd ..
- name: Initialize Dapr runtime ${{ env.DAPR_RUNTIME_VER }}
run: |
dapr uninstall --all
dapr init --runtime-version ${{ env.DAPR_RUNTIME_VER }}
- name: Build and override daprd with referenced commit.
if: env.DAPR_REF != ''
run: |
cd dapr
make
mkdir -p $HOME/.dapr/bin/
cp dist/linux_amd64/release/daprd $HOME/.dapr/bin/daprd
cd ..
- name: Override placement service.
if: env.DAPR_REF != ''
run: |
docker stop dapr_placement
cd dapr
./dist/linux_amd64/release/placement &
- name: Install utilities dependencies
run: |
echo "PATH=$PATH:$HOME/.local/bin" >> $GITHUB_ENV
pip3 install setuptools wheel
pip3 install mechanical-markdown
- name: Install Vault CLI
run: |
# From the installtion page of vault https://learn.hashicorp.com/tutorials/vault/getting-started-install?in=vault/getting-started
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install vault
# Verify vault is installed
vault -h
- name: Clean up files
run: mvn clean
- name: Build sdk
run: mvn compile -q
- name: Install jars
run: mvn install -q
- name: Validate invoke http example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/invoke/http/README.md
- name: Validate invoke grpc example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/invoke/grpc/README.md
- name: Validate expection handling example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/exception/README.md
- name: Validate state example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/state/README.md
- name: Validate pubsub HTTP example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/pubsub/http/README.md
- name: Validate bindings HTTP example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/bindings/http/README.md
- name: Validate secrets example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/secrets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public static void main(String[] args) throws Exception {

int count = 0;
while (!Thread.currentThread().isInterrupted()) {
String message = "Message #" + (count++);
String message = "Message #" + (count);

// Randomly decides between a class type or string type to be sent.
if (Math.random() >= 0.5) {
// On even number, send class message
if (count % 2 == 0) {
// This is an example of sending data in a user-defined object. The input binding will receive:
// {"message":"hello"}
MyClass myClass = new MyClass();
Expand All @@ -56,6 +56,7 @@ public static void main(String[] args) throws Exception {
System.out.println("sending a plain string: " + message);
client.invokeBinding(BINDING_NAME, BINDING_OPERATION, message).block();
}
count++;

try {
Thread.sleep((long) (10000 * Math.random()));
Expand Down
78 changes: 70 additions & 8 deletions examples/src/main/java/io/dapr/examples/bindings/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,23 @@ cd examples

### Setting Kafka locally

Before getting into the application code, follow these steps in order to setup a local instance of Kafka. This is needed for the local instances. Steps are:
Before getting into the application code, follow these steps in order to set up a local instance of Kafka. This is needed for the local instances. Steps are:

1. To run container locally run:

<!-- STEP
name: Setup kafka container
expected_stderr_lines:
- 'Creating network "http_default" with the default driver'
sleep: 5
-->

```bash
docker-compose -f ./src/main/java/io/dapr/examples/bindings/http/docker-compose-single-kafka.yml up -d
```

<!-- END_STEP -->

1. Run `docker-compose -f ./src/main/java/io/dapr/examples/bindings/http/docker-compose-single-kafka.yml up -d` to run the container locally
2. Run `docker ps` to see the container running locally:

```bash
Expand Down Expand Up @@ -93,10 +107,24 @@ public class InputBindingController {
```

Execute the follow script in order to run the Input Binding example:
```sh

<!-- STEP
name: Run input binding
expected_stdout_lines:
- '== APP == Received message through binding: {"message":"Message #0"}'
- '== APP == Received message through binding: "Message #1"'
- '== APP == Received message through binding: {"message":"Message #2"}'
- '== APP == Received message through binding: "Message #3"'
background: true
sleep: 5
-->

```bash
dapr run --components-path ./components/bindings --app-id inputbinding --app-port 3000 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.bindings.http.InputBindingExample -p 3000
```

<!-- END_STEP -->

### Running the Output binding sample

The output binding application is a simple java class with a main method that uses the Dapr Client to invoke binding.
Expand All @@ -114,10 +142,10 @@ public class OutputBindingExample{

int count = 0;
while (!Thread.currentThread().isInterrupted()) {
String message = "Message #" + (count++);
String message = "Message #" + (count);

// Randomly decides between a class type or string type to be sent.
if (Math.random() >= 0.5) {
// On even number, send class message
if (count % 2 == 0) {
// This is an example of sending data in a user-defined object. The input binding will receive:
// {"message":"hello"}
MyClass myClass = new MyClass();
Expand All @@ -129,6 +157,7 @@ public class OutputBindingExample{
System.out.println("sending a plain string: " + message);
client.invokeBinding(BINDING_NAME, BINDING_OPERATION, message).block();
}
count++;

try {
Thread.sleep((long) (10000 * Math.random()));
Expand All @@ -149,10 +178,23 @@ This example binds two events: A user-defined data object (using the `myClass` o

Use the follow command to execute the Output Binding example:

```sh
<!-- STEP
name: Run output binding
expected_stdout_lines:
- '== APP == sending a class with message: Message #0'
- '== APP == sending a plain string: Message #1'
- '== APP == sending a class with message: Message #2'
- '== APP == sending a plain string: Message #3'
background: true
sleep: 30
-->

```bash
dapr run --components-path ./components/bindings --app-id outputbinding -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.bindings.http.OutputBindingExample
```

<!-- END_STEP -->

Once running, the OutputBindingExample should print the output as follows:

![publisheroutput](../../../../../../resources/img/outputbinding.png)
Expand All @@ -165,9 +207,29 @@ Once running, the InputBindingExample should print the output as follows:

Events have been retrieved from the binding.

To stop both apps, press `CTRL+C` or run:

<!-- STEP
name: Cleanup apps
-->

```bash
dapr stop --app-id inputbinding
dapr stop --app-id outputbinding
```

<!-- END_STEP -->

For bringing down the kafka cluster that was started in the beginning, run
```sh

<!-- STEP
name: Cleanup Kafka containers
-->

```bash
docker-compose -f ./src/main/java/io/dapr/examples/bindings/http/docker-compose-single-kafka.yml down
```

<!-- END_STEP -->

For more details on Dapr Spring Boot integration, please refer to [Dapr Spring Boot](../../DaprApplication.java) Application implementation.
31 changes: 29 additions & 2 deletions examples/src/main/java/io/dapr/examples/exception/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,36 @@ The Dapr client is also within a try-with-resource block to properly close the c
### Running the example

Run this example with the following command:
```sh
dapr run -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.exception.Client

<!-- STEP
name: Run exception example
expected_stdout_lines:
- '== APP == Error code: INVALID_ARGUMENT'
- '== APP == Error message: INVALID_ARGUMENT: state store Unknown state store is not found'
background: true
sleep: 5
-->

```bash
dapr run --app-id exception_example -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.exception.Client
```

<!-- END_STEP -->

Once running, the OutputBindingExample should print the output as follows:

![stateouput](../../../../../resources/img/exception.png)

### Cleanup

To stop the app run (or press `CTRL+C`):

<!-- STEP
name: Cleanup
-->

```bash
dapr stop --app-id exception_example
```

<!-- END_STEP -->
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public SayResponse say(SayRequest request) {
String utcNowAsString = DATE_FORMAT.format(utcNow.getTime());

// Handles the request by printing message.
System.out.println("Server: " + request.getMessage() + " @ " + utcNowAsString);
System.out.println("Server: " + request.getMessage());
System.out.println("@ " + utcNowAsString);

// Now respond with current timestamp.
SayResponse.Builder responseBuilder = SayResponse.newBuilder();
Expand Down
Loading