tests/testthat/test-timeseriessummary.R

context("timeseriessummary tests")
wd <- getwd()
setwd(dir = tempdir())

context("testing timeseriessummary report")
test_that("timeseriessummary examples work", {
  library(jsonlite)
  library(whisker)
  library(readr)
  
  report1 <- renderReport(fromJSON(system.file('extdata',"timeseriessummary", "timeseriessummary-example.json",package = 'repgen')), "timeseriessummary", "author");
  renderedHtml1 <- read_file(report1)
  expect_is(report1, 'character')
  expect_equal(grep("<title>Time Series Summary - 01010000</title>", renderedHtml1), 1)
  
  report2 <- renderReport(fromJSON(system.file('extdata',"timeseriessummary", "timeseriessummary-example2.json",package = 'repgen')), "timeseriessummary", "author");
  renderedHtml2 <- read_file(report2)
  expect_is(report2, 'character')
  expect_equal(grep("<title>Time Series Summary - 01047200</title>", renderedHtml2), 1)
  
  report3 <- renderReport(fromJSON(system.file('extdata',"timeseriessummary", "timeseriessummary-example3.json",package = 'repgen')), "timeseriessummary", "author");
  renderedHtml3 <- read_file(report3)
  expect_is(report2, 'character')
  expect_equal(grep("<title>Time Series Summary - 01047200</title>", renderedHtml3), 1)
  
  report4 <- renderReport(fromJSON(system.file('extdata',"timeseriessummary", "timeseriessummary-example4.json",package = 'repgen')), "timeseriessummary", "author");
  renderedHtml4 <- read_file(report4)
  expect_is(report4, 'character')
  expect_equal(grep("<title>Time Series Summary - 01010000</title>", renderedHtml4), 1)
  
  report5 <- renderReport(fromJSON(system.file('extdata',"timeseriessummary", "timeseriessummary-example5.json",package = 'repgen')), "timeseriessummary", "author");
  renderedHtml5 <- read_file(report5)
  expect_is(report5, 'character')
  expect_equal(grep("<title>Time Series Summary - 01014000</title>", renderedHtml5), 1)
})

test_that('parseTSSRealtedSeries properly retrieves the related upchain series', {
  seriesJson <- fromJSON('{
    "upchainTs": [
      {
      "identifier": "Gage height.ft@01047200",
      "parameter": "Gage height",
      "parameterIdentifier": "Gage height",
      "nwisName": "Gage height",
      "nwisPcode": "00065",
      "unit": "ft",
      "computation": "Instantaneous",
      "timezone": "Etc/GMT+5",
      "inverted": false,
      "groundWater": false,
      "discharge": false,
      "sublocation": "",
      "timeSeriesType": "ProcessorDerived",
      "period": "Points",
      "publish": true,
      "primary": true,
      "uniqueId": "5eb2fdadf2784ebeaed2f64c6d02edf8"
      }
    ],
    "downchainTs": [
      {
      "identifier": "Discharge.ft^3/s.diff_per@01047200",
      "parameter": "Discharge",
      "parameterIdentifier": "Discharge",
      "nwisName": "Discharge",
      "nwisPcode": "00060",
      "unit": "ft^3/s",
      "computation": "Instantaneous",
      "timezone": "Etc/GMT+5",
      "inverted": false,
      "groundWater": false,
      "discharge": true,
      "sublocation": "",
      "timeSeriesType": "ProcessorDerived",
      "period": "Points",
      "publish": false,
      "primary": false,
      "uniqueId": "884fc0c281b14685baf9cbf744f0a606"
      }
    ]
  }')
  
  series <- repgen:::parseTSSRelatedSeries(seriesJson)
  nullSeries <- repgen:::parseTSSRelatedSeries(NULL)
  
  expect_equal(nullSeries, list())
  expect_is(series, 'data.frame')
  expect_equal(nrow(series), 1)
  expect_equal(series[['upchain']][[1]], 'Gage height.ft@01047200')
  expect_equal(series[['downchain']][[1]], "Discharge.ft^3/s.diff_per@01047200")
})

test_that('parseTSSQualifiers properly retrieves the qualifiers', {
  timezone <- "Etc/GMT+5"
  qualsJson <- fromJSON('{
    "primaryTsData": {
      "qualifiers": [
        {
        "startTime": "2017-03-05T18:45:00-05:00",
        "endTime": "2017-03-06T05:45:00.0000001-05:00",
        "identifier": "EQUIP",
        "code": "EQP",
        "displayName": "Equipment malfunction",
        "user": "system",
        "dateApplied": "2017-03-11T14:57:13.4625975Z"
        },
        {
        "startTime": "2017-02-26T01:30:00-05:00",
        "endTime": "2017-02-26T01:30:00.0000001-05:00",
        "identifier": "EQUIP",
        "code": "EQP",
        "displayName": "Equipment malfunction",
        "user": "system",
        "dateApplied": "2017-03-11T14:57:13.4625975Z"
        }
      ]
    }
  }')
  
  quals <- repgen:::parseTSSQualifiers(qualsJson, timezone)
  nullQuals <- repgen:::parseTSSQualifiers(NULL)
  
  expect_equal(nullQuals, list())
  expect_is(quals, 'data.frame')
  expect_equal(nrow(quals), 2)
  expect_equal(quals[1,][['startTime']], as.character(flexibleTimeParse('2017-02-26T01:30:00-05:00', timezone)))
  expect_equal(quals[1,][['endTime']], as.character(flexibleTimeParse("2017-02-26T01:30:00.0000001-05:00", timezone)))
  expect_equal(quals[1,][['value']], "EQP - Equipment malfunction")
  expect_equal(quals[1,][['code']], "EQP")
})

test_that('parseTSSNotes properly retrieves the notes', {
  timezone <- "Etc/GMT+5"
  notesJson <- fromJSON('{
    "primaryTsData": {
      "notes": [
        {
        "startTime": "2017-02-24T12:30:00-05:00",
        "endTime": "2017-02-24T14:00:00.0000001-05:00",
        "noteText": "ADAPS Source Flag: *"
        }
      ]
    }
  }')
  
  notes <- repgen:::parseTSSNotes(notesJson, timezone)
  nullNotes <- repgen:::parseTSSNotes(NULL)
  
  expect_equal(nullNotes, list())
  expect_is(notes, 'data.frame')
  expect_equal(length(notes[[1]]), 1)
  expect_equal(notes[['startTime']][[1]], as.character(flexibleTimeParse('2017-02-24T12:30:00-05:00', timezone)))
  expect_equal(notes[['endTime']][[1]], as.character(flexibleTimeParse("2017-02-24T14:00:00.0000001-05:00", timezone)))
  expect_equal(notes[['value']][[1]], "ADAPS Source Flag: *")
})

test_that('parseTSSGrades properly retrieves the grades', {
  timezone <- "Etc/GMT+5"
  gradesJson <- fromJSON('{
    "primaryTsData": {
     "grades": [
       {
       "startTime": "2016-05-01T00:00:00-05:00",
       "endTime": "2017-05-31T00:00:00.0000001-05:00",
       "gradeCode": "50"
       }
     ]
    },
    "reportMetadata": {
      "gradeMetadata": {
        "50": {
        "identifier": "50",
        "displayName": "DEFAULT",
        "description": "Default",
        "color": "#c8c8c8"
        }
      }
    }
  }')
  
  grades <- repgen:::parseTSSGrades(gradesJson, timezone)
  nullGrades <- repgen:::parseTSSGrades(NULL)
  
  expect_equal(nullGrades, list())
  expect_is(grades, 'data.frame')
  expect_equal(length(grades[[1]]), 1)
  expect_equal(grades[['startTime']], as.character(flexibleTimeParse('2016-05-01T00:00:00-05:00', timezone)))
  expect_equal(grades[['endTime']], as.character(flexibleTimeParse("2017-05-31T00:00:00.0000001-05:00", timezone)))
  expect_equal(grades[['value']], "50 Default")
})

test_that('parseTSSRatingCurves properly retrieves the rating curves', {
  timezone <- "Etc/GMT+5"
  curvesJson <- fromJSON('{
  "ratingCurves": [
    {
                         "id": "8.0",
                         "type": "LogarithmicTable",
                         "remarks": "Low-end change based on shifted 7.0. Medium range and high-end change and extension based on Qms 150-152.",
                         "inputParameter": {
                         "parameterName": "Gage height",
                         "parameterUnit": "ft"
                         },
                         "outputParameter": {
                         "parameterName": "Discharge",
                         "parameterUnit": "ft^3/s"
                         },
                         "periodsOfApplicability": [
                         {
                         "startTime": "2014-04-01T04:00:00.0000000Z",
                         "endTime": "2016-11-08T05:00:00.0000000Z",
                         "remarks": "Start new rating at beginning of ice-out rise."
                         },
                         {
                         "startTime": "2016-11-08T05:00:00.0000000Z",
                         "endTime": "2017-04-13T02:00:00.0000000Z",
                         "remarks": "Start new rating at beginning of ice-out rise."
                         }
                         ],
                         "shifts": [
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-04-07T17:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate on for scour to gage pool based on Qms 153-154."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 1.9,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-04-15T20:00:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on Qms 153-154."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.025
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-07-06T05:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate back to base rating based on Qm 155 over recession for fill in the gage pool."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.025
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-07-16T06:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on measurement 155 verifying the base rating."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-08-01T05:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate to aquatic growth shift based on measurements 156-157."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-08-14T07:00:00.0000000Z",
                         "endTime": "2014-08-22T12:35:01.0000000Z",
                         "remarks": "Based on measurements 156-157. Ended after control cleaned on 08/22."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.01
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-04-28T04:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate on over rise."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 0,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-05-09T07:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on measurements 164-170."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-09-13T21:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Hold on until 09/13 event."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-09-15T00:15:00.0000000Z",
                         "endTime": "2015-09-15T00:15:02.0000000Z",
                         "remarks": "Prorate back to base rating over rise based on Qms 171-172."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-04-02T04:00:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on Qms 175-182. Prorate from zero to full on 4/5/2016."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-04-05T04:00:00.0000000Z",
                         "endTime": "2016-11-08T19:02:00.0000000Z",
                         "remarks": "Based on Qms 175-182. Carried into next period."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-11-08T19:02:00.0000000Z",
                         "endTime": "2017-04-13T02:00:00.0000000Z",
                         "remarks": "Carried over from previous period. Ended when Rating 9.0 was started. JRC"
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         }
                         ],
                         "baseRatingTable": [
                         {
                         "inputValue": 1.22,
                         "outputValue": 0.54
                         },
                         {
                         "inputValue": 1.831,
                         "outputValue": 11.324
                         },
                         {
                         "inputValue": 2.062,
                         "outputValue": 19.302
                         },
                         {
                         "inputValue": 2.368,
                         "outputValue": 30.964
                         },
                         {
                         "inputValue": 2.664,
                         "outputValue": 42.756
                         },
                         {
                         "inputValue": 3.28,
                         "outputValue": 71.49
                         },
                         {
                         "inputValue": 4.11,
                         "outputValue": 114.36
                         },
                         {
                         "inputValue": 5.13,
                         "outputValue": 170.67
                         },
                         {
                         "inputValue": 7.93,
                         "outputValue": 340
                         },
                         {
                         "inputValue": 8.446,
                         "outputValue": 369.589
                         }
                         ],
                         "offsets": [
                         {
                         "offset": 0.95
                         }
                         ]
}
  ]
}')
  
  curves <- repgen:::parseTSSRatingCurves(curvesJson, timezone)
  nullCurves <- repgen:::parseTSSRatingCurves(list(), timezone)
  
  expect_equal(nullCurves, list())
  expect_is(curves, 'data.frame')
  expect_equal(nrow(curves), 2)
  expect_equal(curves[1,][['startTime']], '2014-03-31 23:00:00')
  expect_equal(curves[1,][['endTime']], "2016-11-08 00:00:00")
  expect_equal(curves[1,][['id']], "8.0")
})

