Skip to content

Commit 336f85b

Browse files
author
Juliya Smith
committed
Migrate from orignal repo
1 parent 95ad107 commit 336f85b

14 files changed

+8317
-0
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM stoplight/prism
2+
3+
RUN apk update
4+
COPY docs docs

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
IMAGE_TAG = c42/mock-microservice-endpoints:1.0
2+
3+
all:: start
4+
5+
start::
6+
@docker-compose up --build
7+
8+
stop::
9+
@docker-compose down --rmi all

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Mock Microservice Endpoints
2+
3+
To start the servers:
4+
5+
```bash
6+
make
7+
```
8+
9+
To stop the servers:
10+
```bash
11+
make stop
12+
```
13+
14+
## Adding a new service
15+
16+
Since we are currently controlling the OpenAPI documentation that powers the mock server,
17+
the format we are using is OpenAPI version 3 as yml.
18+
19+
You can convert existing JSON Swagger v2 swagger using https://editor.swagger.io/.
20+
21+
Some minor editing might be required to get the conversion to work. For example, warnings may appears on the left
22+
margin of the Swagger editor. Address all the warnings before re-attempting the conversion.
23+
A common problem from our docs is that the conversion tool does not like having `descriptions`
24+
(or any additional properties) next a `ref`.
25+
26+
For example, replace:
27+
28+
```yml
29+
description: Indicates setting of how to interact with user list.
30+
$ref: '#/definitions/UsersToAlertOn-alert-rules'
31+
```
32+
33+
with just:
34+
35+
```yml
36+
$ref: '#/definitions/UsersToAlertOn-alert-rules'
37+
```
38+
39+
You may also have issues with enum values. See the section below.
40+
41+
## Endpoint Enums
42+
43+
Another common problem is that our Code42 docs often declare enums as having a type of `integer`.
44+
This will cause failures in Prism. Change the enum type to `string`. This is also needed for the conversion
45+
to OpenAPI 3 to work properly.
46+
47+
Prism is case-sensitive when it comes to enum values whereas typically Code42 servers are not.
48+
To allow case-insensitivity, convert the request enum to use a Pattern.
49+
50+
Let's say you have an OpenAPI yml file containing this enum:
51+
52+
```yml
53+
enum:
54+
- HIGH
55+
- MEDIUM
56+
- LOW
57+
x-enumNames:
58+
- Low
59+
- Medium
60+
- High
61+
```
62+
63+
To use a Pattern instead, replace it with:
64+
65+
```yml
66+
pattern: '^(?i)LOW|MEDIUM|HIGH$'
67+
```
68+
69+
Now, all casings are supported in the mock. `Low`, `low`, `LOW`, etc, are all valid.
70+
71+
## Examples
72+
73+
We have to often control the output of the mocked endpoints. To do this, set examples.
74+
75+
Convert
76+
77+
```yml
78+
archiveBytes:
79+
type: integer
80+
format: int64
81+
```
82+
83+
to
84+
85+
86+
```yml
87+
archiveBytes:
88+
type: integer
89+
format: int64
90+
example: 10000
91+
```
92+
93+
It is not necessary to do this for every response property. Here are reasons you may need to:
94+
95+
* Prism complains about the type returned. If ths happens, setting the example will control the output so Prism can't generate faulty
96+
examples by mistake.
97+
* You are expecting a specific response value in a test. This is how the Key-Value mock endpoints work: they return localhost with
98+
different ports (see section below).
99+
* You want to constrain the response value. Prism likes to use negative integers when the type is an integer, but you might not want that,
100+
especially for IDs like the `userId`.
101+
102+
## docker-compose and the Mock Key-Value Store
103+
104+
Each microservice gets its own port that gets returned from the mock Key-Value Store found in `core.yml`. To add a new service, include
105+
the mocked endpoint for getting your microservice's URL.
106+
107+
Example:
108+
109+
```yml
110+
/v1/KEY_VALUE_FOR_SERVICE_URL:
111+
get:
112+
tags:
113+
- keyvaluestore
114+
summary: Get the microservice URL
115+
operationId: ServiceNameUrl_Get
116+
responses:
117+
200:
118+
description: A successful response
119+
content:
120+
'*/*':
121+
schema:
122+
type: string
123+
example: http://127.0.0.1:4220
124+
```
125+
126+
Then, in the `docker-compose.yml` file, add another entry using your service's mock `.yml` file and an incremented port (the same one you used in
127+
the mock key-value store endpoint).
128+
129+
```yml
130+
service:
131+
image: c42/mock-microservice-endpoints:1.0
132+
build:
133+
context: .
134+
dockerfile: Dockerfile
135+
container_name: mock_service
136+
restart: always
137+
ports:
138+
- "4220:4220"
139+
command: mock docs/service.yml -p 4220 -h 0.0.0.0
140+
```
141+
142+
Notice the port number appears in three places in the `yml` for the docker-compose file.
143+
144+
## Returning empty JSON responses
145+
146+
If your endpoint claims to return application/json but you are returning an empty response,
147+
you might have your service's yml (after conversion from JSON) looking like this:
148+
149+
```yml
150+
200:
151+
description: 'Success: Given alerts are updated to the indicated status.'
152+
content: {}
153+
```
154+
155+
Instead, to return an empty response in a way that Prism understands, do this:
156+
157+
```yml
158+
200:
159+
description: 'Success: Given alerts are updated to the indicated status.'
160+
content:
161+
application/json:
162+
schema:
163+
type: object
164+
```
165+
166+
## Optional Request Parameters
167+
168+
If you don't explitly mark your required request parameters, Prism assumes they are all required.

