/*
 * 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.shim.ihealth.mapper;

import com.fasterxml.jackson.databind.JsonNode;
import org.hamcrest.Matchers;
import org.openmhealth.schema.domain.omh.DataPoint;
import org.openmhealth.schema.domain.omh.DurationUnitValue;
import org.openmhealth.schema.domain.omh.StepCount1;
import org.openmhealth.schema.domain.omh.TimeInterval;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;
import static org.openmhealth.schema.domain.omh.DataPointModality.SELF_REPORTED;
import static org.openmhealth.schema.domain.omh.DataPointModality.SENSED;
import static org.openmhealth.schema.domain.omh.DurationUnit.DAY;
import static org.openmhealth.schema.domain.omh.StepCount1.SCHEMA_ID;


/**
 * @author Chris Schaefbauer
 */
public class IHealthStepCountDataPointMapperUnitTests extends IHealthDataPointMapperUnitTests {

    private JsonNode responseNode;
    private IHealthStepCountDataPointMapper mapper = new IHealthStepCountDataPointMapper();
    private List<DataPoint<StepCount1>> dataPoints;


    @BeforeClass
    public void initializeResponseNode() {

        responseNode = asJsonNode("org/openmhealth/shim/ihealth/mapper/ihealth-activity.json");
    }

    @BeforeMethod
    public void initializeDataPoints() {

        dataPoints = mapper.asDataPoints(responseNode);
    }

    @Test
    public void asDataPointsShouldNotMapDataPointsWithZeroSteps() {

        JsonNode nodeWithNoSteps = asJsonNode("org/openmhealth/shim/ihealth/mapper/ihealth-activity-no-steps.json");

        assertThat(mapper.asDataPoints(nodeWithNoSteps), is(empty()));
    }

    @Test
    public void asDataPointsShouldReturnCorrectNumberOfDataPoints() {

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

    @Test
    public void asDataPointsShouldReturnCorrectDataPointsWhenSensed() {

        StepCount1 expectedStepCount = new StepCount1.Builder(21)
                .setEffectiveTimeFrame(
                        TimeInterval.ofStartDateTimeAndDuration(OffsetDateTime.parse("2015-11-16T00:00:00+05:00"),
                                new DurationUnitValue(DAY, 1)))
                .build();

        assertThat(dataPoints.get(0).getBody(), equalTo(expectedStepCount));

        testDataPointHeader(dataPoints.get(0).getHeader(), SCHEMA_ID, SENSED,
                "ac67c4ccf64af669d92569af85d19f59", OffsetDateTime.parse("2015-11-17T19:23:21Z"));
    }

    @Test
    public void asDataPointsShouldReturnDataPointWithUserNoteWhenNoteIsPresent() {

        StepCount1 expectedStepCount = new StepCount1.Builder(4398)
                .setEffectiveTimeFrame(
                        TimeInterval.ofStartDateTimeAndDuration(OffsetDateTime.parse("2015-11-18T00:00:00Z"),
                                new DurationUnitValue(DAY, 1))).setUserNotes("Great steps")
                .build();

        assertThat(dataPoints.get(1).getBody(), Matchers.equalTo(expectedStepCount));

        assertThat(dataPoints.get(0).getBody().getUserNotes(), nullValue());
        assertThat(dataPoints.get(1).getBody().getUserNotes(), equalTo("Great steps"));
    }

    @Test
    public void asDataPointsShouldReturnSensedDataPointWhenManuallyEntered() {

        assertThat(mapper.asDataPoints(responseNode).get(1).getHeader().getAcquisitionProvenance().getModality(),
                equalTo(SELF_REPORTED));
    }

    @Test
    public void asDataPointsShouldReturnEmptyListWhenEmptyIHealthResponse() {

        JsonNode emptyNode = asJsonNode("/org/openmhealth/shim/ihealth/mapper/ihealth-activity-empty.json");

        assertThat(mapper.asDataPoints(emptyNode), is(empty()));
    }
}

@@ -688,38 +688,8 @@
 de;%0A
-import org.hamcrest.Matchers;%0A
 impo
@@ -846,17 +846,17 @@
 tepCount
-1
+2
 ;%0Aimport
@@ -1044,52 +1044,65 @@
 ava.
+u
 ti
-me.OffsetDateTime;%0Aimport
+l.List;%0A%0Aimport static
  java.
-u
 ti
-l.List;%0A
+me.OffsetDateTime.parse;
 %0Aimp
@@ -1611,17 +1611,17 @@
 tepCount
-1
+2
 .SCHEMA_
@@ -1624,16 +1624,105 @@
 EMA_ID;%0A
+import static org.openmhealth.schema.domain.omh.TimeInterval.ofStartDateTimeAndDuration;%0A
 %0A%0A/**%0A *
@@ -2012,17 +2012,17 @@
 tepCount
-1
+2
 %3E%3E dataP
@@ -2849,114 +2849,45 @@
     
-StepCount1 expectedStepCount = new StepCount1.Builder(21)%0A                .setEffectiveTimeFrame(%0A        
+TimeInterval effectiveTimeInterval =%0A
     
@@ -2894,37 +2894,24 @@
             
-TimeInterval.
 ofStartDateT
@@ -2921,39 +2921,24 @@
 AndDuration(
-OffsetDateTime.
 parse(%222015-
@@ -2981,32 +2981,24 @@
             
-        
 new Duration
@@ -3015,16 +3015,106 @@
 DAY, 1))
+;%0A%0A        StepCount2 expectedStepCount = new StepCount2.Builder(21, effectiveTimeInterval
 )%0A      
@@ -3342,31 +3342,16 @@
 19f59%22, 
-OffsetDateTime.
 parse(%222
@@ -3487,116 +3487,45 @@
     
-StepCount1 expectedStepCount = new StepCount1.Builder(4398)%0A                .setEffectiveTimeFrame(%0A        
+TimeInterval effectiveTimeInterval =%0A
     
@@ -3536,29 +3536,16 @@
         
-TimeInterval.
 ofStartD
@@ -3563,31 +3563,16 @@
 uration(
-OffsetDateTime.
 parse(%222
@@ -3614,32 +3614,24 @@
             
-        
 new Duration
@@ -3648,17 +3648,126 @@
 DAY, 1))
-)
+;%0A%0A        StepCount2 expectedStepCount = new StepCount2.Builder(4398, effectiveTimeInterval)%0A                
 .setUser
@@ -3862,25 +3862,16 @@
 Body(), 
-Matchers.
 equalTo(

Update iHealth mapper tests to use StepCount2