|
| 1 | +/* |
| 2 | + * © 2021. TU Dortmund University, |
| 3 | + * Institute of Energy Systems, Energy Efficiency and Energy Economics, |
| 4 | + * Research group Distribution grid planning and operation |
| 5 | + */ |
| 6 | +package edu.ie3.datamodel.io.source.influxdb |
| 7 | + |
| 8 | +import edu.ie3.datamodel.io.connectors.InfluxDbConnector |
| 9 | +import edu.ie3.datamodel.io.factory.timeseries.CosmoTimeBasedWeatherValueFactory |
| 10 | +import edu.ie3.datamodel.io.factory.timeseries.IconTimeBasedWeatherValueFactory |
| 11 | +import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries |
| 12 | +import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue |
| 13 | +import edu.ie3.datamodel.models.value.WeatherValue |
| 14 | +import edu.ie3.test.common.IconWeatherTestData |
| 15 | +import edu.ie3.test.helper.WeatherSourceTestHelper |
| 16 | +import edu.ie3.util.geo.GeoUtils |
| 17 | +import edu.ie3.util.interval.ClosedInterval |
| 18 | +import edu.ie3.util.naming.NamingConvention |
| 19 | +import org.locationtech.jts.geom.Point |
| 20 | +import org.testcontainers.containers.InfluxDBContainer |
| 21 | +import org.testcontainers.spock.Testcontainers |
| 22 | +import org.testcontainers.utility.MountableFile |
| 23 | +import spock.lang.Shared |
| 24 | +import spock.lang.Specification |
| 25 | + |
| 26 | +@Testcontainers |
| 27 | +class InfluxDbWeatherSourceIconIT extends Specification implements WeatherSourceTestHelper { |
| 28 | + |
| 29 | + @Shared |
| 30 | + InfluxDBContainer influxDbContainer = new InfluxDBContainer("1.8.4") |
| 31 | + .withAuthEnabled(false) |
| 32 | + .withDatabase("test_weather") |
| 33 | + |
| 34 | + @Shared |
| 35 | + InfluxDbWeatherSource source |
| 36 | + |
| 37 | + def setupSpec() { |
| 38 | + // Copy import file into docker and then import it via influx CLI |
| 39 | + // more information on file format and usage here: https://docs.influxdata.com/influxdb/v1.7/tools/shell/#import-data-from-a-file-with-import |
| 40 | + MountableFile influxWeatherImportFile = MountableFile.forClasspathResource("/testcontainersFiles/influxDb/icon/weather.txt") |
| 41 | + influxDbContainer.copyFileToContainer(influxWeatherImportFile, "/home/weather.txt") |
| 42 | + def execResult = influxDbContainer.execInContainer("influx", "-import", "-path=/home/weather.txt", "-precision=ms") |
| 43 | + println "Command \"influx -import -path=/home/weather.txt -precision=ms\" returned:" |
| 44 | + if(!execResult.stderr.isEmpty()) println execResult.getStderr() |
| 45 | + if(!execResult.stdout.isEmpty()) println execResult.getStdout() |
| 46 | + |
| 47 | + def connector = new InfluxDbConnector(influxDbContainer.url,"test_weather", "test_scenario") |
| 48 | + def weatherFactory = new IconTimeBasedWeatherValueFactory("yyyy-MM-dd'T'HH:mm:ss[.S[S][S]]'Z'") |
| 49 | + source = new InfluxDbWeatherSource(connector, IconWeatherTestData.coordinateSource, NamingConvention.SNAKE, weatherFactory) |
| 50 | + } |
| 51 | + |
| 52 | + def "The test container can establish a valid connection"() { |
| 53 | + when: |
| 54 | + def connector = new InfluxDbConnector(influxDbContainer.url,"test_weather", "test_scenario") |
| 55 | + then: |
| 56 | + connector.connectionValid |
| 57 | + } |
| 58 | + |
| 59 | + def "An InfluxDbWeatherSource can read and correctly parse a single value for a specific date and coordinate"() { |
| 60 | + given: |
| 61 | + def expectedTimeBasedValue = new TimeBasedValue(IconWeatherTestData.TIME_15H , IconWeatherTestData.WEATHER_VALUE_67775_15H) |
| 62 | + |
| 63 | + when: |
| 64 | + def optTimeBasedValue = source.getWeather(IconWeatherTestData.TIME_15H , IconWeatherTestData.COORDINATE_67775) |
| 65 | + |
| 66 | + then: |
| 67 | + optTimeBasedValue.present |
| 68 | + equalsIgnoreUUID(optTimeBasedValue.get(), expectedTimeBasedValue) |
| 69 | + } |
| 70 | + |
| 71 | + def "An InfluxDbWeatherSource can read multiple time series values for multiple coordinates"() { |
| 72 | + given: |
| 73 | + def coordinates = [ |
| 74 | + IconWeatherTestData.COORDINATE_67775, |
| 75 | + IconWeatherTestData.COORDINATE_67776 |
| 76 | + ] |
| 77 | + def timeInterval = new ClosedInterval(IconWeatherTestData.TIME_16H , IconWeatherTestData.TIME_17H) |
| 78 | + def timeseries67775 = new IndividualTimeSeries(null, |
| 79 | + [ |
| 80 | + new TimeBasedValue(IconWeatherTestData.TIME_16H , IconWeatherTestData.WEATHER_VALUE_67775_16H), |
| 81 | + new TimeBasedValue(IconWeatherTestData.TIME_17H , IconWeatherTestData.WEATHER_VALUE_67775_17H)] |
| 82 | + as Set<TimeBasedValue>) |
| 83 | + def timeseries67776 = new IndividualTimeSeries(null, |
| 84 | + [ |
| 85 | + new TimeBasedValue(IconWeatherTestData.TIME_16H , IconWeatherTestData.WEATHER_VALUE_67776_16H)] as Set<TimeBasedValue>) |
| 86 | + |
| 87 | + when: |
| 88 | + def coordinateToTimeSeries = source.getWeather(timeInterval, coordinates) |
| 89 | + |
| 90 | + then: |
| 91 | + coordinateToTimeSeries.keySet().size() == 2 |
| 92 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67775), timeseries67775) |
| 93 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67776), timeseries67776) |
| 94 | + } |
| 95 | + |
| 96 | + def "An InfluxDbWeatherSource can read all weather data in a given time interval"() { |
| 97 | + given: |
| 98 | + def timeInterval = new ClosedInterval(IconWeatherTestData.TIME_15H , IconWeatherTestData.TIME_17H) |
| 99 | + def timeseries_67775 = new IndividualTimeSeries(null, |
| 100 | + [ |
| 101 | + new TimeBasedValue(IconWeatherTestData.TIME_15H ,IconWeatherTestData.WEATHER_VALUE_67775_15H), |
| 102 | + new TimeBasedValue(IconWeatherTestData.TIME_16H ,IconWeatherTestData.WEATHER_VALUE_67775_16H), |
| 103 | + new TimeBasedValue(IconWeatherTestData.TIME_17H ,IconWeatherTestData.WEATHER_VALUE_67775_17H)] as Set<TimeBasedValue>) |
| 104 | + def timeseries_67776 = new IndividualTimeSeries(null, |
| 105 | + [ |
| 106 | + new TimeBasedValue(IconWeatherTestData.TIME_15H ,IconWeatherTestData.WEATHER_VALUE_67776_15H), |
| 107 | + new TimeBasedValue(IconWeatherTestData.TIME_16H ,IconWeatherTestData.WEATHER_VALUE_67776_16H)] as Set<TimeBasedValue>) |
| 108 | + |
| 109 | + when: |
| 110 | + def coordinateToTimeSeries = source.getWeather(timeInterval) |
| 111 | + |
| 112 | + then: |
| 113 | + coordinateToTimeSeries.keySet().size() == 2 |
| 114 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67775).getEntries(), timeseries_67775.getEntries()) |
| 115 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67776).getEntries(), timeseries_67776.getEntries()) |
| 116 | + } |
| 117 | + |
| 118 | + def "An InfluxDbWeatherSource will return an equivalent to 'empty' when being unable to map a coordinate to it's ID"() { |
| 119 | + def validCoordinate = IconWeatherTestData.COORDINATE_67775 |
| 120 | + def invalidCoordinate = GeoUtils.xyToPoint(48d, 7d) |
| 121 | + def time = IconWeatherTestData.TIME_15H |
| 122 | + def timeInterval = new ClosedInterval(IconWeatherTestData.TIME_15H , IconWeatherTestData.TIME_17H) |
| 123 | + def emptyTimeSeries = new IndividualTimeSeries(UUID.randomUUID(), Collections.emptySet()) |
| 124 | + def timeseries_67775 = new IndividualTimeSeries(null, |
| 125 | + [ |
| 126 | + new TimeBasedValue(IconWeatherTestData.TIME_15H ,IconWeatherTestData.WEATHER_VALUE_67775_15H), |
| 127 | + new TimeBasedValue(IconWeatherTestData.TIME_16H ,IconWeatherTestData.WEATHER_VALUE_67775_16H), |
| 128 | + new TimeBasedValue(IconWeatherTestData.TIME_17H ,IconWeatherTestData.WEATHER_VALUE_67775_17H)] as Set<TimeBasedValue>) |
| 129 | + |
| 130 | + when: |
| 131 | + def coordinateAtDate = source.getWeather(time, invalidCoordinate) |
| 132 | + def coordinateInInterval = source.getWeather(timeInterval, invalidCoordinate) |
| 133 | + def coordinatesToTimeSeries = source.getWeather(timeInterval, [ |
| 134 | + validCoordinate, |
| 135 | + invalidCoordinate |
| 136 | + ]) |
| 137 | + |
| 138 | + then: |
| 139 | + coordinateAtDate == Optional.empty() |
| 140 | + equalsIgnoreUUID(coordinateInInterval, emptyTimeSeries) |
| 141 | + coordinatesToTimeSeries.keySet() == [validCoordinate].toSet() |
| 142 | + equalsIgnoreUUID(coordinatesToTimeSeries.get(validCoordinate), timeseries_67775) |
| 143 | + } |
| 144 | +} |
0 commit comments