package org.infobip.mobile.messaging.platform;

import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.test.runner.AndroidJUnit4;

import org.infobip.mobile.messaging.MobileMessagingCore;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import static org.infobip.mobile.messaging.MobileMessagingJob.MM_JOB_SCHEDULER_START_ID;
import static org.infobip.mobile.messaging.MobileMessagingJob.ON_NETWORK_AVAILABLE_JOB_ID;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * @author sslavin
 * @since 18/09/2017.
 */

@RunWith(AndroidJUnit4.class)
public class MobileMessagingJobServiceTest {

    private final MobileMessagingCore mmcMock = mock(MobileMessagingCore.class);
    private final JobScheduler givenJobScheduler = mock(JobScheduler.class);
    private final JobParameters givenJobParameters = mock(JobParameters.class);

    @After
    public void after() {
        reset(givenJobScheduler, mmcMock);
    }

    @Test
    public void shouldInvokeResyncWhenJobStarts() throws Exception {
        // Given
        PackageManager packageManagerMock = Mockito.mock(PackageManager.class);
        MobileMessagingJobService givenJobService = spy(new MobileMessagingJobService(mmcMock));
        doReturn(packageManagerMock).when(givenJobService).getPackageManager();
        doReturn(givenJobScheduler).when(givenJobService).getSystemService(eq(Context.JOB_SCHEDULER_SERVICE));
        doReturn(getClass().getPackage().getName()).when(givenJobService).getPackageName();
        doReturn(ON_NETWORK_AVAILABLE_JOB_ID + MM_JOB_SCHEDULER_START_ID).when(givenJobParameters).getJobId();

        // When
        givenJobService.onStartJob(givenJobParameters);

        // Then
        verify(mmcMock, times(1)).retrySyncOnNetworkAvailable();
    }
}
@@ -710,16 +710,57 @@
 o.mock;%0A
+import static org.mockito.Mockito.never;%0A
 import s
@@ -792,16 +792,16 @@
 .reset;%0A
-
 import s
@@ -1639,16 +1639,80 @@
 cMock));
+%0A%0A        doReturn(%22123fgh%22).when(mmcMock).getApplicationCode();
 %0A       
@@ -2184,24 +2184,24 @@
     // Then%0A
-
         veri
@@ -2258,10 +2258,923 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldNotInvokeResyncWhenJobStartsAndAppCodeDoesNotExist() throws Exception %7B%0A        // Given%0A        PackageManager packageManagerMock = Mockito.mock(PackageManager.class);%0A        MobileMessagingJobService givenJobService = spy(new MobileMessagingJobService(mmcMock));%0A%0A        doReturn(null).when(mmcMock).getApplicationCode();%0A        doReturn(packageManagerMock).when(givenJobService).getPackageManager();%0A        doReturn(givenJobScheduler).when(givenJobService).getSystemService(eq(Context.JOB_SCHEDULER_SERVICE));%0A        doReturn(getClass().getPackage().getName()).when(givenJobService).getPackageName();%0A        doReturn(ON_NETWORK_AVAILABLE_JOB_ID + MM_JOB_SCHEDULER_START_ID).when(givenJobParameters).getJobId();%0A%0A        // When%0A        givenJobService.onStartJob(givenJobParameters);%0A%0A        // Then%0A        verify(mmcMock, never()).retrySyncOnNetworkAvailable();%0A    %7D
 %0A%7D

Test for JobService fixed