test_that("parseTSSThresholds properly retrieves the threshold data", {
  timezone <- "Etc/GMT+5"
  thresholdJSON <- fromJSON('{
                            "reportMetadata": {
                            "timezone": "Etc/GMT+5"
                            },
                            "primaryTsMetadata": {
                              "thresholds": [
                              {
                              "name": "VERY HIGH",
                              "referenceCode": "AQUARIUS only",
                              "type": "ThresholdAbove",
                              "severity": 0,
                              "description": "Unspecified threshold value",
                              "periods": [
                              {
                              "startTime": "2000-01-01T00:00:00Z",
                              "endTime": "2015-05-31T23:59:59.9999999Z",
                              "appliedTime": "2016-03-10T02:53:07.8904293Z",
                              "referenceValue": 4000,
                              "suppressData": true
                              },
                              {
                              "startTime": "2015-06-02T00:00:00Z",
                              "endTime": "9999-05-31T23:59:59.9999999Z",
                              "appliedTime": "2016-03-10T02:53:07.8904293Z",
                              "referenceValue": 1234,
                              "suppressData": true
                              }
                              ]
                              },
                              {
                              "name": "VERY LOW",
                              "referenceCode": "AQUARIUS only",
                              "type": "ThresholdBelow",
                              "severity": 0,
                              "description": "Unspecified threshold value",
                              "periods": [
                              {
                              "startTime": "0001-01-01T00:00:00Z",
                              "endTime": "9999-12-31T23:59:59.9999999Z",
                              "appliedTime": "2016-03-10T02:53:08.1400229Z",
                              "referenceValue": 0,
                              "suppressData": true
                              }
                              ]
                              }
                              ]
                            }
  }')

  thresholds <- repgen:::parseTSSThresholds(thresholdJSON, timezone)
  nullThresholds <- repgen:::parseTSSThresholds(NULL)
  
  expect_equal(nullThresholds, list())
  expect_is(thresholds, 'data.frame')
  expect_equal(nrow(thresholds), 3)
  expect_equal(thresholds[1,][['type']], 'ThresholdAbove')
  expect_equal(thresholds[2,][['type']], 'ThresholdAbove')
  expect_equal(thresholds[3,][['type']], 'ThresholdBelow')
})

test_that('parseTSSRatingShifts data returns as expected', {
  timezone <- "Etc/GMT+5"
  reportData <- fromJSON('{
    "ratingShifts" : [
        {
          "curveNumber": "9",
          "shiftPoints": [
            0,
            0
          ],
          "stagePoints": [
            3.5,
            5
          ],
          "applicableStartDateTime": "2014-10-09T10:50:00.000-05:00",
          "applicableEndDateTime": "2015-10-09T10:50:00.000-05:00",
          "shiftNumber": 1
        }
      ]
  }')
  
  ratingShifts <- repgen:::parseTSSRatingShifts(reportData, timezone)
  nullShifts <- repgen:::parseTSSRatingShifts(NULL)
  
  expect_equal(nullShifts, list())
  expect_equal(length(ratingShifts$shiftPoints[[1]]), 2)
  expect_equal(length(ratingShifts$stagePoints[[1]]), 2)
  expect_equal(ratingShifts$curveNumber, "9")
  expect_equal(ratingShifts$shiftNumber, 1)
  expect_equal(ratingShifts$applicableStartDateTime, as.character(flexibleTimeParse("2014-10-09T10:50:00.000-05:00", timezone)))
})

test_that('formatDataTable properly formats a list or data frame into table rows to be rendered by whikser', {
  testDataFrame <- data.frame(testCol1 = c(1,2,3,4,5), testCol2=c(5,4,3,2,1))
  testDataList <- list(testCol1 = c(1,2,3,4,5), testCol2=c(5,4,3,2,1))
  
  testFrameRows <- repgen:::formatDataTable(testDataFrame)
  testListRows <- repgen:::formatDataTable(testDataList)
  nullRows <- repgen:::formatDataTable(NULL)
  
  expect_equal(nullRows, data.frame())
  expect_equal(testFrameRows, testListRows)
  expect_equal(length(testFrameRows), 5)
  expect_equal(testListRows[[3]][['testCol1']], 3)
  expect_equal(testListRows[[3]][['testCol2']], 3)
})

test_that('parseTSSGapTolerances properly retrieves the gap tolerances', {
  timezone <- "Etc/GMT+5"
  tolerancesJson <- fromJSON('{
    "primaryTsData": {
       "gapTolerances": [
         {
         "startTime": "2016-06-01T00:00:00-05:00",
         "endTime": "2017-06-03T00:00:00.0000001-05:00",
         "toleranceInMinutes": 120
         }
       ]
    }
  }')
  
  tolerances <- repgen:::parseTSSGapTolerances(tolerancesJson, timezone)
  nullTolerances <- repgen:::parseTSSGapTolerances(NULL, timezone)
  
  expect_equal(nullTolerances, NULL)
  expect_is(tolerances, 'data.frame')
  expect_equal(nrow(tolerances), 1)
  expect_equal(tolerances[1,][['startTime']], as.character(repgen:::flexibleTimeParse("2016-06-01T00:00:00-05:00", timezone)))
  expect_equal(tolerances[1,][['endTime']], as.character(repgen:::flexibleTimeParse("2017-06-03T00:00:00.0000001-05:00", timezone)))
  expect_equal(tolerances[1,][['toleranceInMinutes']], 120)
})

test_that('parseTSSApprovals properly retrieves the approvals', {
  timezone <- "Etc/GMT+5"
  approvalsJson <- fromJSON('{
    "primaryTsData": {
      "approvals": [
        {
          "approvalLevel": 1200,
          "levelDescription": "Approved",
          "comment": "",
          "dateAppliedUtc": "2017-02-02T21:16:24.937095Z",
          "startTime": "2007-10-01T00:00:00-05:00",
          "endTime": "2016-11-16T00:00:00-05:00"
        },
        {
          "approvalLevel": 900,
          "levelDescription": "Working",
          "comment": "",
          "dateAppliedUtc": "2017-02-02T21:15:49.5368596Z",
          "startTime": "2016-11-16T00:00:00-05:00",
          "endTime": "9999-12-31T23:59:59.9999999Z"
        }
      ]
    }
  }')
  
  approvals <- repgen:::parseTSSApprovals(approvalsJson, timezone)
  nullApprovals <- repgen:::parseTSSApprovals(NULL, timezone)
  
  expect_equal(nullApprovals, NULL)
  expect_is(approvals, 'data.frame')
  expect_equal(nrow(approvals), 2)
  expect_equal(approvals[1,][['startTime']], as.character(flexibleTimeParse('2007-10-01T00:00:00-05:00', timezone)))
  expect_equal(approvals[1,][['levelDescription']], "Approved")
})

test_that('parseTSSRatingCurves properly sorts the curves by startPeriod', {
  timezone <- "Etc/GMT+5"
  curvesJson <- fromJSON('{
    "ratingCurves": [
    {
                         "id": "8.0",
                         "type": "LogarithmicTable",
                         "remarks": "Low-end change based on shifted 7.0. Medium range and high-end change and extension based on Qms 150-152.",
                         "inputParameter": {
                         "parameterName": "Gage height",
                         "parameterUnit": "ft"
                         },
                         "outputParameter": {
                         "parameterName": "Discharge",
                         "parameterUnit": "ft^3/s"
                         },
                         "periodsOfApplicability": [
                         {
                         "startTime": "2014-04-01T04:00:00.0000000Z",
                         "endTime": "2016-11-08T05:00:00.0000000Z",
                         "remarks": "Start new rating at beginning of ice-out rise."
                         },
                         {
                         "startTime": "2016-11-08T05:00:00.0000000Z",
                         "endTime": "2017-04-13T02:00:00.0000000Z",
                         "remarks": "Start new rating at beginning of ice-out rise."
                         }
                         ],
                         "shifts": [
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-04-07T17:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate on for scour to gage pool based on Qms 153-154."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 1.9,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-04-15T20:00:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on Qms 153-154."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.025
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-07-06T05:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate back to base rating based on Qm 155 over recession for fill in the gage pool."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.025
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-07-16T06:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on measurement 155 verifying the base rating."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-08-01T05:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate to aquatic growth shift based on measurements 156-157."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2014-08-14T07:00:00.0000000Z",
                         "endTime": "2014-08-22T12:35:01.0000000Z",
                         "remarks": "Based on measurements 156-157. Ended after control cleaned on 08/22."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.01
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-04-28T04:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Prorate on over rise."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 0,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-05-09T07:30:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on measurements 164-170."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-09-13T21:15:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Hold on until 09/13 event."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2015-09-15T00:15:00.0000000Z",
                         "endTime": "2015-09-15T00:15:02.0000000Z",
                         "remarks": "Prorate back to base rating over rise based on Qms 171-172."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.6,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-04-02T04:00:00.0000000Z",
                         "endTime": "9999-12-31T23:59:59.9999999Z",
                         "remarks": "Based on Qms 175-182. Prorate from zero to full on 4/5/2016."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": 0
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-04-05T04:00:00.0000000Z",
                         "endTime": "2016-11-08T19:02:00.0000000Z",
                         "remarks": "Based on Qms 175-182. Carried into next period."
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         },
                         {
                         "periodOfApplicability": {
                         "startTime": "2016-11-08T19:02:00.0000000Z",
                         "endTime": "2017-04-13T02:00:00.0000000Z",
                         "remarks": "Carried over from previous period. Ended when Rating 9.0 was started. JRC"
                         },
                         "shiftPoints": [
                         {
                         "inputValue": 1.4,
                         "shift": -0.02
                         },
                         {
                         "inputValue": 2,
                         "shift": 0
                         }
                         ]
                         }
                         ],
                         "baseRatingTable": [
                         {
                         "inputValue": 1.22,
                         "outputValue": 0.54
                         },
                         {
                         "inputValue": 1.831,
                         "outputValue": 11.324
                         },
                         {
                         "inputValue": 2.062,
                         "outputValue": 19.302
                         },
                         {
                         "inputValue": 2.368,
                         "outputValue": 30.964
                         },
                         {
                         "inputValue": 2.664,
                         "outputValue": 42.756
                         },
                         {
                         "inputValue": 3.28,
                         "outputValue": 71.49
                         },
                         {
                         "inputValue": 4.11,
                         "outputValue": 114.36
                         },
                         {
                         "inputValue": 5.13,
                         "outputValue": 170.67
                         },
                         {
                         "inputValue": 7.93,
                         "outputValue": 340
                         },
                         {
                         "inputValue": 8.446,
                         "outputValue": 369.589
                         }
                         ],
                         "offsets": [
                         {
                         "offset": 0.95
                         }
                         ]
}
  ]
  }')
  
  curves <- repgen:::parseTSSRatingCurves(curvesJson, timezone)
  nullCurves <- repgen:::parseTSSRatingCurves(NULL, timezone)
  
  expect_equal(nullCurves, list())
  expect_equal(curves[1,][['startTime']], as.character("2014-03-31 23:00:00"))
  expect_equal(curves[2,][['startTime']], as.character("2016-11-08 00:00:00"))
})

