Skip to content

Commit

Permalink
Add Google Fit geoposition mapping unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonf committed Oct 6, 2017
1 parent 74c02cf commit 66de50d
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 39 deletions.
Expand Up @@ -43,16 +43,17 @@ protected Optional<DataPoint<Geoposition>> asDataPoint(JsonNode listNode) {
double latitude = asRequiredDouble(listValueNode.get(0), "fpVal");
double longitude = asRequiredDouble(listValueNode.get(1), "fpVal");
// TODO add accuracy to geoposition
Optional<Double> accuracyInM = asOptionalDouble(listValueNode.get(2), "fpVal");
Optional<Double> altitudeInM = asOptionalDouble(listValueNode.get(3), "fpVal");
Optional<Double> accuracyInM = asOptionalDouble(listValueNode.get(2), "fpVal");

Geoposition.Builder measureBuilder =
new Geoposition.Builder(
DEGREE_OF_ARC.newUnitValue(latitude),
DEGREE_OF_ARC.newUnitValue(longitude),
getTimeFrame(listNode));

altitudeInM.ifPresent((a) -> measureBuilder.setElevation(METER.newUnitValue(a)));
if (listValueNode.size() >= 4) {
measureBuilder.setElevation(METER.newUnitValue(asRequiredDouble(listValueNode.get(3), "fpVal")));
}

Geoposition geoposition = measureBuilder.build();

Expand Down
Expand Up @@ -21,6 +21,8 @@
import org.openmhealth.schema.domain.omh.TimeFrame;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.util.Optional.empty;
Expand All @@ -36,9 +38,7 @@ public class GoogleFitTestProperties {
private String sourceOriginId;
private SchemaId bodySchemaId;
private DataPointModality modality;
private String stringValue;
private long intValue;
private double fpValue;
private List<Object> values = new ArrayList<>();
private OffsetDateTime effectiveStartDateTime;
private OffsetDateTime effectiveEndDateTime;

Expand Down Expand Up @@ -69,28 +69,36 @@ public void setModality(DataPointModality modality) {
}

public String getStringValue() {
return stringValue;
return getStringValue(0);
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
public String getStringValue(int index) {
return getValue(String.class, index);
}

public long getIntValue() {
return intValue;
public <T> T getValue(Class<T> clazz, int index) {
return clazz.cast(values.get(index));
}

public void setIntValue(long integerValue) {
this.intValue = integerValue;
public Long getIntValue() {
return getIntValue(0);
}

public double getFpValue() {
return fpValue;
public Long getIntValue(int index) {
return getValue(Long.class, index);
}

public void setFpValue(double fpValue) {
public Double getFpValue() {
return getFpValue(0);
}

public Double getFpValue(int index) {
return getValue(Double.class, index);
}

this.fpValue = fpValue;
public GoogleFitTestProperties addValue(Object value) {
this.values.add(value);
return this;
}

public Optional<OffsetDateTime> getEffectiveEndDateTime() {
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.openmhealth.shim.googlefit.common.GoogleFitTestProperties;

import java.io.IOException;
import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -55,7 +56,8 @@ public void assertThatDataPointMatches(DataPoint<T> dataPoint, GoogleFitTestProp

if (testProperties.getModality().isPresent()) {

assertThat(dataPointHeader.getAcquisitionProvenance().getModality(),
assertThat(
dataPointHeader.getAcquisitionProvenance().getModality(),
equalTo(testProperties.getModality().get()));
}
else {
Expand All @@ -67,36 +69,34 @@ public void assertThatDataPointMatches(DataPoint<T> dataPoint, GoogleFitTestProp
/**
* Creates a test properties object used to generate an expected value data point to test google fit data points
* that use floating point values in their response.
*
* @deprecated use varargs instead
*/
@Deprecated
public GoogleFitTestProperties createFloatingPointTestProperties(double fpValue, String startDateTime,
String endDateTime, String sourceOriginId, SchemaId schemaId) {

GoogleFitTestProperties testProperties =
createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId);

testProperties.setFpValue(fpValue);

return testProperties;
return createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId, fpValue);
}

/**
* Creates a test properties object used to generate an expected value data point to test google fit data points
* that use integer values in their response.
*
* @deprecated use varargs instead
*/
@Deprecated
public GoogleFitTestProperties createIntegerTestProperties(long intValue, String startDateTime, String endDateTime,
String sourceOriginId, SchemaId schemaId) {

GoogleFitTestProperties testProperties =
createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId);

testProperties.setIntValue(intValue);

return testProperties;
return createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId, intValue);
}

/**
* Creates a test properties object used to generate an expected value data point to test google fit data points
* that use strings to represent values.
*
* @deprecated use varargs instead
*/
public GoogleFitTestProperties createStringTestProperties(
String stringValue,
Expand All @@ -105,19 +105,15 @@ public GoogleFitTestProperties createStringTestProperties(
String sourceOriginId,
SchemaId schemaId) {

GoogleFitTestProperties testProperties =
createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId);

testProperties.setStringValue(stringValue);

return testProperties;
return createTestProperties(startDateTime, endDateTime, sourceOriginId, schemaId, stringValue);
}

private GoogleFitTestProperties createTestProperties(
public GoogleFitTestProperties createTestProperties(
String startDateTimeString,
String endDateTimeString,
String sourceOriginId,
SchemaId schemaId) {
SchemaId schemaId,
Object... values) {

GoogleFitTestProperties testProperties = new GoogleFitTestProperties();

Expand All @@ -140,6 +136,8 @@ private GoogleFitTestProperties createTestProperties(

testProperties.setBodySchemaId(schemaId);

Arrays.stream(values).forEach(testProperties::addValue);

return testProperties;
}
}
@@ -0,0 +1,83 @@
/*
* Copyright 2017 Open mHealth
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openmhealth.shim.googlefit.mapper;

import org.openmhealth.schema.domain.omh.DataPoint;
import org.openmhealth.schema.domain.omh.Geoposition;
import org.openmhealth.schema.domain.omh.LengthUnit;
import org.openmhealth.shim.googlefit.common.GoogleFitTestProperties;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.openmhealth.schema.domain.omh.Geoposition.SCHEMA_ID;
import static org.openmhealth.schema.domain.omh.PlaneAngleUnit.DEGREE_OF_ARC;


/**
* @author Emerson Farrugia
*/
public class GoogleFitGeopositionDataPointMapperUnitTests extends GoogleFitDataPointMapperUnitTests<Geoposition> {

private final GoogleFitGeopositionDataPointMapper mapper = new GoogleFitGeopositionDataPointMapper();

@BeforeClass
@Override
public void initializeResponseNode() throws IOException {

responseNode = asJsonNode("org/openmhealth/shim/googlefit/mapper/googlefit-merge-location-samples.json");
}

@Test
public void asDataPointsShouldReturnCorrectNumberOfDataPoints() {

assertThat(mapper.asDataPoints(responseNode).size(), equalTo(2));
}

@Test
public void asDataPointsShouldReturnCorrectDataPointsWithoutElevation() {

List<DataPoint<Geoposition>> dataPoints = mapper.asDataPoints(responseNode);

assertThatDataPointMatches(
dataPoints.get(1),
createTestProperties(
"2017-10-04T13:45:54Z",
null,
"raw:com.google.location.sample:com.google.android.gms:LGE:Nexus 5:bar:live_location",
SCHEMA_ID,
51.233600616455078, -0.57613241672515869, 43.0, 50.0));
}

@Override
public void assertThatMeasureMatches(Geoposition testMeasure, GoogleFitTestProperties testProperties) {

Geoposition expectedGeoposition =
new Geoposition.Builder(
DEGREE_OF_ARC.newUnitValue(testProperties.getFpValue(0)),
DEGREE_OF_ARC.newUnitValue(testProperties.getFpValue(1)),
testProperties.getEffectiveTimeFrame().orElseThrow(IllegalArgumentException::new))
.setElevation(LengthUnit.METER.newUnitValue(testProperties.getFpValue(3)))
.build();

assertThat(testMeasure, equalTo(expectedGeoposition));
}
}
Expand Up @@ -52,7 +52,7 @@ public void asDataPointsShouldReturnCorrectNumberOfDataPoints() {
}

@Test
public void asDataPointsShouldReturnCorrectDataPointsForSingleTimePoint() {
public void asDataPointsShouldReturnCorrectDataPoints() {

List<DataPoint<Speed>> dataPoints = mapper.asDataPoints(responseNode);

Expand Down
@@ -0,0 +1,53 @@
{
"minStartTimeNs": "1506816000000000000",
"maxEndTimeNs": "1509494399000000000",
"dataSourceId": "derived:com.google.location.sample:com.google.android.gms:merge_location_samples",
"point": [
{
"modifiedTimeMillis": "1507028376296",
"startTimeNanos": "1507027899615000000",
"endTimeNanos": "1507027899615000000",
"value": [
{
"mapVal": [ ],
"fpVal": 51.211826373291016
},
{
"mapVal": [ ],
"fpVal": -0.57192360877990723
},
{
"mapVal": [ ],
"fpVal": 20.0
}
],
"dataTypeName": "com.google.location.sample",
"originDataSourceId": "raw:com.google.location.sample:com.google.android.gms:LGE:Nexus 5:foo:live_location"
},
{
"modifiedTimeMillis": "1507127495556",
"startTimeNanos": "1507124754000000000",
"endTimeNanos": "1507124754000000000",
"value": [
{
"mapVal": [ ],
"fpVal": 51.233600616455078
},
{
"mapVal": [ ],
"fpVal": -0.57613241672515869
},
{
"mapVal": [ ],
"fpVal": 43.0
},
{
"mapVal": [ ],
"fpVal": 50.0
}
],
"dataTypeName": "com.google.location.sample",
"originDataSourceId": "raw:com.google.location.sample:com.google.android.gms:LGE:Nexus 5:bar:live_location"
}
]
}

0 comments on commit 66de50d

Please sign in to comment.