Skip to content

Commit

Permalink
Removed 2 unnecessary stubbings in HomekitRootTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ARUS2023 committed Oct 6, 2023
1 parent 26bf090 commit ab26fd1
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ public HomekitStandaloneAccessoryServer createStandaloneAccessory(
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
}
}

public HomekitStandaloneAccessoryServer createStandaloneAccessory(
HomekitAuthInfo authInfo, HomekitAccessory accessory, int category)
throws IOException, ExecutionException, InterruptedException {
if (jmdns != null) {
return new HomekitStandaloneAccessoryServer(accessory, http, jmdns, authInfo, category);
} else {
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo, category);
return new HomekitStandaloneAccessoryServer(
accessory, http, localAddress, authInfo, category);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class HomekitStandaloneAccessoryServer {
root = new HomekitRoot(accessory.getName().get(), webHandler, jmdns, authInfo);
root.addAccessory(accessory);
}

HomekitStandaloneAccessoryServer(
HomekitAccessory accessory,
HomekitWebHandler webHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.hapjava.server.impl;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.github.hapjava.accessories.HomekitAccessory;
import io.github.hapjava.server.HomekitAccessoryCategories;
import io.github.hapjava.server.HomekitAuthInfo;
import io.github.hapjava.server.HomekitWebHandler;
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class FourthHomekitRootTest {

private HomekitAccessory accessory;
private HomekitRoot root;
private HomekitWebHandler webHandler;
private JmdnsHomekitAdvertiser advertiser;
private HomekitAuthInfo authInfo;

private static final int PORT = 12345;
private static final String SETUPID = "Gx12";

private static final String LABEL = "Test Label";

@Before
public void setup() throws Exception {
accessory = mock(HomekitAccessory.class);
when(accessory.getId()).thenReturn(2);
webHandler = mock(HomekitWebHandler.class);
advertiser = mock(JmdnsHomekitAdvertiser.class);
authInfo = mock(HomekitAuthInfo.class);
root =
new HomekitRoot(LABEL, HomekitAccessoryCategories.OTHER, webHandler, authInfo, advertiser);
}

@Test
public void verifyRegistryAdded() throws Exception {
root.addAccessory(accessory);
Assert.assertTrue(
"Registry does not contain accessory",
root.getRegistry().getAccessories().contains(accessory));
}

@Test
public void verifyRegistryRemoved() throws Exception {
root.addAccessory(accessory);
root.removeAccessory(accessory);
Assert.assertFalse(
"Registry still contains accessory",
root.getRegistry().getAccessories().contains(accessory));
}
}
56 changes: 0 additions & 56 deletions src/test/java/io/github/hapjava/server/impl/HomekitRootTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.hapjava.server.impl;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
Expand All @@ -11,10 +10,8 @@
import io.github.hapjava.server.HomekitAccessoryCategories;
import io.github.hapjava.server.HomekitAuthInfo;
import io.github.hapjava.server.HomekitWebHandler;
import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory;
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
import java.util.concurrent.CompletableFuture;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -43,53 +40,6 @@ public void setup() throws Exception {
new HomekitRoot(LABEL, HomekitAccessoryCategories.OTHER, webHandler, authInfo, advertiser);
}

@Test
public void verifyRegistryAdded() throws Exception {
root.addAccessory(accessory);
Assert.assertTrue(
"Registry does not contain accessory",
root.getRegistry().getAccessories().contains(accessory));
}

@Test
public void verifyRegistryRemoved() throws Exception {
root.addAccessory(accessory);
root.removeAccessory(accessory);
Assert.assertFalse(
"Registry still contains accessory",
root.getRegistry().getAccessories().contains(accessory));
}

@Test
public void testWebHandlerStarts() throws Exception {
root.start();
verify(webHandler).start(any(HomekitClientConnectionFactory.class));
}

@Test
public void testWebHandlerStops() throws Exception {
root.start();
root.stop();
verify(webHandler).stop();
}

@Test
public void testAdvertiserStarts() throws Exception {
final String mac = "00:00:00:00:00:00";
when(authInfo.getMac()).thenReturn(mac);
when(authInfo.getSetupId()).thenReturn(SETUPID);

root.start();
verify(advertiser).advertise(eq(LABEL), eq(1), eq(mac), eq(PORT), eq(1), eq(SETUPID));
}

@Test
public void testAdvertiserStops() throws Exception {
root.start();
root.stop();
verify(advertiser).stop();
}

@Test
public void testAddAccessoryDoesntResetWeb() {
root.start();
Expand All @@ -104,10 +54,4 @@ public void testRemoveAccessoryDoesntResetWeb() {
root.removeAccessory(accessory);
verify(webHandler, never()).resetConnections();
}

@Test(expected = IndexOutOfBoundsException.class)
public void testAddIndexOneAccessory() throws Exception {
when(accessory.getId()).thenReturn(1);
root.addAccessory(accessory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.hapjava.server.impl;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.github.hapjava.accessories.HomekitAccessory;
import io.github.hapjava.server.HomekitAccessoryCategories;
import io.github.hapjava.server.HomekitAuthInfo;
import io.github.hapjava.server.HomekitWebHandler;
import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory;
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;

public class SecondHomekitRootTest {

private HomekitAccessory accessory;
private HomekitRoot root;
private HomekitWebHandler webHandler;
private JmdnsHomekitAdvertiser advertiser;
private HomekitAuthInfo authInfo;

private static final int PORT = 12345;
private static final String SETUPID = "Gx12";

private static final String LABEL = "Test Label";

@Before
public void setup() throws Exception {
accessory = mock(HomekitAccessory.class);
webHandler = mock(HomekitWebHandler.class);
when(webHandler.start(any())).thenReturn(CompletableFuture.completedFuture(PORT));
advertiser = mock(JmdnsHomekitAdvertiser.class);
authInfo = mock(HomekitAuthInfo.class);
root =
new HomekitRoot(LABEL, HomekitAccessoryCategories.OTHER, webHandler, authInfo, advertiser);
}

@Test
public void testWebHandlerStarts() throws Exception {
root.start();
verify(webHandler).start(any(HomekitClientConnectionFactory.class));
}

@Test
public void testWebHandlerStops() throws Exception {
root.start();
root.stop();
verify(webHandler).stop();
}

@Test
public void testAdvertiserStarts() throws Exception {
final String mac = "00:00:00:00:00:00";
when(authInfo.getMac()).thenReturn(mac);
when(authInfo.getSetupId()).thenReturn(SETUPID);

root.start();
verify(advertiser).advertise(eq(LABEL), eq(1), eq(mac), eq(PORT), eq(1), eq(SETUPID));
}

@Test
public void testAdvertiserStops() throws Exception {
root.start();
root.stop();
verify(advertiser).stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.hapjava.server.impl;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.github.hapjava.accessories.HomekitAccessory;
import io.github.hapjava.server.HomekitAccessoryCategories;
import io.github.hapjava.server.HomekitAuthInfo;
import io.github.hapjava.server.HomekitWebHandler;
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
import org.junit.Before;
import org.junit.Test;

public class ThirdHomekitRootTest {

private HomekitAccessory accessory;
private HomekitRoot root;
private HomekitWebHandler webHandler;
private JmdnsHomekitAdvertiser advertiser;
private HomekitAuthInfo authInfo;

private static final int PORT = 12345;
private static final String SETUPID = "Gx12";

private static final String LABEL = "Test Label";

@Before
public void setup() throws Exception {
accessory = mock(HomekitAccessory.class);
webHandler = mock(HomekitWebHandler.class);
advertiser = mock(JmdnsHomekitAdvertiser.class);
authInfo = mock(HomekitAuthInfo.class);
root =
new HomekitRoot(LABEL, HomekitAccessoryCategories.OTHER, webHandler, authInfo, advertiser);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testAddIndexOneAccessory() throws Exception {
when(accessory.getId()).thenReturn(1);
root.addAccessory(accessory);
}
}

0 comments on commit ab26fd1

Please sign in to comment.