docker-compose.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
version: '3'
2+
services:
3+
4+
core:
5+
image: c42/mock-microservice-endpoints:1.0
6+
build:
7+
context: .
8+
dockerfile: Dockerfile
9+
container_name: mock_core
10+
restart: always
11+
ports:
12+
- "4200:4200"
13+
command: mock docs/core.yml -p 4200 -h 0.0.0.0
14+
15+
alerts:
16+
image: c42/mock-microservice-endpoints:1.0
17+
build:
18+
context: .
19+
dockerfile: Dockerfile
20+
container_name: mock_alerts
21+
restart: always
22+
ports:
23+
- "4201:4201"
24+
command: mock docs/alerts.yml -p 4201 -h 0.0.0.0
25+
26+
alert_rules:
27+
image: c42/mock-microservice-endpoints:1.0
28+
build:
29+
context: .
30+
dockerfile: Dockerfile
31+
container_name: mock_alert_rules
32+
restart: always
33+
ports:
34+
- "4202:4202"
35+
command: mock docs/alert_rules.yml -p 4202 -h 0.0.0.0
36+
37+
detection_lists:
38+
image: c42/mock-microservice-endpoints:1.0
39+
build:
40+
context: .
41+
dockerfile: Dockerfile
42+
container_name: mock_detection_lists
43+
restart: always
44+
ports:
45+
- "4203:4203"
46+
command: mock docs/detection_lists.yml -p 4203 -h 0.0.0.0
47+
48+
cases:
49+
image: c42/mock-microservice-endpoints:1.0
50+
build:
51+
context: .
52+
dockerfile: Dockerfile
53+
container_name: mock_cases
54+
restart: always
55+
ports:
56+
- "4204:4204"
57+
command: mock docs/cases.yml -p 4204 -h 0.0.0.0
58+
59+
storage:
60+
image: c42/mock-microservice-endpoints:1.0
61+
build:
62+
context: .
63+
dockerfile: Dockerfile
64+
container_name: mock_storage
65+
restart: always
66+
ports:
67+
- "4205:4205"
68+
command: mock docs/storage.yml -p 4205 -h 0.0.0.0
69+
70+
connected_server:
71+
image: c42/mock-microservice-endpoints:1.0
72+
build:
73+
context: .
74+
dockerfile: Dockerfile
75+
container_name: mock_connected_server
76+
restart: always
77+
ports:
78+
- "4206:4206"
79+
command: mock docs/connected_server.yml -p 4206 -h 0.0.0.0
80+
81+
audit_logs:
82+
image: c42/mock-microservice-endpoints:1.0
83+
build:
84+
context: .
85+
dockerfile: Dockerfile
86+
container_name: mock_audit_logs
87+
restart: always
88+
ports:
89+
- "4207:4207"
90+
command: mock docs/audit_logs.yml -p 4207 -h 0.0.0.0
91+
92+
file-events:
93+
image: c42/mock-microservice-endpoints:1.0
94+
build:
95+
context: .
96+
dockerfile: Dockerfile
97+
container_name: mock_file_events
98+
restart: always
99+
ports:
100+
- "4208:4208"
101+
command: mock docs/file-events.yml -p 4208 -h 0.0.0.0
102+
103+
preservation-data-service:
104+
image: c42/mock-microservice-endpoints:1.0
105+
build:
106+
context: .
107+
dockerfile: Dockerfile
108+
container_name: mock_preservation_data_service
109+
restart: always
110+
ports:
111+
- "4209:4209"
112+
command: mock docs/preservation-data-service.yml -p 4209 -h 0.0.0.0

0 commit comments

Comments
 (0)