|
| 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. |
0 commit comments