package com.ushahidi.android.data.repository;

import com.ushahidi.android.BaseTestCase;
import com.ushahidi.android.data.entity.DeploymentEntity;
import com.ushahidi.android.data.entity.mapper.DeploymentEntityDataMapper;
import com.ushahidi.android.data.repository.datasource.deployment.DeploymentDataSource;
import com.ushahidi.android.data.repository.datasource.deployment.DeploymentDataSourceFactory;
import com.ushahidi.android.domain.entity.Deployment;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

import rx.Observable;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;

/**
 * Test suite for {@link DeploymentDataRepository}
 *
 * @author Ushahidi Team <team@ushahidi.com>
 */
public class DeploymentDataRepositoryTest extends BaseTestCase {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    private DeploymentDataRepository mDeploymentDataRepository;

    @Mock
    private DeploymentDataSourceFactory mMockDeploymentDataSourceFactory;

    @Mock
    private DeploymentEntityDataMapper mMockDeploymentEntityMapper;

    @Mock
    private DeploymentDataSource mMockDataSource;

    @Mock
    private Deployment mMockDeployment;

    @Mock
    private DeploymentEntity mMockDeploymentEntity;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        clearSingleton(DeploymentDataRepository.class);
        mDeploymentDataRepository = new DeploymentDataRepository(mMockDeploymentDataSourceFactory,
                mMockDeploymentEntityMapper);
        given(mMockDeploymentDataSourceFactory.createDatabaseDataSource())
                .willReturn(mMockDataSource);
    }

    @Test
    public void shouldSuccessfullyAddADeployment() {
        given(mMockDataSource.addDeploymentEntity(mMockDeploymentEntity)).willReturn(
                Observable.just(1l));
        given(mMockDeploymentEntityMapper.map(mMockDeployment)).willReturn(mMockDeploymentEntity);
        mDeploymentDataRepository.addEntity(mMockDeployment);

        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();
        verify(mMockDataSource).addDeploymentEntity(mMockDeploymentEntity);
    }

    @Test
    public void shouldSuccessfullyUpdateADeployment() {
        given(mMockDataSource.updateDeploymentEntity(mMockDeploymentEntity)).willReturn(
                Observable.just(1l));
        given(mMockDeploymentEntityMapper.map(mMockDeployment)).willReturn(mMockDeploymentEntity);
        mDeploymentDataRepository.updateEntity(mMockDeployment);

        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();
        verify(mMockDataSource).updateDeploymentEntity(mMockDeploymentEntity);
    }

    @Test
    public void shouldSuccessfullyDeleteADeployment() {
        given(mMockDataSource.deleteDeploymentEntity(1l)).willReturn(
                Observable.just(1l));
        mDeploymentDataRepository.deleteEntity(1l);

        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();
        verify(mMockDataSource).deleteDeploymentEntity(1l);
    }

    @Test
    public void shouldSuccessfullyGetADeployment() {
        given(mMockDataSource.getDeploymentEntity(1l)).willReturn(
                Observable.just(mMockDeploymentEntity));
        given(mMockDeploymentEntityMapper.map(mMockDeployment)).willReturn(mMockDeploymentEntity);
        mDeploymentDataRepository.getEntity(1l);

        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();
        verify(mMockDataSource).getDeploymentEntity(1l);
    }

    @Test
    public void shouldSuccessfullyGetAListOfDeployments() {
        List<DeploymentEntity> deploymentEntities = new ArrayList<>();
        deploymentEntities.add(new DeploymentEntity());
        List<Deployment> deployments = new ArrayList<>();
        deployments.add(new Deployment());
        given(mMockDataSource.getDeploymentEntityList()).willReturn(
                Observable.just(deploymentEntities));
        given(mMockDeploymentEntityMapper.map(deploymentEntities)).willReturn(deployments);
        mDeploymentDataRepository.getEntities();

        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();
        verify(mMockDataSource).getDeploymentEntityList();
    }
}

@@ -2162,32 +2162,33 @@
 loymentEntity);%0A
+%0A
         mDeploym
@@ -2678,32 +2678,33 @@
 loymentEntity);%0A
+%0A
         mDeploym
@@ -3569,24 +3569,25 @@
 entEntity);%0A
+%0A
         mDep
@@ -4059,24 +4059,25 @@
 loyment());%0A
+%0A
         give
@@ -4275,24 +4275,25 @@
 ployments);%0A
+%0A
         mDep
@@ -4399,32 +4399,32 @@
 seDataSource();%0A
-
         verify(m
@@ -4471,11 +4471,1025 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldSuccessfullyGetADeploymentByItsStatus() %7B%0A        DeploymentEntity deploymentEntity = new DeploymentEntity();%0A        deploymentEntity.setStatus(DeploymentEntity.Status.ACTIVATED);%0A        Deployment deployment = new Deployment();%0A        deployment.setStatus(Deployment.Status.ACTIVATED);%0A%0A        given(mMockDataSource.getByStatus(DeploymentEntity.Status.ACTIVATED)).willReturn(%0A                Observable.just(deploymentEntity));%0A        given(mMockDeploymentEntityMapper.map(Deployment.Status.ACTIVATED))%0A                .willReturn(DeploymentEntity.Status.ACTIVATED);%0A        given(mMockDeploymentEntityMapper.map(deployment)).willReturn(deploymentEntity);%0A%0A        mDeploymentDataRepository.getByStatus(Deployment.Status.ACTIVATED);%0A%0A        verify(mMockDeploymentDataSourceFactory).createDatabaseDataSource();%0A        verify(mMockDeploymentEntityMapper).map(Deployment.Status.ACTIVATED);%0A        verify(mMockDataSource).getByStatus(DeploymentEntity.Status.ACTIVATED);%0A    %7D
 %0A%7D%0A

Test Deployment fetch by status