]> git.basschouten.com Git - openhab-addons.git/blob
e5ea27182c33e296f808f62952d97c1265ddc1f8
[openhab-addons.git] /
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 package org.openhab.binding.energidataservice.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.math.BigDecimal;
19 import java.time.Clock;
20 import java.time.Duration;
21 import java.time.Instant;
22 import java.time.ZoneId;
23 import java.time.temporal.ChronoUnit;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.energidataservice.internal.api.dto.ElspotpriceRecord;
30
31 /**
32  * Tests for {@link CacheManager}.
33  * 
34  * @author Jacob Laursen - Initial contribution
35  */
36 @NonNullByDefault
37 @ExtendWith(MockitoExtension.class)
38 public class CacheManagerTest {
39
40     @Test
41     void areSpotPricesFullyCachedToday() {
42         Instant now = Instant.parse("2023-02-07T08:38:47Z");
43         Instant first = Instant.parse("2023-02-06T08:00:00Z");
44         Instant last = Instant.parse("2023-02-07T22:00:00Z");
45         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
46         CacheManager cacheManager = new CacheManager(clock);
47         populateWithSpotPrices(cacheManager, first, last);
48         assertThat(cacheManager.areSpotPricesFullyCached(), is(true));
49     }
50
51     @Test
52     void areSpotPricesFullyCachedTodayMissingAtStart() {
53         Instant now = Instant.parse("2023-02-07T08:38:47Z");
54         Instant first = Instant.parse("2023-02-06T21:00:00Z");
55         Instant last = Instant.parse("2023-02-07T22:00:00Z");
56         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
57         CacheManager cacheManager = new CacheManager(clock);
58         populateWithSpotPrices(cacheManager, first, last);
59         assertThat(cacheManager.areSpotPricesFullyCached(), is(false));
60     }
61
62     @Test
63     void areSpotPricesFullyCachedTodayMissingAtEnd() {
64         Instant now = Instant.parse("2023-02-07T08:38:47Z");
65         Instant first = Instant.parse("2023-02-06T20:00:00Z");
66         Instant last = Instant.parse("2023-02-07T21:00:00Z");
67         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
68         CacheManager cacheManager = new CacheManager(clock);
69         populateWithSpotPrices(cacheManager, first, last);
70         assertThat(cacheManager.areSpotPricesFullyCached(), is(false));
71     }
72
73     @Test
74     void areSpotPricesFullyCachedTodayOtherTimezoneIsIgnored() {
75         Instant now = Instant.parse("2023-02-07T08:38:47Z");
76         Instant first = Instant.parse("2023-02-06T08:00:00Z");
77         Instant last = Instant.parse("2023-02-07T22:00:00Z");
78         Clock clock = Clock.fixed(now, ZoneId.of("Asia/Tokyo"));
79         CacheManager cacheManager = new CacheManager(clock);
80         populateWithSpotPrices(cacheManager, first, last);
81         assertThat(cacheManager.areSpotPricesFullyCached(), is(true));
82     }
83
84     @Test
85     void areSpotPricesFullyCachedTomorrow() {
86         Instant now = Instant.parse("2023-02-07T12:00:00Z");
87         Instant first = Instant.parse("2023-02-06T12:00:00Z");
88         Instant last = Instant.parse("2023-02-08T22:00:00Z");
89         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
90         CacheManager cacheManager = new CacheManager(clock);
91         populateWithSpotPrices(cacheManager, first, last);
92         assertThat(cacheManager.areSpotPricesFullyCached(), is(true));
93     }
94
95     @Test
96     void areHistoricSpotPricesCached() {
97         Instant now = Instant.parse("2023-02-07T08:38:47Z");
98         Instant first = Instant.parse("2023-02-06T08:00:00Z");
99         Instant last = Instant.parse("2023-02-07T07:00:00Z");
100         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
101         CacheManager cacheManager = new CacheManager(clock);
102         populateWithSpotPrices(cacheManager, first, last);
103         assertThat(cacheManager.areHistoricSpotPricesCached(), is(true));
104     }
105
106     @Test
107     void areHistoricSpotPricesCachedFirstHourMissing() {
108         Instant now = Instant.parse("2023-02-07T08:38:47Z");
109         Instant first = Instant.parse("2023-02-06T21:00:00Z");
110         Instant last = Instant.parse("2023-02-07T08:00:00Z");
111         Clock clock = Clock.fixed(now, EnergiDataServiceBindingConstants.NORD_POOL_TIMEZONE);
112         CacheManager cacheManager = new CacheManager(clock);
113         populateWithSpotPrices(cacheManager, first, last);
114         assertThat(cacheManager.areHistoricSpotPricesCached(), is(false));
115     }
116
117     private void populateWithSpotPrices(CacheManager cacheManager, Instant first, Instant last) {
118         int size = (int) Duration.between(first, last).getSeconds() / 60 / 60 + 1;
119         ElspotpriceRecord[] records = new ElspotpriceRecord[size];
120         int i = 0;
121         for (Instant hourStart = first; !hourStart.isAfter(last); hourStart = hourStart.plus(1, ChronoUnit.HOURS)) {
122             records[i++] = new ElspotpriceRecord(hourStart, BigDecimal.ONE, BigDecimal.ZERO);
123         }
124         cacheManager.putSpotPrices(records, EnergiDataServiceBindingConstants.CURRENCY_DKK);
125     }
126 }