EchoProtocolUdpTask extends AbstractEchoProtocolTask { public QoSTestResult call() throws Exception { final QoSTestResult result = initQoSTestResult(QosMeasurementType.ECHO_PROTOCOL); try { onStart(result); if (testHost != null && testPort != null) { try (final DatagramSocket socket = new DatagramSocket()) { socket.connect(InetAddress.getByName(this.testHost), this.testPort); socket.setSoTimeout((int) (this.timeout / 1e6)); final byte[] payloadBytes = payload.getBytes(); final DatagramPacket sendPacket = new DatagramPacket(payloadBytes, payloadBytes.length, InetAddress.getByName(this.testHost), this.testPort); final DatagramPacket receivePacket = new DatagramPacket(new byte[payloadBytes.length + 1], payloadBytes.length + 1); final long startTime = System.nanoTime(); socket.send(sendPacket); socket.receive(receivePacket); final long duration = System.nanoTime() - startTime; final String testResponse = new String(receivePacket.getData(), 0, receivePacket.getLength()); if (sendPacket.getLength() == receivePacket.getLength() && payload.equals(testResponse)) { result.getResultMap().put(RESULT_STATUS, "OK"); } else { result.getResultMap().put(RESULT_STATUS, "ERROR"); } result.getResultMap().put(RESULT_RTT_NS, Long.toString(duration)); result.getResultMap().put(RESULT, testResponse); } catch (SocketTimeoutException ex) { result.getResultMap().put(RESULT_STATUS, "TIMEOUT"); } catch (Exception ex) { ex.printStackTrace(); result.getResultMap().put(RESULT_STATUS, "ERROR"); } } else { result.getResultMap().put(RESULT_STATUS, "ERROR"); } } catch (final Exception ex) { ex.printStackTrace(); result.getResultMap().put(RESULT_STATUS, "ERROR"); } finally { if (this.testPort != null) { result.getResultMap().put(RESULT_PORT, testPort); } if (this.testHost != null) { result.getResultMap().put("echo_protocol_objective_host", this.testHost); } result.getResultMap().put(RESULT_TIMEOUT, timeout); result.getResultMap().put(AbstractEchoProtocolTask.PARAM_PAYLOAD, this.payload); result.getResultMap().put(AbstractEchoProtocolTask.RESULT_PROTOCOL, AbstractEchoProtocolTask.PROTOCOL_UDP); onEnd(result); } return result; } EchoProtocolUdpTask(QualityOfServiceTest nnTest, TaskDesc taskDesc, int threadId); QoSTestResult call();  }
@Test public void testCorrectUdpEchoResponse(@Mocked final DatagramSocket mock, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = toReturn; mock.send((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { sendPacket = packet; } }; mock.receive((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { packet.setData(CORRECT_MESSAGE.getBytes()); } }; }}; final EchoProtocolUdpTask task = new EchoProtocolUdpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE, new String(sendPacket.getData())); assertEquals("Result did not return OK","OK", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_UDP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + CORRECT_MESSAGE, CORRECT_MESSAGE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testWrongUdpEchoResponse (@Mocked final DatagramSocket mock, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = toReturn; mock.send((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { sendPacket = packet; } }; mock.receive((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { packet.setData(WRONG_MESSAGE.getBytes()); } }; }}; final EchoProtocolUdpTask task = new EchoProtocolUdpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE, new String(sendPacket.getData())); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_UDP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + WRONG_MESSAGE, WRONG_MESSAGE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testCorrectUdpEchoResponseWithAdditionalWhiteSpace (@Mocked final DatagramSocket mock, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = toReturn; mock.send((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { sendPacket = packet; } }; mock.receive((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { packet.setData(WRONG_MESSAGE_ADDITIONAL_WHITESPACE.getBytes()); } }; }}; final EchoProtocolUdpTask task = new EchoProtocolUdpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE, new String(sendPacket.getData())); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_UDP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + WRONG_MESSAGE_ADDITIONAL_WHITESPACE, WRONG_MESSAGE_ADDITIONAL_WHITESPACE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testUdpEchoResponseWithSocketError (@Mocked final DatagramSocket mock, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = toReturn; mock.send((DatagramPacket) any); result = result = new SocketException("Forcefully thrown exception"); }}; final EchoProtocolUdpTask task = new EchoProtocolUdpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_UDP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); }
@Test public void testUdpEchoResponseWithSocketTimeoutError (@Mocked final DatagramSocket mock, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = toReturn; mock.send((DatagramPacket) any); result = new Delegate() { void delegateMethod(DatagramPacket packet) { sendPacket = packet; } }; mock.receive((DatagramPacket) any); result = new SocketTimeoutException("Forcefully thrown exception"); }}; final EchoProtocolUdpTask task = new EchoProtocolUdpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Result did not return TIMEOUT","TIMEOUT", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_UDP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); }