spring mvc : item domain
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.example.itemservice.domain.item;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class Item {
|
||||
|
||||
private Long id;
|
||||
private String itemName;
|
||||
private Integer price;
|
||||
private Integer quantity;
|
||||
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String itemName, Integer price, Integer quantity) {
|
||||
this.itemName = itemName;
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.example.itemservice.domain.item;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public class ItemRepository {
|
||||
|
||||
private static final Map<Long, Item> store = new HashMap<>(); // static
|
||||
private static long sequence = 0L; // static
|
||||
|
||||
public Item save(Item item) {
|
||||
item.setId(++sequence);
|
||||
store.put(item.getId(), item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public Item findById(Long id) {
|
||||
return store.get(id);
|
||||
}
|
||||
|
||||
public List<Item> findAll() {
|
||||
return new ArrayList<>(store.values());
|
||||
}
|
||||
|
||||
public void update(Long itemId, Item updateParam) {
|
||||
Item findItem = findById(itemId);
|
||||
findItem.setItemName(updateParam.getItemName());
|
||||
findItem.setPrice(updateParam.getPrice());
|
||||
findItem.setQuantity(updateParam.getQuantity());
|
||||
}
|
||||
|
||||
public void clearStore() {
|
||||
store.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.example.itemservice.domain.item;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ItemRepositoryTest {
|
||||
|
||||
ItemRepository itemRepository = new ItemRepository();
|
||||
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
itemRepository.clearStore();
|
||||
}
|
||||
|
||||
@Test
|
||||
void save() throws Exception {
|
||||
// given
|
||||
Item item = new Item("itemA", 10000, 10);
|
||||
|
||||
// when
|
||||
Item savedItem = itemRepository.save(item);
|
||||
|
||||
// then
|
||||
Item findItem = itemRepository.findById(item.getId());
|
||||
assertThat(findItem).isEqualTo(savedItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAll() throws Exception {
|
||||
// given
|
||||
Item item1 = new Item("itemA", 10000, 10);
|
||||
Item item2 = new Item("itemB", 15000, 20);
|
||||
|
||||
itemRepository.save(item1);
|
||||
itemRepository.save(item2);
|
||||
|
||||
// when
|
||||
List<Item> result = itemRepository.findAll();
|
||||
|
||||
// then
|
||||
assertThat(result.size()).isEqualTo(2);
|
||||
assertThat(result).contains(item1, item2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update() throws Exception {
|
||||
// given
|
||||
Item item = new Item("itemA", 10000, 10);
|
||||
|
||||
Item savedItem = itemRepository.save(item);
|
||||
Long itemId = savedItem.getId();
|
||||
|
||||
// when
|
||||
Item updateParam = new Item("itemB", 20000, 30);
|
||||
itemRepository.update(itemId, updateParam);
|
||||
|
||||
// then
|
||||
Item findItem = itemRepository.findById(itemId);
|
||||
|
||||
assertThat(findItem.getItemName()).isEqualTo(updateParam.getItemName());
|
||||
assertThat(findItem.getPrice()).isEqualTo(updateParam.getPrice());
|
||||
assertThat(findItem.getQuantity()).isEqualTo(updateParam.getQuantity());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user