]> git.basschouten.com Git - openhab-addons.git/blob
0e1d97015129ea88b26419b8ce5650dde039ba9a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.dwdunwetter.internal.dto;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.time.Instant;
19
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22
23 /**
24  * Tests for {@link org.openhab.binding.dwdunwetter.internal.dto.DwdWarningCache}
25  *
26  * @author Martin Koehler - Initial contribution
27  */
28 public class DwdWarningCacheTest {
29
30     private DwdWarningCache cache;
31
32     @BeforeEach
33     public void setUp() {
34         cache = new DwdWarningCache();
35     }
36
37     @Test
38     public void testAddEntry() {
39         DwdWarningData data = createData("ID", 0);
40
41         assertThat(cache.addEntry(data), is(true));
42         assertThat(cache.addEntry(data), is(false));
43     }
44
45     @Test
46     public void testDeleteOldEntries() {
47         DwdWarningData data = createData("ID", 0);
48         cache.addEntry(data);
49
50         cache.deleteOldEntries();
51         assertThat(cache.addEntry(data), is(false));
52
53         data = createData("ID", 60 * 60);
54         assertThat(cache.addEntry(data), is(false));
55         cache.deleteOldEntries();
56         assertThat(cache.addEntry(data), is(true));
57     }
58
59     private DwdWarningData createData(String id, long secondsBeforeNow) {
60         DwdWarningData data = new DwdWarningData();
61         data.setId(id);
62         data.setExpires(Instant.now().minusSeconds(secondsBeforeNow));
63         return data;
64     }
65 }