You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "zregvart (via GitHub)" <gi...@apache.org> on 2023/01/31 10:45:31 UTC

[GitHub] [camel-website] zregvart commented on a diff in pull request #966: Add blog post on testing Camel K with YAKS

zregvart commented on code in PR #966:
URL: https://github.com/apache/camel-website/pull/966#discussion_r1091751202


##########
content/blog/2023/01/camel-k-yaks/index.md:
##########
@@ -0,0 +1,311 @@
+---
+title: "Testing Camel K with YAKS"
+date: 2023-01-31
+draft: false
+authors: [christophd]
+categories: ["Howtos", "Camel K", "Test", "YAKS"]
+preview: "How to verify a Camel K integration with YAKS - locally and on cloud infrastructure"
+---
+
+This post describes the steps to test a Camel K integration with YAKS both locally and on the Kubernetes platform.
+
+# What is YAKS?
+
+[YAKS](https://github.com/citrusframework/yaks) is an Open Source test automation platform that leverages Behavior Driven
+Development concepts for running tests locally and on Cloud infrastructure (e.g. [Kubernetes](https://kubernetes.io/) or
+[OpenShift](https://www.openshift.com/)).
+This means that the testing tool is able to run your tests both as local tests and natively on Kubernetes.
+The framework is specifically designed to verify Serverless and Microservice applications and aims for integration testing
+with the application under test up and running in a production-like environment.
+A typical YAKS test uses the very same infrastructure as the application under test and exchanges data/events over different
+messaging transports (e.g. Http REST, Knative eventing, Kafka, JMS and many more).
+
+As YAKS itself is written in Java the runtime uses a Java virtual machine with build tools such as Maven and integrates with
+well known Java testing frameworks such as [JUnit](https://junit.org/junit5/), [Cucumber](https://cucumber.io/) and
+[Citrus](https://citrusframework.org) to run the tests.
+
+# Understanding the Camel K example
+
+First of all here is a small sample Camel K integration that we would like to test in the following.
+The integration exposes a Http service to the user.
+The service accepts client Http POST requests that add fruit model objects.
+The Camel K route applies content based routing to store the fruits in different AWS S3 buckets.
+
+![Test Scenario](test-scenario.png)
+
+In the test scenario YAKS is going to invoke the Camel K service and verify that the message content has been sent to the right AWS S3 bucket.
+
+Here is a sample fruit model object that is subject to be stored in AWS S3:
+```json
+{
+  "id": 1000,
+  "name": "Pineapple",
+  "category":{
+    "id": "1",
+    "name":"tropical"
+  },
+  "nutrition":{
+    "calories": 50,
+    "sugar": 9
+  },
+  "status": "AVAILABLE",
+  "price": 1.59,
+  "tags": ["sweet"]
+}
+```
+
+Here is the Camel K integration route:
+
+```groovy
+from('platform-http:/fruits')
+    .log('received fruit ${body}')
+    .unmarshal().json()
+    .removeHeaders("*")
+    .setHeader("CamelAwsS3Key", constant("fruit.json"))
+    .choice()
+        .when().simple('${body[nutrition][sugar]} <= 5')
+            .setHeader("CamelAwsS3BucketName", constant("low-sugar"))
+        .when().simple('${body[nutrition][sugar]} > 5 && ${body[nutrition][sugar]} <= 10')
+            .setHeader("CamelAwsS3BucketName", constant("medium-sugar"))
+        .otherwise()
+            .setHeader("CamelAwsS3BucketName", constant("high-sugar"))
+    .end()
+    .marshal().json()
+    .log('sending ${body}')
+    .to("aws2-s3://noop?$parameters")
+```
+
+The route uses content based routing EAP based on the nutrition sugar rating of a given fruit in order to send the fruits
+to different AWS S3 buckets (low-sugar, medium-sugar, high-sugar).
+
+In the following the test case for this integration needs to invoke the exposed service with different fruits and verify its outcome on AWS S3.
+
+# How to test locally with YAKS
+
+In the beginning let's just write the test and run it locally.
+For now, we do not care how to deploy the application under test in the Cloud infrastructure as everything is running on
+the local machine using [JBang](https://www.jbang.dev/).
+
+JBang is a fantastic way to just start coding and running Java code and also Camel K integrations (also see this former
+blog post about [how JBang integrates with Camel K](/blog/2022/11/camel-k-jbang/)).
+
+YAKS as a framework brings a set of ready-to-use domain specific languages (XML, YAML, Groovy, BDD Cucumber steps) for writing
+tests in order to verify your deployed services.
+
+This post uses the Behavior Driven Development integration via Cucumber.
+So the YAKS test is a single feature file that uses BDD Gherkin syntax like this:
+
+```gherkin
+Feature: Camel K Fruit Store
+
+  Background:
+    Given URL: http://localhost:8080
+
+  Scenario: Create infrastructure
+    # Start AWS S3 container
+    Given Enable service S3
+    Given start LocalStack container
+
+    # Create Camel K integration
+    Given Camel K integration property file aws-s3-credentials.properties
+    When load Camel K integration fruit-service.groovy
+    Then Camel K integration fruit-service should print Started route1 (platform-http:///fruits)
+
+  Scenario: Verify fruit service
+    # Invoke Camel K service
+    Given HTTP request body: yaks:readFile('pineapple.json')
+    And HTTP request header Content-Type="application/json"
+    When send POST /fruits
+    Then receive HTTP 200 OK
+
+    # Verify uploaded S3 file
+    Given New global Camel context
+    Given load to Camel registry amazonS3Client.groovy
+    Given Camel exchange message header CamelAwsS3Key="fruit.json"
+    Given receive Camel exchange from("aws2-s3://medium-sugar?amazonS3Client=#amazonS3Client&deleteAfterRead=true") with body: yaks:readFile('pineapple.json')
+```
+
+Let’s walk through the test step by step.
+First of all the feature file uses the usual Given-When-Then BDD syntax to give context, describe the actions and verify the outcome.
+Each step calls a specific YAKS action that is provided out of the box by the framework.
+The user is able to choose from a [huge set of steps](https://github.com/citrusframework/yaks/tree/main/java/steps) that
+automatically perform actions like sending/receiving Http requests/responses, starting [Testcontainers](https://www.testcontainers.org/),
+running Camel routes, connecting to a database, publishing events on Kafka or Knative brokers and many more.
+
+In the first scenario the test automatically prepares some required infrastructure.
+The YAKS test starts a Localstack [Testcontainer](https://www.testcontainers.org/) to have an AWS S3 test instance running (`Given start LocalStack container`).
+Then the test loads and starts the Camel K integration under test (`When load Camel K integration fruit-service.groovy`) and waits for it to properly start.
+In local testing this step starts the Camel K integration using JBang.
+Later the post will also run the test in a Kubernetes environment.
+
+Now the infrastructure is up and running and the test is able to load the fruit model object as Http request body
+(`Given HTTP request body: yaks:readFile('pineapple.json')`) and invoke the Camel K service (`When send POST /fruits`).
+The test waits for the Http response and verifies its 200 OK status.
+
+In the last step the test verifies that the fruit object has been added to the right AWS S3 bucket (medium-sugar).
+As YAKS itself is not able to connect to AWS S3 the test uses Apache Camel for this step.
+The test creates a Camel context, loads a AWS client and connects to AWS S3 with a temporary Camel route
+(`Given receive Camel exchange from("aws2-s3://medium-sugar?amazonS3Client=#amazonS3Client&deleteAfterRead=true")`).
+With this Apache Camel integration YAKS is able to use the complete 300+ Camel components for sending and receiving messages
+to various messaging transports. The Camel exchange body should be the same fruit model object (`yaks:readFile('pineapple.json'`)
+as posted in the initial Http request.
+
+YAKS uses the powerful message payload validation capabilities provided by Citrus for this message content verification.
+The validation is able to compare message contents of type XML, Json, plaintext and many more.
+
+This completes the test case. You can now run this test with Cucumber and JUnit for instance.
+The easiest way though to directly run tests with YAKS is to use the [YAKS command line client](https://github.com/citrusframework/yaks/releases).
+You do not need to set up a whole project with Maven dependencies and so on.
+Just write the test file and run with:
+
+```shell script
+$ yaks run fruit-service.feature --local
+```
+
+You should see some log output like this:
+
+```
+INFO | 

Review Comment:
   @christophd try removing trailing whitespace like this:
   
   ```suggestion
   INFO |
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org