Fork 0

Update README with solution documentation

Frida Hjelm 2 years ago
parent 64be5911f5
commit aa2394bf66

@ -1,6 +1,6 @@
# Java exercise for the backend recruiting process
# Solution to the Java exercise for the backend recruiting process
## Prerequisites
## Running the application
1. Make sure you have a working Java development environment. The Maven configuration of the boilerplate requires at least Java 11.
2. Clone this repository and don't fork as your code would be visible to other candidates in case you choose to push your implementation to a public repository.
@ -9,14 +9,6 @@
5. Test the API by fetching data from the status resource by executing `curl http://localhost:8282/status`.
6. If any of the above fails you need to revisit your development environment or talk to [hannes@metasolutions.se](mailto:hannes@metasolutions.se) (it may be a bug).
## Boilerplate
As you could see as the result of 5. above, the boilerplate is a very basic, but fully functional implementation of a REST API. The boilerplate is built using the [Restlet framework](https://restlet.talend.com/). You may find its documentation of the [resource architecture](https://restlet.talend.com/documentation/user-guide/2.4/core/resource/overview) and the [server tutorial](https://restlet.talend.com/documentation/user-guide/2.4/introduction/first-steps/first-application) useful.
There are only two REST resources: a status resource that attaches to `/status` and a default resource that responds at `/`.
Implementing a new resource involves creating a new class (see `StatusResource.java` as example) and adding it to the router in `RestApplication.java`.
## Exercise
1. Implement a new resource `/echo` that responds with the request body when posting (HTTP POST) to it; it should simply mirror the request back as response.
@ -42,3 +34,52 @@ If anything is unclear or if you get stuck somewhere in the exercise, do not hes
## Result
Document your implementation in Text or Markdown format and send it along with your code as a tar.gz archive to [Hannes](mailto:hannes@metasolutions.se). You can also send a link to a Git repository.
## Solution
The `/echo` endpoint will convert any data posted with `Content-Type: text/csv` and `Accept: text/html` to a simple html table.
All other data will be echoed back unchanged. Empty datasets will return an explanatory message.
* `RestApplication.java`
* New router added for path `/echo`
* `resources/EchoResource.java`
* `POST` requests are handled depending on `Content-Type`. CSV-data is handled by the `tablify` method, all others by the `echo` method
* The `tablify` method:
* Checks for `Accept: text/html` to determine if table conversion should be performed. Requests that do not match are treated like other `Content-Type`s.
* Assumes the CSV format in the example.
* Returns an explanatory string if POST data is missing or cannot be parsed.
### Example use
``` bash
$ cat test.csv
Important document,This is an important document,2022-03-31
Less important document,,2022-03-31
Last document,,
$ curl -H 'Content-Type: text/csv' -H 'Accept: text/html' -X POST --data-binary @test.csv http://localhost:8282/echo
<td>Important document</td>
<td>This is an important document</td>
<td>Less important document</td>
<td>Last document</td>
$ curl -X POST -d "Example string" http://localhost:8282/echo
Example string