|
| 1 | +package io.github.hapjava.services.impl; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | +import static org.mockito.Mockito.withSettings; |
| 8 | + |
| 9 | +import io.github.hapjava.accessories.DoorbellAccessory; |
| 10 | +import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithMute; |
| 11 | +import io.github.hapjava.characteristics.Characteristic; |
| 12 | +import io.github.hapjava.characteristics.impl.audio.MuteCharacteristic; |
| 13 | +import io.github.hapjava.characteristics.impl.common.ProgrammableSwitchEventCharacteristic; |
| 14 | +import java.util.concurrent.CompletableFuture; |
| 15 | +import javax.json.JsonObject; |
| 16 | +import javax.json.JsonValue; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class DoorbellServiceTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void doorbellWithoutOptionalsOnlyHasSwitchEvent() { |
| 23 | + DoorbellService service = new DoorbellService(mock(DoorbellAccessory.class)); |
| 24 | + |
| 25 | + assertThat(service.getType()).isEqualTo("00000121-0000-1000-8000-0026BB765291"); |
| 26 | + assertThat(service.getCharacteristics()) |
| 27 | + .hasExactlyElementsOfTypes(ProgrammableSwitchEventCharacteristic.class); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void doorbellWithMuteHasMuteCharacteristic() throws Exception { |
| 32 | + DoorbellAccessory accessory = |
| 33 | + mock(DoorbellAccessory.class, withSettings().extraInterfaces(AccessoryWithMute.class)); |
| 34 | + when(((AccessoryWithMute) accessory).isMuted()) |
| 35 | + .thenReturn(CompletableFuture.completedFuture(true)); |
| 36 | + |
| 37 | + DoorbellService service = new DoorbellService(accessory); |
| 38 | + |
| 39 | + assertThat(service.getCharacteristics()) |
| 40 | + .hasExactlyElementsOfTypes( |
| 41 | + ProgrammableSwitchEventCharacteristic.class, MuteCharacteristic.class); |
| 42 | + |
| 43 | + Characteristic mute = service.getCharacteristics().get(1); |
| 44 | + JsonObject json = mute.toJson(2).join(); |
| 45 | + assertThat(json.getString("type")).isEqualTo("11A"); |
| 46 | + assertThat(json.getBoolean("value")).isTrue(); |
| 47 | + |
| 48 | + mute.setValue(JsonValue.FALSE); |
| 49 | + verify((AccessoryWithMute) accessory).setMute(false); |
| 50 | + } |
| 51 | +} |
0 commit comments