Skip to content

Commit

Permalink
Merge pull request #1212 from OpenSRP/tt--layer-unit-tests
Browse files Browse the repository at this point in the history
Unit test  Layer Package
  • Loading branch information
githengi authored Jan 19, 2021
2 parents 65f5f63 + 9f3f0ae commit e710c39
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* Created by samuelgithengi on 10/1/19.
*/
public class DigitalGlobeLayer extends BaseLayer {
private String satelliteLayerId = "DG-EarthWatch-Satellite";
private String satelliteSourceId = "dg-earthWatch-imagery";
protected static final String satelliteLayerId = "DG-EarthWatch-Satellite";
protected static final String satelliteSourceId = "dg-earthWatch-imagery";

private LinkedHashSet<Layer> layers = new LinkedHashSet<>();
private List<Source> sources = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Created by samuelgithengi on 10/1/19.
*/
public class MapBoxLayer extends BaseLayer {
private String satelliteLayerId = "mapbox-satellite";
private String satelliteSourceId = "mapbox://mapbox.satellite";
protected static final String satelliteLayerId = "mapbox-satellite";
protected static final String satelliteSourceId = "mapbox://mapbox.satellite";

private LinkedHashSet<Layer> layers = new LinkedHashSet<>();
private List<Source> sources = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<View> getViewsFromJson(String stepName, Context context, JsonFormFra
textView.setTag(com.vijay.jsonwizard.R.id.address, stepName + ":" + jsonObject.getString(KEY));
textView.setTag(com.vijay.jsonwizard.R.id.key, jsonObject.getString(KEY));
addRequiredValidator(jsonObject, textView);
((JsonApi) context).addFormDataView(textView);
formFragment.getJsonApi().addFormDataView(textView);
return views;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.smartregister.reveal.layer;

import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.RasterSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.utils.ThreadUtils;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.reveal.BaseUnitTest;
import org.smartregister.reveal.shadow.RasterLayerShadow;
import org.smartregister.reveal.shadow.RasterSourceShadow;

import java.util.LinkedHashSet;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
* Created by samuelgithengi on 1/12/21.
*/
@Config(shadows = {RasterSourceShadow.class, RasterLayerShadow.class})
public class DigitalGlobeLayerTest extends BaseUnitTest {

private DigitalGlobeLayer layer;


@Before
public void setUp() {
ThreadUtils.init(RuntimeEnvironment.application);
layer = new DigitalGlobeLayer();

}

@Test
public void testConstructorShouldInitializesLayers() {
LinkedHashSet<Layer> layers = ReflectionHelpers.getField(layer, "layers");
List<Source> sources = ReflectionHelpers.getField(layer, "sources");
assertEquals(1, layers.size());
assertEquals(1, sources.size());
RasterLayer rasterLayer = (RasterLayer) layers.iterator().next();
assertEquals(DigitalGlobeLayer.satelliteLayerId, rasterLayer.getId());
assertEquals(DigitalGlobeLayer.satelliteSourceId, rasterLayer.getSourceId());

RasterSource rasterSource = (RasterSource) sources.get(0);
assertEquals(DigitalGlobeLayer.satelliteSourceId, rasterSource.getId());
}

@Test
public void testGetDisplayNameShouldReturnCorrectName() {
assertEquals("Digital Globe", layer.getDisplayName());
}


@Test
public void testGetSourceIdsReturnCorrectIds() {
assertArrayEquals(new String[]{DigitalGlobeLayer.satelliteSourceId}, layer.getSourceIds());
}


@Test
public void testGetLayersReturnCorrectLayers() {
LinkedHashSet<Layer> layers = layer.getLayers();
assertEquals(1, layers.size());
Layer layer = layers.iterator().next();
MatcherAssert.assertThat(layers.iterator().next(), Matchers.instanceOf(RasterLayer.class));
assertEquals(DigitalGlobeLayer.satelliteLayerId, layer.getId());
assertEquals(DigitalGlobeLayer.satelliteSourceId, ((RasterLayer) layer).getSourceId());
}

@Test
public void testGetSourcesReturnCorrectSources() {
List<Source> sources = layer.getSources();
assertEquals(1, sources.size());
Source source = sources.iterator().next();
MatcherAssert.assertThat(sources.iterator().next(), Matchers.instanceOf(Source.class));
assertEquals(DigitalGlobeLayer.satelliteSourceId, source.getId());
}

@Test
public void testGetIdShouldReturnCorrectId() {
assertEquals("dg-satellite-base-layer", layer.getId());
}

@Test
public void testGetLayerIdsShouldReturnCorrectId() {
assertArrayEquals(new String[]{DigitalGlobeLayer.satelliteLayerId}, layer.getLayerIds());
}

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

import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.RasterSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.utils.ThreadUtils;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.reveal.BaseUnitTest;
import org.smartregister.reveal.shadow.RasterLayerShadow;
import org.smartregister.reveal.shadow.RasterSourceShadow;

import java.util.LinkedHashSet;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
* Created by samuelgithengi on 1/12/21.
*/
@Config(shadows = {RasterSourceShadow.class, RasterLayerShadow.class})
public class MapboxLayerTest extends BaseUnitTest {

private MapBoxLayer layer;


@Before
public void setUp() {
ThreadUtils.init(RuntimeEnvironment.application);
layer = new MapBoxLayer();

}

@Test
public void testConstructorShouldInitializesLayers() {
LinkedHashSet<Layer> layers = ReflectionHelpers.getField(layer, "layers");
List<Source> sources = ReflectionHelpers.getField(layer, "sources");
assertEquals(1, layers.size());
assertEquals(1, sources.size());
RasterLayer rasterLayer = (RasterLayer) layers.iterator().next();
assertEquals(MapBoxLayer.satelliteLayerId, rasterLayer.getId());
assertEquals(MapBoxLayer.satelliteSourceId, rasterLayer.getSourceId());

RasterSource rasterSource = (RasterSource) sources.get(0);
assertEquals(MapBoxLayer.satelliteSourceId, rasterSource.getId());
}

@Test
public void testGetDisplayNameShouldReturnCorrectName() {
assertEquals("Mapbox Satellite", layer.getDisplayName());
}


@Test
public void testGetSourceIdsReturnCorrectIds() {
assertArrayEquals(new String[]{MapBoxLayer.satelliteSourceId}, layer.getSourceIds());
}


@Test
public void testGetLayersReturnCorrectLayers() {
LinkedHashSet<Layer> layers = layer.getLayers();
assertEquals(1, layers.size());
Layer layer = layers.iterator().next();
MatcherAssert.assertThat(layers.iterator().next(), Matchers.instanceOf(RasterLayer.class));
assertEquals(MapBoxLayer.satelliteLayerId, layer.getId());
assertEquals(MapBoxLayer.satelliteSourceId, ((RasterLayer) layer).getSourceId());
}

@Test
public void testGetSourcesReturnCorrectSources() {
List<Source> sources = layer.getSources();
assertEquals(1, sources.size());
Source source = sources.iterator().next();
MatcherAssert.assertThat(sources.iterator().next(), Matchers.instanceOf(Source.class));
assertEquals(MapBoxLayer.satelliteSourceId, source.getId());
RasterSource rasterSource = (RasterSource) source;
assertEquals(MapBoxLayer.satelliteSourceId, rasterSource.getUri());
}

@Test
public void testGetIdShouldReturnCorrectId() {
assertEquals("mapbox-satellite-base-layer", layer.getId());
}

@Test
public void testGetLayerIdsShouldReturnCorrectId() {
assertArrayEquals(new String[]{MapBoxLayer.satelliteLayerId}, layer.getLayerIds());
}

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

import com.mapbox.mapboxsdk.style.layers.RasterLayer;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

/**
* Created by samuelgithengi on 1/12/21.
*/

@Implements(RasterLayer.class)
public class RasterLayerShadow extends LayerShadow {

private String sourceId;

@Implementation
public void __constructor__(String layerId, String sourceId) {
// Do nothing
setLayerId(layerId);
this.sourceId = sourceId;
}

@Implementation
public String getSourceId() {
return sourceId;
}

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

import com.mapbox.mapboxsdk.style.sources.RasterSource;
import com.mapbox.mapboxsdk.style.sources.TileSet;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

/**
* Created by samuelgithengi on 1/12/21.
*/
@Implements(RasterSource.class)
public class RasterSourceShadow extends SourceShadow {


private String id;
private String uri;

@Implementation
public void __constructor__(String id, TileSet tileSet, int tileSize) {
this.id = id;
}

@Implementation
public void __constructor__(String id, String uri, int tileSize) {
this.id = id;
this.uri = uri;
}

@Implementation
protected String nativeGetId() {
return id;
}

@Implementation
protected String nativeGetUrl() {
return uri;
}


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

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.interfaces.JsonApi;
import com.vijay.jsonwizard.utils.ValidationStatus;
import com.vijay.jsonwizard.validators.edittext.RequiredValidator;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.robolectric.RuntimeEnvironment;
import org.smartregister.reveal.BaseUnitTest;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Created by samuelgithengi on 1/12/21.
*/
public class RevealToasterNotesFactoryTest extends BaseUnitTest {

private RevealToasterNotesFactory factory;

private Context context;

@Mock
private JsonFormFragment formFragment;
@Mock
private CommonListener listener;
@Mock
private JSONObject jsonObject;
@Mock
private JsonApi jsonApi;
@Captor
private ArgumentCaptor<TextView> textViewArgumentCaptor;


@Before
public void setUp() throws JSONException {
factory = new RevealToasterNotesFactory();
jsonObject = new JSONObject("{\"key\":\"displayRecommendedNumNetsToDistribute\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"2 nets should be distributed\",\"text_color\":\"#000000\",\"toaster_type\":\"info\",\"v_required\":{\"value\":true,\"err\":\"Toast notes Field required\"}}");
context = RuntimeEnvironment.application;
when(formFragment.getJsonApi()).thenReturn(jsonApi);
}

@Test
public void testGetViewsFromJsonShouldReturnView() throws Exception {
List<View> views = factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener);
assertEquals(1, views.size());
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture());
TextView textView = textViewArgumentCaptor.getValue();
assertEquals(com.vijay.jsonwizard.R.id.toaster_notes_text, textView.getId());
assertEquals("step1:displayRecommendedNumNetsToDistribute", textView.getTag(com.vijay.jsonwizard.R.id.address));
assertEquals("displayRecommendedNumNetsToDistribute", textView.getTag(com.vijay.jsonwizard.R.id.key));
MatcherAssert.assertThat(textView.getTag(com.vijay.jsonwizard.R.id.v_required), Matchers.instanceOf(RequiredValidator.class));
}

@Test
public void testValidateShouldReturnFailedValidation() throws Exception {
factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener);
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture());
TextView textView = textViewArgumentCaptor.getValue();
ValidationStatus validateStatus = RevealToasterNotesFactory.validate(formFragment, textView);
assertFalse(validateStatus.isValid());
assertEquals("Toast notes Field required", validateStatus.getErrorMessage());
}

@Test
public void testValidateShouldReturnValidValidationWhenViewIsNotShown() throws Exception {
factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener);
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture());
TextView textView = textViewArgumentCaptor.getValue();
View view = (View) textView.getParent().getParent();
view.setVisibility(View.GONE);
ValidationStatus validateStatus = RevealToasterNotesFactory.validate(formFragment, textView);
assertTrue(validateStatus.isValid());
assertNull(validateStatus.getErrorMessage());
}
}

0 comments on commit e710c39

Please sign in to comment.