]> git.basschouten.com Git - openhab-addons.git/blob
96ecfbc7ac508e9a4e085de94b75f347b26c6ce4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Optional;
18
19 import org.junit.jupiter.api.Test;
20
21 /**
22  * Tests the parser which extracts latency values from the output of the ping command.
23  *
24  * @author Andreas Hirsch - Initial contribution
25  */
26 public class LatencyParserTest {
27
28     @Test
29     public void parseLinuxAndMacResultFoundTest() {
30         // Arrange
31         LatencyParser latencyParser = new LatencyParser();
32         String input = "64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.225 ms";
33
34         // Act
35         Optional<Double> resultLatency = latencyParser.parseLatency(input);
36
37         // Assert
38         assertTrue(resultLatency.isPresent());
39         assertEquals(1.225, resultLatency.get(), 0);
40     }
41
42     @Test
43     public void parseLinuxAndMacResultNotFoundTest() {
44         // Arrange
45         LatencyParser latencyParser = new LatencyParser();
46         // This is the output of the command. We exclude the line which contains the latency, because here we want
47         // to test that no latency is returned for all other lines.
48         String[] inputLines = { "ping -c 1 192.168.1.1", "PING 192.168.1.1 (192.168.1.1): 56 data bytes",
49                 // "64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=1.225 ms",
50                 "--- 192.168.1.1 ping statistics ---", "1 packets transmitted, 1 packets received, 0.0% packet loss",
51                 "round-trip min/avg/max/stddev = 1.225/1.225/1.225/0.000 ms" };
52
53         for (String inputLine : inputLines) {
54             // Act
55             Optional<Double> resultLatency = latencyParser.parseLatency(inputLine);
56
57             // Assert
58             assertFalse(resultLatency.isPresent());
59         }
60     }
61
62     @Test
63     public void parseWindows10ResultFoundTest() {
64         // Arrange
65         LatencyParser latencyParser = new LatencyParser();
66         String input = "Reply from 192.168.178.207: bytes=32 time=2ms TTL=64";
67
68         // Act
69         Optional<Double> resultLatency = latencyParser.parseLatency(input);
70
71         // Assert
72         assertTrue(resultLatency.isPresent());
73         assertEquals(2, resultLatency.get(), 0);
74     }
75 }