package com.github.openwebnet.service;

import com.github.openwebnet.BuildConfig;
import com.github.openwebnet.component.ApplicationComponentTest;
import com.github.openwebnet.component.DaggerApplicationComponentTest;
import com.github.openwebnet.component.Injector;
import com.github.openwebnet.component.module.ApplicationContextModuleTest;
import com.github.openwebnet.component.module.DomoticModuleTest;
import com.github.openwebnet.component.module.RepositoryModuleTest;
import com.github.openwebnet.model.GatewayModel;
import com.github.openwebnet.repository.GatewayRepository;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

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

import javax.inject.Inject;

import rx.Observable;
import rx.observers.TestSubscriber;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PrepareForTest({Injector.class, GatewayModel.class})
public class GatewayServiceTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Inject
    GatewayRepository gatewayRepository;

    @Inject
    GatewayService gatewayService;

    @Before
    public void setupDagger() {
        ApplicationComponentTest applicationComponentTest = DaggerApplicationComponentTest.builder()
            .applicationContextModuleTest(new ApplicationContextModuleTest())
            .domoticModuleTest(new DomoticModuleTest())
            .repositoryModuleTest(new RepositoryModuleTest(true))
            .build();

        PowerMockito.mockStatic(Injector.class);
        PowerMockito.when(Injector.getApplicationComponent()).thenReturn(applicationComponentTest);

        ((ApplicationComponentTest) Injector.getApplicationComponent()).inject(this);
    }

    @Test
    @Ignore
    public void gatewayService_add() {
        String GATEWAY_UUID = "gatewayUuid";
        String GATEWAY_HOST = "1.1.1.1";
        Integer GATEWAY_PORT = 88;

        GatewayModel gateway = new GatewayModel();
        gateway.setUuid(GATEWAY_UUID);
        gateway.setHost(GATEWAY_HOST);
        gateway.setPort(GATEWAY_PORT);

        PowerMockito.mockStatic(GatewayModel.class);
        when(GatewayModel.newGateway(GATEWAY_HOST, GATEWAY_PORT)).thenReturn(gateway);

        when(gatewayRepository.add(gateway)).thenReturn(Observable.just(GATEWAY_UUID));
    }

    @Test
    public void gatewayService_findAll() {
        List<GatewayModel> gateways = new ArrayList<>();

        when(gatewayRepository.findAll()).thenReturn(Observable.just(gateways));

        TestSubscriber<List<GatewayModel>> tester = new TestSubscriber<>();
        gatewayService.findAll().subscribe(tester);

        verify(gatewayRepository).findAll();

        tester.assertValue(gateways);
        tester.assertCompleted();
        tester.assertNoErrors();
    }

    @Test
    public void gatewayService_findById() {
        String GATEWAY_UUID = "gatewayUuid";
        GatewayModel gateway = new GatewayModel();
        gateway.setUuid(GATEWAY_UUID);
        gateway.setHost("1.1.1.1");
        gateway.setPort(88);

        when(gatewayRepository.findById(GATEWAY_UUID)).thenReturn(Observable.just(gateway));

        TestSubscriber<GatewayModel> tester = new TestSubscriber<>();
        gatewayService.findById(GATEWAY_UUID).subscribe(tester);

        verify(gatewayRepository).findById(GATEWAY_UUID);

        tester.assertValue(gateway);
        tester.assertCompleted();
        tester.assertNoErrors();
    }

}

@@ -607,33 +607,8 @@
 re;%0A
-import org.junit.Ignore;%0A
 impo
@@ -2142,20 +2142,8 @@
 est%0A
-    @Ignore%0A
     
@@ -2608,17 +2608,16 @@
 teway);%0A
-%0A
         
@@ -2695,16 +2695,314 @@
 _UUID));
+%0A%0A        TestSubscriber%3CString%3E tester = new TestSubscriber%3C%3E();%0A        gatewayService.add(GATEWAY_HOST, GATEWAY_PORT).subscribe(tester);%0A%0A        verify(gatewayRepository).add(gateway);%0A%0A        tester.assertValue(GATEWAY_UUID);%0A        tester.assertCompleted();%0A        tester.assertNoErrors();
 %0A    %7D%0A%0A

test gateway service