test_that('parseTSSRatingShifts properly sorts the shifts by applicableStartDateTime', {
  timezone <- "Etc/GMT+5"
  shiftsJson <- fromJSON('{
                         "ratingShifts": [
    {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.03,
                         0.12,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.3,
                         7.1
                         ],
                         "applicableStartDateTime": "2015-10-06T16:06:01-05:00",
                         "applicableEndDateTime": "9999-12-31T23:59:59.9999999Z",
                         "shiftNumber": 0,
                         "shiftRemarks": "Continued WY2015 BRS from rating 6.1. ARC"
},
                         {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.03,
                         0.12,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.3,
                         7.1
                         ],
                         "applicableStartDateTime": "2016-03-09T13:00:00-05:00",
                         "applicableEndDateTime": "9999-12-31T23:59:59.9999999Z",
                         "shiftNumber": 0,
                         "shiftRemarks": "Continued WY2015 BRS from rating 6.1. ARC"
                         },
                         {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.02,
                         0.06,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.3,
                         7.1
                         ],
                         "applicableStartDateTime": "2016-03-10T23:00:00-05:00",
                         "applicableEndDateTime": "2016-11-16T08:10:00-05:00",
                         "shiftNumber": 0,
                         "shiftRemarks": "prorated to WY2016 BRS during the Mar. 9-10 ice-out event, based on measurements 102-107. ARC"
                         },
                         {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.02,
                         0.06,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.3,
                         7.1
                         ],
                         "applicableStartDateTime": "2016-11-16T08:10:00-05:00",
                         "applicableEndDateTime": "9999-12-31T23:59:59.9999999Z",
                         "shiftNumber": 0,
                         "shiftRemarks": "Continued BRS into the next period worked. ARC"
                         },
                         {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.02,
                         0.06,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.3,
                         7.1
                         ],
                         "applicableStartDateTime": "2017-02-25T22:30:00-05:00",
                         "applicableEndDateTime": "9999-12-31T23:59:59.9999999Z",
                         "shiftNumber": 0,
                         "shiftRemarks": "Continue BRS until ice-out. ARC"
                         },
                         {
                         "curveNumber": "6.2",
                         "shiftPoints": [
                         0.02,
                         0.12,
                         0
                         ],
                         "stagePoints": [
                         3.9,
                         5.8,
                         7.7
                         ],
                         "applicableStartDateTime": "2017-02-26T02:00:00-05:00",
                         "applicableEndDateTime": "9999-12-31T23:59:59.9999999Z",
                         "shiftNumber": 0,
                         "shiftRemarks": "Prorated to BRS#2 during the Feb. 25-26 ice-out event; based on measurements 113-115. ARC"
                         }
                         ]
}')
  
  shifts <- repgen:::parseTSSRatingShifts(shiftsJson, timezone)
  nullShifts <- repgen:::parseTSSRatingShifts(NULL, timezone)
  
  expect_equal(nullShifts, list())
  expect_equal(shifts[1,][['applicableStartDateTime']], as.character("2015-10-06 16:06:01"))
  expect_equal(shifts[2,][['applicableStartDateTime']], as.character("2016-03-09 13:00:00"))
  expect_equal(shifts[3,][['applicableStartDateTime']], as.character("2016-03-10 23:00:00"))
  expect_equal(shifts[4,][['applicableStartDateTime']], as.character("2016-11-16 08:10:00"))
  expect_equal(shifts[5,][['applicableStartDateTime']], as.character("2017-02-25 22:30:00"))
  expect_equal(shifts[6,][['applicableStartDateTime']], as.character("2017-02-26 02:00:00"))
  
})

test_that('parseTSSQualifiers properly sorts the qualifiers by startTime', {
  timezone <- "Etc/GMT+5"
  qualifiersJson <- fromJSON('{
                         "primaryTsData": {
                         "qualifiers": [
                         {
                         "startTime": "2017-02-26T01:30:00-05:00",
                         "endTime": "2017-02-26T01:30:00.0000001-05:00",
                         "identifier": "EQUIP",
                         "user": "system",
                         "dateApplied": "2017-03-11T14:57:13.4625975Z"
                         },
                         {
                         "startTime": "2017-03-05T18:45:00-05:00",
                         "endTime": "2017-03-06T05:45:00.0000001-05:00",
                         "identifier": "EQUIP",
                         "user": "system",
                         "dateApplied": "2017-03-11T14:57:13.4625975Z"
                         },
                         {
                         "startTime": "2016-11-23T00:00:00-05:00",
                         "endTime": "2016-11-26T12:00:00.0000001-05:00",
                         "identifier": "ESTIMATED",
                         "user": "acloutie",
                         "dateApplied": "2017-02-28T15:42:28.183755Z"
                         },
                         {
                         "startTime": "2016-11-29T12:00:00-05:00",
                         "endTime": "2017-02-23T12:00:00.0000001-05:00",
                         "identifier": "ESTIMATED",
                         "user": "acloutie",
                         "dateApplied": "2017-02-28T16:14:51.1790218Z"
                         },
                         {
                         "startTime": "2017-02-23T12:00:00-05:00",
                         "endTime": "2017-02-28T10:00:00.0000001-05:00",
                         "identifier": "ESTIMATED",
                         "user": "acloutie",
                         "dateApplied": "2017-02-28T20:05:45.8107059Z"
                         },
                         {
                         "startTime": "2017-02-28T10:15:00-05:00",
                         "endTime": "2017-04-03T00:00:00.0000001-05:00",
                         "identifier": "ICE",
                         "user": "lflight",
                         "dateApplied": "2017-03-01T12:53:18.2261003Z"
                         }
                         ]
                      },
                      "reportMetadata": {
                        "qualifierMetadata": {
                        "ESTIMATED": {
                          "identifier": "ESTIMATED",
                          "code": "E",
                          "displayName": "Estimated"
                        },
                        "EQUIP": {
                          "identifier": "EQUIP",
                          "code": "EQP",
                          "displayName": "Equipment malfunction"
                        },
                        "ICE": {
                          "identifier": "ICE",
                          "code": "ICE",
                          "displayName": "Flow at station affected by ice"
                        }
                      }
}
}')
  
  qualifiers <- repgen:::parseTSSQualifiers(qualifiersJson, timezone)
  nullQualifiers <- repgen:::parseTSSQualifiers(NULL, timezone)
  
  expect_equal(nullQualifiers, list())
  expect_equal(qualifiers[1,][['startTime']], as.character("2016-11-23 00:00:00"))
  expect_equal(qualifiers[2,][['startTime']], as.character("2016-11-29 12:00:00"))
  expect_equal(qualifiers[3,][['startTime']], as.character("2017-02-23 12:00:00"))
  expect_equal(qualifiers[4,][['startTime']], as.character("2017-02-26 01:30:00"))
  expect_equal(qualifiers[5,][['startTime']], as.character("2017-02-28 10:15:00"))
  expect_equal(qualifiers[6,][['startTime']], as.character("2017-03-05 18:45:00"))
})


test_that('parseTSSNotes properly sorts the notes by startTime', {
  timezone <- "Etc/GMT+5"
  notesJson <- fromJSON('{
                             "primaryTsData": {
                              "notes": [
                                {
                                 "startTime": "2017-01-01T00:00:00-05:00",
                                 "endTime": "2017-01-04T12:00:00.0000001-05:00",
                                 "noteText": "note creator/user test (lflight)"
                                },
                                {
                                  "startTime": "2016-01-01T00:00:00-05:00",
                                  "endTime": "2016-01-04T12:00:00.0000001-05:00",
                                  "noteText": "note creator/user test (lflight)"
                                },
                                {
                                "startTime": "2012-01-01T00:00:00-05:00",
                                "endTime": "2012-01-04T12:00:00.0000001-05:00",
                                "noteText": "note creator/user test (lflight)"
                                }
                              ]
                             }
}')
  
  notes <- repgen:::parseTSSNotes(notesJson, timezone)
  nullNotes <- repgen:::parseTSSNotes(NULL, timezone)
  
  expect_equal(nullNotes, list())
  expect_equal(notes[1,][['startTime']], as.character("2012-01-01 00:00:00"))
  expect_equal(notes[2,][['startTime']], as.character("2016-01-01 00:00:00"))
  expect_equal(notes[3,][['startTime']], as.character("2017-01-01 00:00:00"))
})

test_that('parseTSSGrades properly sorts the grades by startDate', {
  timezone <- "Etc/GMT+5"
  gradesJson <- fromJSON('{
                        "primaryTsData": {
                        "grades": [
                        {
                          "startTime": "2016-10-01T00:00:00-05:00",
                          "endTime": "2017-06-07T00:00:00.0000001-05:00",
                          "gradeCode": "50"
                        },
                        {
                          "startTime": "2015-04-01T00:00:00-05:00",
                          "endTime": "2015-06-07T00:00:00.0000001-05:00",
                          "gradeCode": "99"
                        },
                        {
                          "startTime": "2011-05-01T00:00:00-05:00",
                          "endTime": "2011-06-07T00:00:00.0000001-05:00",
                          "gradeCode": "4"
                        }
                        ]
                        },
                        "reportMetadata": {
                          "gradeMetadata": {
                            "50": {
                              "identifier": "50",
                              "displayName": "DEFAULT",
                              "description": "Default",
                              "color": "#c8c8c8"
                            },
                            "99": {
                              "identifier": "99",
                              "displayName": "IV VERIFICATION (USER)",
                              "description": "IV verified by USGS, no comparison",
                              "color": "#191919"
                            },
                            "4": {
                              "identifier": "4",
                              "displayName": "PARTIAL",
                              "description": "Incomplete or Partial Aggregated Record",
                              "color": "#ff915b"
                            }
                          }
                        }
}')
  
  grades <- repgen:::parseTSSGrades(gradesJson, timezone)
  nullGrades <- repgen:::parseTSSGrades(NULL, timezone)
  
  expect_equal(nullGrades, list())
  expect_equal(grades[1,][['startTime']], as.character("2011-05-01 00:00:00"))
  expect_equal(grades[2,][['startTime']], as.character("2015-04-01 00:00:00"))
  expect_equal(grades[3,][['startTime']], as.character("2016-10-01 00:00:00"))
})

test_that('parseTSSProcessingCorrections properly sorts the corrections by startTime', {
  timezone <- "Etc/GMT+5"
  correctionsJson <- fromJSON('{
        "corrections": {
                "normal": [
                         {
                         "appliedTimeUtc": "2017-05-31T21:54:23.0717097Z",
                         "startTime": "2017-05-31T08:27:31.0328297-05:00",
                         "comment": "Delete region // Erroneous value during orifice swap. MEB",
                         "endTime": "2017-05-31T08:31:16-05:00",
                         "dominantType": "DeleteRegion",
                         "type": "DeleteRegion",  
                         "user": "mbeardsley",
                         "processingOrder": "NORMAL"
                         },
                         {
                         "appliedTimeUtc": "2017-03-06T11:38:52.742006Z",
                         "startTime": "2016-08-04T12:45:00-05:00",
                         "comment": "Delete Region - Sensor reading during purge.",
                         "endTime": "2016-08-04T12:45:00.0000001-05:00",
                         "dominantType": "DeleteRegion",
                         "type": "DeleteRegion",
                         "user": "jkinsey",
                         "processingOrder": "NORMAL"
                         }
                         ],
              "postProcessing": [
                         {
                         "appliedTimeUtc": "2017-03-10T13:21:08.1047147Z",
                         "startTime": "2017-03-01T17:00:00-05:00",
                         "comment": "Offset Correction with value of 7.430ft // for sheered orifice line. JMK // Ended when orifice read offset (0.45+7.43=7.88).",
                         "endTime": "2017-03-11T06:30:00.0000001-05:00",
                         "dominantType": "Offset",
                         "type": "Offset",
                         "parameters": {
                            "Offset": 7.430
                          },
                         "user": "jkinsey",
                         "processingOrder": "POST_PROCESSING"
                         }
                         ],
              "preProcessing": [
                         {
                         "appliedTimeUtc": "2017-01-30T12:03:38.1722106Z",
                         "startTime": "2016-07-07T16:15:00-05:00",
                         "endTime": "2016-07-07T16:15:00.0000001-05:00",
                         "dominantType": "DeleteRegion",
                         "type": "DeleteRegion",
                         "user": "admin",
                         "processingOrder": "PRE_PROCESSING"
                         },
                         {
                         "appliedTimeUtc": "2017-03-06T11:34:17.8751927Z",
                         "startTime": "2016-08-04T10:30:00-05:00",
                         "comment": "Copy and Paste from Gage height.ft.EDL@01034500 - JMK",
                         "endTime": "2016-08-04T11:30:00.0000001-05:00",
                         "dominantType": "CopyPaste",
                         "type": "CopyPaste",
                         "user": "jkinsey",
                         "processingOrder": "PRE_PROCESSING"
                         },
                         {
                         "appliedTimeUtc": "2017-03-06T11:20:48.140056Z",
                         "startTime": "2016-09-12T08:30:00-05:00",
                         "comment": "Copy and Paste from Gage height.ft.EDL@01034500 - JMK",
                         "endTime": "2016-09-12T11:15:00.0000001-05:00",
                         "dominantType": "CopyPaste",
                         "type": "CopyPaste",
                         "user": "jkinsey",
                         "processingOrder": "PRE_PROCESSING"
                         },
                         {
                         "appliedTimeUtc": "2017-03-06T11:43:07.4928065Z",
                         "startTime": "2016-11-14T11:30:00-05:00",
                         "comment": "Copy and Paste from Gage height.ft.EDL@01034500 - JMK",
                         "endTime": "2016-11-14T12:15:00.0000001-05:00",
                         "dominantType": "CopyPaste",
                         "type": "CopyPaste",
                         "user": "jkinsey",
                         "processingOrder": "PRE_PROCESSING"
                         },
                         {
                         "appliedTimeUtc": "2017-03-06T11:45:03.8824656Z",
                         "startTime": "2016-12-29T22:30:00-05:00",
                         "comment": "Copy and Paste from Gage height.ft.EDL@01034500 - JMK",
                         "endTime": "2016-12-29T23:45:00.0000001-05:00",
                         "dominantType": "CopyPaste",
                         "type": "CopyPaste",
                         "user": "jkinsey",
                         "processingOrder": "PRE_PROCESSING"
                         },
                         {
                         "appliedTimeUtc": "2017-03-18T11:54:14.8686833Z",
                         "startTime": "2017-03-11T06:00:00-05:00",
                         "comment": "Orifice out of water, as orifice line damaged by ice. ARC // Delete Region",
                         "endTime": "2017-03-17T14:15:00.0000001-05:00",
                         "dominantType": "DeleteRegion",
                         "type": "DeleteRegion",
                         "user": "acloutie",
                         "processingOrder": "PRE_PROCESSING"
                         }
                         ]
        }
  }')
  
  corrections <- repgen:::parseTSSProcessingCorrections(correctionsJson, "pre", timezone)
  nullCorrs <- repgen:::parseTSSProcessingCorrections(NULL, "pre", timezone)
  nullCorrs2 <- repgen:::parseTSSProcessingCorrections(correctionsJson, "invalid", timezone)
  
  expect_equal(nullCorrs, NULL)
  expect_equal(nullCorrs2, NULL)
  expect_equal(corrections[1,][['startTime']], as.character("2016-07-07 16:15:00"))
  expect_equal(corrections[2,][['startTime']], as.character("2016-08-04 10:30:00"))
  expect_equal(corrections[3,][['startTime']], as.character("2016-09-12 08:30:00"))
  expect_equal(corrections[4,][['startTime']], as.character("2016-11-14 11:30:00"))
  expect_equal(corrections[5,][['startTime']], as.character("2016-12-29 22:30:00"))
  expect_equal(corrections[6,][['startTime']], as.character("2017-03-11 06:00:00"))
  
})

