2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dsmr.internal;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.fail;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.dsmr.internal.device.connector.DSMRErrorStatus;
25 import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram;
26 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramListener;
27 import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramParser;
30 * Util class to read test input telegrams.
32 * @author Hilbrand Bouwkamp - Initial contribution
35 public final class TelegramReaderUtil {
36 private static final String TELEGRAM_EXT = ".telegram";
38 private TelegramReaderUtil() {
43 * Reads the raw bytes of the telegram given the file relative to this package and returns the objects.
45 * @param telegramName name of the telegram file to read
46 * @return The raw bytes of a telegram
48 public static byte[] readRawTelegram(final String telegramName) {
49 try (InputStream is = TelegramReaderUtil.class.getResourceAsStream(telegramName + TELEGRAM_EXT)) {
51 fail("Could not find telegram file with name:" + telegramName + TELEGRAM_EXT);
53 return is.readAllBytes();
54 } catch (final IOException e) {
55 throw new AssertionError("IOException reading telegram data: ", e);
60 * Reads a telegram given the file relative to this package and returns the objects.
62 * @param telegramName name of the telegram file to read
63 * @param expectedTelegramState expected state of the telegram read
64 * @return a P1Telegram object
66 public static P1Telegram readTelegram(final String telegramName) {
67 final byte[] telegram = readRawTelegram(telegramName);
68 final P1TelegramListenerImpl listener = new P1TelegramListenerImpl();
69 final P1TelegramParser parser = new P1TelegramParser(listener, true);
71 parser.setLenientMode(true);
72 parser.parse(telegram, telegram.length);
73 final P1Telegram p1Telegram = listener.telegram;
75 assertNotNull(p1Telegram, "Telegram state should have been set. (Missing newline at end of message?)");
76 assertNull(listener.state, "Expected TelegramState should not be set");
80 public static class P1TelegramListenerImpl implements P1TelegramListener {
81 public @Nullable P1Telegram telegram;
82 public @Nullable DSMRErrorStatus state;
85 public void telegramReceived(final P1Telegram telegram) {
86 this.telegram = telegram;
90 public void onError(final DSMRErrorStatus state, final String error) {