diff --git a/src/main/java/jlifx/bulb/DiscoveryService.java b/src/main/java/jlifx/bulb/DiscoveryService.java index c978dc63..62e58166 100644 --- a/src/main/java/jlifx/bulb/DiscoveryService.java +++ b/src/main/java/jlifx/bulb/DiscoveryService.java @@ -64,7 +64,7 @@ private static GatewayBulb waitForReply(DatagramSocket socket) throws IOExceptio socket.setSoTimeout(500); int retries = 3; byte[] byteArray = new byte[128]; - while (retries > 0) { + while (retries-- > 0) { DatagramPacket answer = new DatagramPacket(byteArray, byteArray.length); try { socket.receive(answer); @@ -76,7 +76,6 @@ private static GatewayBulb waitForReply(DatagramSocket socket) throws IOExceptio Packet packet = Packet.fromDatagramPacket(answer); return new GatewayBulb(answer.getAddress(), packet.getGatewayMac(), packet.getTargetMac()); } - retries--; } return result; } diff --git a/src/main/java/jlifx/commandline/command/ScanCommand.java b/src/main/java/jlifx/commandline/command/ScanCommand.java index 1cb79a1d..89d3e93b 100644 --- a/src/main/java/jlifx/commandline/command/ScanCommand.java +++ b/src/main/java/jlifx/commandline/command/ScanCommand.java @@ -19,10 +19,7 @@ public boolean execute(String[] args, PrintStream out) throws IOException { out.println(""); return false; } else { - out.println("Found LIFX gateway bulb:"); - out.println("IP address : " + gatewayBulb.getInetAddress().getHostAddress()); - out.println("GW MAC address : " + gatewayBulb.getMacAddressAsString()); - out.println("MAC address : " + gatewayBulb.getGatewayMacAddressAsString()); + showGatewayBulbInfo(out, gatewayBulb); Collection allBulbs = DiscoveryService.discoverAllBulbs(gatewayBulb); out.println("Found " + allBulbs.size() + " bulb(s) in network:"); for (IBulb bulb : allBulbs) { @@ -33,4 +30,11 @@ public boolean execute(String[] args, PrintStream out) throws IOException { return true; } + private void showGatewayBulbInfo(PrintStream out, GatewayBulb gatewayBulb) { + out.println("Found LIFX gateway bulb:"); + out.println("IP address : " + gatewayBulb.getInetAddress().getHostAddress()); + out.println("GW MAC address : " + gatewayBulb.getMacAddressAsString()); + out.println("MAC address : " + gatewayBulb.getGatewayMacAddressAsString()); + } + } \ No newline at end of file diff --git a/src/main/java/jlifx/packet/Packet.java b/src/main/java/jlifx/packet/Packet.java index 7a8fac93..9dd0412a 100644 --- a/src/main/java/jlifx/packet/Packet.java +++ b/src/main/java/jlifx/packet/Packet.java @@ -34,6 +34,14 @@ public Packet(byte[] targetMac, byte[] gatewayMac, byte[] timestamp, byte type) setType(type); } + public Packet(Packet packet) { + setTargetMac(packet.getTargetMac()); + setGatewayMac(packet.getGatewayMac()); + setTimestamp(packet.getTimestamp()); + setType(packet.getType()); + setPayload(packet.getPayload()); + } + public byte[] getTargetMac() { return targetMac; } diff --git a/src/main/java/jlifx/packet/WifiInfoResponsePacket.java b/src/main/java/jlifx/packet/WifiInfoResponsePacket.java index 3af3df8b..5a54150f 100644 --- a/src/main/java/jlifx/packet/WifiInfoResponsePacket.java +++ b/src/main/java/jlifx/packet/WifiInfoResponsePacket.java @@ -1,13 +1,13 @@ package jlifx.packet; -public class WifiInfoResponsePacket { - private final Packet packet; +public class WifiInfoResponsePacket extends Packet { public WifiInfoResponsePacket(Packet packet) { - this.packet = packet; + super(packet); } public float getSignalStrength() { return 0; // TODO } -} + +} \ No newline at end of file diff --git a/src/test/java/jlifx/bulb/AbstractJLifxTestCase.java b/src/test/java/jlifx/bulb/AbstractJLifxTestCase.java new file mode 100644 index 00000000..d92db935 --- /dev/null +++ b/src/test/java/jlifx/bulb/AbstractJLifxTestCase.java @@ -0,0 +1,25 @@ +package jlifx.bulb; + +import java.io.PrintStream; +import java.util.Collections; + +import jlifx.commandline.AbstractBulbCommand; + +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.easymock.EasyMockSupport; + +public class AbstractJLifxTestCase extends EasyMockSupport { + private final IBulb mockedBulb = createMock(IBulb.class); + + protected IBulb getMockedBulb() { + return mockedBulb; + } + + protected PrintStream getPrintStream() { + return new PrintStream(new ByteArrayOutputStream()); + } + + protected void executeCommand(AbstractBulbCommand command, IBulb bulb, String... commandArgs) throws Exception { + command.execute(Collections.singletonList(bulb), commandArgs, getPrintStream()); + } +} diff --git a/src/test/java/jlifx/commandline/command/BlinkCommandTest.java b/src/test/java/jlifx/commandline/command/BlinkCommandTest.java index 83fd1504..f0703604 100644 --- a/src/test/java/jlifx/commandline/command/BlinkCommandTest.java +++ b/src/test/java/jlifx/commandline/command/BlinkCommandTest.java @@ -1,29 +1,23 @@ package jlifx.commandline.command; -import java.io.PrintStream; -import java.util.Collections; - +import jlifx.bulb.AbstractJLifxTestCase; import jlifx.bulb.IBulb; -import org.apache.commons.io.output.ByteArrayOutputStream; import org.easymock.EasyMock; -import org.easymock.EasyMockSupport; import org.junit.Test; -public class BlinkCommandTest extends EasyMockSupport { +public class BlinkCommandTest extends AbstractJLifxTestCase { @Test public void testBlinkTwoTimes() throws Exception { - BlinkCommand command = new BlinkCommand(); - IBulb bulb = createMock(IBulb.class); + IBulb bulb = getMockedBulb(); bulb.switchOn(); EasyMock.expectLastCall().times(2); bulb.switchOff(); EasyMock.expectLastCall().times(2); replayAll(); - command.execute(Collections.singletonList(bulb), new String[] {"-times", "2"}, new PrintStream( - new ByteArrayOutputStream())); + executeCommand(new BlinkCommand(), bulb, "times", "2"); verifyAll(); } diff --git a/src/test/java/jlifx/commandline/command/DimCommandTest.java b/src/test/java/jlifx/commandline/command/DimCommandTest.java new file mode 100644 index 00000000..dfdece8e --- /dev/null +++ b/src/test/java/jlifx/commandline/command/DimCommandTest.java @@ -0,0 +1,24 @@ +package jlifx.commandline.command; + +import static org.easymock.EasyMock.expectLastCall; +import jlifx.bulb.AbstractJLifxTestCase; +import jlifx.bulb.IBulb; + +import org.junit.Test; + +public class DimCommandTest extends AbstractJLifxTestCase { + + @Test + public void testDimBulb() throws Exception { + DimCommand command = new DimCommand(); + IBulb bulb = getMockedBulb(); + bulb.setDim(0.5F); + expectLastCall().once(); + replayAll(); + + executeCommand(command, bulb, "dim", "0.5"); + + verifyAll(); + } + +} diff --git a/src/test/java/jlifx/commandline/command/RainbowCommandTest.java b/src/test/java/jlifx/commandline/command/RainbowCommandTest.java new file mode 100644 index 00000000..97629af3 --- /dev/null +++ b/src/test/java/jlifx/commandline/command/RainbowCommandTest.java @@ -0,0 +1,27 @@ +package jlifx.commandline.command; + +import static org.easymock.EasyMock.isA; + +import java.awt.Color; + +import jlifx.bulb.AbstractJLifxTestCase; +import jlifx.bulb.IBulb; + +import org.easymock.EasyMock; +import org.junit.Test; + +public class RainbowCommandTest extends AbstractJLifxTestCase { + + @Test + public void testTimed() throws Exception { + IBulb bulb = getMockedBulb(); + bulb.colorize(isA(Color.class), EasyMock.gt(0), EasyMock.eq(1.0F)); + EasyMock.expectLastCall().atLeastOnce(); + replayAll(); + + executeCommand(new RainbowCommand(), bulb, "duration", "2"); + + verifyAll(); + } + +} \ No newline at end of file diff --git a/src/test/java/jlifx/commandline/command/SwitchCommandTest.java b/src/test/java/jlifx/commandline/command/SwitchCommandTest.java new file mode 100644 index 00000000..e1697a85 --- /dev/null +++ b/src/test/java/jlifx/commandline/command/SwitchCommandTest.java @@ -0,0 +1,22 @@ +package jlifx.commandline.command; + +import static org.easymock.EasyMock.expectLastCall; +import jlifx.bulb.AbstractJLifxTestCase; +import jlifx.bulb.IBulb; + +import org.junit.Test; + +public class SwitchCommandTest extends AbstractJLifxTestCase { + + @Test + public void testSwitchOn() throws Exception { + IBulb bulb = getMockedBulb(); + bulb.switchOn(); + expectLastCall().once(); + replayAll(); + + executeCommand(new SwitchCommand(), bulb, "switch", "on"); + + verifyAll(); + } +} diff --git a/src/test/java/jlifx/packet/PacketServiceTest.java b/src/test/java/jlifx/packet/PacketServiceTest.java index 73fa3f4d..d9e7e896 100644 --- a/src/test/java/jlifx/packet/PacketServiceTest.java +++ b/src/test/java/jlifx/packet/PacketServiceTest.java @@ -22,7 +22,7 @@ public class PacketServiceTest extends EasyMockSupport { private static final byte[] TEST_MAC_ADDRESS_1 = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; private static final byte[] TEST_MAC_ADDRESS_2 = new byte[] {0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; - private PacketWriter buildSimplePacketWriterMock(Class packetClass) throws UnknownHostException, + private PacketWriter buildPacketWriterMock(Class packetClass) throws UnknownHostException, IOException { PacketWriter result = createMock(PacketWriter.class); result.connect(InetAddress.getLocalHost()); @@ -31,11 +31,20 @@ private PacketWriter buildSimplePacketWriterMock(Class packetC return result; } + private PacketWriter buildPacketWriterMockWithResponse(Class packetClass, Packet responsePacket) + throws UnknownHostException, IOException { + PacketWriter result = createMock(PacketWriter.class); + List returnValue = Collections.singletonList(responsePacket); + EasyMock.expect(result.sendPacketAndWaitForResponse(isA(GatewayBulb.class), isA(packetClass))) + .andReturn(returnValue); + return result; + } + @Test public void testSendPowerManagementPacket() throws Exception { GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2); PacketService packetService = new PacketService(); - packetService.setPacketWriter(buildSimplePacketWriterMock(PowerManagementPacket.class)); + packetService.setPacketWriter(buildPacketWriterMock(PowerManagementPacket.class)); replayAll(); packetService.sendPowerManagementPacket(bulb, true); @@ -47,7 +56,7 @@ public void testSendPowerManagementPacket() throws Exception { public void testSendColorManagementPacket() throws Exception { GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2); PacketService packetService = new PacketService(); - packetService.setPacketWriter(buildSimplePacketWriterMock(ColorManagementPacket.class)); + packetService.setPacketWriter(buildPacketWriterMock(ColorManagementPacket.class)); replayAll(); packetService.sendColorManagementPacket(bulb, Color.BLUE, 3, 0.5F); @@ -59,7 +68,7 @@ public void testSendColorManagementPacket() throws Exception { public void testSendSetDimAbsolutePacket() throws Exception { GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2); PacketService packetService = new PacketService(); - packetService.setPacketWriter(buildSimplePacketWriterMock(SetDimAbsolutePacket.class)); + packetService.setPacketWriter(buildPacketWriterMock(SetDimAbsolutePacket.class)); replayAll(); packetService.sendSetDimAbsolutePacket(bulb, 0.5F); @@ -71,19 +80,29 @@ public void testSendSetDimAbsolutePacket() throws Exception { public void testSendStatusRequestPacket() throws Exception { GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2); PacketService packetService = new PacketService(); - PacketWriter packetWriter = createMock(PacketWriter.class); - List returnValue = Collections.singletonList(new StatusResponsePacket(new Packet())); - EasyMock.expect( - packetWriter.sendPacketAndWaitForResponse(isA(GatewayBulb.class), isA(StatusRequestPacket.class))) - .andReturn( - returnValue); - packetService.setPacketWriter(packetWriter); + packetService.setPacketWriter(buildPacketWriterMockWithResponse(StatusRequestPacket.class, + new StatusResponsePacket(new Packet()))); replayAll(); - + List result = packetService.sendStatusRequestPacket(bulb); verifyAll(); assertNotNull(result); assertEquals(1, result.size()); } + + @Test + public void testSendWifiInfoRequestPacket() throws Exception { + GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2); + PacketService packetService = new PacketService(); + packetService.setPacketWriter(buildPacketWriterMockWithResponse(WifiInfoRequestPacket.class, + new WifiInfoResponsePacket(new Packet()))); + replayAll(); + + List result = packetService.sendWifiInfoRequestPacket(bulb); + + verifyAll(); + assertNotNull(result); + assertEquals(1, result.size()); + } } \ No newline at end of file