test_that('parseTSSGaps properly sorts the gaps by startTime', {
  timezone <- "Etc/GMT+5"
  gapJson <- fromJSON('{
    "primaryTsData":{
      "gaps": [
        {
        "startTime": "2016-11-23T00:00:00-05:00",
        "endTime": "2016-11-23T12:00:00-05:00"
        },
        {
        "startTime": "2016-11-24T12:00:00-05:00",
        "endTime": "2016-11-25T00:00:00-05:00"
        },
        {
        "startTime": "2016-11-21T12:00:00-05:00",
        "endTime": "2016-11-22T00:00:00-05:00"
        }
      ]
    }
  }')
  
  gaps <- repgen:::parseTSSGaps(gapJson, timezone, isStatDerived=FALSE)
  nullGaps <- repgen:::parseTSSGaps(NULL, timezone)
  
  expect_equal(nullGaps, NULL)
  expect_equal(gaps[1,][['startTime']], as.character("2016-11-21 12:00:00"))
  expect_equal(gaps[2,][['startTime']], as.character("2016-11-23 00:00:00"))
  expect_equal(gaps[3,][['startTime']], as.character("2016-11-24 12:00:00"))
})

test_that('parseTSSApprovals properly sorts the approvals by startTime', {
  timezone <- "Etc/GMT+5"
  approvalsJson <- fromJSON('{
    "primaryTsData": {
      "approvals": [
        {
        "approvalLevel": 1200,
        "levelDescription": "Approved",
        "comment": "",
        "dateAppliedUtc": "2017-02-02T21:16:24.937095Z",
        "startTime": "2007-10-01T00:00:00-05:00",
        "endTime": "2016-11-16T00:00:00-05:00"
        },
        {
        "approvalLevel": 900,
        "levelDescription": "Working",
        "comment": "",
        "dateAppliedUtc": "2017-02-02T21:15:49.5368596Z",
        "startTime": "2016-11-16T00:00:00-05:00",
        "endTime": "9999-12-31T23:59:59.9999999Z"
        }
      ]
    }
  }')
  
  approvals <- repgen:::parseTSSApprovals(approvalsJson, timezone)
  expect_equal(approvals[1,][['startTime']], as.character("2007-10-01 00:00:00"))
  expect_equal(approvals[2,][['startTime']], as.character("2016-11-16 00:00:00"))
})

test_that('parseTSSGapTolerances properly sorts the gapTolerances by startTime', {
  timezone <- "Etc/GMT+5"
  gapTolerancesJson <- fromJSON('{
    "primaryTsData":{
      "gapTolerances": [
        {
            "startTime": "2016-06-01T00:00:00-05:00",
            "endTime": "2017-06-07T00:00:00.0000001-05:00",
            "toleranceInMinutes": 120
        },
        {
          "startTime": "2014-06-01T00:00:00-05:00",
          "endTime": "2015-06-07T00:00:00.0000001-05:00",
          "toleranceInMinutes": 120
        },
            {
          "startTime": "2011-06-01T00:00:00-05:00",
          "endTime": "2013-06-07T00:00:00.0000001-05:00",
          "toleranceInMinutes": 120
        }
      ]
    }
  }')
  
  gapTolerances <- repgen:::parseTSSGapTolerances(gapTolerancesJson, timezone)
  nullGapTolerances <- repgen:::parseTSSGapTolerances(NULL, timezone)
  
  expect_equal(nullGapTolerances, NULL)
  expect_equal(gapTolerances[1,][['startTime']], as.character("2011-06-01 00:00:00"))
  expect_equal(gapTolerances[2,][['startTime']], as.character("2014-06-01 00:00:00"))
  expect_equal(gapTolerances[3,][['startTime']], as.character("2016-06-01 00:00:00"))
})

test_that('parseTSSPrimaryTsMetadata properly retrieves the primary TS metadata', {
  metadataJson <- fromJSON('{
                           "primaryTsMetadata": {
                           "identifier": "Gage height.ft.(New site WY2011)@01014000",
                           "computationPeriodIdentifier": "Points",
                           "utcOffset": -5,
                           "timezone": "Etc/GMT+5",
                           "description": "DD019,(New site WY2011),00065,ft,DCP",
                           "timeSeriesType": "ProcessorDerived",
                           "extendedAttributes": [
                            {
                           "name": "ACCESS_LEVEL",
                           "type": "String",
                           "value": "0-Public"
                            },
                           {
                           "name": "PLOT_MEAS",
                           "type": "String",
                           "value": "Y"
                           },
                           {
                           "name": "DATA_GAP",
                           "type": "Decimal"
                           },
                           {
                           "name": "ACTIVE_FLAG",
                           "type": "String",
                           "value": "Y"
                           },
                           {
                           "name": "WEB_DESCRIPTION",
                           "type": "String"
                           },
                           {
                           "name": "STAT_BEGIN_YEAR",
                           "type": "String"
                           },
                           {
                           "name": "ADAPS_DD",
                           "type": "Decimal",
                           "value": 2
                           },
                           {
                           "name": "PRIMARY_FLAG",
                           "type": "String",
                           "value": "Primary"
                           },
                           {
                           "name": "TRANSPORT_CODE",
                           "type": "String"
                           },
                           {
                           "name": "SPECIAL_DATA_TYPE",
                           "type": "String"
                           }
                           ],
                           "computationIdentifier": "Instantaneous",
                           "unit": "ft",
                           "nwisName": "Gage height",
                           "parameter": "Gage height",
                           "publish": true,
                           "subLocationIdentifier": "",
                           "comment": "",
                           "parameterIdentifier": "Gage height",
                           "uniqueId": "c289a526bea1493bb33ee6e8dd389b92",
                           "nwisPcode": "00065"
                           }
  }')
  
  metadata <- repgen:::parseTSSPrimaryTsMetadata(metadataJson)
  nullMetadata <- repgen:::parseTSSPrimaryTsMetadata(NULL)
  
  expect_equal(nullMetadata, NULL)
  expect_equal(metadata[['computationPeriodIdentifier']], "Points")
  expect_equal(metadata[['unit']], "ft")
  expect_equal(metadata[['publish']], TRUE)
  expect_equal(metadata[['parameter']], "Gage height")
  expect_is(metadata[['extendedAttributes']], 'data.frame')
})

test_that('parseTSSMethods properly retrieves the primary ts methods', {
  timezone <- "Etc/GMT+5"
  methodsJson <- fromJSON('{
                          "primaryTsData": {
                          "methods": [
                          {
                          "methodCode": "DefaultNone",
                          "startTime": "2016-06-01T00:00:00-05:00",
                          "endTime": "2017-06-22T00:00:00.0000001-05:00"
                          }
                          ]
                          }
  }')
  
  methods <- repgen:::parseTSSMethods(methodsJson, timezone)
  nullMethods <- repgen:::parseTSSMethods(NULL, timezone)
  
  expect_equal(nullMethods, NULL)
  expect_equal(methods[['methodCode']], "DefaultNone")
  expect_equal(methods[['startTime']], as.character(repgen:::flexibleTimeParse("2016-06-01T00:00:00-05:00", timezone)))
  expect_equal(methods[['endTime']], as.character(repgen:::flexibleTimeParse("2017-06-22T00:00:00.0000001-05:00", timezone)))
})

test_that('parseTSSInterpolationTypes properly retrieves the primary ts interpolation types', {
  timezone <- "Etc/GMT+5"
  itsJson <- fromJSON('{
                      "primaryTsData": {
                      "interpolationTypes": [
                      {
                      "type": "InstantaneousValues",
                      "startTime": "2016-06-01T00:00:00-05:00",
                      "endTime": "2017-06-22T00:00:00.0000001-05:00"
                      }
                      ]
                      }
  }')
  
  its <- repgen:::parseTSSInterpolationTypes(itsJson, timezone)
  nullIts <- repgen:::parseTSSInterpolationTypes(NULL, timezone)
  
  expect_equal(nullIts, NULL)
  expect_equal(its[['type']], "InstantaneousValues")
  expect_equal(its[['startTime']], as.character(repgen:::flexibleTimeParse("2016-06-01T00:00:00-05:00", timezone)))
  expect_equal(its[['endTime']], as.character(repgen:::flexibleTimeParse("2017-06-22T00:00:00.0000001-05:00", timezone)))
})

test_that('parseTSSProcessors properly retrieves the primary ts processors', {
  timezone <- "Etc/GMT+5"
  procsJson <- fromJSON('{
                        "primaryTsData": {
                        "processors": [
                          {
                          "processorType": "statistics",
                          "inputTimeSeriesUniqueIds": [
                            "fd49bf3b90ed4e399070ac4ab1548adb"
                          ],
                          "outputTimeSeriesUniqueId": "bc1b655bad944693af5e46654390f5a4",
                          "processorPeriod": {
                            "startTime": "0001-01-01T00:00:00.0000000Z",
                            "endTime": "9999-12-31T23:59:59.9999999Z"
                          },
                          "settings": {
                            "TimeStep": "Daily",
                            "MinimumCoverageRequiredForPartialGradeAsPercent": "100",
                            "NewValueLocation": "End",
                            "TimeStepCount": "1",
                            "StatisticType": "Mean",
                            "RequireMinimumCoverage": "true",
                            "PartialCoverageGrade": "4",
                            "StartTimeOffsetInMinutes": "0"
                          }
                        }
  ]
                        }
  }')
  
  procs <- repgen:::parseTSSProcessors(procsJson, timezone)
  nullProcs <- repgen:::parseTSSProcessors(NULL, timezone)

  expect_equal(nullProcs, NULL)
  expect_equal(procs[['processorType']], "statistics")
  expect_equal(procs[['processorPeriod']][['startTime']], "Open")
  expect_equal(procs[['processorPeriod']][['endTime']], "Open")
})

