Per-test data recipe: the example files
=======================================

The files behind the "Per-test data recipe" page of the Traffic Parrot documentation
(Dynamic Responses > Per-test data). They keep customers, each with a list of addresses,
in a database keyed by an eight-digit reference, so that a test suite can create the
record each test needs by posting only the fields it cares about.

Where each file goes
--------------------

Unzip this file into the Traffic Parrot installation directory. Its folders match the
installation layout, so every file lands where Traffic Parrot reads it:

  database-connections.json                  installation root: the JDBC connection "example.db"
  sql/test-data.sql                          installation root, sql/: the tables and the sequence
  mappings/test-data-customer-create.json    mappings/: POST /test-data/customers, the create
  mappings/test-data-customer-create-existing-reference.json
                                             mappings/: the priority-1 mapping that answers 409
  mappings/customer-by-reference.json        mappings/: GET /customers/<reference>, the read
  templates/test-data/customer-defaults.json templates/test-data/: the defaults a request may leave out
  templates/test-data/customer-create.hbs    templates/test-data/: the create template
  templates/test-data/customer-exists.hbs    templates/test-data/: the request matcher script
  templates/test-data/customer-exists-response.hbs
                                             templates/test-data/: the 409 body
  templates/test-data/customer-by-reference.hbs
                                             templates/test-data/: the read template

database-connections.json is the whole file for a fresh installation. If yours already
lists connections, add the "example.db" entry to it instead of replacing it.

Then put the JDBC driver jar for your database in lib/external and start Traffic Parrot.
The connection as shipped is H2 in memory (driver class org.h2.Driver, the h2 jar from
h2database.com), with placeholder credentials: user "sa", empty password. For a database
server, replace jdbcUrl, user and password with your own, leave INIT out of the URL, and
run sql/test-data.sql once when the database is created.

Copying the files into place is the whole installation. There is no import step for a
bundle like this: the HTTP mapping import on the web console takes mappings alone, not
templates or database connections. The mappings appear on the HTTP Add/Edit page once
Traffic Parrot has loaded them.

Try it
------

Against the virtual service HTTP port (8081 unless you changed it):

  curl -s -X POST localhost:8081/test-data/customers -H 'Content-Type: application/json' -d '{"givenName": "Sam"}'
    201  {"reference": "90000000"}

  curl -s -X POST localhost:8081/test-data/customers -H 'Content-Type: application/json' -d '{"reference": "90000000"}'
    409  {"error": "Customer '90000000' already exists"}

  curl -s localhost:8081/customers/90000000
    200  {"reference": "90000000", "givenName": "Sam", "familyName": "Example", "status": "active", "addresses": [{"kind": "home", "line1": "10 Default Road", "city": "Exampleton", "postcode": "EX9 9ZZ"}]}

  curl -s localhost:8081/customers/00000000
    404  {"error": "No customer '00000000'"}

A record lives until the database is recreated: for H2 in memory, restart Traffic Parrot;
on a server, re-run your drop, create and seed script.
