From 1bd946161ef3ec32f793f020368fa0ac38df479c Mon Sep 17 00:00:00 2001 From: robvanderleek Date: Sat, 12 Sep 2015 18:46:05 +0200 Subject: [PATCH] Working on #2 --- .../java/jlifx/boblightd/NetworkUtils.java | 42 +++++++ .../commandline/command/ColorCommandTest.java | 40 +++++++ .../commandline/command/ScanCommandTest.java | 7 ++ .../jlifx/packet/TcpPacketReaderTest.java | 112 ++++++++++++++++++ .../jlifx/packet/UdpPacketReaderTest.java | 92 ++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 src/main/java/jlifx/boblightd/NetworkUtils.java create mode 100644 src/test/java/jlifx/commandline/command/ColorCommandTest.java create mode 100644 src/test/java/jlifx/commandline/command/ScanCommandTest.java create mode 100644 src/test/java/jlifx/packet/TcpPacketReaderTest.java create mode 100644 src/test/java/jlifx/packet/UdpPacketReaderTest.java diff --git a/src/main/java/jlifx/boblightd/NetworkUtils.java b/src/main/java/jlifx/boblightd/NetworkUtils.java new file mode 100644 index 00000000..d7aa3071 --- /dev/null +++ b/src/main/java/jlifx/boblightd/NetworkUtils.java @@ -0,0 +1,42 @@ +package jlifx.boblightd; + +import io.netty.bootstrap.Bootstrap; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioDatagramChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +public final class NetworkUtils { + private NetworkUtils() {} + + public static void startTcpServer(int port, ChannelHandlerAdapter channelHandlerAdapter) + throws InterruptedException { + EventLoopGroup bossGroup = new NioEventLoopGroup(); + EventLoopGroup workerGroup = new NioEventLoopGroup(); + ServerBootstrap server = new ServerBootstrap(); + server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(channelHandlerAdapter); + }; + }).option(ChannelOption.SO_BACKLOG, 100); + ChannelFuture f = server.bind(port).sync(); + f.channel().closeFuture().sync(); + } + + public static void startUdpServer(int port, ChannelHandler channelHandler) throws InterruptedException { + EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); + Bootstrap server = new Bootstrap(); + server.group(eventLoopGroup).channel(NioDatagramChannel.class).handler(channelHandler) + .option(ChannelOption.SO_BACKLOG, 100); + ChannelFuture f = server.bind(port).sync(); + f.channel().closeFuture().sync(); + } +} \ No newline at end of file diff --git a/src/test/java/jlifx/commandline/command/ColorCommandTest.java b/src/test/java/jlifx/commandline/command/ColorCommandTest.java new file mode 100644 index 00000000..fa9f7370 --- /dev/null +++ b/src/test/java/jlifx/commandline/command/ColorCommandTest.java @@ -0,0 +1,40 @@ +package jlifx.commandline.command; + +import static org.easymock.EasyMock.expectLastCall; + +import java.awt.Color; + +import jlifx.bulb.AbstractJLifxTestCase; +import jlifx.bulb.IBulb; + +import org.junit.Test; + +public class ColorCommandTest extends AbstractJLifxTestCase { + + @Test + public void testColorBulbDefaultBrightness() throws Exception { + ColorCommand command = new ColorCommand(); + IBulb bulb = getMockedBulb(); + bulb.colorize(Color.GREEN, 3, 1.0F); + expectLastCall().once(); + replayAll(); + + executeCommand(command, bulb, "color", "green"); + + verifyAll(); + } + + @Test + public void testColorBulbDefaultLowBrightness() throws Exception { + ColorCommand command = new ColorCommand(); + IBulb bulb = getMockedBulb(); + bulb.colorize(Color.GREEN, 3, 0.1F); + expectLastCall().once(); + replayAll(); + + executeCommand(command, bulb, "color", "green", "0.1"); + + verifyAll(); + } + +} diff --git a/src/test/java/jlifx/commandline/command/ScanCommandTest.java b/src/test/java/jlifx/commandline/command/ScanCommandTest.java new file mode 100644 index 00000000..655ba851 --- /dev/null +++ b/src/test/java/jlifx/commandline/command/ScanCommandTest.java @@ -0,0 +1,7 @@ +package jlifx.commandline.command; + +import jlifx.bulb.AbstractJLifxTestCase; + +public class ScanCommandTest extends AbstractJLifxTestCase { + +} diff --git a/src/test/java/jlifx/packet/TcpPacketReaderTest.java b/src/test/java/jlifx/packet/TcpPacketReaderTest.java new file mode 100644 index 00000000..426b707b --- /dev/null +++ b/src/test/java/jlifx/packet/TcpPacketReaderTest.java @@ -0,0 +1,112 @@ +package jlifx.packet; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import org.junit.Test; + +import jlifx.bulb.AbstractJLifxTestCase; + +public class TcpPacketReaderTest extends AbstractJLifxTestCase { + + @Test + public void testNoPacket() throws Exception { + InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] {})); + + TcpPacketReader packetReader = new TcpPacketReader(inputStream); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertTrue(receivedPackets.isEmpty()); + } + + @Test + public void testOnePacket() throws Exception { + PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_1, true); + byte[] bytes = packet.toByteArray(); + InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(bytes)); + + TcpPacketReader packetReader = new TcpPacketReader(inputStream); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertFalse(receivedPackets.isEmpty()); + assertEquals(1, receivedPackets.size()); + + PowerManagementPacket receivedPacket = new PowerManagementPacket(receivedPackets.get(0)); + + assertArrayEquals(TEST_MAC_ADDRESS_1, receivedPacket.getTargetMac()); + assertTrue(receivedPacket.on()); + } + + private final class InputStreamExtension extends InputStream { + private int request = 0; + + @Override + public int read() throws IOException { + return 0; + } + + private int copy(byte[] src, byte[] dst) { + for (int i = 0; i < src.length; i++) { + dst[i] = src[i]; + } + return src.length; + } + + @Override + public int read(byte[] b) throws IOException { + if (request == 0) { + request++; + PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_1, true); + byte[] bytes = packet.toByteArray(); + return copy(bytes, b); + } else + if (request == 1) { + request++; + PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_2, false); + byte[] bytes = packet.toByteArray(); + return copy(bytes, b); + } else { + return -1; + } + } + } + + @Test + public void testTwoPackets() throws Exception { + InputStream inputStream = new InputStreamExtension(); + + TcpPacketReader packetReader = new TcpPacketReader(inputStream); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertFalse(receivedPackets.isEmpty()); + assertEquals(2, receivedPackets.size()); + + PowerManagementPacket receivedPacket = new PowerManagementPacket(receivedPackets.get(0)); + + assertArrayEquals(TEST_MAC_ADDRESS_1, receivedPacket.getTargetMac()); + assertTrue(receivedPacket.on()); + + receivedPacket = new PowerManagementPacket(receivedPackets.get(1)); + + assertArrayEquals(TEST_MAC_ADDRESS_2, receivedPacket.getTargetMac()); + assertFalse(receivedPacket.on()); + } + +} \ No newline at end of file diff --git a/src/test/java/jlifx/packet/UdpPacketReaderTest.java b/src/test/java/jlifx/packet/UdpPacketReaderTest.java new file mode 100644 index 00000000..4f4e50b2 --- /dev/null +++ b/src/test/java/jlifx/packet/UdpPacketReaderTest.java @@ -0,0 +1,92 @@ +package jlifx.packet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; + +import jlifx.bulb.AbstractJLifxTestCase; + +public class UdpPacketReaderTest extends AbstractJLifxTestCase { + + private class DatagramSocketStub extends DatagramSocket { + private final Iterator packetIterator; + + public DatagramSocketStub(List packets) throws SocketException { + this.packetIterator = packets.iterator(); + } + + @Override + public synchronized void receive(DatagramPacket p) throws IOException { + if (packetIterator.hasNext()) { + p.setData(packetIterator.next().toByteArray()); + } else { + throw new SocketTimeoutException(); + } + } + + } + + @Test + public void testNoPacket() throws Exception { + UdpPacketReader packetReader = new UdpPacketReader(new DatagramSocketStub(new ArrayList())); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertTrue(receivedPackets.isEmpty()); + } + + @Test + public void testOnePacket() throws Exception { + DatagramSocket datagramSocket = new DatagramSocketStub(Collections.singletonList(new StatusRequestPacket())); + UdpPacketReader packetReader = new UdpPacketReader(datagramSocket); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertFalse(receivedPackets.isEmpty()); + assertEquals(1, receivedPackets.size()); + + Packet receivedPacket = receivedPackets.get(0); + + assertEquals(StatusRequestPacket.TYPE, receivedPacket.getType()); + } + + @Test + public void testTwoPackets() throws Exception { + List packets = new ArrayList(); + packets.add(new StatusRequestPacket()); + packets.add(new PowerManagementPacket(TEST_MAC_ADDRESS_1, true)); + UdpPacketReader packetReader = new UdpPacketReader(new DatagramSocketStub(packets)); + packetReader.start(); + Thread.sleep(500); + List receivedPackets = packetReader.getReceivedPackets(); + packetReader.stopRunning(); + + assertFalse(receivedPackets.isEmpty()); + assertEquals(2, receivedPackets.size()); + + Packet receivedPacket = receivedPackets.get(0); + + assertEquals(StatusRequestPacket.TYPE, receivedPacket.getType()); + + receivedPacket = receivedPackets.get(1); + + assertEquals(PowerManagementPacket.TYPE, receivedPacket.getType()); + } + +}