@Test public void testActive2AndDeactivate1() throws Exception { TestHardClient hardClient2 = new TestHardClient("localhost", tcpHardPort); hardClient2.start(); Profile newProfile = JsonParser.parseProfile(readTestUserProfile("user_profile_json_3_dashes.txt"), 1); clientPair.appClient.send("createDash " + newProfile.dashBoards[1]); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); clientPair.appClient.send("activate 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(2, OK))); clientPair.appClient.send("activate 2"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(3, DEVICE_NOT_IN_NETWORK))); clientPair.appClient.reset(); clientPair.appClient.send("getToken 2"); String token2 = clientPair.appClient.getBody(); hardClient2.send("login " + token2); verify(hardClient2.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); clientPair.appClient.reset(); clientPair.hardwareClient.send("hardware aw 1 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(produce(1, HARDWARE, b("1 aw 1 1")))); hardClient2.send("hardware aw 1 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(produce(2, HARDWARE, b("2 aw 1 1")))); clientPair.appClient.send("deactivate 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); clientPair.hardwareClient.send("hardware aw 1 1"); verify(clientPair.appClient.responseMock, timeout(500).times(0)) .channelRead(any(), eq(new ResponseMessage(2, NO_ACTIVE_DASHBOARD))); hardClient2.send("hardware aw 1 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(produce(3, HARDWARE, b("2 aw 1 1")))); hardClient2.stop().awaitUninterruptibly(); }
@Test public void testHardwareLoginWithInfo() throws Exception { TestHardClient hardClient2 = new TestHardClient("localhost", tcpHardPort); hardClient2.start(); clientPair.appClient.send("getToken 1"); String token2 = clientPair.appClient.getBody(); hardClient2.send("login " + token2); verify(hardClient2.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); hardClient2.send( "info " + b(" ver 0.3.1 h-beat 10 buff-in 256 dev Arduino cpu ATmega328P con W5100")); verify(hardClient2.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(2, OK))); hardClient2.stop().awaitUninterruptibly(); }
@Test public void testSendHardwareCommandToNotActiveDashboard() throws Exception { clientPair.appClient.send("createDash " + "{\"id\":2,\"name\":\"My Dashboard2\"}"); verify(clientPair.appClient.responseMock, timeout(1000)) .channelRead(any(), eq(new ResponseMessage(1, OK))); clientPair.appClient.reset(); clientPair.appClient.send("getToken 2"); // getting token for second GetTokenMessage ArgumentCaptor<GetTokenMessage> objectArgumentCaptor = ArgumentCaptor.forClass(GetTokenMessage.class); verify(clientPair.appClient.responseMock, timeout(2000).times(1)) .channelRead(any(), objectArgumentCaptor.capture()); List<GetTokenMessage> arguments = objectArgumentCaptor.getAllValues(); GetTokenMessage getTokenMessage = arguments.get(0); String token = getTokenMessage.body; clientPair.appClient.reset(); // connecting separate hardware to non active dashboard TestHardClient nonActiveDashHardClient = new TestHardClient("localhost", tcpHardPort); nonActiveDashHardClient.start(); nonActiveDashHardClient.send("login " + token); verify(nonActiveDashHardClient.responseMock, timeout(2000)) .channelRead(any(), eq(new ResponseMessage(1, OK))); nonActiveDashHardClient.reset(); // sending hardware command from hardware that has no active dashboard nonActiveDashHardClient.send("hardware aw 1 1"); verify(nonActiveDashHardClient.responseMock, timeout(1000)) .channelRead(any(), eq(new ResponseMessage(1, NO_ACTIVE_DASHBOARD))); verify(clientPair.appClient.responseMock, timeout(1000).times(0)).channelRead(any(), any()); clientPair.hardwareClient.send("hardware aw 1 1"); verify(clientPair.hardwareClient.responseMock, timeout(1000).times(0)) .channelRead(any(), any()); verify(clientPair.appClient.responseMock, timeout(1000)) .channelRead(any(), eq(produce(1, HARDWARE, b("1 aw 1 1")))); nonActiveDashHardClient.stop().awaitUninterruptibly(); }
@Test public void testSendGeneratedPinModeCommandWhenHardwareGoesOnline() throws Exception { ChannelFuture channelFuture = clientPair.hardwareClient.stop(); channelFuture.awaitUninterruptibly(); assertTrue(channelFuture.isDone()); clientPair.appClient.send("hardware 1 vw 1 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, DEVICE_NOT_IN_NETWORK))); TestHardClient hardClient = new TestHardClient("localhost", tcpHardPort); hardClient.start(); hardClient.send("login " + clientPair.token); verify(hardClient.responseMock, timeout(1000)) .channelRead(any(), eq(new ResponseMessage(1, OK))); String expectedBody = "pm 1 out 2 out 3 out 5 out 6 in 7 in 8 in"; verify(hardClient.responseMock, timeout(500)) .channelRead(any(), eq(produce(1, HARDWARE, b(expectedBody)))); verify(hardClient.responseMock, times(2)).channelRead(any(), any()); hardClient.stop().awaitUninterruptibly(); }
@Test public void testTimerWidgetTriggeredAndSendCommandToCorrectDevice() throws Exception { TestHardClient hardClient2 = new TestHardClient("localhost", tcpHardPort); hardClient2.start(); Executors.newScheduledThreadPool(1) .scheduleAtFixedRate( new TimerWorker(holder.userDao, holder.sessionDao), 0, 1000, TimeUnit.MILLISECONDS); clientPair.appClient.send("deactivate 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); Timer timer = new Timer(); timer.id = 1; timer.x = 1; timer.y = 1; timer.pinType = PinType.DIGITAL; timer.pin = 5; timer.startValue = "dw 5 1"; timer.stopValue = "dw 5 0"; LocalTime localDateTime = LocalTime.now(ZoneId.of("UTC")); long curTime = localDateTime.getSecond() + localDateTime.getMinute() * 60 + localDateTime.getHour() * 3600; timer.startTime = curTime + 1; timer.stopTime = curTime + 2; DashBoard dashBoard = new DashBoard(); dashBoard.id = 1; dashBoard.name = "Test"; dashBoard.widgets = new Widget[] {timer}; clientPair.appClient.send("saveDash " + dashBoard.toString()); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(2, OK))); dashBoard.id = 2; clientPair.appClient.send("createDash " + dashBoard.toString()); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(3, OK))); clientPair.appClient.send("activate 1"); verify(clientPair.appClient.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(4, OK))); clientPair.appClient.reset(); clientPair.appClient.send("getToken 2"); verify(clientPair.appClient.responseMock, timeout(500)).channelRead(any(), any()); hardClient2.send("login " + clientPair.appClient.getBody()); verify(hardClient2.responseMock, timeout(500)) .channelRead(any(), eq(new ResponseMessage(1, OK))); hardClient2.reset(); verify(clientPair.hardwareClient.responseMock, timeout(2000)) .channelRead(any(), eq(produce(7777, HARDWARE, "dw 5 1"))); clientPair.hardwareClient.reset(); verify(clientPair.hardwareClient.responseMock, timeout(2000)) .channelRead(any(), eq(produce(7777, HARDWARE, "dw 5 0"))); verify(hardClient2.responseMock, never()).channelRead(any(), any()); hardClient2.stop().awaitUninterruptibly(); }