test_that('constructTSDetails properly constructs the two tables for the TSS details section', {
  timezone <- "Etc/GMT+5"
  reportData <- fromJSON('{
      "primaryTsData":{
        "notes": [],
        "methods": [
           {
           "methodCode": "DefaultNone",
           "startTime": "2016-06-01T00:00:00-05:00",
           "endTime": "2017-06-22T00:00:00.0000001-05:00"
           }
         ],
         "approvals": [
           {
           "approvalLevel": 1100,
           "levelDescription": "Analyzed",
           "comment": "Approval changed to Analyzed by lflight.",
           "user": "lflight",
           "dateAppliedUtc": "2017-03-30T11:50:39.664749Z",
           "startTime": "2016-01-01T00:00:00-05:00",
           "endTime": "2016-10-06T05:15:00-05:00"
           },
           {
           "approvalLevel": 900,
           "levelDescription": "Working",
           "comment": "",
           "user": "admin",
           "dateAppliedUtc": "2017-02-02T21:00:53.1287246Z",
           "startTime": "2016-10-06T05:15:00-05:00",
           "endTime": "9999-12-31T23:59:59.9999999Z"
           }
         ],
         "qualifiers": [],
         "grades": [
           {
           "startTime": "2016-06-01T00:00:00-05:00",
           "endTime": "2017-06-22T00:00:00.0000001-05:00",
           "gradeCode": "50"
           }
         ],
         "processors": [
           {
           "processorType": "correctedpassthrough",
           "inputTimeSeriesUniqueIds": [
           "bf943ea7d46e4b30853d5ce0dcd90410"
           ],
           "outputTimeSeriesUniqueId": "c289a526bea1493bb33ee6e8dd389b92",
           "processorPeriod": {
              "startTime": "0001-01-01T00:00:00Z",
              "endTime": "9999-12-31T23:59:59.9999999Z"
            }
           }
         ],
         "gaps": [
           {
           "startTime": "2016-12-01T14:30:00-05:00",
           "endTime": "2016-12-02T08:45:00-05:00",
           "durationInHours": 18.25,
           "gapExtent": "CONTAINED"
           },
           {
           "startTime": "2016-12-02T22:30:00-05:00",
           "endTime": "2016-12-03T12:30:00-05:00",
           "durationInHours": 14,
           "gapExtent": "CONTAINED"
           },
           {
           "startTime": "2017-04-21T21:30:00-05:00",
           "endTime": "2017-04-21T23:45:00-05:00",
           "durationInHours": 2.25,
           "gapExtent": "CONTAINED"
           }
         ],
         "gapTolerances": [
           {
           "startTime": "2016-06-01T00:00:00-05:00",
           "endTime": "2017-06-22T00:00:00.0000001-05:00",
           "toleranceInMinutes": 120
           }
         ],
         "interpolationTypes": [
           {
           "type": "InstantaneousValues",
           "startTime": "2016-06-01T00:00:00-05:00",
           "endTime": "2017-06-22T00:00:00.0000001-05:00"
           },
           {
             "type": "DailyValues",
             "startTime": "2016-06-23T00:00:00-05:00",
             "endTime": "2017-06-25T00:00:00.0000001-05:00"
           }
         ]
      },
      "primaryTsMetadata": {
        "identifier": "Gage height.ft.(New site WY2011)@01014000",
        "computationPeriodIdentifier": "Points",
        "utcOffset": -5,
        "timezone": "Etc/GMT+5",
        "description": "DD019,(New site WY2011),00065,ft,DCP",
        "timeSeriesType": "ProcessorDerived",
        "extendedAttributes": [
          {
           "name": "ACCESS_LEVEL",
           "type": "String",
           "value": "0-Public"
          },
         {
           "name": "PLOT_MEAS",
           "type": "String",
           "value": "Y"
         },
         {
           "name": "DATA_GAP",
           "type": "Decimal",
           "value": 72
         },
         {
           "name": "ACTIVE_FLAG",
           "type": "String",
           "value": "Y"
         },
         {
           "name": "WEB_DESCRIPTION",
           "type": "String"
         },
         {
           "name": "STAT_BEGIN_YEAR",
           "type": "String"
         },
         {
           "name": "ADAPS_DD",
           "type": "Decimal",
           "value": 19
         },
         {
         "name": "PRIMARY_FLAG",
         "type": "String",
         "value": "Primary"
         },
         {
         "name": "TRANSPORT_CODE",
         "type": "String"
         },
         {
         "name": "SPECIAL_DATA_TYPE",
         "type": "String"
         }
         ],
        "computationIdentifier": "Instantaneous",
        "unit": "ft",
        "nwisName": "Gage height",
        "parameter": "Gage height",
        "publish": true,
        "subLocationIdentifier": "",
        "comment": "",
        "parameterIdentifier": "Gage height",
        "uniqueId": "c289a526bea1493bb33ee6e8dd389b92",
        "nwisPcode": "00065"
      },
        "reportMetadata": {
          "gradeMetadata": {
            "50": {
              "identifier": "50",
              "displayName": "DEFAULT",
              "description": "Default",
              "color": "#c8c8c8"
            }
          }
        }
  }')
  
  reportData2 <- fromJSON('{
      "primaryTsData":{
        "notes": [],
        "methods": [],
        "approvals": [
          {
          "approvalLevel": 1100,
          "levelDescription": "Analyzed",
          "comment": "Approval changed to Analyzed by lflight.",
          "user": "lflight",
          "dateAppliedUtc": "2017-03-30T11:50:39.664749Z",
          "startTime": "2016-01-01T00:00:00-05:00",
          "endTime": "2016-10-06T05:15:00-05:00"
          },
          {
          "approvalLevel": 900,
          "levelDescription": "Working",
          "comment": "",
          "user": "admin",
          "dateAppliedUtc": "2017-02-02T21:00:53.1287246Z",
          "startTime": "2016-10-06T05:15:00-05:00",
          "endTime": "9999-12-31T23:59:59.9999999Z"
          }
        ],
        "qualifiers": [],
        "grades": [
          {
          "startTime": "2016-06-01T00:00:00-05:00",
          "endTime": "2017-06-22T00:00:00.0000001-05:00",
          "gradeCode": "50"
          }
        ],
        "processors": [],
        "gaps": [
          {
          "startTime": "2016-12-01T14:30:00-05:00",
          "endTime": "2016-12-02T08:45:00-05:00",
          "durationInHours": 18.25,
          "gapExtent": "CONTAINED"
          },
          {
          "startTime": "2016-12-02T22:30:00-05:00",
          "endTime": "2016-12-03T12:30:00-05:00",
          "durationInHours": 14,
          "gapExtent": "CONTAINED"
          },
          {
          "startTime": "2017-04-21T21:30:00-05:00",
          "endTime": "2017-04-21T23:45:00-05:00",
          "durationInHours": 2.25,
          "gapExtent": "CONTAINED"
          }
        ],
        "gapTolerances": [
          {
          "startTime": "2016-06-01T00:00:00-05:00",
          "endTime": "2017-06-22T00:00:00.0000001-05:00",
          "toleranceInMinutes": 120
          }
          ],
        "interpolationTypes": []
      },
      "primaryTsMetadata": {
        "identifier": "Gage height.ft.(New site WY2011)@01014000",
        "computationPeriodIdentifier": "Points",
        "utcOffset": -5,
        "timezone": "Etc/GMT+5",
        "description": "DD019,(New site WY2011),00065,ft,DCP",
        "timeSeriesType": "ProcessorDerived",
        "extendedAttributes": [
          {
          "name": "ACCESS_LEVEL",
          "type": "String"
          },
          {
          "name": "PLOT_MEAS",
          "type": "String"
          },
          {
          "name": "DATA_GAP",
          "type": "Decimal"
          },
          {
          "name": "ACTIVE_FLAG",
          "type": "String"
          },
          {
          "name": "WEB_DESCRIPTION",
          "type": "String"
          },
          {
          "name": "STAT_BEGIN_YEAR",
          "type": "String"
          },
          {
          "name": "ADAPS_DD",
          "type": "Decimal"
          },
          {
          "name": "PRIMARY_FLAG",
          "type": "String"
          },
          {
          "name": "TRANSPORT_CODE",
          "type": "String"
          },
          {
          "name": "SPECIAL_DATA_TYPE",
          "type": "String"
          }
        ],
        "computationIdentifier": "Instantaneous",
        "unit": "ft",
        "nwisName": "Gage height",
        "parameter": "Gage height",
        "subLocationIdentifier": "",
        "comment": "",
        "publish": true,
        "parameterIdentifier": "Gage height",
        "uniqueId": "c289a526bea1493bb33ee6e8dd389b92",
        "nwisPcode": "00065"
      },
        "reportMetadata": {
          "gradeMetadata": {
            "50": {
              "identifier": "50",
              "displayName": "DEFAULT",
              "description": "Default",
              "color": "#c8c8c8"
            }
          }
        }
  }')
  
  tsDetails <- repgen:::constructTSDetails(reportData, timezone)
  tsDetails2 <- repgen:::constructTSDetails(reportData2, timezone)
  
  expect_is(tsDetails[['tsAttrs']], 'data.frame')
  expect_is(tsDetails[['tsExtAttrs']], 'data.frame')
  expect_equal(length(tsDetails), 3)
  expect_equal(tsDetails[['changeNote']], TRUE)
  expect_equal(nrow(tsDetails[['tsAttrs']]), 16)
  expect_equal(nrow(tsDetails[['tsExtAttrs']]), 10)
  
  expect_is(tsDetails2[['tsAttrs']], 'data.frame')
  expect_is(tsDetails2[['tsExtAttrs']], 'data.frame')
  expect_equal(length(tsDetails2), 3)
  expect_equal(tsDetails2[['changeNote']], FALSE)
  expect_equal(nrow(tsDetails2[['tsAttrs']]), 13)
  expect_equal(nrow(tsDetails2[['tsExtAttrs']]), 10)
  
  row_mm <- which(tsDetails2[['tsAttrs']][['label']] == "Current Measurement Method")
  expect_equal(tsDetails2[['tsAttrs']][row_mm,][['value']], "")
  row_pt <- which(tsDetails2[['tsAttrs']][['label']] == "Processing Type")
  expect_equal(tsDetails2[['tsAttrs']][row_pt,][['value']], "")
  row_mst <- which(tsDetails2[['tsAttrs']][['label']] == "Method Start Time")
  expect_equal(row_mst, integer(0))
  row_pst <- which(tsDetails2[['tsAttrs']][['label']] == "Period Start Time")
  expect_equal(row_pst, integer(0))
  row_pet <- which(tsDetails2[['tsAttrs']][['label']] == "Period End Time")
  expect_equal(row_pet, integer(0))
  
  expect_equal(tsDetails[['tsAttrs']][1,][['label']], "Label")
  expect_equal(tsDetails[['tsAttrs']][1,][['value']], "Gage height.ft.(New site WY2011)@01014000")
  expect_equal(tsDetails[['tsAttrs']][1,][['indent']], 8)
  expect_equal(tsDetails[['tsAttrs']][6,][['label']], "UTC Offset")
  expect_equal(tsDetails[['tsAttrs']][6,][['value']], "-5")
  expect_equal(tsDetails[['tsAttrs']][6,][['indent']], 8)
  expect_equal(tsDetails[['tsAttrs']][15,][['label']], "Period Start Time")
  expect_equal(tsDetails[['tsAttrs']][15,][['value']], "Open")
  expect_equal(tsDetails[['tsAttrs']][15,][['indent']], 26)
  expect_equal(tsDetails[['tsExtAttrs']][7,][['label']], "ADAPS DD")
  expect_equal(tsDetails[['tsExtAttrs']][7,][['value']], "19")
  expect_equal(tsDetails[['tsAttrs']][13,][['value']],"2016-06-01 00:00:00 or earlier")
})

