Skip to content

Commit

Permalink
Merge pull request #1 from lbroudoux/microcks-alternative
Browse files Browse the repository at this point in the history
Microcks alternative to WireMock with OpenAPI contract test
  • Loading branch information
shelajev authored Mar 11, 2024
2 parents 826b249 + 379428e commit da9e7c5
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 76 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<java.version>17</java.version>
<awspring.version>3.1.0</awspring.version>
<commons-io.version>1.3.2</commons-io.version>
<wiremock-testcontainers-module.version>1.0-alpha-13</wiremock-testcontainers-module.version>
<microcks-testcontainers-module.version>0.2.4</microcks-testcontainers-module.version>
<spotless-maven-plugin.version>2.41.1</spotless-maven-plugin.version>
</properties>

Expand Down Expand Up @@ -125,9 +125,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock.integrations.testcontainers</groupId>
<artifactId>wiremock-testcontainers-module</artifactId>
<version>${wiremock-testcontainers-module.version}</version>
<groupId>io.github.microcks</groupId>
<artifactId>microcks-testcontainers</artifactId>
<version>${microcks-testcontainers-module.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
96 changes: 96 additions & 0 deletions src/main/resources/catalog-openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
openapi: 3.0.2
info:
title: Catalog Service
version: 1.0
description: API definition of Catalog Service
license:
name: MIT License
url: https://opensource.org/licenses/MIT
paths:
/api/products/{code}:
get:
parameters:
- name: code
description: product code
schema:
type: string
in: path
required: true
examples:
P101:
value: P101
P102:
value: P102
P103:
value: P103
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
examples:
P101:
value:
id: 1
code: P101
name: Product P101
description: Product P101 description
imageUrl: null
price: 34.0
available: true
P102:
value:
id: 1
code: P101
name: Product P102
description: Product P102 description
imageUrl: null
price: 25.0
available: true
P103:
value:
id: 3
code: P103
name: Product P103
description: Product P103 description
imageUrl: null
price: 15.0
available: false
components:
schemas:
Product:
title: Root Type for catalog Product
type: object
properties:
id:
description: Unique identifier of this product
type: number
code:
description: Code of this product
type: string
name:
description: Name of this product
type: string
description:
description: Description of this product
type: string
imageUrl:
description: Url of image of this product
type: string
nullable: true
price:
description: Price of this product
type: number
available:
description: Availability of this product
type: boolean
required:
- id
- code
- name
- description
- price
- imageUrl
- available
additionalProperties: false
2 changes: 1 addition & 1 deletion step-1-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ This might be helpful if the internet connection at the workshop venue is somewh
```shell
docker pull postgres:16-alpine
docker pull localstack/localstack:2.3
docker pull wiremock/wiremock:3.2.0-alpine
docker pull quay.io/microcks/microcks-uber:1.8.1
docker pull confluentinc/cp-kafka:7.5.0
docker pull confluentinc/cp-schema-registry:7.5.0
docker pull confluentinc/cp-enterprise-control-center:7.5.0
Expand Down
2 changes: 1 addition & 1 deletion step-2-exploring-the-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ and `com.testcontainers.catalog.events.ProductEventListener`.

## External Service Integrations
Our application talks to `inventory-service` to fetch the product availability information.
We will use [WireMock](https://wiremock.org/) to mock the `inventory-service` during local development and testing.
We will use [Microcks](https://microcks.io/) to mock the `inventory-service` during local development and testing.

## API Endpoints

Expand Down
153 changes: 87 additions & 66 deletions step-3-local-development-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,97 +238,118 @@ When we invoke the `GET /api/products/{code}` API endpoint,
the application tried to call the inventory service to get the inventory details.
As the inventory service is not running, we get the above error.
Let's use WireMock to mock the inventory service APIs for our local development and testing.
Let's use Microcks to mock the inventory service APIs for our local development and testing.
## Configure WireMock
## Configure Microcks
Add the following dependency to your `pom.xml`:
```xml
<dependency>
<groupId>org.wiremock.integrations.testcontainers</groupId>
<artifactId>wiremock-testcontainers-module</artifactId>
<version>1.0-alpha-13</version>
<groupId>io.github.microcks</groupId>
<artifactId>microcks-testcontainers</artifactId>
<version>0.2.4</version>
<scope>test</scope>
</dependency>
```
Create `src/test/resources/mocks-config.json` to define Mock API behaviour.
```json
{
"mappings": [
{
"request": {
"method": "GET",
"urlPattern": "/api/inventory/P101"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"code": "P101",
"quantity": 25
}
}
},
{
"request": {
"method": "GET",
"urlPattern": "/api/inventory/P102"
},
"response": {
"status": 500,
"headers": {
"Content-Type": "application/json"
}
}
},
{
"request": {
"method": "GET",
"urlPattern": "/api/inventory/P103"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"code": "P103",
"quantity": 0
}
}
}
]
}
Imagine you got an OpenAPI definition for the inventory service from the service provider.
Create `src/test/resources/inventory-openapi.yaml` with this content; this will define the Mocks behaviour:
```yaml
openapi: 3.0.2
info:
title: Inventory Service
version: 1.0
description: API definition of Inventory Service
license:
name: MIT License
url: https://opensource.org/licenses/MIT
paths:
/api/inventory/{code}:
get:
parameters:
- name: code
description: product code
schema:
type: string
in: path
required: true
examples:
P101:
value: P101
P102:
value: P102
P103:
value: P103
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
examples:
P101:
value:
code: P101
quantity: 25
P103:
value:
code: P103
quantity: 0
"500":
content:
application/json:
schema:
type: string
examples:
P102:
value: ""
components:
schemas:
Product:
title: Root Type for Product
type: object
properties:
code:
description: Code of this product
type: string
quantity:
description: Remaining quantity for this product
type: number
required:
- code
- quantity
additionalProperties: false
```

