package unit;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.buscapecompany.poc.cucumbermicroservice.model.Customer;
import com.buscapecompany.poc.cucumbermicroservice.repository.CustomerRepository;
import com.buscapecompany.poc.cucumbermicroservice.services.CustomerServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class CustomerServiceTest {

	@Mock
	private CustomerRepository repository;
	private CustomerServiceImpl service;
	
	@Before
	public void setup(){
		repository = mock(CustomerRepository.class);
		service = new CustomerServiceImpl(repository);
	}
	
	@Test
	public void testGetAllCustomer(){
		when(repository.findAll())
			.thenReturn(Arrays.asList(1l,2l,3l,4l)
			.stream()
			.map(id -> new Customer(id, "CUstomer"+id, "CUstomer"+id, "CUstomer"+id, 99))
			.collect(Collectors.toList()));
		List<Customer> result = service.findAll();
		assertEquals(4, result.size());
	}
	
	@Test
	public void testGetCustomerById(){
		when(repository.findOne(1L)).thenReturn(new Customer(1l, "CUstomer1", "CUstomer1", "CUstomer1", 99));
		Customer result = service.findOne(1);
		
		assertEquals(true, new Long(1).equals(result.getId()));
		assertEquals("CUstomer1", result.getFirstName());
		assertEquals("CUstomer1", result.getSurName());
		assertEquals("CUstomer1", result.getLastName());
		assertEquals(true, new Integer(99).equals(result.getAge()));
	}
	
	@Test
	public void saveCustomer(){
		Customer Customer = new Customer(8l, "Customer 8", "Customer Sample 8", null);
		when(repository.save(Customer)).thenReturn(Customer);
		Customer result = service.save(Customer);
		assertEquals(true, new Long(8).equals(result.getId()));
		assertEquals("Customer 8", result.getName());
		assertEquals("Customer Sample 8", result.getDescription());
	}
	
	@Test
	public void removeCustomer(){
		Customer Customer = new Customer(8l, "Customer 8", "Customer Sample 8", null);
		service.delete(Customer);
        verify(CustomerRepository, times(1)).delete(Customer);
	}
}

@@ -2062,45 +2062,47 @@
 , %22C
-u
+U
 stomer
- 8
+1
 %22, %22C
-u
+U
 stomer
- Sample 8%22, null
+1%22, %22CUstomer1%22, 99
 );%0A%09
@@ -2275,25 +2275,24 @@
 quals(%22C
-u
+U
 stomer
- 8
+1
 %22, resul
@@ -2296,16 +2296,21 @@
 sult.get
+First
 Name());
@@ -2331,49 +2331,151 @@
 s(%22C
-u
+U
 stomer
- Sample 8%22, result.getDescription(
+1%22, result.getSurName());%0A%09%09assertEquals(%22CUstomer1%22, result.getLastName());%0A%09%09assertEquals(true, new Integer(99).equals(result.getAge()
 ));%0A
@@ -2562,45 +2562,47 @@
 , %22C
-u
+U
 stomer
- 8
+1
 %22, %22C
-u
+U
 stomer
- Sample 8%22, null
+1%22, %22CUstomer1%22, 99
 );%0A%09
@@ -2643,25 +2643,17 @@
  verify(
-CustomerR
+r
 epositor

fix CustomerServiceTest