2 * Copyright (c) 2010-2021 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;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel;
27 import org.openhab.binding.avmfritz.internal.dto.templates.TemplateModel;
28 import org.openhab.binding.avmfritz.internal.util.JAXBUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Tests for {@link TemplateListModel}.
35 * @author Christoph Weitkamp - Initial contribution
38 public class AVMFritzTemplateListModelTest {
40 private final Logger logger = LoggerFactory.getLogger(AVMFritzTemplateListModelTest.class);
42 private @NonNullByDefault({}) TemplateListModel templates;
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>" +
55 Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
56 templates = (TemplateListModel) u.unmarshal(new StringReader(xml));
57 } catch (JAXBException e) {
58 logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
63 public void validateDeviceListModel() {
64 assertNotNull(templates);
65 assertEquals(2, templates.getTemplates().size());
66 assertEquals("1", templates.getVersion());
70 public void validateTemplate1() {
71 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39DC738C5");
72 assertTrue(optionalTemplate.isPresent());
73 assertTrue(optionalTemplate.get() instanceof TemplateModel);
75 TemplateModel template = optionalTemplate.get();
76 assertEquals("30103", template.getTemplateId());
77 assertEquals("Test template #1", template.getName());
79 assertEquals(2, template.getDeviceList().getDevices().size());
83 public void validateTemplate2() {
84 Optional<TemplateModel> optionalTemplate = findModelByIdentifier("tmpXXXXX-39722FC0F");
85 assertTrue(optionalTemplate.isPresent());
86 assertTrue(optionalTemplate.get() instanceof TemplateModel);
88 TemplateModel template = optionalTemplate.get();
89 assertEquals("30003", template.getTemplateId());
90 assertEquals("Test template #2", template.getName());
92 assertEquals(1, template.getDeviceList().getDevices().size());
95 private Optional<TemplateModel> findModelByIdentifier(String identifier) {
96 return templates.getTemplates().stream().filter(it -> identifier.equals(it.getIdentifier())).findFirst();