Skip to content

Commit

Permalink
Merge commit '9c0297d521ba4b0d265ebd716007c8ce4aaad1fd'
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonf committed Oct 6, 2017
2 parents 6401962 + 9c0297d commit 0246c73
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 1 deletion.
2 changes: 1 addition & 1 deletion java-schema-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray' // see https://github.com/bintray/gradle-bintray-plugin for details

archivesBaseName = 'omh-schema-sdk'
version = '1.2.0'
version = '1.2.1'

ext {
jacksonVersion = '2.8.9'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.schema.domain.omh;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.openmhealth.schema.serializer.SerializationConstructor;

import java.time.OffsetDateTime;
import java.util.Objects;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static com.google.common.base.Preconditions.checkNotNull;


/**
* A speed measurement.
*
* @author Emerson Farrugia
* @version 1.0
* @see <a href="http://www.openmhealth.org/documentation/#/schema-docs/schema-library/schemas/omh_speed">speed</a>
*/
@JsonInclude(NON_NULL)
@JsonNaming(SnakeCaseStrategy.class)
public class Speed extends Measure {

public static final SchemaId SCHEMA_ID = new SchemaId(OMH_NAMESPACE, "speed", "1.0");

private SpeedUnitValue speed;


@SerializationConstructor
protected Speed() {
}

public static class Builder
extends Measure.EffectiveTimeFrameBuilder<Speed, Builder> {

private SpeedUnitValue speed;

public Builder(SpeedUnitValue speed, TimeFrame effectiveTimeFrame) {

super(effectiveTimeFrame);

checkNotNull(speed, "A speed hasn't been specified.");
this.speed = speed;
}

public Builder(SpeedUnitValue speed, TimeInterval effectiveTimeInterval) {

super(effectiveTimeInterval);

checkNotNull(speed, "A speed hasn't been specified.");
this.speed = speed;
}

public Builder(SpeedUnitValue speed, OffsetDateTime effectiveDateTime) {

super(effectiveDateTime);

checkNotNull(speed, "A speed hasn't been specified.");
this.speed = speed;
}

@Override
public Speed build() {
return new Speed(this);
}
}

private Speed(Builder builder) {
super(builder);

this.speed = builder.speed;
}

public SpeedUnitValue getSpeed() {
return speed;
}

@Override
public SchemaId getSchemaId() {
return SCHEMA_ID;
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

if (!super.equals(o)) {
return false;
}

Speed speed1 = (Speed) o;

return Objects.equals(speed, speed1.speed);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), speed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.schema.domain.omh;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
* A unit of speed.
*
* @author Emerson Farrugia
* @version 1.0
* @see <a href="http://www.openmhealth.org/documentation/#/schema-docs/schema-library/schemas/omh_speed-unit-value">speed-unit-value</a>
*/
public enum SpeedUnit implements Unit {

METERS_PER_SECOND("m/s"),
KILOMETERS_PER_HOUR("km/h");

private String schemaValue;
private static final Map<String, SpeedUnit> constantsBySchemaValue = new HashMap<>();

static {
for (SpeedUnit constant : values()) {
constantsBySchemaValue.put(constant.getSchemaValue(), constant);
}
}

SpeedUnit(String schemaValue) {
this.schemaValue = schemaValue;
}

@Override
@JsonValue
public String getSchemaValue() {
return this.schemaValue;
}

@Nullable
@JsonCreator
public static SpeedUnit findBySchemaValue(String schemaValue) {
return constantsBySchemaValue.get(schemaValue);
}

public SpeedUnitValue newUnitValue(BigDecimal value) {
return new SpeedUnitValue(this, value);
}

public SpeedUnitValue newUnitValue(double value) {
return new SpeedUnitValue(this, value);
}

public SpeedUnitValue newUnitValue(long value) {
return new SpeedUnitValue(this, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.schema.domain.omh;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openmhealth.schema.serializer.SerializationConstructor;

import java.math.BigDecimal;


/**
* @author Emerson Farrugia
* @version 1.0
* @see <a href="http://www.openmhealth.org/documentation/#/schema-docs/schema-library/schemas/omh_speed-unit-value">speed-unit-value</a>
*/
public class SpeedUnitValue extends TypedUnitValue<SpeedUnit> {

public static final SchemaId SCHEMA_ID = new SchemaId(OMH_NAMESPACE, "speed-unit-value", "1.0");

@SerializationConstructor
protected SpeedUnitValue() {
}

@JsonCreator
public SpeedUnitValue(@JsonProperty("unit") SpeedUnit unit, @JsonProperty("value") BigDecimal value) {
super(unit, value);
}

@Override
public SchemaId getSchemaId() {
return SCHEMA_ID;
}

public SpeedUnitValue(SpeedUnit unit, double value) {
super(unit, value);
}

public SpeedUnitValue(SpeedUnit unit, long value) {
super(unit, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2015 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.schema.domain.omh;

import org.testng.annotations.Test;

import java.time.OffsetDateTime;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.openmhealth.schema.domain.omh.DescriptiveStatistic.AVERAGE;
import static org.openmhealth.schema.domain.omh.DescriptiveStatistic.MINIMUM;
import static org.openmhealth.schema.domain.omh.SpeedUnit.KILOMETERS_PER_HOUR;
import static org.openmhealth.schema.domain.omh.SpeedUnit.METERS_PER_SECOND;
import static org.openmhealth.schema.domain.omh.TimeFrameFactory.FIXED_MONTH;
import static org.openmhealth.schema.domain.omh.TimeFrameFactory.FIXED_POINT_IN_TIME;


/**
* @author Emerson Farrugia
*/
public class SpeedUnitTests extends SerializationUnitTests {

public static final String SCHEMA_FILENAME = "schema/omh/speed-1.0.json";

@Test(expectedExceptions = NullPointerException.class)
public void constructorShouldThrowExceptionOnUndefinedSpeed() {

new Speed.Builder(null, FIXED_MONTH);
}

@Test(expectedExceptions = NullPointerException.class)
public void constructorShouldThrowExceptionOnUndefinedEffectiveTimeFrame() {

new Speed.Builder(METERS_PER_SECOND.newUnitValue(15), (OffsetDateTime) null);
}

@Test
public void buildShouldConstructMeasureUsingOnlyRequiredProperties() {

SpeedUnitValue speedUnitValue = KILOMETERS_PER_HOUR.newUnitValue(10);

Speed speed = new Speed.Builder(speedUnitValue, FIXED_POINT_IN_TIME).build();

assertThat(speed, notNullValue());
assertThat(speed.getSpeed(), equalTo(speedUnitValue));
assertThat(speed.getEffectiveTimeFrame(), equalTo(FIXED_POINT_IN_TIME));
assertThat(speed.getDescriptiveStatistic(), nullValue());
assertThat(speed.getUserNotes(), nullValue());
}

@Test
public void buildShouldConstructMeasureUsingOptionalProperties() {

SpeedUnitValue speedUnitValue = KILOMETERS_PER_HOUR.newUnitValue(10);

Speed speed = new Speed.Builder(speedUnitValue, FIXED_POINT_IN_TIME)
.setDescriptiveStatistic(AVERAGE)
.setUserNotes("feeling fine")
.build();

assertThat(speed, notNullValue());
assertThat(speed.getSpeed(), equalTo(speedUnitValue));
assertThat(speed.getEffectiveTimeFrame(), equalTo(FIXED_POINT_IN_TIME));
assertThat(speed.getDescriptiveStatistic(), equalTo(AVERAGE));
assertThat(speed.getUserNotes(), equalTo("feeling fine"));
}

@Override
protected String getSchemaFilename() {
return SCHEMA_FILENAME;
}

@Test
public void measureShouldSerializeCorrectly() throws Exception {

Speed speed = new Speed.Builder(KILOMETERS_PER_HOUR.newUnitValue(10), FIXED_POINT_IN_TIME)
.setDescriptiveStatistic(MINIMUM)
.setUserNotes("feeling fine")
.build();

String document = "{\n" +
" \"speed\": {\n" +
" \"value\": 10,\n" +
" \"unit\": \"km/h\"\n" +
" },\n" +
" \"effective_time_frame\": {\n" +
" \"date_time\": \"2015-10-21T16:29:00-07:00\"\n" +
" },\n" +
" \"descriptive_statistic\": \"minimum\",\n" +
" \"user_notes\": \"feeling fine\"\n" +
"}";

serializationShouldCreateValidDocument(speed, document);
deserializationShouldCreateValidObject(document, speed);
}
}

0 comments on commit 0246c73

Please sign in to comment.