Skip to content

Commit db7645a

Browse files
authored
Merge pull request #15 from znsio/multi-value-dict
Add multi-value dictionary documentation
2 parents c64c9c1 + 29d6c07 commit db7645a

File tree

4 files changed

+668
-422
lines changed

4 files changed

+668
-422
lines changed

documentation/contract_tests.md

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ Contract Tests
6363
- [Advanced Features](#advanced-features)
6464
- [Generative Tests](#generative-tests)
6565
- [Limiting the Count of Tests](#limiting-the-count-of-tests)
66-
- [Dictionary](#dictionary)
67-
- [Create the Specification](#create-the-specification)
68-
- [Create a Dictionary](#create-a-dictionary)
69-
- [Run the tests Tests](#run-the-tests-tests)
70-
- [Generative Tests](#generative-tests-1)
7166
- [Sample Project](#sample-project)
7267

7368
### Overview
@@ -1648,174 +1643,6 @@ In this example, we may ensure that just the first 2 tests run with the followin
16481643
System.setProperty("MAX_TEST_REQUEST_COMBINATIONS", "2");
16491644
```
16501645

1651-
### Dictionary
1652-
1653-
When Specmatic generates requests for tests and does not find any examples, it will create the requests using the structure and keys defined by the request schema in the specifications, and it will fill in the structure with random values, based again on the same schema.
1654-
1655-
In order to obtain domain-specific requests without examples, you can supply a dictionary of values to Specmatic. Specmatic will then reference this dictionary when it needs to generate a request.
1656-
1657-
#### Create the Specification
1658-
To understand how this works, create the `employees.yaml` specification file as follows:
1659-
1660-
```yaml
1661-
openapi: 3.0.0
1662-
info:
1663-
title: Employees
1664-
version: '1.0'
1665-
servers: []
1666-
paths:
1667-
/employees:
1668-
post:
1669-
requestBody:
1670-
content:
1671-
application/json:
1672-
schema:
1673-
$ref: '#/components/schemas/EmployeeDetails'
1674-
responses:
1675-
200:
1676-
description: Employee Created
1677-
content:
1678-
application/json:
1679-
schema:
1680-
$ref: '#/components/schemas/Employee'
1681-
1682-
400:
1683-
description: Bad Request
1684-
components:
1685-
schemas:
1686-
Employee:
1687-
type: object
1688-
required:
1689-
- id
1690-
- name
1691-
- department
1692-
properties:
1693-
id:
1694-
type: integer
1695-
employeeCode:
1696-
type: string
1697-
name:
1698-
type: string
1699-
department:
1700-
type: string
1701-
1702-
EmployeeDetails:
1703-
type: object
1704-
required:
1705-
- name
1706-
- department
1707-
properties:
1708-
name:
1709-
type: string
1710-
department:
1711-
type: string
1712-
employeeCode:
1713-
type: string
1714-
```
1715-
1716-
1717-
#### Create a Dictionary
1718-
Now create a dictionary file named `employees_dictionary.yaml` in the same directory, the dictionary can also be in `JSON` format,
1719-
following the naming convention `<spec-name>_dictionary.<format>` as follows:
1720-
1721-
{% tabs dictionary %}
1722-
{% tab dictionary yaml %}
1723-
```yaml
1724-
EmployeeDetails.name: John Doe
1725-
EmployeeDetails.department: IT
1726-
EmployeeDetails.employeeCode: "12345"
1727-
```
1728-
{% endtab %}
1729-
{% tab dictionary json %}
1730-
```json
1731-
{
1732-
"EmployeeDetails.name": "John Doe",
1733-
"EmployeeDetails.department": "IT",
1734-
"EmployeeDetails.employeeCode": "12345"
1735-
}
1736-
```
1737-
{% endtab %}
1738-
{% endtabs %}
1739-
1740-
> **Note:** The order of priority for dictionary formats is as follows: `.yml`, `.yaml`, and then `.json`.
1741-
<br/>To understand the syntax of dictionary refer to [service-virtualization](/documentation/service_virtualization_tutorial.html#use-meaningful-response-values-from-an-external-dictionary)
1742-
1743-
#### Run the tests Tests
1744-
Now to execute contract tests on the specification using the dictionary a service is required, we will utilize [service-virtualization](/documentation/service_virtualization_tutorial.html) for this purpose.
1745-
1746-
{% tabs test %}
1747-
{% tab test java %}
1748-
```shell
1749-
java -jar specmatic.jar stub employees.yaml
1750-
```
1751-
{% endtab %}
1752-
{% tab test npm %}
1753-
```shell
1754-
npx specmatic stub employees.yaml
1755-
```
1756-
{% endtab %}
1757-
{% tab test docker %}
1758-
```shell
1759-
docker run --rm --network host -v "$(pwd)/employees.yaml:/usr/src/app/employees.yaml" -v "$(pwd)/employees_dictionary.yaml:/usr/src/app/employees_dictionary.yaml" znsio/specmatic stub "employees.yaml"
1760-
```
1761-
{% endtab %}
1762-
{% endtabs %}
1763-
1764-
Next, execute the contract tests by running the following command:
1765-
1766-
{% tabs test %}
1767-
{% tab test java %}
1768-
```shell
1769-
java -jar specmatic.jar test employees.yaml
1770-
```
1771-
{% endtab %}
1772-
{% tab test npm %}
1773-
```shell
1774-
npx specmatic test employees.yaml
1775-
```
1776-
{% endtab %}
1777-
{% tab test docker %}
1778-
```shell
1779-
docker run --rm --network host -v "$(pwd)/employees.yaml:/usr/src/app/employees.yaml" -v "$(pwd)/employees_dictionary.yaml:/usr/src/app/employees_dictionary.yaml" znsio/specmatic test "employees.yaml"
1780-
```
1781-
{% endtab %}
1782-
{% endtabs %}
1783-
1784-
We can now examine the request sent to the service by reviewing the logs.
1785-
```shell
1786-
POST /employees
1787-
Accept-Charset: UTF-8
1788-
Accept: */*
1789-
Content-Type: application/json
1790-
{
1791-
"name": "John Doe",
1792-
"department": "IT",
1793-
"employeeCode": "12345"
1794-
}
1795-
```
1796-
Notice that the values from the dictionary are utilized in the requests.
1797-
1798-
#### Generative Tests
1799-
1800-
As it's evident that only valid values can be included in the dictionary. hence, generative tests will ignore the values in the dictionary for the key being mutated.
1801-
The other keys will still retrieve values from the dictionary if available; otherwise, random values will be generated.
1802-
1803-
For instance, if you execute the specification with generative tests enabled, one of the request will appear as follows:
1804-
```shell
1805-
POST /employees
1806-
Accept-Charset: UTF-8
1807-
Accept: */*
1808-
Content-Type: application/json
1809-
{
1810-
"name": null,
1811-
"department": "IT",
1812-
"employeeCode": "12345"
1813-
}
1814-
```
1815-
1816-
In this case, the key `name` is being mutated, which results in the value from the dictionary being disregarded.
1817-
While the values for `department` and `employeeCode` are still being retrieved from the dictionary.
1818-
18191646
### Sample Project
18201647

18211648
[https://github.com/znsio/specmatic-order-api-java](https://github.com/znsio/specmatic-order-api-java)

0 commit comments

Comments
 (0)