|
| 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.couchbase |
| 7 | + |
| 8 | +import edu.ie3.datamodel.io.connectors.CouchbaseConnector |
| 9 | +import edu.ie3.datamodel.io.factory.timeseries.IconTimeBasedWeatherValueFactory |
| 10 | +import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueFactory |
| 11 | +import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries |
| 12 | +import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue |
| 13 | +import edu.ie3.test.common.IconWeatherTestData |
| 14 | +import edu.ie3.test.helper.WeatherSourceTestHelper |
| 15 | +import edu.ie3.util.TimeUtil |
| 16 | +import edu.ie3.util.interval.ClosedInterval |
| 17 | +import org.testcontainers.couchbase.BucketDefinition |
| 18 | +import org.testcontainers.couchbase.CouchbaseContainer |
| 19 | +import org.testcontainers.spock.Testcontainers |
| 20 | +import org.testcontainers.utility.MountableFile |
| 21 | +import spock.lang.Shared |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +import java.time.ZoneId |
| 25 | + |
| 26 | +@Testcontainers |
| 27 | +class CouchbaseWeatherSourceIconIT extends Specification implements WeatherSourceTestHelper { |
| 28 | + |
| 29 | + @Shared |
| 30 | + BucketDefinition bucketDefinition = new BucketDefinition("ie3_in") |
| 31 | + |
| 32 | + @Shared |
| 33 | + CouchbaseContainer couchbaseContainer = new CouchbaseContainer("couchbase/server:6.0.2").withBucket(bucketDefinition) |
| 34 | + .withExposedPorts(8091, 8092, 8093, 8094, 11210) |
| 35 | + |
| 36 | + @Shared |
| 37 | + CouchbaseWeatherSource source |
| 38 | + |
| 39 | + static String coordinateIdColumnName = TimeBasedWeatherValueFactory.COORDINATE_ID_NAMING.flatCase() |
| 40 | + |
| 41 | + def setupSpec() { |
| 42 | + // Copy import file with json array of documents into docker |
| 43 | + def couchbaseWeatherJsonsFile = MountableFile.forClasspathResource("/testcontainersFiles/couchbase/icon/weather.json") |
| 44 | + couchbaseContainer.copyFileToContainer(couchbaseWeatherJsonsFile, "/home/weather.json") |
| 45 | + |
| 46 | + // create an index for the document keys |
| 47 | + couchbaseContainer.execInContainer("cbq", |
| 48 | + "-e", "http://localhost:8093", |
| 49 | + "-u", couchbaseContainer.username, |
| 50 | + "-p", couchbaseContainer.password, |
| 51 | + "-s", "CREATE index id_idx ON `" + bucketDefinition.name + "` (META().id);") |
| 52 | + |
| 53 | + //import the json documents from the copied file |
| 54 | + couchbaseContainer.execInContainer("cbimport", "json", |
| 55 | + "-cluster", "http://localhost:8091", |
| 56 | + "--bucket", "ie3_in", |
| 57 | + "--username", couchbaseContainer.username, |
| 58 | + "--password", couchbaseContainer.password, |
| 59 | + "--format", "list", |
| 60 | + "--generate-key", "weather::%" + coordinateIdColumnName + "%::%time%", |
| 61 | + "--dataset", "file:///home/weather.json") |
| 62 | + |
| 63 | + def connector = new CouchbaseConnector(couchbaseContainer.connectionString, bucketDefinition.name, couchbaseContainer.username, couchbaseContainer.password) |
| 64 | + def weatherFactory = new IconTimeBasedWeatherValueFactory(new TimeUtil(ZoneId.of("UTC"), Locale.GERMANY, "yyyy-MM-dd'T'HH:mm:ssxxx")) |
| 65 | + source = new CouchbaseWeatherSource(connector, IconWeatherTestData.coordinateSource, weatherFactory) |
| 66 | + } |
| 67 | + |
| 68 | + def "The test container can establish a valid connection"() { |
| 69 | + when: |
| 70 | + def connector = new CouchbaseConnector(couchbaseContainer.connectionString, bucketDefinition.name, couchbaseContainer.username, couchbaseContainer.password) |
| 71 | + |
| 72 | + then: |
| 73 | + connector.connectionValid |
| 74 | + } |
| 75 | + |
| 76 | + def "A CouchbaseWeatherSource can read and correctly parse a single value for a specific date and coordinate"() { |
| 77 | + given: |
| 78 | + def expectedTimeBasedValue = new TimeBasedValue(IconWeatherTestData.TIME_15H, IconWeatherTestData.WEATHER_VALUE_67775_15H) |
| 79 | + |
| 80 | + when: |
| 81 | + def optTimeBasedValue = source.getWeather(IconWeatherTestData.TIME_15H, IconWeatherTestData.COORDINATE_67775) |
| 82 | + |
| 83 | + then: |
| 84 | + optTimeBasedValue.present |
| 85 | + equalsIgnoreUUID(optTimeBasedValue.get(), expectedTimeBasedValue) |
| 86 | + } |
| 87 | + |
| 88 | + def "A CouchbaseWeatherSource can read multiple time series values for multiple coordinates"() { |
| 89 | + given: |
| 90 | + def coordinates = [ |
| 91 | + IconWeatherTestData.COORDINATE_67775, |
| 92 | + IconWeatherTestData.COORDINATE_67776 |
| 93 | + ] |
| 94 | + def timeInterval = new ClosedInterval(IconWeatherTestData.TIME_16H, IconWeatherTestData.TIME_17H) |
| 95 | + def timeSeries67775 = new IndividualTimeSeries(null, |
| 96 | + [ |
| 97 | + new TimeBasedValue(IconWeatherTestData.TIME_16H, IconWeatherTestData.WEATHER_VALUE_67775_16H), |
| 98 | + new TimeBasedValue(IconWeatherTestData.TIME_17H, IconWeatherTestData.WEATHER_VALUE_67775_17H)] |
| 99 | + as Set<TimeBasedValue>) |
| 100 | + def timeSeries67776 = new IndividualTimeSeries(null, |
| 101 | + [ |
| 102 | + new TimeBasedValue(IconWeatherTestData.TIME_16H, IconWeatherTestData.WEATHER_VALUE_67776_16H)] as Set<TimeBasedValue>) |
| 103 | + |
| 104 | + when: |
| 105 | + def coordinateToTimeSeries = source.getWeather(timeInterval, coordinates) |
| 106 | + |
| 107 | + then: |
| 108 | + coordinateToTimeSeries.keySet().size() == 2 |
| 109 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67775), timeSeries67775) |
| 110 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67776), timeSeries67776) |
| 111 | + } |
| 112 | + |
| 113 | + def "A CouchbaseWeatherSource can read all weather data in a given time interval"() { |
| 114 | + given: |
| 115 | + def timeInterval = new ClosedInterval(IconWeatherTestData.TIME_15H, IconWeatherTestData.TIME_17H) |
| 116 | + def timeSeries67775 = new IndividualTimeSeries(null, |
| 117 | + [ |
| 118 | + new TimeBasedValue(IconWeatherTestData.TIME_15H, IconWeatherTestData.WEATHER_VALUE_67775_15H), |
| 119 | + new TimeBasedValue(IconWeatherTestData.TIME_16H, IconWeatherTestData.WEATHER_VALUE_67775_16H), |
| 120 | + new TimeBasedValue(IconWeatherTestData.TIME_17H, IconWeatherTestData.WEATHER_VALUE_67775_17H)] as Set<TimeBasedValue>) |
| 121 | + def timeSeries67776 = new IndividualTimeSeries(null, |
| 122 | + [ |
| 123 | + new TimeBasedValue(IconWeatherTestData.TIME_15H, IconWeatherTestData.WEATHER_VALUE_67776_15H), |
| 124 | + new TimeBasedValue(IconWeatherTestData.TIME_16H, IconWeatherTestData.WEATHER_VALUE_67776_16H)] as Set<TimeBasedValue>) |
| 125 | + |
| 126 | + when: |
| 127 | + def coordinateToTimeSeries = source.getWeather(timeInterval) |
| 128 | + |
| 129 | + then: |
| 130 | + coordinateToTimeSeries.keySet().size() == 2 |
| 131 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67775).entries, timeSeries67775.entries) |
| 132 | + equalsIgnoreUUID(coordinateToTimeSeries.get(IconWeatherTestData.COORDINATE_67776).entries, timeSeries67776.entries) |
| 133 | + } |
| 134 | +} |
0 commit comments