content: package kr.co.fastcampus.eatgo.application;
  
  import kr.co.fastcampus.eatgo.domain.Restaurant;
  import kr.co.fastcampus.eatgo.domain.RestaurantNotFoundException;
  import kr.co.fastcampus.eatgo.domain.RestaurantRepository;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Optional;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.assertj.core.api.Assertions.assertThatThrownBy;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.BDDMockito.given;
  
  public class RestaurantServiceTests {
  
      @InjectMocks
      private RestaurantService restaurantService;
  
      @Mock
      private RestaurantRepository restaurantRepository;
  
      @BeforeEach
      public void setUp() {
          MockitoAnnotations.initMocks(this);
  
          mockRestaurantRepository();
      }
  
      private void mockRestaurantRepository() {
          List<Restaurant> restaurants = new ArrayList<>();
          Restaurant restaurant = Restaurant.builder()
                  .id(1004L)
                  .categoryId(1L)
                  .address("Seoul")
                  .name("Bob zip")
                  .build();
          restaurants.add(restaurant);
  
          given(restaurantRepository.findAll()).willReturn(restaurants);
  
          given(restaurantRepository.findById(1004L))
                  .willReturn(Optional.of(restaurant));
      }
  
      @Test
      public void getRestaurants() {
          List<Restaurant> restaurants = restaurantService.getRestaurants();
  
          Restaurant restaurant = restaurants.get(0);
  
          assertThat(restaurant.getId()).isEqualTo(1004L);
      }
  
      @Test
      public void getRestaurantWithExisted() {
          Restaurant restaurant = restaurantService.getRestaurant(1004L);
  
          assertThat(restaurant.getId()).isEqualTo(1004L);
      }
  
      @Test
      public void getRestaurantWithNotExisted() {
          assertThatThrownBy(() -> {
              restaurantService.getRestaurant(404L);
          }).isInstanceOf(RestaurantNotFoundException.class);
      }
  
      @Test
      public void addRestaurant() {
          given(restaurantRepository.save(any())).will(invocation -> {
              Restaurant restaurant = invocation.getArgument(0);
              restaurant.setId(1234L);
              return restaurant;
          });
  
          Restaurant restaurant = Restaurant.builder()
                  .name("BeRyong")
                  .address("Busan")
                  .build();
  
          Restaurant created = restaurantService.addRestaurant(restaurant);
  
          assertThat(created.getId()).isEqualTo(1234L);
      }
  
      @Test
      public void updateRestaurant() {
          Restaurant restaurant = Restaurant.builder()
                  .id(1004L)
                  .name("Bob zip")
                  .address("Seoul")
                  .build();
  
          given(restaurantRepository.findById(1004L))
                  .willReturn(Optional.of(restaurant));
  
          restaurantService.updateRestaurant(1004L, 1L, "Sool zip", "Busan");
  
          assertThat(restaurant.getName()).isEqualTo("Sool zip");
          assertThat(restaurant.getAddress()).isEqualTo("Busan");
      }
  }
  