Skip to content

Commit

Permalink
Merge branch 'master' into dynamic-tasking
Browse files Browse the repository at this point in the history
  • Loading branch information
githengi committed Dec 2, 2020
2 parents b749711 + 039d61f commit b6a6164
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ public void testSaveCaseConfirmation() throws JSONException {
interactor.saveJsonForm(formObject.toString());
verify(eventClientRepository, timeout(ASYNC_TIMEOUT)).addEvent(anyString(), eventJSONObjectCaptor.capture());
verify(clientProcessor, timeout(ASYNC_TIMEOUT)).calculateBusinessStatus(any());
verify(taskRepository,timeout(ASYNC_TIMEOUT)).addOrUpdate(taskArgumentCaptor.capture());
assertEquals(org.smartregister.reveal.util.Constants.EventType.CASE_CONFIRMATION_EVENT, eventJSONObjectCaptor.getValue().getString("eventType"));
assertFalse(RevealApplication.getInstance().getSynced());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.smartregister.reveal.presenter;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.configurableviews.helper.ConfigurableViewsHelper;
import org.smartregister.configurableviews.model.View;
import org.smartregister.configurableviews.model.ViewConfiguration;
import org.smartregister.reveal.BaseUnitTest;
import org.smartregister.reveal.contract.EventRegisterContract;
import org.smartregister.reveal.interactor.EventRegisterFragmentInteractor;
import org.smartregister.reveal.util.Constants.EventsRegister;

import java.util.Collections;
import java.util.Set;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

/**
* Created by samuelgithengi on 12/1/20.
*/
public class EventRegisterFragmentPresenterTest extends BaseUnitTest {
@Mock
private EventRegisterContract.View view;

@Mock
private ConfigurableViewsHelper viewsHelper;

@Mock
private EventRegisterFragmentInteractor interactor;

private EventRegisterFragmentPresenter presenter;

private Set<View> visibleColumns;

@Before
public void setUp() {
presenter = new EventRegisterFragmentPresenter(view, EventsRegister.VIEW_IDENTIFIER);
ReflectionHelpers.setField(presenter, "viewsHelper", viewsHelper);
ReflectionHelpers.setField(presenter, "interactor", interactor);
View view = new View();
view.setIdentifier(UUID.randomUUID().toString());
visibleColumns = Collections.singleton(view);
}

@Test
public void testProcessViewConfigurationsShouldRegisterActiveColumns() {
ViewConfiguration viewConfiguration = new ViewConfiguration();
viewConfiguration.setIdentifier(UUID.randomUUID().toString());
when(viewsHelper.getViewConfiguration(EventsRegister.VIEW_IDENTIFIER)).thenReturn(viewConfiguration);
presenter.processViewConfigurations();
verify(viewsHelper).getViewConfiguration(eq(EventsRegister.VIEW_IDENTIFIER));
verify(viewsHelper).getRegisterActiveColumns(eq(EventsRegister.VIEW_IDENTIFIER));
verifyNoMoreInteractions(viewsHelper);
}

@Test
public void testInitializeQueries() {
String mainCondition = "task.group_id = ? AND task.plan_id = ? AND task.status NOT IN (?,?)";
Whitebox.setInternalState(presenter, "visibleColumns", visibleColumns);
presenter.initializeQueries(mainCondition);
verify(view).initializeAdapter(eq(visibleColumns));
String[] columns = ArrayUtils.addFirst(presenter.mainColumns(EventsRegister.TABLE_NAME), "ec_events.id as _id");
verify(view).initializeQueryParams(EventsRegister.TABLE_NAME, "SELECT COUNT(*) FROM ec_events WHERE " + mainCondition + " ", String.format("Select %s FROM %s WHERE %s ", StringUtils.join(columns, " , "), EventsRegister.TABLE_NAME, mainCondition));
verify(view).countExecute();
verify(view).filterandSortInInitializeQueries();


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.smartregister.reveal.presenter;

import android.location.Location;

import com.mapbox.mapboxsdk.geometry.LatLng;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.smartregister.reveal.BaseUnitTest;
import org.smartregister.reveal.R;
import org.smartregister.reveal.contract.UserLocationContract;

import java.util.concurrent.atomic.AtomicInteger;

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

/**
* Created by samuelgithengi on 12/1/20.
*/
public class ValidateUserLocationPresenterTest extends BaseUnitTest {

@Mock
private UserLocationContract.UserLocationView locationView;

@Mock
private UserLocationContract.UserLocationCallback callback;

private ValidateUserLocationPresenter presenter;

private Location location;

@Before
public void setUp() {
presenter = new ValidateUserLocationPresenter(locationView, callback);
location = new Location("test");
location.setLatitude(12.1212);
location.setLongitude(67.2232);
}

@Test
public void testRequestUserLocationShouldInvokeGetUserLocation() {
presenter.requestUserLocation();
verify(locationView).requestUserLocation();
}

@Test
public void testOnGetUserLocationShouldRequestSupervisorPassword() {

when(callback.getTargetCoordinates()).thenReturn(new LatLng(11.23232, 34.223222));

presenter.onGetUserLocation(location);
verify(callback).requestUserPassword();
verify(callback).getTargetCoordinates();
verify(locationView).hideProgressDialog();
}

@Test
public void testOnGetUserLocationShouldInvokeLocationValidate() {

when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude()));

presenter.onGetUserLocation(location);
verify(locationView).hideProgressDialog();
verify(callback).getTargetCoordinates();
verify(callback).onLocationValidated();
}

@Test
public void testOnGetUserLocationFailedShouldRequestForPasword() {
presenter.onGetUserLocationFailed();
verify(locationView).hideProgressDialog();
verify(callback).requestUserPassword();
}


@Test
public void testWaitForUserLocationShouldInvokeOnGetUserLocation() {
presenter = spy(presenter);
when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude()));

when(locationView.getUserCurrentLocation()).thenReturn(location);
presenter.waitForUserLocation();

verify(presenter, timeout(ASYNC_TIMEOUT)).onGetUserLocation(location);
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog();
verify(callback, timeout(ASYNC_TIMEOUT)).getTargetCoordinates();
verify(callback, timeout(ASYNC_TIMEOUT)).onLocationValidated();
}


@Test
public void testWaitForUserLocationInvokeOnGetUserLocationFailed() {
presenter = spy(presenter);
when(locationView.getUserCurrentLocation()).thenAnswer(invocation -> {
Thread.sleep(3000);
return null;
});
ReflectionHelpers.callInstanceMethod(presenter, "waitForUserLocation", ClassParameter.from(long.class, 61000));

verify(presenter, timeout(ASYNC_TIMEOUT)).onGetUserLocationFailed();
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog();
verify(callback, timeout(ASYNC_TIMEOUT)).requestUserPassword();
}


@Test
public void testWaitForUserLocationTryEvery2SecondsUntilTimeout() {
AtomicInteger count = new AtomicInteger();
when(locationView.getUserCurrentLocation()).thenAnswer(invocation -> {
Thread.sleep(1000);
count.getAndIncrement();
return count.get() == 3 ? location : null;
});
when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude()));


presenter.waitForUserLocation();
int ASYNC_TIMEOUT = 10 * 1000;
verify(locationView, timeout(ASYNC_TIMEOUT).times(3)).getUserCurrentLocation();
verify(locationView, timeout(ASYNC_TIMEOUT).times(2)).showProgressDialog(R.string.narrowing_location_title, R.string.narrowing_location_message);
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog();
verify(callback, timeout(ASYNC_TIMEOUT)).onLocationValidated();

}
}

0 comments on commit b6a6164

Please sign in to comment.