]> git.basschouten.com Git - openhab-addons.git/blob
8f3c9a8b781577e8357a17b95584c638713f535f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.emotiva.internal.dto;
14
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;
19
20 import java.util.List;
21
22 import javax.xml.bind.JAXBException;
23
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;
29
30 /**
31  * Unit tests for EmotivaSubscription requests.
32  *
33  * @author Espen Fossen - Initial contribution
34  */
35 @NonNullByDefault
36 class EmotivaSubscriptionRequestTest extends AbstractDTOTestBase {
37
38     public EmotivaSubscriptionRequestTest() throws JAXBException {
39     }
40
41     @Test
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>"));
49     }
50
51     @Test
52     void marshallWithTwoSubscriptionsNoAck() {
53         EmotivaCommandDTO command1 = new EmotivaCommandDTO(EmotivaControlCommands.volume, "10", "yes");
54         EmotivaCommandDTO command2 = new EmotivaCommandDTO(EmotivaControlCommands.power_off);
55
56         EmotivaSubscriptionRequest dto = new EmotivaSubscriptionRequest(List.of(command1, command2),
57                 PROTOCOL_V2.value());
58
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>")));
66     }
67
68     @Test
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()));
74
75         List<EmotivaNotifyDTO> commands = xmlUtils.unmarshallToNotification(dto.getTags());
76
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()));
81
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()));
86
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()));
91     }
92 }