2 * Copyright (c) 2010-2024 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.emotiva.internal.dto;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.CHANNEL_TUNER_RDS;
18 import static org.openhab.binding.emotiva.internal.protocol.EmotivaProtocolVersion.PROTOCOL_V2;
20 import java.util.List;
22 import javax.xml.bind.JAXBException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.emotiva.internal.AbstractDTOTestBase;
27 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands;
28 import org.openhab.binding.emotiva.internal.protocol.EmotivaSubscriptionTags;
31 * Unit tests for EmotivaSubscription requests.
33 * @author Espen Fossen - Initial contribution
36 class EmotivaSubscriptionRequestTest extends AbstractDTOTestBase {
38 public EmotivaSubscriptionRequestTest() throws JAXBException {
42 void marshalFromChannelUID() {
43 EmotivaSubscriptionTags subscriptionChannel = EmotivaSubscriptionTags.fromChannelUID(CHANNEL_TUNER_RDS);
44 EmotivaSubscriptionRequest emotivaSubscriptionRequest = new EmotivaSubscriptionRequest(subscriptionChannel);
45 String xmlString = xmlUtils.marshallJAXBElementObjects(emotivaSubscriptionRequest);
46 assertThat(xmlString, containsString("<emotivaSubscription protocol=\"2.0\">"));
47 assertThat(xmlString, containsString("<tuner_RDS ack=\"yes\" />"));
48 assertThat(xmlString, containsString("</emotivaSubscription>"));
52 void marshallWithTwoSubscriptionsNoAck() {
53 EmotivaCommandDTO command1 = new EmotivaCommandDTO(EmotivaControlCommands.volume, "10", "yes");
54 EmotivaCommandDTO command2 = new EmotivaCommandDTO(EmotivaControlCommands.power_off);
56 EmotivaSubscriptionRequest dto = new EmotivaSubscriptionRequest(List.of(command1, command2),
59 String xmlString = xmlUtils.marshallJAXBElementObjects(dto);
60 assertThat(xmlString, containsString("<emotivaSubscription protocol=\"2.0\">"));
61 assertThat(xmlString, containsString("<volume value=\"10\" ack=\"yes\" />"));
62 assertThat(xmlString, containsString("<power_off />"));
63 assertThat(xmlString, containsString("</emotivaSubscription>"));
64 assertThat(xmlString, not(containsString("<volume>")));
65 assertThat(xmlString, not(containsString("<command>")));
69 void unmarshall() throws JAXBException {
70 var dto = (EmotivaSubscriptionResponse) xmlUtils.unmarshallToEmotivaDTO(emotivaSubscriptionRequest);
71 assertThat(dto, is(notNullValue()));
72 assertThat(dto.getTags().size(), is(3));
73 assertThat(dto.getProperties(), is(nullValue()));
75 List<EmotivaNotifyDTO> commands = xmlUtils.unmarshallToNotification(dto.getTags());
77 assertThat(commands.get(0).getName(), is(EmotivaSubscriptionTags.selected_mode.name()));
78 assertThat(commands.get(0).getStatus(), is(nullValue()));
79 assertThat(commands.get(0).getValue(), is(nullValue()));
80 assertThat(commands.get(0).getVisible(), is(nullValue()));
82 assertThat(commands.get(1).getName(), is(EmotivaSubscriptionTags.power.name()));
83 assertThat(commands.get(1).getStatus(), is(nullValue()));
84 assertThat(commands.get(1).getValue(), is(nullValue()));
85 assertThat(commands.get(1).getVisible(), is(nullValue()));
87 assertThat(commands.get(2).getName(), is("unknown"));
88 assertThat(commands.get(2).getStatus(), is(nullValue()));
89 assertThat(commands.get(2).getValue(), is(nullValue()));
90 assertThat(commands.get(2).getVisible(), is(nullValue()));