From 87db58366417680a96b2837f7e6d7e673fa0e3b4 Mon Sep 17 00:00:00 2001 From: merxgrc Date: Wed, 17 Sep 2025 01:28:41 -0700 Subject: [PATCH] Adds implementation for getItemById, findById, and some debugging in item.java, tests passes --- .../inventory/controller/ItemController.java | 13 +++++++++++++ .../java/com/mrxgrc/inventory/model/Item.java | 6 +++--- .../com/mrxgrc/inventory/service/ItemService.java | 15 +++++++++++---- .../com/mrxgrc/inventory/ItemControllerTest.java | 12 ++++++++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mrxgrc/inventory/controller/ItemController.java b/src/main/java/com/mrxgrc/inventory/controller/ItemController.java index ddc7c0b..e9c7bcd 100644 --- a/src/main/java/com/mrxgrc/inventory/controller/ItemController.java +++ b/src/main/java/com/mrxgrc/inventory/controller/ItemController.java @@ -2,7 +2,9 @@ import com.mrxgrc.inventory.model.Item; import com.mrxgrc.inventory.service.ItemService; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -20,4 +22,15 @@ public ItemController(ItemService itemService) { public List getItems() { return itemService.getAllItems(); } + + @GetMapping("/items/{requestedId}") + public ResponseEntity getItemById(@PathVariable Long requestedId) { + // Call itemService.findById(requestedId) and return the appropriate response entity + Item item = itemService.findById(requestedId); + if (item == null) { + return ResponseEntity.notFound().build(); + } else { + return ResponseEntity.ok(item); + } + } } diff --git a/src/main/java/com/mrxgrc/inventory/model/Item.java b/src/main/java/com/mrxgrc/inventory/model/Item.java index 38ccaf7..af298d7 100644 --- a/src/main/java/com/mrxgrc/inventory/model/Item.java +++ b/src/main/java/com/mrxgrc/inventory/model/Item.java @@ -1,17 +1,17 @@ package com.mrxgrc.inventory.model; public class Item { - private int id; + private Long id; private String name; // Constructor - public Item(int id, String name) { + public Item(Long id, String name) { this.id = id; this.name = name; } // Getters - public int getId() { + public Long getId() { return id; } public String getName() { diff --git a/src/main/java/com/mrxgrc/inventory/service/ItemService.java b/src/main/java/com/mrxgrc/inventory/service/ItemService.java index 922c4aa..0fc885a 100644 --- a/src/main/java/com/mrxgrc/inventory/service/ItemService.java +++ b/src/main/java/com/mrxgrc/inventory/service/ItemService.java @@ -12,17 +12,24 @@ public class ItemService { // Constructor public ItemService() { - items.add(new Item(1, "Hand Soap")); - items.add(new Item(2, "Hand Sanitizer")); - items.add(new Item(3, "Toilet Paper")); + items.add(new Item(1L, "Hand Soap")); + items.add(new Item(2L, "Hand Sanitizer")); + items.add(new Item(3L, "Toilet Paper")); } public List getAllItems() { - List result = new ArrayList<>(); for (Item item : items) { result.add(item); } return result; } + + public Item findById(Long requestedId) { + // Retrieve the item with the given ID from list + return items.stream() + .filter(item -> item.getId().equals(requestedId)) + .findFirst() + .orElse(null); + } } diff --git a/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java b/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java index 0850b9a..0357bbc 100644 --- a/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java +++ b/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java @@ -7,7 +7,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; + import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -37,7 +37,15 @@ public void getItemsReturnsListofItems() throws Exception{ .andExpect(jsonPath("$[*].name").isNotEmpty()) // Optionally: ensure name fields are strings .andExpect(jsonPath("$[*].name", everyItem(instanceOf(String.class)))); + } - + // request an item by id, expect json with correct id + name. This is an integration test. + @Test + public void getItemByIdReturnsItem() throws Exception { + this.mockMvc.perform(get("/items/1").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id", equalTo(1))) + .andExpect(jsonPath("$.name", equalTo("Hand Soap"))); } }