|
| 1 | +package com.beowulfe.hap; |
| 2 | + |
| 3 | +import static org.mockito.Matchers.any; |
| 4 | +import static org.mockito.Matchers.eq; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import java.util.concurrent.CompletableFuture; |
| 8 | + |
| 9 | +import org.junit.*; |
| 10 | + |
| 11 | +import com.beowulfe.hap.impl.HomekitWebHandler; |
| 12 | +import com.beowulfe.hap.impl.http.HomekitClientConnectionFactory; |
| 13 | +import com.beowulfe.hap.impl.jmdns.JmdnsHomekitAdvertiser; |
| 14 | + |
| 15 | +public class HomekitRootTest { |
| 16 | + |
| 17 | + private HomekitAccessory accessory; |
| 18 | + private HomekitRoot root; |
| 19 | + private HomekitWebHandler webHandler; |
| 20 | + private JmdnsHomekitAdvertiser advertiser; |
| 21 | + private HomekitAuthInfo authInfo; |
| 22 | + |
| 23 | + private final static int PORT = 12345; |
| 24 | + private final static String LABEL = "Test Label"; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setup() throws Exception { |
| 28 | + accessory = mock(HomekitAccessory.class); |
| 29 | + when(accessory.getId()).thenReturn(2); |
| 30 | + webHandler = mock(HomekitWebHandler.class); |
| 31 | + when(webHandler.start(any())).thenReturn(CompletableFuture.completedFuture(PORT)); |
| 32 | + advertiser = mock(JmdnsHomekitAdvertiser.class); |
| 33 | + authInfo = mock(HomekitAuthInfo.class); |
| 34 | + root = new HomekitRoot(LABEL, webHandler, authInfo, advertiser); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void verifyRegistryAdded() throws Exception { |
| 39 | + root.addAccessory(accessory); |
| 40 | + Assert.assertTrue("Registry does not contain accessory", root.getRegistry().getAccessories().contains(accessory)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void verifyRegistryRemoved() throws Exception { |
| 45 | + root.addAccessory(accessory); |
| 46 | + root.removeAccessory(accessory); |
| 47 | + Assert.assertFalse("Registry still contains accessory", root.getRegistry().getAccessories().contains(accessory)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testWebHandlerStarts() throws Exception { |
| 52 | + root.start(); |
| 53 | + verify(webHandler).start(any(HomekitClientConnectionFactory.class)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testWebHandlerStops() throws Exception { |
| 58 | + root.start(); |
| 59 | + root.stop(); |
| 60 | + verify(webHandler).stop(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAdvertiserStarts() throws Exception { |
| 65 | + String mac = "00:00:00:00:00:00"; |
| 66 | + when(authInfo.getMac()).thenReturn(mac); |
| 67 | + root.start(); |
| 68 | + verify(advertiser).advertise(eq(LABEL), eq(mac), eq(PORT)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testAdvertiserStops() throws Exception { |
| 73 | + root.start(); |
| 74 | + root.stop(); |
| 75 | + verify(advertiser).stop(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testAddAccessoryResetsWeb() { |
| 80 | + root.start(); |
| 81 | + verify(webHandler, never()).resetConnections(); |
| 82 | + root.addAccessory(accessory); |
| 83 | + verify(webHandler).resetConnections(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testRemoveAccessoryResetsWeb() { |
| 88 | + root.addAccessory(accessory); |
| 89 | + root.start(); |
| 90 | + verify(webHandler, never()).resetConnections(); |
| 91 | + root.removeAccessory(accessory); |
| 92 | + verify(webHandler).resetConnections(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test(expected=IndexOutOfBoundsException.class) |
| 96 | + public void testAddIndexOneAccessory() throws Exception { |
| 97 | + when(accessory.getId()).thenReturn(1); |
| 98 | + root.addAccessory(accessory); |
| 99 | + } |
| 100 | +} |
0 commit comments