# Mappings in git

## A repository for the files

Keep the mocks in a repository of their own, or in a folder of the repository that holds the code they are for. That folder is the files root: [point Traffic Parrot at it](https://trafficparrot.com/documentation/5.67.x/file-layout.html#files-root).

```
./start.sh trafficparrot.virtualservice.trafficFilesRootUrl=file:/path/to/repository/mocks
```

The install directory itself stays out of git. It holds the release, the license and the logs, and each machine has its own.

There are two ways to run from a checkout:

- On your own machine, point Traffic Parrot at the files root in your checkout. A mapping you save in the web UI, record or import is written there, ready to review and commit.
- In a CI job, copy the files root and point Traffic Parrot at the copy, as the [CI example](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#ci-example) does, so that nothing Traffic Parrot writes reaches the checkout.

A request writes nothing to the files root, unless its response uses the [object store](https://trafficparrot.com/documentation/5.67.x/dynamic.html#object-store), which keeps its objects as files in `data/`. The requests Traffic Parrot receives are held in memory and written to its logs, in the install directory.

## What to commit and what to ignore

A files root that Traffic Parrot serves holds your files and a few that Traffic Parrot writes itself.

| Path | In git | Why |
| --- | --- | --- |
| `mappings/`, `__files/`, `templates/`, `data/`, `grpc-mappings/`, `proto/`, `jms-mappings/`, `ibm-mq-mappings/`, `file-mappings/`, `websocket-mappings/`, `openapi/`, `asyncapi/`, `copybooks/` | Commit | Your mocks: [The file layout](https://trafficparrot.com/documentation/5.67.x/file-layout.html) says what the mapping directories hold. `openapi/` and `asyncapi/` hold the specifications that [`validate`](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#validate) checks mappings against, and `copybooks/` the [CICS bridge's copybooks](https://trafficparrot.com/documentation/5.67.x/cics.html#copybooks). |
| `scenarios/Name/`, with the same directories inside | Commit | One folder for each [virtual service](https://trafficparrot.com/documentation/5.67.x/virtual_services.html). |
| `scenarios/Name/service.properties` | Commit, with its password lines empty or holding [references](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#passwords) | The virtual service's [ports and replay settings](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#vs-configuration). Traffic Parrot writes it the first time the virtual service is selected. Three of its lines are for HTTPS store passwords. |
| `jms-connections.json`, `ibm-mq-connections.json` and `database-connections.json` in `scenarios/Name/` | Ignore, unless every password is a [reference](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#passwords) | They hold user names and passwords. Traffic Parrot writes them itself into any virtual service's folder that has none. |
| `database-connections.json` at the top of the files root | Ignore, unless every password is a [reference](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#passwords) | The `Default` virtual service reads its database connections, passwords included, from here. |
| `scenarios/Composite/` | Ignore | Traffic Parrot creates it at its first start, and fills it with copies when you combine virtual services: see [below](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#composite). |
| `deleted-scenarios/` | Ignore | Where a virtual service you delete in the web UI is moved. |

A `.gitignore` at the top of the files root that does this:

```
# Written by Traffic Parrot
/scenarios/Composite/
/deleted-scenarios/

# Connection files hold passwords
jms-connections.json
ibm-mq-connections.json
database-connections.json
```

When it starts, Traffic Parrot also creates the directories it serves from that the files root lacks, and an empty `proto-bin/`. Git does not track an empty directory, so they need no entry and no placeholder file. The files the [object store](https://trafficparrot.com/documentation/5.67.x/dynamic.html#object-store) keeps in `data/` are written as requests arrive: ignore them too, unless your mocks start from them. A virtual service whose `data/` is missing or empty is given a copy of the top-level `data/` at each start, so a fresh clone shows it as new files. Commit an empty placeholder file, such as `scenarios/Name/data/.gitkeep`, to a virtual service that has no data files of its own and whose mappings read none of the top-level ones. One whose mappings do read them gets them through this copy: commit the copied files to it instead.

### Combined virtual services

When you [combine virtual services](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#composite-scenarios), `scenarios/Composite/` holds a copy of each one's mappings, [rebuilt](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#composite-copy) when a virtual service is added or removed or a mapping is saved into one, the list of which are combined, and Composite's own ports. Ignore all of it: the copies repeat files that are already in git. A fresh clone therefore starts with nothing combined. Combine the virtual services again after the first start, in the web UI or with the [API](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#composite-api-endpoints):

```bash
curl -X POST "http://localhost:8080/api/scenarios/http/composite/add?scenario=Orders"
```

- Do not commit only the list, `.composite-metadata.json`. A clone then shows the virtual services as combined and serves none of their mappings until one is added or removed, or a mapping is saved into one.
- An edit made while `Composite` is selected is [saved to the virtual service the mapping came from](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#composite-edits) as well, so the change to commit is in that virtual service's folder.
- A mapping file changed on disk, by `git pull` for example, reaches Composite at its [next rebuild](https://trafficparrot.com/documentation/5.67.x/virtual_services.html#composite-copy), not at once. Removing the virtual service from Composite and adding it again rebuilds it, and clears the conflict winners chosen for that virtual service.

### Passwords

A password in a connection file, or one of the three HTTPS key store passwords in a virtual service's `service.properties`, can name where Traffic Parrot reads it instead of holding it:

- `${env:NAME}` reads the environment variable `NAME`;
- `${file:PATH}` reads the file at `PATH`, absolute or relative to the Traffic Parrot install directory, less one final line break.

The reference must be the whole value: `secret-${env:MQ_PASSWORD}` is a plain password. Traffic Parrot reads the secret when it connects, or for a key store when the virtual service starts, and never writes it back, so the file keeps the reference. An unset variable or a file it cannot read fails that connection with a message naming the reference, never the secret. The fields that take a reference:

- `jms-connections.json`: `connectionData.password` for ActiveMQ, RabbitMQ and IBM MQ, and `connectionData.sharedAccessKey` for Azure Service Bus;
- `ibm-mq-connections.json`: `connectionData.password`. In either file, an IBM MQ connection without MQCSP authentication sends the first 12 characters of the secret it read;
- `database-connections.json`: `properties.password` for JDBC and `password` for Couchbase;
- `root-directories.json`: `connectionData.password` for SFTP and FTP;
- `service.properties`: `https.key.store.password`, `https.key.manager.password` and `https.trust.store.password`.

With every password a reference, the connection files can be committed: take their lines out of the `.gitignore` above. Otherwise keep them out of git, unless every password in them is one you would publish, such as the password of a broker that a test starts and throws away. A key store that a virtual service's `service.properties` names holds a private key: add its path to the `.gitignore` too.

Traffic Parrot reads a reference with its own permissions, so restrict who can edit connections, or add `-Dtrafficparrot.password.references.enabled=false` to [`jvm.args`](https://trafficparrot.com/documentation/5.67.x/user_guide.html#jvm-arguments) to read every password as written.

Two kinds of connection are not in the files root at all: the file roots of every virtual service, and the `Default` virtual service's connections to JMS brokers and queue managers. The shipped settings read them from the install directory: [`root-directories.json`](https://trafficparrot.com/documentation/5.67.x/filemessage.html#configuration), [`jms-connections.json`](https://trafficparrot.com/documentation/5.67.x/jms.html#define-jms-connections) and [`ibm-mq-connections.json`](https://trafficparrot.com/documentation/5.67.x/ibmmq.html#define-connections).

### What stays in the install directory

- `logs/`. The [logs](https://trafficparrot.com/documentation/5.67.x/logging.html#where-are-the-logs) record each request Traffic Parrot receives, headers included, so they can hold the tokens and passwords your clients sent.
- `trafficparrot.license`. Give a container or a CI job its license [through an environment variable](https://trafficparrot.com/documentation/5.67.x/user_guide.html#supplying-the-license), not through the repository.
- `trafficparrot.properties` and `jvm.args`, one machine's settings. A job passes the few settings it needs to the [start script](https://trafficparrot.com/documentation/5.67.x/file-layout.html#settings).

## File names and ids that stay put

Git knows a mapping by its file name; your tests and logs know it by its `id` and `name`. A diff stays small when none of them changes from one save to the next. Give the file a name of your own and write the `id` and the `name` into it, as [Write a mapping by hand](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#template-file) does. The `id` then stays put however the mapping is saved. A save in the web UI keeps an HTTP mapping in its file, in a folder of `mappings/` too, unless you change its Name in the editor, which moves it to a file named after the new name ([below](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#ui-save)). A save in the gRPC editor writes the mapping to a file named after its `name`, and removes the file it came from. So for gRPC write the `name` as the file name, and keep mapping files at the top of `grpc-mappings/`: a gRPC mapping in a folder, or one whose `name` is not its file name, is moved by its first save. A mapping file with no `id` is served under one made from its file name, so renaming the file changes its id.

What each other way of writing a mapping gives it:

| Written by | File name | `id` | Done again |
| --- | --- | --- | --- |
| An edit saved in the web UI: HTTP, gRPC, JMS, native IBM MQ or file messages | Kept; for HTTP, unless you change the Name; for gRPC, only when the `name` is the file name and the file is at the top of `grpc-mappings/` | Kept | See [below](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#ui-save) |
| An add in the web UI, HTTP | The method, the URL and the id: `get-orders-<id>.json` | Random | A new file |
| An add in the web UI, gRPC | From a [template](https://trafficparrot.com/documentation/5.67.x/mappings-in-git.html#grpc-file-names): `shop.02.Orders-GetOrder[OK].json` | Random | A new file |
| An add in the web UI for JMS, native IBM MQ or file messages, or a JMS recording | `saved-mapping-<id>.json` | Random | A new file; a JMS exchange recorded twice gives two |
| An HTTP recording from the Record page | `mapping-<URL>-<five characters>.json`, and its response body in `__files/body-<URL>-<the same five>.json` | Random | A request already recorded is answered from the recording and not recorded again |
| An OpenAPI or HAR import | From the operation or the request: `[Orders API] 200 GET _orders_{orderId} application_json.json` | Random | The same file name, a new id |
| A cURL import | `[cURL Import] 200 POST _orders.json` | Random | A command already imported is skipped |
| `POST /api/http/__admin/mappings` with `"persistent": true`, or the [MCP](https://trafficparrot.com/documentation/5.67.x/mcp.html) `create_mapping` tool | `<name>.json` when the mapping has a `name`, otherwise as an add in the web UI | The one you send to the API, otherwise random | With no `name`, a new file. The MCP tool refuses a `name` that is already a file |
| An edit of an HTTP mapping through `PUT /api/http/__admin/mappings/<id>` or the MCP `update_mapping` tool | Kept when the mapping you send has the `name` it had. With another `name`, or none, named as in the row above, and the file it came from is removed unless that is the same file | Kept | The same |
| An import of an [HTTP](https://trafficparrot.com/documentation/5.67.x/http.html#import-http-export-zip) or [gRPC](https://trafficparrot.com/documentation/5.67.x/grpc.html#import-grpc-usage) export | The path and name the file had when it was exported | The ids in the file | The same files |

An HTTP or gRPC export copies a mapping file as it is, apart from [the mappings it writes afresh or leaves out](https://trafficparrot.com/documentation/5.67.x/http.html#export-http-usage). So exporting, deleting all the mappings and importing the ZIP file gives the copied files back at the same paths, byte for byte, and git shows no change for them.

An OpenAPI or HAR import done again keeps each file's name and changes its `id`, so git shows two changed lines in each file. The names an import gives hold spaces, brackets and braces: quote them in a shell. For a way of writing a mapping that is not in the table, such as another import format, add one mapping and read `git status`.

### What a save in the web UI changes

A save in the web UI writes the whole file again in its own layout. For an HTTP mapping you wrote by hand, the first save:

- keeps the `id`;
- keeps the mapping in its file, in a folder of `mappings/` too, and keeps its `name`, as long as you leave the editor's Name field as it is: `orders-paid.json` with the name `Paid orders` keeps both. A mapping with no `name` gains one, its path under `mappings/`: `orders/shipped.json`;
- moves the mapping when you change the Name: to a file at the top of `mappings/` named after the new name, with `.json` added, in place of its last extension when it has one, and each run of `/ \ : * ? " < > |` replaced by `_`, and with `name` set to that file name. Git shows that as one file deleted and another added; if that is the name the mapping had, the file stays where it is;
- removes the `$schema` line, and adds `uuid`, a copy of the `id`, and `insertionIndex`;
- changes the spacing and the order of the keys, and removes the newline at the end of the file;
- writes the mapping's body file again with Windows line endings, so git lists it as changed although you did not edit it, and the response it serves gains a carriage return at the end of each line.

So the first save shows in git as a change to almost every line of the mapping. Commit it on its own. After that a save changes only what you edited: a save with nothing edited leaves both files as they were, and no save touches another mapping's file.

Enabling or disabling an HTTP mapping in the mappings list keeps its file too, and so does an edit through `PUT /api/http/__admin/mappings/<id>` or the [MCP](https://trafficparrot.com/documentation/5.67.x/mcp.html) `update_mapping` tool that sends the mapping's `name` as it was. One that sends another `name`, or none, writes the mapping to a file named as a new mapping's is, and removes the file it came from unless that is the same file. With [`trafficparrot.http.mappings.editKeepsFile=false`](https://trafficparrot.com/documentation/5.67.x/properties.html#trafficparrot.http.mappings.editKeepsFile), an HTTP edit of a mapping in a file of its own writes it to a file named after its `name` at the top of `mappings/`, the editor adding `.json` to the name, and removes the file it came from when that is another file.

A save in the gRPC editor, with the edit dialog's keep-name option left ticked, as it is by default, keeps the `id` and writes the mapping to a file named after its `name` at the top of `grpc-mappings/`, removing the file it came from. So a gRPC mapping keeps its file only when its `name` is the file name, with or without the extension, and the file is at the top of `grpc-mappings/`. Its `name` becomes the file name, extension included, and a JSON body or matcher written as a string is saved as an object. A JMS, native IBM MQ or file message mapping keeps its file name and `id`, loses `$schema` and has its `mappingName` set to the file name. A JMS or native IBM MQ mapping also gains the keys you left at their defaults.

Reload the editor after you switch branch. An edit form that was open before a `git checkout` still shows the other branch's mapping, and its Save writes that over the branch you are now on, body file included, with no warning.

### gRPC file names

The web UI names a new gRPC mapping from a template, [`trafficparrot.virtualservice.grpc.saved.mapping.file.name.template`](https://trafficparrot.com/documentation/5.67.x/properties.html#trafficparrot.virtualservice.grpc.saved.mapping.file.name.template). The shipped template puts a number in the name, one more than the mappings the package already holds: the second mapping in package `shop` is `shop.02.Orders-GetOrder[OK].json`. Two branches that each add a mapping for the same method are therefore given the same file name, and git reports a conflict when they merge. A template that names the file from the mapping alone avoids that:

```
trafficparrot.virtualservice.grpc.saved.mapping.file.name.template={{ mapping.service }}-{{ mapping.method }}-{{ mapping.id }}
```

With it, a new mapping for `Orders/CancelOrder` is saved as `Orders-CancelOrder-<id>.json`. Set it in the `trafficparrot.properties` of every install whose web UI saves to the repository.

## One set of files for a team

- Each person and each job clones the repository and points its own instance at the files root in its own checkout.
- An HTTP mapping or body file that a `git pull`, or a `git checkout` of another branch, changes is served from the next request with no restart, under the [shipped settings](https://trafficparrot.com/documentation/5.67.x/file-layout.html#mapping-cache). [The file layout](https://trafficparrot.com/documentation/5.67.x/file-layout.html#when-changes-take-effect) lists what waits for replay to be restarted or for a virtual service to be selected.
- Kept in the repository of the code they are for, the mocks follow its branches: a branch that changes an API carries the mappings that match it.
- Run [`validate`](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#validate) on the files root before a commit and in CI.
- A job selects the virtual service it needs with [one API call](https://trafficparrot.com/documentation/5.67.x/working-with-files.html#ui-file-api).

### A schema for everyone's editor

A `$schema` line that names `http://localhost:8080` works only where an instance is running on that port, and a save in the web UI removes the line. For a repository several people edit, commit a copy of each [mapping schema](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#editor-setup) and the editor settings that map it to its mapping directory, so that every editor checks the files with no instance running. Fetch the copies again after an upgrade: the schemas describe the release that serves them.

Keep the copies in a folder of their own at the top of the files root, such as `schemas/`, never in a mapping directory, where [Traffic Parrot would load them as mappings](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#editor-setup). Traffic Parrot and `validate` both ignore a `schemas/` folder and a `.vscode/` folder at the top of the files root.

## Review a recording before you commit it

A recording is a copy of what a real system sent. Before it goes into git, read it for three things.

- **Values that change.** An HTTP recording keeps the response headers the system sent, `Date` among them. Delete the headers your tests do not read.
- **Credentials.** A response header such as `Set-Cookie` is saved as it arrived, and so is a key or token in the URL's query string or in a body. A request header is saved only when you list it under [Record request headers for matching](https://trafficparrot.com/documentation/5.67.x/http.html#recording-headers). [Header redaction](https://trafficparrot.com/documentation/5.67.x/http.html#recording-header-redaction), off until you turn it on, masks credential headers, and the [request body rewrite rules](https://trafficparrot.com/documentation/5.67.x/http.html#recording-request-body-rewriting) rewrite a request body, as the recording is made. Read the recorded URLs and response bodies yourself.
- **Names.** Recorded files have generated names and ids. Rename a mapping file to say what it is: its `id` is written in the file, so it stays. The response body is a file of its own in `__files/`: to rename it too, change the mapping's `bodyFileName` to the new name.

To record a request afresh, delete its mapping and its body file first. While they are there, the recorder answers the request from them and records nothing. The new recording has a new file name and a new id.

A JMS recording writes one `saved-mapping-<id>.json` for each exchange, the same exchange twice included, so delete the repeats. Then run [`validate`](https://trafficparrot.com/documentation/5.67.x/mappings-by-hand.html#validate), and commit.

**Missing something?** See [When the docs do not say](https://trafficparrot.com/documentation/5.67.x/when-the-docs-do-not-say.html) for where else to look, or [tell us what this page lacks](https://trafficparrot.com/support.html?page=/documentation/mappings-in-git.html).
