오늘은 11~12강 레이어 분리에 대해서 공부해보았다.
우리가 만든 프로그램은 UI Layer와 Domain Layer를 사용했다.
인터페이시스에는 컨트롤러, 도메인에는 도메인 모델과 레파지토리가 들어가있다.
우리가 이전에 설계했던 레이어에는 애플리케이션 레이어가 있었다.
그 부분을 추가하는 강의이다.
이것의 필요성을 위해 복잡한 로직인 애플리케이션 모델을 만들고 레스토랑 서비스라는 객체를 만들어 본다.
현재까지 만들어진 코드 정리.
eatgo-api/src/main/java/eatgo/application
/RestaurantService
package kr.co.fastcampus.eatgo.application;
import kr.co.fastcampus.eatgo.domain.MenuItem;
import kr.co.fastcampus.eatgo.domain.MenuItemRepository;
import kr.co.fastcampus.eatgo.domain.Restaurant;
import kr.co.fastcampus.eatgo.domain.RestaurantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RestaurantService {
@Autowired
RestaurantRepository restaurantRepository;
@Autowired
MenuItemRepository menuItemRepository;
public RestaurantService(RestaurantRepository restaurantRepository,
MenuItemRepository menuItemRepository) {
this.restaurantRepository = restaurantRepository;
this.menuItemRepository = menuItemRepository;
}
public List<Restaurant> getRestaurants(){
List<Restaurant> restaurants = restaurantRepository.findAll();
return restaurants;
}
public Restaurant getRestaurant(Long id){
Restaurant restaurant = restaurantRepository.findById(id);
List<MenuItem> menuItems = menuItemRepository.findAllByRestaurantId(id);
restaurant.setMenuItems(menuItems);
return restaurant;
}
}
eatgo-api/src/main/java/eatgo/domain
/MenuItem
package kr.co.fastcampus.eatgo.domain;
public class MenuItem {
private final String name;
public MenuItem(String name) {
this.name = name;
}
public String getName(){
return name;
}
}
/MenuItemRepository
package kr.co.fastcampus.eatgo.domain;
import java.util.List;
public interface MenuItemRepository {
List<MenuItem> findAllByRestaurantId(Long restaurantId);
}
/MenuItemRepositoryImpl
package kr.co.fastcampus.eatgo.domain;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class MenuItemRepositoryImpl implements MenuItemRepository {
private List<MenuItem> menuItems = new ArrayList<>();
public MenuItemRepositoryImpl(){
menuItems.add(new MenuItem("Kimchi"));
}
@Override
public List<MenuItem> findAllByRestaurantId(Long restaurantId) {
return menuItems;
}
}
/Restaurant
package kr.co.fastcampus.eatgo.domain;
import java.util.ArrayList;
import java.util.List;
public class Restaurant {
private final Long id;
private final String name ;
private final String address;
private List<MenuItem> menuItems = new ArrayList<MenuItem>();
public Restaurant(Long id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getInformation() {
return (name + " in "+ address);
}
public List<MenuItem> getMenuItems() {
return menuItems;
}
public void addMenuItem(MenuItem menuItem) {
menuItems.add(menuItem);
}
public void setMenuItems(List<MenuItem> menuItems) {
for (MenuItem menuItem : menuItems){
addMenuItem(menuItem);
}
}
}
/RestaurantRepository
package kr.co.fastcampus.eatgo.domain;
import java.util.List;
public interface RestaurantRepository {
List<Restaurant> findAll();
Restaurant findById(Long id);
}
/RestaurantRepositoryImpl
package kr.co.fastcampus.eatgo.domain;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class RestaurantRepositoryImpl implements RestaurantRepository {
private List<Restaurant> restaurants = new ArrayList<>();
public RestaurantRepositoryImpl(){
restaurants.add(new Restaurant(1004L,"Bob zip", "Seoul"));
restaurants.add(new Restaurant(2020L,"Cyber Food","Seoul"));
}
@Override
public List<Restaurant> findAll() {
return restaurants;
}
@Override
public Restaurant findById(Long id) {
return restaurants.stream()
.filter(r-> r.getId().equals(id))
.findFirst()
.orElse(null);
}
}
eatgo-api/src/main/java/eatgointerfaces
/RestaurantController
package kr.co.fastcampus.eatgo.interfaces;
import kr.co.fastcampus.eatgo.application.RestaurantService;
import kr.co.fastcampus.eatgo.domain.MenuItem;
import kr.co.fastcampus.eatgo.domain.MenuItemRepository;
import kr.co.fastcampus.eatgo.domain.Restaurant;
import kr.co.fastcampus.eatgo.domain.RestaurantRepository;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
public class RestaurantController {
@Autowired
private RestaurantService restaurantService;
@GetMapping("/restaurants")
public List<Restaurant> list(){
List<Restaurant> restaurants = restaurantService.getRestaurants();
return restaurants;
}
@GetMapping("/restaurants/{id}")
public Restaurant detail(@PathVariable("id") Long id){
Restaurant restaurant = restaurantService.getRestaurant(id);
return restaurant;
}
}
/WelcomeController
package kr.co.fastcampus.eatgo.interfaces;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@GetMapping("/")
public String hello(){
return "Hello, world!!!";
}
}
eatgo-api/src/main/java/eatgo/
/EatgoApplication
package kr.co.fastcampus.eatgo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EatgoApplication {
public static void main(String[] args) {
SpringApplication.run(EatgoApplication.class, args);
}
}
eatgo-api/src/test/java/eatgo/application
/RestaurantServiceTest
package kr.co.fastcampus.eatgo.application;
import kr.co.fastcampus.eatgo.domain.*;
import kr.co.fastcampus.eatgo.domain.MenuItem;
import org.junit.Before;
import org.junit.Test;
import java.awt.*;
import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class RestaurantServiceTest {
private RestaurantService restaurantService;
private RestaurantRepository restaurantRepository;
private MenuItemRepository menuItemRepository;
@Before
public void setUp(){
restaurantRepository = new RestaurantRepositoryImpl();
menuItemRepository = new MenuItemRepositoryImpl();
restaurantService = new RestaurantService(restaurantRepository, menuItemRepository);
}
@Test
public void getRestaurants(){
List<Restaurant> restaurants = restaurantService.getRestaurants();
Restaurant restaurant = restaurants.get(0);
assertThat(restaurant.getId(), is(1004L));
}
@Test
public void getRestaurant(){
Restaurant restaurant = restaurantService.getRestaurant(1004L);
assertThat(restaurant.getId(), is(1004L));
MenuItem menuItem = restaurant.getMenuItems().get(0);
assertThat(menuItem.getName(), is("Kimchi"));
}
}
eatgo-api/src/test/java/eatgo/domain
/RestaurantTests
package kr.co.fastcampus.eatgo.domain;
import org.junit.jupiter.api.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class RestaurantTests {
@Test
public void creation(){
Restaurant restaurant = new Restaurant(1004L,"Bob zip", "Seoul");
assertThat(restaurant.getId(), is(1004L));
assertThat(restaurant.getName(), is("Bob zip"));
assertThat(restaurant.getAddress(), is("Seoul"));
}
@Test
public void information(){
Restaurant restaurant = new Restaurant(1004L,"Bob zip", "Seoul");
assertThat(restaurant.getInformation(), is("Bob zip in Seoul"));
}
}
eatgo-api/src/test/java/eatgo/interfaces
/RestaurantControllerTest
package kr.co.fastcampus.eatgo.interfaces;
import kr.co.fastcampus.eatgo.application.RestaurantService;
import kr.co.fastcampus.eatgo.domain.MenuItemRepository;
import kr.co.fastcampus.eatgo.domain.MenuItemRepositoryImpl;
import kr.co.fastcampus.eatgo.domain.RestaurantRepository;
import kr.co.fastcampus.eatgo.domain.RestaurantRepositoryImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.core.StringContains.containsString;
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;
@RunWith(SpringRunner.class)
@WebMvcTest(RestaurantController.class)
public class RestaurantControllerTest {
@Autowired
private MockMvc mvc;
@SpyBean(RestaurantService.class)
private RestaurantService restaurantService;
@SpyBean(RestaurantRepositoryImpl.class)
private RestaurantRepository restaurantRepository;
@SpyBean(MenuItemRepositoryImpl.class)
private MenuItemRepository menuItemRepository;
//@SpyBean(MenuItemRepositoryImpl.class) 도 추가
@Test
public void list() throws Exception {
mvc.perform(get("/restaurants"))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"id\":1004")
))
.andExpect(content().string(
containsString("\"name\":\"Bob zip\"")
));
}
@Test
public void detail() throws Exception {
mvc.perform(get("/restaurants/1004"))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"id\":1004")
))
.andExpect(content().string(
containsString("\"name\":\"Bob zip\"")
))
.andExpect(content().string(
containsString("Kimchi")
));
mvc.perform(get("/restaurants/2020"))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"id\":2020")
))
.andExpect(content().string(
containsString("\"name\":\"Cyber Food\"")
));
}
}
eatgo-api/src/test/java/eatgo
/EatgoApplicationTests
package kr.co.fastcampus.eatgo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EatgoApplicationTests {
@Test
void contextLoads() {
}
}
패스트캠퍼스 강의: https://bit.ly/3ilMbIO
'언어공부 > JAVA&SPRING' 카테고리의 다른 글
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 26회차 미션 (0) | 2020.09.04 |
---|---|
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 25회차 미션 (0) | 2020.09.03 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 23회차 미션 (0) | 2020.09.01 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 22회차 미션 (0) | 2020.08.31 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 21회차 미션 (0) | 2020.08.30 |
댓글