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