package com.twistlet.qir.common.model.service;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.twistlet.qir.common.model.entity.Role;
import com.twistlet.qir.common.model.entity.User;
import com.twistlet.qir.common.model.repository.RoleRepository;
import com.twistlet.qir.common.model.repository.UserRepository;

public class ElasticsearchDataInitServiceTest {

	private ElasticsearchDataInitService sut;

	@Mock
	private UserRepository userRepository;

	@Mock
	private RoleRepository roleRepository;

	@Before
	public void init() {
		MockitoAnnotations.initMocks(this);
		sut = new ElasticsearchDataInitService(new BCryptPasswordEncoder(),
				userRepository, roleRepository);
	}

	@Test
	public void testInitOnNonEmptyData() {
		when(userRepository.count()).thenReturn(1L);
		when(roleRepository.count()).thenReturn(1L);
		sut.init();
		verify(userRepository).count();
		verify(roleRepository).count();
		verifyNoMoreInteractions(userRepository);
		verifyNoMoreInteractions(roleRepository);
	}

	@Test
	@SuppressWarnings("unchecked")
	public void testInitOnEmptyData() {
		when(userRepository.count()).thenReturn(0L);
		when(roleRepository.count()).thenReturn(0L);
		sut.init();
		verify(userRepository).count();
		verify(roleRepository).count();
		verify(userRepository, atLeastOnce()).save(isA(User.class));
		verify(roleRepository, atLeastOnce()).save(
				(Iterable<Role>) isA(Iterable.class));
	}
}

@@ -1671,19 +1671,335 @@
 e.class));%0D%0A
+%09%09verifyNoMoreInteractions(userRepository);%0D%0A%09%09verifyNoMoreInteractions(roleRepository);%0D%0A%09%7D%0D%0A%0D%0A%09@Test%0D%0A%09public void testClear() %7B%0D%0A%09%09sut.clear();%0D%0A%09%09verify(userRepository).deleteAll();%0D%0A%09%09verify(roleRepository).deleteAll();%0D%0A%09%09verifyNoMoreInteractions(userRepository);%0D%0A%09%09verifyNoMoreInteractions(roleRepository);%0D%0A
 %09%7D%0D%0A%7D%0D%0A

test clear on data init service