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 {
48 <templatelist version="1">\
49 <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>\
50 <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>\
54 XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml));
55 Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
56 templates = u.unmarshal(xsr, TemplateListModel.class).getValue();
60 public void validateDeviceListModel() {
61 assertNotNull(templates);
62 assertEquals(2, templates.getTemplates().size());
63 assertEquals("1", templates.getVersion());
67 public void validateTemplate1() {
68 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39DC738C5");
69 assertTrue(optionalTemplate.isPresent());
70 assertTrue(optionalTemplate.get() instanceof TemplateModel);
72 TemplateModel template = optionalTemplate.get();
73 assertEquals("30103", template.getTemplateId());
74 assertEquals("Test template #1", template.getName());
76 assertEquals(2, template.getDeviceList().getDevices().size());
80 public void validateTemplate2() {
81 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39722FC0F");
82 assertTrue(optionalTemplate.isPresent());
83 assertTrue(optionalTemplate.get() instanceof TemplateModel);
85 TemplateModel template = optionalTemplate.get();
86 assertEquals("30003", template.getTemplateId());
87 assertEquals("Test template #2", template.getName());
89 assertEquals(1, template.getDeviceList().getDevices().size());
92 private Optional<TemplateModel> findModelByIdentifier(String identifier) {
93 return templates.getTemplates().stream().filter(it -> identifier.equals(it.getIdentifier())).findFirst();