diff --git a/src/main/java/com/mrxgrc/inventory/InventoryApiApplication.java b/src/main/java/com/mrxgrc/inventory/InventoryApiApplication.java index 032d629..198642f 100644 --- a/src/main/java/com/mrxgrc/inventory/InventoryApiApplication.java +++ b/src/main/java/com/mrxgrc/inventory/InventoryApiApplication.java @@ -1,6 +1,7 @@ // Main entry point of the application package com.mrxgrc.inventory; +import com.mrxgrc.inventory.service.ItemService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/com/mrxgrc/inventory/controller/ItemController.java b/src/main/java/com/mrxgrc/inventory/controller/ItemController.java index 87dce9b..ddc7c0b 100644 --- a/src/main/java/com/mrxgrc/inventory/controller/ItemController.java +++ b/src/main/java/com/mrxgrc/inventory/controller/ItemController.java @@ -1,5 +1,7 @@ package com.mrxgrc.inventory.controller; +import com.mrxgrc.inventory.model.Item; +import com.mrxgrc.inventory.service.ItemService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -8,8 +10,14 @@ @RestController public class ItemController { + private final ItemService itemService; + + public ItemController(ItemService itemService) { + this.itemService = itemService; + } + @GetMapping("/items") - public List getItems() { - return List.of("Items", "Items2"); + public List getItems() { + return itemService.getAllItems(); } } diff --git a/src/main/java/com/mrxgrc/inventory/model/Item.java b/src/main/java/com/mrxgrc/inventory/model/Item.java new file mode 100644 index 0000000..38ccaf7 --- /dev/null +++ b/src/main/java/com/mrxgrc/inventory/model/Item.java @@ -0,0 +1,20 @@ +package com.mrxgrc.inventory.model; + +public class Item { + private int id; + private String name; + + // Constructor + public Item(int id, String name) { + this.id = id; + this.name = name; + } + + // Getters + public int getId() { + return id; + } + public String getName() { + return name; + } +} diff --git a/src/main/java/com/mrxgrc/inventory/service/ItemService.java b/src/main/java/com/mrxgrc/inventory/service/ItemService.java new file mode 100644 index 0000000..922c4aa --- /dev/null +++ b/src/main/java/com/mrxgrc/inventory/service/ItemService.java @@ -0,0 +1,28 @@ +package com.mrxgrc.inventory.service; + +import com.mrxgrc.inventory.model.Item; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class ItemService { + private final List items = new ArrayList<>(); + + // Constructor + public ItemService() { + items.add(new Item(1, "Hand Soap")); + items.add(new Item(2, "Hand Sanitizer")); + items.add(new Item(3, "Toilet Paper")); + } + + public List getAllItems() { + + List result = new ArrayList<>(); + for (Item item : items) { + result.add(item); + } + return result; + } +} diff --git a/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java b/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java index 464dadc..0850b9a 100644 --- a/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java +++ b/src/test/java/com/mrxgrc/inventory/ItemControllerTest.java @@ -1,19 +1,25 @@ package com.mrxgrc.inventory; import com.mrxgrc.inventory.controller.ItemController; +import com.mrxgrc.inventory.service.ItemService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +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; +import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -@WebMvcTest(ItemController.class) +@SpringBootTest +@AutoConfigureMockMvc public class ItemControllerTest { @Autowired @@ -24,7 +30,13 @@ public void getItemsReturnsListofItems() throws Exception{ this.mockMvc.perform(MockMvcRequestBuilders.get("/items").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(content().json("[\"Items\", \"Items2\"]")); + // Verify it's an array + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$.length()", greaterThan(0))) + .andExpect(jsonPath("$[*].id").isNotEmpty()) + .andExpect(jsonPath("$[*].name").isNotEmpty()) + // Optionally: ensure name fields are strings + .andExpect(jsonPath("$[*].name", everyItem(instanceOf(String.class)))); }