You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: step-2-exploring-the-app.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ but also relies on an existing API we have [introduced in a previous post](https
6
6
7
7

8
8
9
-
The `Order Service` application has been designed around 5 main components that are directly mapped on Spring Boot components and classes:
9
+
The `Order Service` application has been designed around 5 main components that are directly mapped on NestJS components and classes:
10
10
* The [`OrderController`](src/order/order.controller.ts) is responsible for exposing an `Order API` to the outer world.
11
11
* The [`OrderService`](src/order/order.service.ts) is responsible for implementing the business logic around the creation of orders.
12
12
* The [`PastryAPIClient`](src/pastry/pastry.service.ts) is responsible for calling the `Pastry API` in *Product Domain* and get details or list of pastries.
This is a super powerful way to ensure that your application logic (caching, no caching, etc.) is correctly implemented and
151
+
use the mock endpoints when required 🎉
152
+
98
153
## Second Test - Verify the technical conformance of Order Service API
99
154
100
155
The 2nd thing we want to validate is the conformance of the `Order API` we'll expose to consumers. In this section and the next one,
@@ -133,7 +188,7 @@ test we want to run:
133
188
* We ask for testing our endpoint against the service interface of `Order Service API` in version `0.1.0`.
134
189
These are the identifiers found in the `order-service-openapi.yml` file.
135
190
* We ask Microcks to validate the `OpenAPI Schema` conformance by specifying a `runnerType`.
136
-
* We ask Microcks to validate the localhost endpoint on the dynamic port provided by the Spring Test (we use the `host.testcontainers.internal` alias for that).
191
+
* We ask Microcks to validate the localhost endpoint on the dynamic port provided by the `beforeAll()` function (we use the `host.testcontainers.internal` alias for that).
137
192
138
193
Finally, we're retrieving a `TestResult` from Microcks containers, and we can assert stuffs on this result, checking it's a success.
Copy file name to clipboardExpand all lines: step-5-write-async-tests.md
+69-1
Original file line number
Diff line number
Diff line change
@@ -151,9 +151,43 @@ sequenceDiagram
151
151
```
152
152
153
153
Because the test is a success, it means that Microcks has received an `OrderEvent` on the specified topic and has validated the message
154
-
conformance with the AsyncAPI contract or this event-driven architecture. So you're sure that all your Spring Boot configuration, Kafka JSON serializer
154
+
conformance with the AsyncAPI contract or this event-driven architecture. So you're sure that all your NestJS configuration, Kafka JSON serializer
155
155
configuration and network communication are actually correct!
156
156
157
+
### 🎁 Bonus step - Verify the event content
158
+
159
+
So you're now sure that an event has been sent to Kafka and that it's valid regarding the AsyncAPI contract. But what about the content
160
+
of this event? If you want to go further and check the content of the event, you can do it by asking Microcks the events read during the
161
+
test execution and actually check their content. This can be done adding a few lines of code:
162
+
163
+
```ts
164
+
it ('should publish an Event when Order is created', async () => {
165
+
// [...] Unchanged comparing previous step.
166
+
167
+
// Get the Microcks test result.
168
+
var testResult =awaittestResultPromise;
169
+
170
+
// [...] Unchanged comparing previous step.
171
+
172
+
// Check the content of the emitted event, read from Kafka topic.
173
+
let events:UnidirectionalEvent[] =awaitensemble.getMicrocksContainer().getEventMessagesForTestCase(testResult, "SUBSCRIBE orders-created");
174
+
175
+
expect(events.length).toBe(1);
176
+
177
+
let message:EventMessage=events[0].eventMessage;
178
+
let orderEvent =JSON.parse(message.content);
179
+
180
+
expect(orderEvent.changeReason).toBe('Creation');
181
+
let order =orderEvent.order;
182
+
expect(order.customerId).toBe("123-456-789");
183
+
expect(order.totalPrice).toBe(8.4);
184
+
expect(order.productQuantities.length).toBe(2);
185
+
});
186
+
```
187
+
188
+
Here, we're using the `getEventMessagesForTestCase()` method on the Microcks container to retrieve the messages read during the test execution.
189
+
Using the wrapped `EventMessage` class, we can then check the content of the message and assert that it matches the order we've created.
190
+
157
191
## Second Test - Verify our OrderEventListener is processing events
158
192
159
193
In this section, we'll focus on testing the `Event Consumer` + `Order Service` components of our application:
@@ -164,7 +198,41 @@ The final thing we want to test here is that our `OrderEventListener` component
164
198
for consuming messages, for de-serializing them into correct Java objects and for triggering the processing on the `OrderService`.
165
199
That's a lot to do and can be quite complex! But things remain very simple with Microcks 😉
166
200
201
+
Let's review the test spec `order-event.listener.e2e-spec.ts` under `test`.
167
202
203
+
```ts
204
+
it ('should consume an Event and process Service', async () => {
205
+
let retry =0;
206
+
207
+
while (retry<10) {
208
+
try {
209
+
let order =orderService.getOrder('123-456-789');
210
+
if (orderIsValid(order)) {
211
+
break;
212
+
}
213
+
} catch (e) {
214
+
if (einstanceofOrderNotFoundException) {
215
+
// Continue until we get the end of the poll loop.
216
+
} else {
217
+
// Exit here.
218
+
throwe;
219
+
}
220
+
}
221
+
awaitdelay(500);
222
+
retry++
223
+
}
224
+
});
225
+
226
+
function orderIsValid(order:Order) :boolean {
227
+
if (order) {
228
+
expect(order.customerId).toBe('lbroudoux');
229
+
expect(order.status).toBe(OrderStatus.VALIDATED);
230
+
expect(order.productQuantities.length).toBe(2);
231
+
returntrue;
232
+
}
233
+
returnfalse;
234
+
}
235
+
```
168
236
169
237
To fully understand this test, remember that as soon as you're launching the test, we start Kafka and Microcks containers and that Microcks
170
238
is immediately starting publishing mock messages on this broker. So this test actually starts with a waiting loop, just checking that the
0 commit comments