package com.bellini.recipecatalog.test.service.dishtype;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.Instant;
import java.util.Collections;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.bellini.recipecatalog.dao.v1.dishtype.DishTypeRepository;
import com.bellini.recipecatalog.exception.dishtype.DuplicateDishTypeException;
import com.bellini.recipecatalog.model.v1.DishType;
import com.bellini.recipecatalog.service.v1.dishtype.DishTypeService;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DishTypeServiceTest {

    private static final String DUMMY_DISHTYPE_NAME = "Antipasti";

    @Autowired
    private DishTypeService dtSrv;

    @MockBean
    private DishTypeRepository dtRepo;

    @Test(expected = DuplicateDishTypeException.class)
    public void create_shouldThrowExceptionWhenDuplicate() {
        when(dtRepo.findByNameIgnoreCase(ArgumentMatchers.eq(DUMMY_DISHTYPE_NAME))).thenReturn(Collections.singleton(new DishType()));
        dtSrv.create(dummyDishType());
        // verify method calls
        verify(dtRepo).findByNameIgnoreCase(ArgumentMatchers.eq(DUMMY_DISHTYPE_NAME));
    }

    private DishType dummyDishType() {
        DishType dt = new DishType();
        dt.setId(1L);
        dt.setName(DUMMY_DISHTYPE_NAME);
        dt.setCreationTime(Instant.now());
        dt.setLastModificationTime(Instant.now());
        return dt;
    }
}

@@ -132,16 +132,62 @@
 to.when;
+%0Aimport static org.mockito.ArgumentMatchers.*;
 %0A%0Aimport
@@ -235,16 +235,43 @@
 ections;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -752,16 +752,98 @@
 eption;%0A
+import com.bellini.recipecatalog.exception.dishtype.NotExistingDishTypeException;%0A
 import c
@@ -1532,16 +1532,16 @@
 d calls%0A
-
         
@@ -1626,16 +1626,528 @@
 %0A    %7D%0A%0A
+    @Test(expected = NotExistingDishTypeException.class)%0A    public void get_shouldThrowWhenNotFound() %7B%0A        when(dtRepo.findById(anyLong())).thenReturn(Optional.empty());%0A        dtSrv.get(1L);%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void update_shouldThrowWhenNullId() %7B%0A        dtSrv.update(null, dummyDishType());%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void update_shouldThrowWhenNullUpdatedObject() %7B%0A        dtSrv.update(1L, null);%0A    %7D%0A%0A
     priv

Add some tests for dish type service