Repository navigation
Expand file tree
/
Copy pathJmdnsHomekitAdvertiserTest.java
More file actions
91 lines (78 loc) · 2.65 KB
/
Copy pathJmdnsHomekitAdvertiserTest.java
File metadata and controls
91 lines (78 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package io.github.hapjava.server.impl.jmdns;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
public class JmdnsHomekitAdvertiserTest {
JmdnsHomekitAdvertiser subject;
JmDNS jmdns;
@Before
public void setup() throws UnknownHostException, IOException {
jmdns = mock(JmDNS.class);
subject = new JmdnsHomekitAdvertiser(jmdns);
}
@Test
public void testAdvertiseTwiceFails() throws Exception {
advertise();
assertThatThrownBy(() -> advertise()).isNotNull();
}
/*
* Verify that the unregister call is for the initial registered service
* when changing discoverability causes advertising to be toggled.
*/
@Test
public void testSetDiscoverableAfterAdvertise() throws Exception {
subject.setDiscoverable(false);
advertise();
subject.setDiscoverable(true);
assertThat(getArgumentFromUnregister().getPropertyString("sf")).isEqualTo("0");
}
/*
* Verify that the unregister call is for the initial registered service
* when changing the config index causes advertising to be toggled.
*/
@Test
public void testSetConfigurationIndex() throws Exception {
subject.setConfigurationIndex(1);
advertise();
subject.setConfigurationIndex(2);
assertThat(getArgumentFromUnregister().getPropertyString("c#")).isEqualTo("1");
}
@Test
public void testStopClosesOwnedJmdns() throws Exception {
subject = new JmdnsHomekitAdvertiser(jmdns, true);
advertise();
subject.stop();
verify(jmdns).close();
}
@Test
public void testStopDoesNotCloseSuppliedJmdns() throws Exception {
advertise();
subject.stop();
verify(jmdns, never()).close();
}
@Test
public void testSetDiscoverableAfterStopDoesNotRegister() throws Exception {
advertise();
subject.stop();
subject.setDiscoverable(false);
verify(jmdns).registerService(any(ServiceInfo.class));
}
private ServiceInfo getArgumentFromUnregister() {
ArgumentCaptor<ServiceInfo> serviceInfoCaptor = ArgumentCaptor.forClass(ServiceInfo.class);
verify(jmdns).unregisterService(serviceInfoCaptor.capture());
return serviceInfoCaptor.getValue();
}
private void advertise() throws Exception {
subject.advertise("test", 1, "00:00:00:00:00:00", 1234, 1, "1");
}
}