diff --git a/ilp-cw-api/[GET] droneDetails/drone 1 details.bru b/ilp-cw-api/[GET] droneDetails/drone 1 details.bru new file mode 100644 index 0000000..5ef59ad --- /dev/null +++ b/ilp-cw-api/[GET] droneDetails/drone 1 details.bru @@ -0,0 +1,29 @@ +meta { + name: drone 1 details + type: http + seq: 1 +} + +get { + url: {{API_BASE}}/droneDetails/1 + body: none + auth: inherit +} + +assert { + res.body.id: eq "1" + res.body.capability.capacity: eq 4.0 + res.body.capability.heating: eq true +} + +tests { + test("Status code is 200", function() { + expect(res.status).to.equal(200); + }); + +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/ilp-cw-api/[GET] droneDetails/drone 10 details.bru b/ilp-cw-api/[GET] droneDetails/drone 10 details.bru new file mode 100644 index 0000000..39a687a --- /dev/null +++ b/ilp-cw-api/[GET] droneDetails/drone 10 details.bru @@ -0,0 +1,29 @@ +meta { + name: drone 10 details + type: http + seq: 2 +} + +get { + url: {{API_BASE}}/droneDetails/10 + body: none + auth: inherit +} + +assert { + res.body.id: eq "10" + res.body.capability.capacity: eq 12.0 + res.body.capability.heating: eq false +} + +tests { + test("Status code is 200", function() { + expect(res.status).to.equal(200); + }); + +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/ilp-cw-api/[GET] droneDetails/drone 11 details.bru b/ilp-cw-api/[GET] droneDetails/drone 11 details.bru new file mode 100644 index 0000000..c0c1496 --- /dev/null +++ b/ilp-cw-api/[GET] droneDetails/drone 11 details.bru @@ -0,0 +1,23 @@ +meta { + name: drone 11 details + type: http + seq: 3 +} + +get { + url: {{API_BASE}}/droneDetails/11 + body: none + auth: inherit +} + +tests { + test("Status code is 404", function() { + expect(res.status).to.equal(404); + }); + +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/ilp-cw-api/[GET] droneDetails/drone NaN details.bru b/ilp-cw-api/[GET] droneDetails/drone NaN details.bru new file mode 100644 index 0000000..06dcdbe --- /dev/null +++ b/ilp-cw-api/[GET] droneDetails/drone NaN details.bru @@ -0,0 +1,23 @@ +meta { + name: drone NaN details + type: http + seq: 4 +} + +get { + url: {{API_BASE}}/droneDetails/droneNaN + body: none + auth: inherit +} + +tests { + test("Status code is 404", function() { + expect(res.status).to.equal(404); + }); + +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/ilp-cw-api/[GET] droneDetails/folder.bru b/ilp-cw-api/[GET] droneDetails/folder.bru new file mode 100644 index 0000000..dd00a19 --- /dev/null +++ b/ilp-cw-api/[GET] droneDetails/folder.bru @@ -0,0 +1,8 @@ +meta { + name: [GET] droneDetails + seq: 2 +} + +auth { + mode: inherit +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java b/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java index 411e3fe..ecdf1c4 100644 --- a/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java +++ b/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java @@ -1,5 +1,6 @@ package io.github.js0ny.ilp_coursework.controller; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import io.github.js0ny.ilp_coursework.service.DroneInfoService; @@ -43,15 +44,15 @@ public class DroneController { return droneService.dronesWithCooling(state); } -// @GetMapping("/droneDetails/{id}") -// public DroneDto getDroneDetail(@PathVariable int id) { -// String droneUrl = baseUrl + "drones"; -// -// DroneDto[] drones = restTemplate.getForObject(droneUrl, DroneDto[].class); -// -// return new DroneDto(); -// -// } + @GetMapping("/droneDetails/{id}") + public ResponseEntity getDroneDetail(@PathVariable String id) { + try { + DroneDto drone = droneService.droneDetail(id); + return ResponseEntity.ok(drone); + } catch (IllegalArgumentException ex) { + return ResponseEntity.notFound().build(); + } + } @PostMapping("queryAvailableDrones") public int queryAvailableDrones() { diff --git a/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java b/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java index c70862c..2411f73 100644 --- a/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java +++ b/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java @@ -56,18 +56,32 @@ public class DroneInfoService { toArray(); } - // TODO: This is function is WIP - public Stream droneDetail(int id) { + /** + * Return a {@link DroneDto}-style json data structure with the given {@code id} + * + * @param id The id of the drone + * @return drone json body of given id + * @throws NullPointerException when cannot fetch available drones from remote + * @throws IllegalArgumentException when drone with given {@code id} cannot be found + * this should lead to a 404 + */ + public DroneDto droneDetail(String id) { String droneUrl = baseUrl + dronesEndpoint; DroneDto[] drones = restTemplate.getForObject(droneUrl, DroneDto[].class); if (drones == null) { - throw new IllegalArgumentException("drone with that ID cannot be found"); + throw new NullPointerException("drone cannot be found"); } - return Arrays.stream(drones). - filter(drone -> Integer.parseInt(String.valueOf(drone.id())) == id); + for (var drone : drones) { + if (drone.id().equals(id)) { + return drone; + } + } + + throw new IllegalArgumentException("drone with that ID cannot be found"); + } }