test_that('unNestCorrectionParameters correctly unnests parameters data ', {
  timezone <- "Etc/GMT+5"
  correctionsJson <- fromJSON('{
  "corrections": {
      "normal": [
        {
          "appliedTimeUtc": "2017-06-14T13:20:14.7678867Z",
          "comment": "Offset Correction with value of 0.300ft",
          "startTime": "2017-01-04T05:15:00-05:00",
          "endTime": "2017-01-06T23:00:00.0000001-05:00",
          "dominantType": "Offset",
          "type": "Offset",
          "parameters": {
            "Offset": 0.3
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:22:33.7717601Z",
          "comment": "USGS multi-point Start point: (0.000ft, 0.000ft) End point: (0.000ft, 0.500ft)",
          "startTime": "2017-01-07T20:45:00-05:00",
          "endTime": "2017-01-10T09:00:00.0000001-05:00",
          "dominantType": "UsgsMultiPoint",
          "type": "UsgsMultiPoint",
          "parameters": {
            "StartShiftPoints": [
              {
                "Value": 0,
                "Offset": 0
              }
              ],
            "EndShiftPoints": [
              {
                "Value": 0,
                "Offset": 0.5
              }
              ],
            "UsgsType": "Set 2"
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:38:09.4589634Z",
          "comment": "Revert to Raw data",
          "startTime": "2017-02-11T03:30:00-05:00",
          "endTime": "2017-02-11T13:00:00.0000001-05:00",
          "dominantType": "RevertToRaw",
          "type": "RevertToRaw",
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:29:47.5967321Z",
          "comment": "Delete region",
          "startTime": "2017-02-05T22:00:00-05:00",
          "endTime": "2017-02-07T14:30:00.0000001-05:00",
          "dominantType": "DeleteRegion",
          "type": "DeleteRegion",
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:29:56.5328236Z",
          "comment": "Freehand Correction without generating points.",
          "startTime": "2017-02-08T06:30:00-05:00",
          "endTime": "2017-02-09T15:00:00.0000001-05:00",
          "dominantType": "Freehand",
          "type": "CopyPaste",
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:29:35.8450932Z",
          "comment": "Copy and Paste from Gage height.ft.EDL@01069500",
          "startTime": "2017-02-03T13:15:00-05:00",
          "endTime": "2017-02-05T12:30:00.0000001-05:00",
          "dominantType": "CopyPaste",
          "type": "CopyPaste",
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:39:16.8024489Z",
          "comment": "Fill Data Gaps with gap resample period of 30 minutes",
          "startTime": "2017-02-10T06:45:00-05:00",
          "endTime": "2017-02-11T00:15:00.0000001-05:00",
          "dominantType": "FillGaps",
          "type": "FillGaps",
          "parameters": {
            "ResamplePeriod": "PT30M",
            "GapLimit": "MaxDuration"
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:19:48.7283456Z",
          "comment": "Drift Correction of (Date/Time, Diff): (2017-01-01 01:00:00, 0.000ft), (2017-01-03 17:30:00, 0.200ft)",
          "startTime": "2017-01-01T01:00:00-05:00",
          "endTime": "2017-01-03T17:30:00.0000001-05:00",
          "dominantType": "Drift",
          "type": "Drift",
          "parameters": {
            "DriftPoints": [
              {
                "Offset": 0,
                "Time": "2017-01-01T06:00:00Z"
              },
              {
                "Offset": 0.2,
                "Time": "2017-01-03T22:30:00Z"
              }
              ]
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:38:54.8184605Z",
          "comment": "Adjustable trim with Upper threshold: (2017-02-10 09:15:00, 1.273ft), (2017-02-10 23:30:00, 1.273ft)",
          "startTime": "2017-02-10T09:15:00-05:00",
          "endTime": "2017-02-10T23:30:00.0000001-05:00",
          "dominantType": "AdjustableTrim",
          "type": "AdjustableTrim",
          "parameters": {
            "UpperThresholdPoints": [
              {
                "Time": "2017-02-10T14:15:00Z",
                "Value": 1.2727854725856091
              },
              {
                "Time": "2017-02-11T04:30:00Z",
                "Value": 1.2727854725856091
              }
              ]
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:29:19.2290281Z",
          "comment": "Adjustable trim with Upper threshold: (2017-02-01 18:15:00, 1.420ft), (2017-02-04 02:45:00, 1.420ft)",
          "startTime": "2017-02-01T18:15:00-05:00",
          "endTime": "2017-02-04T02:45:00.0000001-05:00",
          "dominantType": "AdjustableTrim",
          "type": "AdjustableTrim",
          "parameters": {
            "UpperThresholdPoints": [
              {
                "Time": "2017-02-01T23:15:00Z",
                "Value": 1.4203813559322032
              },
              {
                "Time": "2017-02-04T07:45:00Z",
                "Value": 1.4203813559322032
              }
              ]
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:20:36.1443457Z",
          "comment": "Single point correction. Changed from 1.46 to 1.99",
          "startTime": "2017-01-07T09:00:00-05:00",
          "endTime": "2017-01-07T09:00:00.0000001-05:00",
          "dominantType": "SinglePoint",
          "type": "SinglePoint",
          "parameters": {
            "Value": 1.99
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        },
        {
          "appliedTimeUtc": "2017-06-14T13:31:17.479716Z",
          "comment": "Outlier trim",
          "startTime": "2017-02-10T08:00:00-05:00",
          "endTime": "2017-02-10T23:30:00.0000001-05:00",
          "dominantType": "Deviation",
          "type": "Deviation",
          "parameters": {
            "DeviationValue": 0.01,
            "DeviationType": "DeviationFromMinimum",
            "WindowSizeInMinutes": 15
          },
          "user": "lflight",
          "processingOrder": "NORMAL"
        }
        ],
      "postProcessing": [],
      "preProcessing": [],
      "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
    }
  }')
  corrections <- repgen:::parseTSSProcessingCorrections(correctionsJson, "normal", timezone)
  expect_equal(length(corrections),22)
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","Offset","StartShiftPoints","EndShiftPoints","UsgsType","ResamplePeriod","GapLimit","DriftPoints","UpperThresholdPoints","Value","DeviationValue","DeviationType","WindowSizeInMinutes","timezone","formattedParameters"))
  
})

test_that('adjustCorrectionTypes renames parameter types correctly', {
  timezone <- "Etc/GMT+5"
  adjustCorrectionJson <- fromJSON('{
  "corrections": {
    "normal": [
                                   {
                                   "appliedTimeUtc": "2017-10-11T14:07:37.4364465Z",
                                   "comment": "Persistence Gap fill with limit fill of 90 minutes with gap resample period of 45 minutes",
                                   "startTime": "2017-08-14T22:00:00-05:00",
                                   "endTime": "2017-08-17T00:45:00.0000001-05:00",
                                   "dominantType": "PersistenceGapFill",
                                   "type": "PersistenceGapFill",
                                   "parameters": {
                                   "ResamplePeriod": "PT45M",
                                   "ResampleInterpolationType": "PreviousPoint",
                                   "GapLimit": "PT1H30M"
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-10-11T14:34:18.4314942Z",
                                   "comment": "Persistence Gap fill with limit fill of 120 minutes with gap resample period of 225 minutes",
                                   "startTime": "2017-08-17T15:45:00-05:00",
                                   "endTime": "2017-08-19T11:00:00.0000001-05:00",
                                   "dominantType": "PersistenceGapFill",
                                   "type": "PersistenceGapFill",
                                   "parameters": {
                                   "ResamplePeriod": "PT3H45M",
                                   "ResampleInterpolationType": "NextPoint",
                                   "GapLimit": "PT2H"
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:53:52.7245843Z",
                                   "comment": "Outlier trim",
                                   "startTime": "2017-07-15T23:15:00-05:00",
                                   "endTime": "2017-07-18T03:30:00.0000001-05:00",
                                   "dominantType": "Deviation",
                                   "type": "Deviation",
                                   "parameters": {
                                   "DeviationValue": 0.05,
                                   "DeviationType": "DeviationFromMaximum",
                                   "WindowSizeInMinutes": 15.0
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:54:31.0347661Z",
                                   "startTime": "2017-07-26T11:45:00-05:00",
                                   "endTime": "2017-07-26T11:45:00.0000001-05:00",
                                   "dominantType": "SinglePoint",
                                   "type": "SinglePoint",
                                   "parameters": {
                                   "Value": 1.76
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:50:35.2219603Z",
                                   "comment": "Adjustable trim with Upper threshold: (2017-06-22 18:45:00, 1.093ft), (2017-06-24 18:30:00, 1.093ft)",
                                   "startTime": "2017-06-22T18:45:00-05:00",
                                   "endTime": "2017-06-24T18:30:00.0000001-05:00",
                                   "dominantType": "AdjustableTrim",
                                   "type": "AdjustableTrim",
                                   "parameters": {
                                   "UpperThresholdPoints": [
                                   {
                                   "Time": "2017-06-22T23:45:00Z",
                                   "Value": 1.0925609756097563
                                   },
                                   {
                                   "Time": "2017-06-24T23:30:00Z",
                                   "Value": 1.0925609756097563
                                   }
                                   ]
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:52:11.1968924Z",
                                   "comment": "Drift Correction of (Date/Time, Diff): (2017-07-02 13:00:00, 0.000ft), (2017-07-04 12:45:00, 0.200ft)",
                                   "startTime": "2017-07-02T13:00:00-05:00",
                                   "endTime": "2017-07-04T12:45:00.0000001-05:00",
                                   "dominantType": "Drift",
                                   "type": "Drift",
                                   "parameters": {
                                   "DriftPoints": [
                                   {
                                   "Offset": 0.0,
                                   "Time": "2017-07-02T18:00:00Z"
                                   },
                                   {
                                   "Offset": 0.2,
                                   "Time": "2017-07-04T17:45:00Z"
                                   }
                                   ]
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:53:04.6755632Z",
                                   "comment": "Fill Data Gaps with gap resample period of 15 minutes",
                                   "startTime": "2017-06-30T23:15:00-05:00",
                                   "endTime": "2017-07-02T04:45:00.0000001-05:00",
                                   "dominantType": "FillGaps",
                                   "type": "FillGaps",
                                   "parameters": {
                                   "ResamplePeriod": "PT15M",
                                   "GapLimit": "MaxDuration"
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:51:19.068767Z",
                                   "comment": "Copy and Paste from Gage height.ft.EDL@01069500",
                                   "startTime": "2017-06-25T07:30:00-05:00",
                                   "endTime": "2017-06-27T10:00:00.0000001-05:00",
                                   "dominantType": "CopyPaste",
                                   "type": "CopyPaste",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:52:23.838792Z",
                                   "comment": "Freehand Correction generating points every 30.0 minutes.",
                                   "startTime": "2017-07-04T20:00:00-05:00",
                                   "endTime": "2017-07-06T18:30:00.0000001-05:00",
                                   "dominantType": "Freehand",
                                   "type": "CopyPaste",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:51:47.461143Z",
                                   "comment": "NORMAL Delete region",
                                   "startTime": "2017-06-29T11:15:00-05:00",
                                   "endTime": "2017-06-30T10:30:00.0000001-05:00",
                                   "dominantType": "DeleteRegion",
                                   "type": "DeleteRegion",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:54:24.4177137Z",
                                   "comment": "Revert to Raw data",
                                   "startTime": "2017-07-22T18:30:00-05:00",
                                   "endTime": "2017-07-25T00:15:00.0000001-05:00",
                                   "dominantType": "RevertToRaw",
                                   "type": "RevertToRaw",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-04-11T14:19:59.7213631Z",
                                   "comment": "+0.01 Inst. drift correction based on SV trends. MEB // Carried forward until resolved. // USGS multi-point Start point: (0.000ft, 0.010ft)",
                                   "startTime": "2016-05-05T05:00:00-05:00",
                                   "endTime": "2017-07-27T07:45:00-05:00",
                                   "dominantType": "UsgsMultiPoint",
                                   "type": "UsgsMultiPoint",
                                   "parameters": {
                                   "StartShiftPoints": [
                                   {
                                   "Value": 0.0,
                                   "Offset": 0.01
                                   }
                                   ],
                                   "UsgsType": "Set 2"
                                   },
                                   "user": "mbeardsley",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-10-11T15:03:44.4950163Z",
                                   "comment": "USGS multi-point Start point: (0.000ft, 0.010ft), (0.500ft, 0.020ft), (1.000ft, 0.030ft) End point: (1.500ft, 0.040ft), (2.000ft, 0.050ft), (2.500ft, 0.060ft)",
                                   "startTime": "2017-08-11T11:45:00-05:00",
                                   "endTime": "2017-08-13T00:15:00.0000001-05:00",
                                   "dominantType": "UsgsMultiPoint",
                                   "type": "UsgsMultiPoint",
                                   "parameters": {
                                   "StartShiftPoints": [
                                   {
                                   "Value": 0.0,
                                   "Offset": 0.01
                                   },
                                   {
                                   "Value": 0.5,
                                   "Offset": 0.02
                                   },
                                   {
                                   "Value": 1.0,
                                   "Offset": 0.03
                                   }
                                   ],
                                   "EndShiftPoints": [
                                   {
                                   "Value": 1.5,
                                   "Offset": 0.04
                                   },
                                   {
                                   "Value": 2.0,
                                   "Offset": 0.05
                                   },
                                   {
                                   "Value": 2.5,
                                   "Offset": 0.06
                                   }
                                   ],
                                   "UsgsType": "Set 2"
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:53:42.9550386Z",
                                   "comment": "Offset Correction with value of 0.200ft",
                                   "startTime": "2017-07-09T15:45:00-05:00",
                                   "endTime": "2017-07-13T05:45:00.0000001-05:00",
                                   "dominantType": "Offset",
                                   "type": "Offset",
                                   "parameters": {
                                   "Offset": 0.2
                                   },
                                   "user": "lflight",
                                   "processingOrder": "Normal"
                                   }
                                   ],
                                   "postProcessing": [
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:54:09.5513609Z",
                                   "comment": "Persistence Gap fill with gap resample period of 15 minutes",
                                   "startTime": "2017-07-19T11:30:00-05:00",
                                   "endTime": "2017-07-21T17:15:00.0000001-05:00",
                                   "dominantType": "PersistenceGapFill",
                                   "type": "PersistenceGapFill",
                                   "parameters": {
                                   "ResamplePeriod": "PT15M",
                                   "ResampleInterpolationType": "MidPoint",
                                   "GapLimit": "MaxDuration"
                                   },
                                   "user": "lflight",
                                   "processingOrder": "PostProcessing"
                                   },
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:51:57.9466666Z",
                                   "comment": "POST Delete region",
                                   "startTime": "2017-06-30T23:30:00-05:00",
                                   "endTime": "2017-07-02T04:30:00.0000001-05:00",
                                   "dominantType": "DeleteRegion",
                                   "type": "DeleteRegion",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "PostProcessing"
                                   }
                                   ],
                                   "preProcessing": [
                                   {
                                   "appliedTimeUtc": "2017-09-20T14:51:30.7906005Z",
                                   "comment": "PRE Delete region",
                                   "startTime": "2017-06-27T18:45:00-05:00",
                                   "endTime": "2017-06-29T05:30:00.0000001-05:00",
                                   "dominantType": "DeleteRegion",
                                   "type": "DeleteRegion",
                                   "parameters": {},
                                   "user": "lflight",
                                   "processingOrder": "PreProcessing"
                                   }
                                   ],
                                   "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8443/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-08-31Z&station=01069500&startDate=2017-06-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
}
  
                                   }')
  corrections <- repgen:::readProcessingCorrections(adjustCorrectionJson,"normal",timezone)
  corrections <- repgen:::adjustCorrectionTypes(corrections)
  expect_equal(corrections[['dominantType']], c("Persistence Gap Fill","Persistence Gap Fill","Outlier Trim","Single Point", "Adjustable Trim", "Drift", "Fill Gaps", "Copy and Paste", "Freehand", "Delete Region", "Revert to Raw", "USGS Multi Point", "USGS Multi Point", "Offset"))
  expect_equal(corrections[['type']], c("PersistenceGapFill","PersistenceGapFill","Deviation","SinglePoint", "AdjustableTrim", "Drift", "FillGaps", "CopyPaste", "CopyPaste", "DeleteRegion", "RevertToRaw", "UsgsMultiPoint", "UsgsMultiPoint", "Offset"))
})

test_that('formatCorrectionsParamOffset correctly formats offset parameters data ', {
  timezone <- "Etc/GMT+5"
  offsetJson <- fromJSON('{
  "corrections": {
                       "normal": [
                       {
                       "appliedTimeUtc": "2017-06-14T13:20:14.7678867Z",
                       "comment": "Offset Correction with value of 0.300ft",
                       "startTime": "2017-01-04T05:15:00-05:00",
                       "endTime": "2017-01-06T23:00:00.0000001-05:00",
                       "dominantType": "Offset",
                       "type": "Offset",
                       "parameters": {
                          "Offset": 0.3
                       },
                       "user": "lflight",
                       "processingOrder": "NORMAL"
                       }
                       ],
                       "postProcessing": [],
                       "preProcessing": [],
                       "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                       }
                       }')
  expect_equal(offsetJson[['corrections']][['normal']][['type']], "Offset")
  expect_named(offsetJson[['corrections']][['normal']][['parameters']], "Offset")
  corrections <- repgen:::parseTSSProcessingCorrections(offsetJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "0.3")
  expect_equal(names(corrections), c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","Offset","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamDrift correctly formats drift parameters data ', {
  timezone <- "Etc/GMT+5"
  driftJson <- fromJSON('{
  "corrections": {
                       "normal": [
                         {
                        "appliedTimeUtc": "2017-06-14T13:19:48.7283456Z",
                         "comment": "Drift Correction of (Date/Time, Diff): (2017-01-01 01:00:00, 0.000ft), (2017-01-03 17:30:00, 0.200ft)",
                         "startTime": "2017-01-01T01:00:00-05:00",
                         "endTime": "2017-01-03T17:30:00.0000001-05:00",
                         "dominantType": "Drift",
                         "type": "Drift",
                         "parameters": {
                         "DriftPoints": [
                         {
                         "Offset": 0,
                         "Time": "2017-01-01T06:00:00Z"
                         },
                         {
                         "Offset": 0.2,
                         "Time": "2017-01-03T22:30:00Z"
                         }
                         ]
                         },
                       "user": "lflight",
                       "processingOrder": "NORMAL"
                       }
                       ],
                       "postProcessing": [],
                       "preProcessing": [],
                       "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                       }
                       }')
  expect_equal(driftJson[['corrections']][['normal']][['type']], "Drift")
  expect_named(driftJson[['corrections']][['normal']][['parameters']], "DriftPoints")
  expect_named(driftJson[['corrections']][['normal']][['parameters']][['DriftPoints']][[1]], c("Offset","Time"))
  corrections <- repgen:::parseTSSProcessingCorrections(driftJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "Offset/Time: 0 at 2017-01-01 01:00:00; 0.2 at 2017-01-03 17:30:00; ")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","DriftPoints","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamSinglePoint correctly formats single point parameters data ', {
  timezone <- "Etc/GMT+5"
  singlePointJson <- fromJSON('{
                        "corrections": {
                        "normal": [
                              {
                              "appliedTimeUtc": "2017-06-14T13:20:36.1443457Z",
                              "comment": "Single point correction. Changed from 1.46 to 1.99",
                              "startTime": "2017-01-07T09:00:00-05:00",
                              "endTime": "2017-01-07T09:00:00.0000001-05:00",
                              "dominantType": "SinglePoint",
                              "type": "SinglePoint",
                              "parameters": {
                              "Value": 1.99
                              },
                              "user": "lflight",
                              "processingOrder": "NORMAL"
                        }
                        ],
                        "postProcessing": [],
                        "preProcessing": [],
                        "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                        }
}')
  expect_equal(singlePointJson[['corrections']][['normal']][['type']], "SinglePoint")
  expect_named(singlePointJson[['corrections']][['normal']][['parameters']], "Value")
  corrections <- repgen:::parseTSSProcessingCorrections(singlePointJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "1.99")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","Value","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamUSGSMultiPoint correctly formats USGS Multi Point parameters data ', {
  timezone <- "Etc/GMT+5"
  USGSMultiPointJson <- fromJSON('{
                              "corrections": {
                              "normal": [
                              {
                              "appliedTimeUtc": "2017-06-14T13:22:33.7717601Z",
                              "comment": "USGS multi-point Start point: (0.000ft, 0.000ft) End point: (0.000ft, 0.500ft)",
                              "startTime": "2017-01-07T20:45:00-05:00",
                              "endTime": "2017-01-10T09:00:00.0000001-05:00",
                              "dominantType": "UsgsMultiPoint",
                              "type": "UsgsMultiPoint",
                              "parameters": {
                              "StartShiftPoints": [
                              {
                              "Value": 0,
                              "Offset": 0
                              }
                              ],
                              "EndShiftPoints": [
                              {
                              "Value": 0,
                              "Offset": 0.5
                              }
                              ],
                              "UsgsType": "Set 2"
                              }
                              }
                              ],
                              "postProcessing": [],
                              "preProcessing": [],
                              "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                              }
}')
  expect_equal(USGSMultiPointJson[['corrections']][['normal']][['type']], "UsgsMultiPoint")
  expect_named(USGSMultiPointJson[['corrections']][['normal']][['parameters']], c("StartShiftPoints","EndShiftPoints","UsgsType"))
  expect_named(USGSMultiPointJson[['corrections']][['normal']][['parameters']][['StartShiftPoints']][[1]], c("Value","Offset"))
  expect_named(USGSMultiPointJson[['corrections']][['normal']][['parameters']][['EndShiftPoints']][[1]], c("Value","Offset"))
  corrections <- repgen:::parseTSSProcessingCorrections(USGSMultiPointJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "Start Shift Points: 0, 0; End Shift Points: 0, 0.5; ")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","StartShiftPoints","EndShiftPoints","UsgsType","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamAdjustableTrim correctly formats adjustable trim parameters data ', {
  timezone <- "Etc/GMT+5"
  adjustableTrimJson <- fromJSON('{
                                 "corrections": {
                                 "normal": [
                                 {
          "appliedTimeUtc": "2017-06-14T13:38:54.8184605Z",
                                 "comment": "Adjustable trim with Upper threshold: (2017-02-10 09:15:00, 1.273ft), (2017-02-10 23:30:00, 1.273ft)",
                                 "startTime": "2017-02-10T09:15:00-05:00",
                                 "endTime": "2017-02-10T23:30:00.0000001-05:00",
                                 "dominantType": "AdjustableTrim",
                                 "type": "AdjustableTrim",
                                 "parameters": {
                                 "UpperThresholdPoints": [
                                 {
                                 "Time": "2017-02-10T14:15:00Z",
                                 "Value": 1.2727854725856091
                                 },
                                 {
                                 "Time": "2017-02-11T04:30:00Z",
                                 "Value": 1.2727854725856091
                                 }
                                 ]
                                 }
                                  },
                                {
                                  "appliedTimeUtc": "2017-06-01T17:30:39.1830491Z",
                                  "comment": "Delete eronious data",
                                  "startTime": "2017-05-25T16:45:00-07:00",
                                  "endTime": "2017-06-01T00:15:00.0000001-07:00",
                                  "dominantType": "AdjustableTrim",
                                  "type": "AdjustableTrim",
                                  "parameters": {
                                  "LowerThresholdPoints": [
                                  {
                                  "Value": 8.238427419354839,
                                  "Time": "2017-05-25T23:45:00Z"
                                  },
                                  {
                                  "Value": 8.639435483870969,
                                  "Time": "2017-05-26T04:30:00Z"
                                  },
                                  {
                                  "Value": 8.133548387096775,
                                  "Time": "2017-05-26T12:00:00Z"
                                  },
                                  {
                                  "Value": 8.670282258064518,
                                  "Time": "2017-05-26T21:15:00Z"
                                  },
                                  {
                                  "Value": 8.127379032258066,
                                  "Time": "2017-05-27T12:00:00Z"
                                  },
                                  {
                                  "Value": 8.016330645161291,
                                  "Time": "2017-05-30T06:45:00Z"
                                  },
                                  {
                                  "Value": 7.584475806451614,
                                  "Time": "2017-05-30T12:00:00Z"
                                  },
                                  {
                                  "Value": 7.491935483870968,
                                  "Time": "2017-05-31T14:15:00Z"
                                  },
                                  {
                                  "Value": 8.102701612903227,
                                  "Time": "2017-05-31T19:45:00Z"
                                  },
                                  {
                                  "Value": 7.84975806451613,
                                  "Time": "2017-06-01T07:15:00Z"
                                  }
                                  ]
                                  } 
                                  }
                                 ],
                                 "postProcessing": [],
                                 "preProcessing": [],
                                 "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                                 }
}')
  expect_equal(adjustableTrimJson[['corrections']][['normal']][['type']][[1]], "AdjustableTrim")
  expect_named(adjustableTrimJson[['corrections']][['normal']][['parameters']], c("UpperThresholdPoints","LowerThresholdPoints"))
  expect_named(adjustableTrimJson[['corrections']][['normal']][['parameters']][['UpperThresholdPoints']][[1]], c("Time","Value"))
  expect_named(adjustableTrimJson[['corrections']][['normal']][['parameters']][['LowerThresholdPoints']][[2]], c("Value","Time"))
  corrections <- repgen:::parseTSSProcessingCorrections(adjustableTrimJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']][[1]], "Upper Threshold Points, Time/Value: 2017-02-10 09:15:00, 1.273; 2017-02-10 23:30:00, 1.273; ")
  expect_equal(corrections[['formattedParameters']][[2]], "Lower Threshold Points, Time/Value: 2017-05-25 18:45:00, 8.238; 2017-05-25 23:30:00, 8.639; 2017-05-26 07:00:00, 8.134; 2017-05-26 16:15:00, 8.67; 2017-05-27 07:00:00, 8.127; 2017-05-30 01:45:00, 8.016; 2017-05-30 07:00:00, 7.584; 2017-05-31 09:15:00, 7.492; 2017-05-31 14:45:00, 8.103; 2017-06-01 02:15:00, 7.85; ")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","UpperThresholdPoints","LowerThresholdPoints","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamFillGaps correctly formats fill gaps parameters data ', {
  timezone <- "Etc/GMT+5"
  fillGapsJson <- fromJSON('{
                                 "corrections": {
                                 "normal": [
                           {
                          "appliedTimeUtc": "2017-06-14T13:39:16.8024489Z",
                           "comment": "Fill Data Gaps with gap resample period of 30 minutes",
                           "startTime": "2017-02-10T06:45:00-05:00",
                           "endTime": "2017-02-11T00:15:00.0000001-05:00",
                           "dominantType": "FillGaps",
                           "type": "FillGaps",
                           "parameters": {
                           "ResamplePeriod": "PT30M",
                           "GapLimit": "MaxDuration"
                           },
                           "user": "lflight",
                           "processingOrder": "NORMAL"
                            }
                                 ],
                                 "postProcessing": [],
                                 "preProcessing": [],
                                 "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                                 }
}')
  expect_equal(fillGapsJson[['corrections']][['normal']][['type']], "FillGaps")
  expect_named(fillGapsJson[['corrections']][['normal']][['parameters']], c("ResamplePeriod","GapLimit"))
  corrections <- repgen:::parseTSSProcessingCorrections(fillGapsJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "Resample Period, 30 min; Gap Limit, Fill all gaps")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","ResamplePeriod","GapLimit","timezone","formattedParameters"))
})

test_that('formatCorrectionsParamDeviation correctly formats deviation parameters data ', {
  timezone <- "Etc/GMT+5"
  deviationJson <- fromJSON('{
                           "corrections": {
                           "normal": [
                            {
          "appliedTimeUtc": "2017-06-14T13:31:17.479716Z",
                            "comment": "Outlier trim",
                            "startTime": "2017-02-10T08:00:00-05:00",
                            "endTime": "2017-02-10T23:30:00.0000001-05:00",
                            "dominantType": "Deviation",
                            "type": "Deviation",
                            "parameters": {
                            "DeviationValue": 0.01,
                            "DeviationType": "DeviationFromMinimum",
                            "WindowSizeInMinutes": 15
                            },
                            "user": "lflight",
                            "processingOrder": "NORMAL"
                            }
                           ],
                           "postProcessing": [],
                           "preProcessing": [],
                           "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                           }
}')
  expect_equal(deviationJson[['corrections']][['normal']][['type']], "Deviation")
  expect_named(deviationJson[['corrections']][['normal']][['parameters']], c("DeviationValue","DeviationType","WindowSizeInMinutes"))
  corrections <- repgen:::parseTSSProcessingCorrections(deviationJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "Deviation Value: 0.01, Deviation Type: From Minimum, Size In Minutes: 15")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","DeviationValue","DeviationType","WindowSizeInMinutes","timezone","formattedParameters"))
})

test_that('unNestCorrectionParameters correctly formats empty parameters data ', {
  timezone <- "Etc/GMT+5"
  emptyParamJson <- fromJSON('{
                            "corrections": {
                            "normal": [
                            {
          "appliedTimeUtc": "2017-06-14T13:38:09.4589634Z",
                            "comment": "Revert to Raw data",
                            "startTime": "2017-02-11T03:30:00-05:00",
                            "endTime": "2017-02-11T13:00:00.0000001-05:00",
                            "dominantType": "RevertToRaw",
                            "type": "RevertToRaw",
                            "user": "lflight",
                            "processingOrder": "NORMAL"
},
                            {
                            "appliedTimeUtc": "2017-06-14T13:29:47.5967321Z",
                            "comment": "Delete region",
                            "startTime": "2017-02-05T22:00:00-05:00",
                            "endTime": "2017-02-07T14:30:00.0000001-05:00",
                            "dominantType": "DeleteRegion",
                            "type": "DeleteRegion",
                            "user": "lflight",
                            "processingOrder": "NORMAL"
                            },
                            {
                            "appliedTimeUtc": "2017-06-14T13:29:56.5328236Z",
                            "comment": "Freehand Correction without generating points.",
                            "startTime": "2017-02-08T06:30:00-05:00",
                            "endTime": "2017-02-09T15:00:00.0000001-05:00",
                            "dominantType": "Freehand",
                            "type": "CopyPaste",
                            "user": "lflight",
                            "processingOrder": "NORMAL"
                            },
                            {
                            "appliedTimeUtc": "2017-06-14T13:29:35.8450932Z",
                            "comment": "Copy and Paste from Gage height.ft.EDL@01069500",
                            "startTime": "2017-02-03T13:15:00-05:00",
                            "endTime": "2017-02-05T12:30:00.0000001-05:00",
                            "dominantType": "CopyPaste",
                            "type": "CopyPaste",
                            "user": "lflight",
                            "processingOrder": "NORMAL"
                            }
                            ],
                            "postProcessing": [],
                            "preProcessing": [],
                            "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                            }
}')
  corrections <- repgen:::parseTSSProcessingCorrections(emptyParamJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']][1], " ")
  expect_equal(corrections[['formattedParameters']][2], " ")
  expect_equal(corrections[['formattedParameters']][3], " ")
  expect_equal(corrections[['formattedParameters']][4], " ")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","timezone","formattedParameters"))
})

test_that('unNestCorrectionParameters handles unknown parameter type', {
  timezone <- "Etc/GMT+5"
  unknownParamJson <- fromJSON('{
                             "corrections": {
                             "normal": [
                             {
                             "appliedTimeUtc": "2017-06-14T13:38:09.4589634Z",
                             "comment": "Revert to Raw data",
                             "startTime": "2017-02-11T03:30:00-05:00",
                             "endTime": "2017-02-11T13:00:00.0000001-05:00",
                             "dominantType": "NotAKnownParameterType",
                             "type": "NotAKnownParameterType",
                             "parameters": {
                                "Some": 0.00,
                                "Dummy": "This wont display!",
                                "Parameters": 42
                              },  
                             "user": "lflight",
                             "processingOrder": "NORMAL"
                             }
                             ],
                             "postProcessing": [],
                             "preProcessing": [],
                             "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
                             }
}')
  corrections <- repgen:::parseTSSProcessingCorrections(unknownParamJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], " ")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","Some","Dummy","Parameters","timezone","formattedParameters"))
  })

test_that('unNestCorrectionParameters handles PersistenceGapFill parameter type', {
  timezone <- "Etc/GMT+5"
  persistenceGapFillParamJson <- fromJSON('{
                             "corrections": {
                             "normal": [
                                          {
        "appliedTimeUtc": "2017-09-20T14:54:09.5513609Z",
                                          "comment": "Persistence Gap fill with gap resample period of 15 minutes",
                                          "startTime": "2017-07-19T11:30:00-05:00",
                                          "endTime": "2017-07-21T17:15:00.0000001-05:00",
                                          "dominantType": "PersistenceGapFill",
                                          "type": "PersistenceGapFill",
                                          "parameters": {
                                          "ResamplePeriod": "PT15M",
                                          "ResampleInterpolationType": "MidPoint",
                                          "GapLimit": "MaxDuration"
                                          },
                                          "user": "lflight",
                                          "processingOrder": "NORMAL"
                                          }
                                          ],
                                          "postProcessing": [],
                                          "preProcessing": [],
                                          "corrUrl": "https://cida-eros-aqcudev.er.usgs.gov:8444/aqcu-webservice/service/reports/correctionsataglance/?endDate=2017-02-28Z&station=01069500&startDate=2017-01-01Z&primaryTimeseriesIdentifier=efb88ed6b5dc41dcaa33931cd6c144a2"
}
}')
  corrections <- repgen:::parseTSSProcessingCorrections(persistenceGapFillParamJson, "normal", timezone)
  expect_equal(corrections[['formattedParameters']], "Resample Period: 15 min, Persistence Method: Mid, Gap Size Limit: Fill all gaps")
  expect_equal(names(corrections),c("appliedTimeUtc","comment","startTime","endTime","dominantType","type","user","processingOrder","ResamplePeriod","ResampleInterpolationType","GapLimit","timezone","formattedParameters"))
})

test_that('formatAdvReportOptions function returns expected info to display on TSS report', {
  timezone <- "Etc/GMT+5"
  advOptionsJson <- fromJSON('{
                             "reportMetadata": {
                               "country": "United States of America",
                               "altitude": "  604.60",
                               "requestingUser": "mhines",
                               "endDate": "2017-08-03T23:59:59.999999999Z",
                               "timezone": "Etc/GMT+5",
                               "latitude": "47.0697222222222",
                               "county": "Aroostook County",
                               "description": "Shows relevant information about a timeseries",
                               "title": "Time Series Summary",
                               "requestParameters": {
                                  "excludedCorrections": [ "DeleteRegion", "AdjustableTrim", "Deviation" ]
                               },
                               "primaryParameter": "Discharge.ft^3/s@01011000",
                               "primaryTsIdentifier": "abd7a6b631874668b953e88cbbf4e374",
                               "requestId": "TimeSeriesSummaryChoreographer-cea278ed-392f-4db1-9c58-ba11ea5eeb96",
                               "siteNumber": "01011000       ",
                               "stationName": "Allagash River near Allagash, Maine",
                               "coordinateDatumCode": "NAD83     ",
                               "state": "Maine",
                               "requestTemporal": "2017-08-03T09:41:46.31",
                               "stationId": "01011000",
                               "longitude": "-69.0794444444444",
                               "agency": "USGS ",
                               "drainageArea": "    1478",
                               "nwisRaAuthToken": "702cea18-631f-4ef1-8658-292bd1f98102",
                               "contributingDrainageArea": "    1229",
                               "altitudeDatumCode": "NGVD29    ",
                               "startDate": "2016-08-01T00:00:00Z"
}
}')
  advOptions <- repgen:::formatAdvReportOptions(repgen:::fetchRequestParametersField(advOptionsJson,'excludedCorrections'))
  expect_equal(advOptions, "Delete corrections excluded.")
})

setwd(dir = wd)
USGS-R/repgen documentation built on April 14, 2021, 2:47 p.m.