package com.bellini.recipecatalog.test.recipe;

import static org.mockito.Mockito.times;

import java.time.Instant;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import com.bellini.recipecatalog.dao.v1.book.BookRepository;
import com.bellini.recipecatalog.dao.v1.dishtype.DishTypeRepository;
import com.bellini.recipecatalog.dao.v1.ingredient.IngredientRepository;
import com.bellini.recipecatalog.dao.v1.publication.PublicationRepository;
import com.bellini.recipecatalog.dao.v1.recipe.RecipeRepository;
import com.bellini.recipecatalog.model.v1.Book;

@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = { "classpath:db_seed/recipe/data.sql" })
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = { "classpath:db_seed/recipe/clean-data.sql" })
public class RecipeRepositoryTest {

    @Mock
    private BookRepository bookRepo;

    @Mock
    private DishTypeRepository dishTypeRepo;

    @Mock
    private IngredientRepository ingredientRepo;

    @Mock
    private PublicationRepository pubRepo;

    @Autowired
//    @InjectMocks
    private RecipeRepository recipeRepo;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void findAll_shouldReturnCorrectElements() {
        Mockito.when(bookRepo.findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class))).thenReturn(Optional.of(dummyBook()));
        recipeRepo.findAll(PageRequest.of(0, 50));
        Mockito.verify(bookRepo, times(2)).findById(org.mockito.ArgumentMatchers.any(Long.class));
//        assertFalse(result.isPresent());
    }

    private Book dummyBook() {
        Book bk = new Book();
        bk.setTitle("Dummy");
        bk.setId(1L);
        bk.setCreationTime(Instant.now());
        bk.setLastModificationTime(Instant.now());
        return bk;
    }
}

@@ -126,24 +126,515 @@
 va.util.
-Optional
+Collections;%0Aimport java.util.Optional;%0A%0Aimport com.bellini.recipecatalog.dao.v1.book.BookRepository;%0Aimport com.bellini.recipecatalog.dao.v1.dishtype.DishTypeRepository;%0Aimport com.bellini.recipecatalog.dao.v1.ingredient.IngredientRepository;%0Aimport com.bellini.recipecatalog.dao.v1.publication.PublicationRepository;%0Aimport com.bellini.recipecatalog.dao.v1.recipe.RecipeRepository;%0Aimport com.bellini.recipecatalog.dao.v1.recipe.RecipeRepositoryImpl;%0Aimport com.bellini.recipecatalog.model.v1.Book
 ;%0A%0Aimpor
@@ -713,33 +713,8 @@
 th;%0A
-import org.mockito.Mock;%0A
 impo
@@ -896,24 +896,84 @@
 ngBootTest;%0A
+import org.springframework.boot.test.mock.mockito.MockBean;%0A
 import org.s
@@ -1258,400 +1258,8 @@
 l;%0A%0A
-import com.bellini.recipecatalog.dao.v1.book.BookRepository;%0Aimport com.bellini.recipecatalog.dao.v1.dishtype.DishTypeRepository;%0Aimport com.bellini.recipecatalog.dao.v1.ingredient.IngredientRepository;%0Aimport com.bellini.recipecatalog.dao.v1.publication.PublicationRepository;%0Aimport com.bellini.recipecatalog.dao.v1.recipe.RecipeRepository;%0Aimport com.bellini.recipecatalog.model.v1.Book;%0A%0A
 @Spr
@@ -1573,32 +1573,36 @@
 est %7B%0A%0A    @Mock
+Bean
 %0A    private Boo
@@ -1625,32 +1625,36 @@
 Repo;%0A%0A    @Mock
+Bean
 %0A    private Dis
@@ -1685,32 +1685,36 @@
 Repo;%0A%0A    @Mock
+Bean
 %0A    private Ing
@@ -1757,16 +1757,20 @@
    @Mock
+Bean
 %0A    pri
@@ -1825,27 +1825,8 @@
 red%0A
-//    @InjectMocks%0A
     
@@ -1856,24 +1856,53 @@
 y recipeRepo
+ = new RecipeRepositoryImpl()
 ;%0A%0A    @Befo
@@ -2024,28 +2024,25 @@
 ould
-Return
+Call
 Correct
-Element
+Method
 s() 
@@ -2134,16 +2134,33 @@
 class)))
+%0A                
 .thenRet
@@ -2186,24 +2186,473 @@
 myBook()));%0A
+        Mockito.when(dishTypeRepo.findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class)))%0A                .thenReturn(Collections.emptyList());%0A        Mockito.when(ingredientRepo.findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class)))%0A                .thenReturn(Collections.emptyList());%0A        Mockito.when(pubRepo.findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class)))%0A                .thenReturn(Optional.ofNullable(null));%0A
         reci
@@ -2686,24 +2686,25 @@
 of(0, 50));%0A
+%0A
         Mock
@@ -2740,16 +2740,22 @@
 ).findBy
+Recipe
 Id(org.m
@@ -2800,18 +2800,16 @@
 ));%0A
-//
         
 asse
@@ -2808,37 +2808,320 @@
     
-assertFalse(result.isPresent(
+Mockito.verify(dishTypeRepo, times(2)).findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class));%0A        Mockito.verify(ingredientRepo, times(2)).findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class));%0A        Mockito.verify(pubRepo, times(2)).findByRecipeId(org.mockito.ArgumentMatchers.any(Long.class
 ));%0A

Fix mock error in test