]> git.basschouten.com Git - openhab-addons.git/blob
141f6644f94d7e5cb3dbf8b907c0ff71e6a6c035
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.network.internal.utils;
14
15 import java.util.Optional;
16
17 import org.junit.Assert;
18 import org.junit.Test;
19
20 /**
21  * Tests the parser which extracts latency values from the output of the ping command.
22  *
23  * @author Andreas Hirsch - Initial contribution
24  */
25 public class LatencyParserTest {
26
27     @Test
28     public void parseLinuxAndMacResultFoundTest() {
29         // Arrange
30         LatencyParser latencyParser = new LatencyParser();
31         String input = "64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.225 ms";
32
33         // Act
34         Optional<Double> resultLatency = latencyParser.parseLatency(input);
35
36         // Assert
37         Assert.assertTrue(resultLatency.isPresent());
38         Assert.assertEquals(1.225, resultLatency.get(), 0);
39     }
40
41     @Test
42     public void parseLinuxAndMacResultNotFoundTest() {
43         // Arrange
44         LatencyParser latencyParser = new LatencyParser();
45         // This is the output of the command. We exclude the line which contains the latency, because here we want
46         // to test that no latency is returned for all other lines.
47         String[] inputLines = { "ping -c 1 192.168.1.1", "PING 192.168.1.1 (192.168.1.1): 56 data bytes",
48                 // "64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.225 ms",
49                 "--- 192.168.1.1 ping statistics ---", "1 packets transmitted, 1 packets received, 0.0% packet loss",
50                 "round-trip min/avg/max/stddev = 1.225/1.225/1.225/0.000 ms" };
51
52         for (String inputLine : inputLines) {
53             // Act
54             Optional<Double> resultLatency = latencyParser.parseLatency(inputLine);
55
56             // Assert
57             Assert.assertFalse(resultLatency.isPresent());
58         }
59     }
60 }