]> git.basschouten.com Git - openhab-addons.git/blob
197b170eef303b22c945b68a2d2eb0009b5b86d9
[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.modbus.e3dc.dto;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Optional;
18
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.modbus.e3dc.internal.dto.InfoBlock;
22 import org.openhab.binding.modbus.e3dc.internal.modbus.Data;
23 import org.openhab.binding.modbus.e3dc.internal.modbus.Data.DataType;
24 import org.openhab.binding.modbus.e3dc.internal.modbus.Parser;
25
26 /**
27  * The {@link InfoTest} Test Data Transfer Objects of Information Block
28  *
29  * @author Bernd Weymann - Initial contribution
30  */
31 public class InfoTest {
32     private Parser mc;
33
34     @BeforeEach
35     public void setup() {
36         byte[] infoBlock = new byte[] { -29, -36, 1, 2, 0, -120, 69, 51, 47, 68, 67, 32, 71, 109, 98, 72, 0, 0, 0, 0, 0,
37                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 32, 69, 32, 65, 73, 79, 0, 0, 0, 0, 0, 0,
38                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 78, 73, 78, 73, 84, 73, 65, 76, 73, 90, 69, 68,
39                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 95, 50, 48, 50, 48, 95, 48, 52, 0,
40                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
41         mc = new Parser(DataType.INFO);
42         mc.setArray(infoBlock);
43     }
44
45     @Test
46     public void testvalidInformationBlock() {
47         Optional<Data> infoOpt = mc.parse(DataType.INFO);
48         assertTrue(infoOpt.isPresent());
49         InfoBlock b = (InfoBlock) infoOpt.get();
50         assertNotNull(b);
51         assertEquals("E3DC", b.modbusId.toString(), "MagicByte");
52         assertEquals("S10 E AIO", b.modelName.toString(), "Model");
53         assertEquals("S10_2020_04", b.firmware.toString(), "Firmware");
54         assertEquals("E3/DC GmbH", b.manufacturer.toString(), "Manufacturer");
55     }
56
57     @Test
58     public void testWrongQuery() {
59         Optional<Data> dataOpt = mc.parse(DataType.POWER);
60         assertFalse(dataOpt.isPresent());
61     }
62
63     @Test
64     public void testInvalidBlockSize() {
65         byte[] infoBlock = new byte[] { -29, -36, 1, 2, 0, -120, 69, 51, 47, 68, 67, 32, 71, 109, 98, 72, 0, 0, 0, 0, 0,
66                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 32, 69, 32, 65, 73, 79, 0, 0, 0, 0, 0, 0,
67                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 78, 73, 78, 73, 84, 73, 65, 76, 73, 90, 69, 68,
68                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 49, 48, 95, 50, 48, 50, 48, 95, 48 };
69         Parser mc = new Parser(DataType.INFO);
70         mc.setArray(infoBlock);
71         Optional<Data> infoOpt = mc.parse(DataType.INFO);
72         InfoBlock b = (InfoBlock) infoOpt.get();
73         // block still valid but data maybe corrupted => logged in warnings
74         assertTrue(infoOpt.isPresent());
75         assertNotNull(b);
76     }
77 }