feat: navigate from root
authorRonald Holshausen <rholshausen@dius.com.au>
Tue, 29 Sep 2020 07:24:56 +0000 (17:24 +1000)
committerRonald Holshausen <rholshausen@dius.com.au>
Tue, 29 Sep 2020 07:24:56 +0000 (17:24 +1000)
consumer/src/__tests__/delete-order.spec.js
consumer/src/consumer.js
provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java
provider/src/main/java/io/pactflow/example/sirenprovider/controllers/RootController.java [new file with mode: 0644]

index 2b79bf1..7bf9dde 100644 (file)
@@ -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",
index b9e3dac..2c193a2 100644 (file)
@@ -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]
index a51184a..6881069 100644 (file)
@@ -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 (file)
index 0000000..ee649f4
--- /dev/null
@@ -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<Root> {}
+
+  @Autowired
+  private EntityLinks entityLinks;
+
+  @GetMapping("/")
+  public ResponseEntity<RepresentationModel<?>> orders() {
+    Link link = entityLinks.linkToCollectionResource(Order.class).withRel("orders");
+    return ResponseEntity.ok().body(new Root().add(link));
+  }
+}