package com.bellini.recipecatalog.test.repository.ingredient;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.comparesEqualTo;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
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.ingredient.IngredientRepository;
import com.bellini.recipecatalog.model.v1.Ingredient;

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

    @Autowired
    private IngredientRepository repo;

    @Test
    public void findAll_shouldReturnNotNull() {
        Page<Ingredient> result = repo.findAll(PageRequest.of(0, Integer.MAX_VALUE)); // retrieve all elements
        assertThat(result, notNullValue());
    }

    @Test
    public void findAll_shouldReturnNotEmptyAndCorrectPageSize() {
        Page<Ingredient> result = repo.findAll(PageRequest.of(0, 5)); // retrieve half of the elements
        final List<Ingredient> content = result.getContent();
        assertThat(result, not(emptyIterable()));
        assertThat(content, hasSize(5));
    }

    @Test
    public void findAll_shouldReturnEmptyForIncorrectPageSize() {
        Page<Ingredient> result = repo.findAll(PageRequest.of(50, 5)); // this page does not exist
        assertThat(result, emptyIterable());
    }

    @Test
    public void findAll_shouldHandlePaginationCorrectly() {
        final List<Ingredient> content = repo.findAll(PageRequest.of(0, Integer.MAX_VALUE)).getContent(); // retrieve all elements
        // fetch two items each time until no results are available
        int startFrom = 0, fetch = 2, total = 0;
        List<Ingredient> contentToCheck = new ArrayList<>();
        List<Ingredient> tmpContent = null;
        do {
            tmpContent = repo.findAll(PageRequest.of(startFrom, fetch)).getContent();
            total += tmpContent.size();
            contentToCheck.addAll(tmpContent);
            ++startFrom;
        } while (!tmpContent.isEmpty());

        // check for matching number of elements
        assertThat(total, comparesEqualTo(10));

        // check that the lists are equal
        assertThat(contentToCheck, is(content));
    }
}

@@ -216,16 +216,59 @@
 qualTo;%0A
+import static org.hamcrest.Matchers.empty;%0A
 import s
@@ -461,24 +461,53 @@
 .ArrayList;%0A
+import java.util.Collection;%0A
 import java.
@@ -3207,11 +3207,634 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void findByNameIgnoreCase_shouldReturnTheSameResult() %7B%0A        Collection%3CIngredient%3E resultUpperCase = repo.findByNameIgnoreCase(%22Chicken%22);%0A        Collection%3CIngredient%3E resultLowerCase = repo.findByNameIgnoreCase(%22chicken%22);%0A%0A        // collections should be equal%0A        assertThat(resultUpperCase, is(resultLowerCase));%0A    %7D%0A%0A    @Test%0A    public void findByNameIgnoreCase_shouldReturnNotNullAndEmptyWhenNoMatch() %7B%0A        Collection%3CIngredient%3E result = repo.findByNameIgnoreCase(%22UnknownIngredient%22);%0A%0A        assertThat(result, notNullValue());%0A        assertThat(result, empty());%0A    %7D
 %0A%7D%0A

Add tests for findByNameIgnoreCase - ingredient