From: Ronald Holshausen Date: Tue, 29 Sep 2020 07:24:56 +0000 (+1000) Subject: feat: navigate from root X-Git-Tag: wip-umbau-array-contains~29 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=1931500aaefbe7fe63e775a39a5408d17672ccbf;p=demos%2Fexample-siren feat: navigate from root --- diff --git a/consumer/src/__tests__/delete-order.spec.js b/consumer/src/__tests__/delete-order.spec.js index 2b79bf1..7bf9dde 100644 --- a/consumer/src/__tests__/delete-order.spec.js +++ b/consumer/src/__tests__/delete-order.spec.js @@ -28,6 +28,20 @@ describe("Siren Pact test", () => { it('deletes the first order using the delete action', () => { provider + .uponReceiving("get root") + .withRequest({ + path: "/", + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/vnd.siren+json' + }, + body: { + class:[ "representation"], + links:[{"rel":["orders"], "href": url("http://localhost:9000", ["orders"]) }] + } + }) .uponReceiving("get all orders") .withRequest({ path: "/orders", diff --git a/consumer/src/consumer.js b/consumer/src/consumer.js index b9e3dac..2c193a2 100644 --- a/consumer/src/consumer.js +++ b/consumer/src/consumer.js @@ -3,7 +3,7 @@ const { Client } = require('ketting') async function deleteFirstOrder(url) { const client = new Client(url) - const ordersResource = client.go('/orders') + const ordersResource = await client.follow('orders') const ordersResp = await ordersResource.get() const firstOrder = ordersResp.getEmbedded()[0] diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java index a51184a..6881069 100644 --- a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.afford; @@ -19,6 +20,7 @@ import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; @RestController @ExposesResourceFor(Order.class) +@RequestMapping("/orders") public class OrderController { @GetMapping("/orders") diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/RootController.java b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/RootController.java new file mode 100644 index 0000000..ee649f4 --- /dev/null +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/RootController.java @@ -0,0 +1,25 @@ +package io.pactflow.example.sirenprovider.controllers; + +import io.pactflow.example.sirenprovider.models.Order; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.RepresentationModel; +import org.springframework.hateoas.server.EntityLinks; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RootController { + + static class Root extends RepresentationModel {} + + @Autowired + private EntityLinks entityLinks; + + @GetMapping("/") + public ResponseEntity> orders() { + Link link = entityLinks.linkToCollectionResource(Order.class).withRel("orders"); + return ResponseEntity.ok().body(new Root().add(link)); + } +}