]> git.basschouten.com Git - openhab-addons.git/blob
69092e0942843c27247459e260d971f3b703d9ba
[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.junit.jupiter.api.Assertions.assertEquals;
16
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.StringWriter;
22 import java.nio.charset.StandardCharsets;
23
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26
27 /**
28  * Tests for {@link org.openhab.binding.dwdunwetter.internal.dto.DwdWarningsDataAccess}
29  *
30  * @author Leo Siepel - Initial contribution
31  */
32 public class DwdWarningsDataAccessTest {
33     private TestDataProvider testDataProvider = new TestDataProvider();
34
35     @BeforeEach
36     public void setUp() throws IOException {
37         this.testDataProvider = new TestDataProvider();
38         loadXmlFromFile();
39     }
40
41     @Test
42     public void testNullOrBlank() {
43         assertEquals(testDataProvider.getDataFromEndpoint(null), "");
44         assertEquals(testDataProvider.getDataFromEndpoint(""), "");
45     }
46
47     @Test
48     public void testInvalidResponse() {
49         TestDataProvider testDataProvider = new TestDataProvider();
50         testDataProvider.rawData = "Server is not returning xml";
51         assertEquals(testDataProvider.getDataFromEndpoint("TestCity"), "");
52     }
53
54     private void loadXmlFromFile() throws IOException {
55         InputStream stream = getClass().getResourceAsStream("warnings.xml");
56         BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
57         String line = null;
58
59         StringWriter stringWriter = new StringWriter();
60         while ((line = reader.readLine()) != null) {
61             stringWriter.write(line);
62         }
63         reader.close();
64         testDataProvider.rawData = stringWriter.toString();
65     }
66
67     private class TestDataProvider extends DwdWarningDataAccess {
68
69         private String rawData = "";
70
71         @Override
72         public String getByURL(String url) {
73             return rawData;
74         }
75     }
76 }