|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2024 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | + |
| 14 | +package org.openhab.binding.awattar.internal; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.ZoneId; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.time.temporal.ChronoUnit; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.SortedSet; |
| 26 | +import java.util.TreeSet; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.openhab.binding.awattar.internal.handler.TimeRange; |
| 30 | + |
| 31 | +/** |
| 32 | + * The {@link AwattarBestPriceTest} contains tests for the |
| 33 | + * {@link AwattarConsecutiveBestPriceResult} and {@link AwattarNonConsecutiveBestPriceResult} logic. |
| 34 | + * |
| 35 | + * @author Thomas Leber - Initial contribution |
| 36 | + */ |
| 37 | +public class AwattarBestPriceTest { |
| 38 | + |
| 39 | + private ZoneId zoneId = ZoneId.of("GMT"); |
| 40 | + |
| 41 | + public static ZonedDateTime getCalendarForHour(int hour, ZoneId zone) { |
| 42 | + return ZonedDateTime.ofInstant(Instant.ofEpochSecond(1731283200L), zone).truncatedTo(ChronoUnit.HOURS) |
| 43 | + .plusHours(hour); |
| 44 | + } |
| 45 | + |
| 46 | + public synchronized SortedSet<AwattarPrice> getPrices() { |
| 47 | + SortedSet<AwattarPrice> prices = new TreeSet<>(Comparator.comparing(AwattarPrice::timerange)); |
| 48 | + |
| 49 | + prices.add(new AwattarPrice(103.87, 103.87, 103.87, 103.87, new TimeRange(1731283200000L, 1731286800000L))); |
| 50 | + prices.add(new AwattarPrice(100.06, 100.06, 100.06, 100.06, new TimeRange(1731286800000L, 1731290400000L))); |
| 51 | + prices.add(new AwattarPrice(99.06, 99.06, 99.06, 99.06, new TimeRange(1731290400000L, 1731294000000L))); |
| 52 | + prices.add(new AwattarPrice(99.12, 99.12, 99.12, 99.12, new TimeRange(1731294000000L, 1731297600000L))); |
| 53 | + prices.add(new AwattarPrice(105.16, 105.16, 105.16, 105.16, new TimeRange(1731297600000L, 1731301200000L))); |
| 54 | + prices.add(new AwattarPrice(124.96, 124.96, 124.96, 124.96, new TimeRange(1731301200000L, 1731304800000L))); |
| 55 | + prices.add(new AwattarPrice(143.91, 143.91, 143.91, 143.91, new TimeRange(1731304800000L, 1731308400000L))); |
| 56 | + prices.add(new AwattarPrice(141.95, 141.95, 141.95, 141.95, new TimeRange(1731308400000L, 1731312000000L))); |
| 57 | + prices.add(new AwattarPrice(135.95, 135.95, 135.95, 135.95, new TimeRange(1731312000000L, 1731315600000L))); |
| 58 | + prices.add(new AwattarPrice(130.39, 130.39, 130.39, 130.39, new TimeRange(1731315600000L, 1731319200000L))); |
| 59 | + prices.add(new AwattarPrice(124.5, 124.5, 124.5, 124.5, new TimeRange(1731319200000L, 1731322800000L))); |
| 60 | + prices.add(new AwattarPrice(119.79, 119.79, 119.79, 119.79, new TimeRange(1731322800000L, 1731326400000L))); |
| 61 | + prices.add(new AwattarPrice(131.13, 131.13, 131.13, 131.13, new TimeRange(1731326400000L, 1731330000000L))); |
| 62 | + prices.add(new AwattarPrice(133.72, 133.72, 133.72, 133.72, new TimeRange(1731330000000L, 1731333600000L))); |
| 63 | + prices.add(new AwattarPrice(141.58, 141.58, 141.58, 141.58, new TimeRange(1731333600000L, 1731337200000L))); |
| 64 | + prices.add(new AwattarPrice(146.94, 146.94, 146.94, 146.94, new TimeRange(1731337200000L, 1731340800000L))); |
| 65 | + prices.add(new AwattarPrice(150.08, 150.08, 150.08, 150.08, new TimeRange(1731340800000L, 1731344400000L))); |
| 66 | + prices.add(new AwattarPrice(146.9, 146.9, 146.9, 146.9, new TimeRange(1731344400000L, 1731348000000L))); |
| 67 | + prices.add(new AwattarPrice(139.87, 139.87, 139.87, 139.87, new TimeRange(1731348000000L, 1731351600000L))); |
| 68 | + prices.add(new AwattarPrice(123.78, 123.78, 123.78, 123.78, new TimeRange(1731351600000L, 1731355200000L))); |
| 69 | + prices.add(new AwattarPrice(119.02, 119.02, 119.02, 119.02, new TimeRange(1731355200000L, 1731358800000L))); |
| 70 | + prices.add(new AwattarPrice(116.87, 116.87, 116.87, 116.87, new TimeRange(1731358800000L, 1731362400000L))); |
| 71 | + prices.add(new AwattarPrice(109.72, 109.72, 109.72, 109.72, new TimeRange(1731362400000L, 1731366000000L))); |
| 72 | + prices.add(new AwattarPrice(107.89, 107.89, 107.89, 107.89, new TimeRange(1731366000000L, 1731369600000L))); |
| 73 | + |
| 74 | + return prices; |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void AwattarConsecutiveBestPriceResult() { |
| 79 | + int length = 8; |
| 80 | + |
| 81 | + List<AwattarPrice> range = new ArrayList<>(getPrices()); |
| 82 | + |
| 83 | + range.sort(Comparator.comparing(AwattarPrice::timerange)); |
| 84 | + AwattarConsecutiveBestPriceResult result = new AwattarConsecutiveBestPriceResult(range, length, zoneId); |
| 85 | + assertEquals("00,01,02,03,04,05,06,07", result.getHours()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void AwattarNonConsecutiveBestPriceResult_nonInverted() { |
| 90 | + int length = 6; |
| 91 | + boolean inverted = false; |
| 92 | + |
| 93 | + List<AwattarPrice> range = new ArrayList<>(getPrices()); |
| 94 | + |
| 95 | + range.sort(Comparator.comparing(AwattarPrice::timerange)); |
| 96 | + AwattarNonConsecutiveBestPriceResult result = new AwattarNonConsecutiveBestPriceResult(range, length, inverted, |
| 97 | + zoneId); |
| 98 | + assertEquals("00,01,02,03,04,23", result.getHours()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void AwattarNonConsecutiveBestPriceResult_inverted() { |
| 103 | + int length = 4; |
| 104 | + boolean inverted = true; |
| 105 | + |
| 106 | + List<AwattarPrice> range = new ArrayList<>(getPrices()); |
| 107 | + |
| 108 | + range.sort(Comparator.comparing(AwattarPrice::timerange)); |
| 109 | + AwattarNonConsecutiveBestPriceResult result = new AwattarNonConsecutiveBestPriceResult(range, length, inverted, |
| 110 | + zoneId); |
| 111 | + assertEquals("06,15,16,17", result.getHours()); |
| 112 | + } |
| 113 | +} |
0 commit comments