Next, update the `ContainersConfig` class to configure the `WireMockContainer` as follows:
Next, update the `ContainersConfig` class to configure the `MicrocksContainer` as follows:

```java
package com.testcontainers.catalog;
import org.wiremock.integrations.testcontainers.WireMockContainer;
import io.github.microcks.testcontainers.MicrocksContainer;
@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {
// [...]
@Bean
WireMockContainer wiremockServer(DynamicPropertyRegistry registry) {
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:3.2.0-alpine")
.withMappingFromResource("mocks-config.json");
registry.add("application.inventory-service-url", wiremockServer::getBaseUrl);
return wiremockServer;
MicrocksContainer microcksContainer(DynamicPropertyRegistry registry) {
MicrocksContainer microcks = new MicrocksContainer("quay.io/microcks/microcks-uber:1.8.1")
.withMainArtifacts("inventory-openapi.yaml")
.withAccessToHost(true);
registry.add(
"application.inventory-service-url", () -> microcks.getRestMockEndpoint("Inventory Service", "1.0"));
return microcks;
}
}
```

Once the WireMock server is started, we are registering the WireMock server URL as `application.inventory-service-url`.
So, when we make a call to `inventory-service` from our application, it will call the WireMock server instead.
Once the Microcks server is started, we are registering the Microcks provided mock endpoint as `application.inventory-service-url`.
So, when we make a call to `inventory-service` from our application, it will call the Microcks endpoint instead.

Now restart the `TestApplication` and invoke the `GET /api/products/P101` API again.

Expand Down Expand Up @@ -367,7 +388,7 @@ You should get the following response with `"available":false` because we mocked
}
```

Now we have a working local development environment with PostgreSQL, Kafka, LocalStack, and WireMock.
Now we have a working local development environment with PostgreSQL, Kafka, LocalStack, and Microcks.

###
[Next](step-4-connect-to-services.md)
Loading

0 comments on commit da9e7c5

Please sign in to comment.