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.avmfritz.internal.dto;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.io.StringReader;
18 import java.util.Optional;
20 import javax.xml.bind.JAXBException;
21 import javax.xml.bind.Unmarshaller;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamReader;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel;
29 import org.openhab.binding.avmfritz.internal.dto.templates.TemplateModel;
30 import org.openhab.binding.avmfritz.internal.util.JAXBUtils;
33 * Tests for {@link TemplateListModel}.
35 * @author Christoph Weitkamp - Initial contribution
38 public class AVMFritzTemplateListModelTest {
40 private @NonNullByDefault({}) TemplateListModel templates;
42 @SuppressWarnings("null")
44 public void setUp() throws JAXBException, XMLStreamException {
47 "<templatelist version=\"1\">" +
48 "<template identifier=\"tmpXXXXX-39DC738C5\" id=\"30103\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #1</name><devices><device identifier=\"YY:5D:AA-900\" /><device identifier=\"XX:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
49 "<template identifier=\"tmpXXXXX-39722FC0F\" id=\"30003\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #2</name><devices><device identifier=\"YY:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
52 XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml));
53 Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
54 templates = u.unmarshal(xsr, TemplateListModel.class).getValue();
58 public void validateDeviceListModel() {
59 assertNotNull(templates);
60 assertEquals(2, templates.getTemplates().size());
61 assertEquals("1", templates.getVersion());
65 public void validateTemplate1() {
66 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39DC738C5");
67 assertTrue(optionalTemplate.isPresent());
68 assertTrue(optionalTemplate.get() instanceof TemplateModel);
70 TemplateModel template = optionalTemplate.get();
71 assertEquals("30103", template.getTemplateId());
72 assertEquals("Test template #1", template.getName());
74 assertEquals(2, template.getDeviceList().getDevices().size());
78 public void validateTemplate2() {
79 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39722FC0F");
80 assertTrue(optionalTemplate.isPresent());
81 assertTrue(optionalTemplate.get() instanceof TemplateModel);
83 TemplateModel template = optionalTemplate.get();
84 assertEquals("30003", template.getTemplateId());
85 assertEquals("Test template #2", template.getName());
87 assertEquals(1, template.getDeviceList().getDevices().size());
90 private Optional<TemplateModel> findModelByIdentifier(String identifier) {
91 return templates.getTemplates().stream().filter(it -> identifier.equals(it.getIdentifier())).findFirst();