Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/mrxgrc/inventory/controller/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,4 +22,15 @@ public ItemController(ItemService itemService) {
public List<Item> getItems() {
return itemService.getAllItems();
}

@GetMapping("/items/{requestedId}")
public ResponseEntity<Item> 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);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/mrxgrc/inventory/model/Item.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/mrxgrc/inventory/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> getAllItems() {

List<Item> 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);
}
}
12 changes: 10 additions & 2 deletions src/test/java/com/mrxgrc/inventory/ItemControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
}
}