|
| 1 | +/* |
| 2 | + * Copyright 2019 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +package org.hyperledger.fabric.traces.impl; |
| 7 | + |
| 8 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import static java.time.temporal.ChronoUnit.DAYS; |
| 18 | +import static java.time.temporal.ChronoUnit.HOURS; |
| 19 | +import static java.time.temporal.ChronoUnit.MILLIS; |
| 20 | +import static java.time.temporal.ChronoUnit.MINUTES; |
| 21 | +import static java.time.temporal.ChronoUnit.SECONDS; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 24 | + |
| 25 | +public class OpenTelemetryPropertiesTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testOverrideValue() { |
| 29 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "bar"), Collections.singletonMap("foo", "foobar")); |
| 30 | + assertThat(props.getString("foo")).isEqualTo("foobar"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCanGetDurationDays() { |
| 35 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5d")); |
| 36 | + assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, DAYS)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testCanGetDurationHours() { |
| 41 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5h")); |
| 42 | + assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, HOURS)); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testCanGetDurationMinutes() { |
| 48 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5m")); |
| 49 | + assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, MINUTES)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testCanGetDurationSeconds() { |
| 54 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5s")); |
| 55 | + assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, SECONDS)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testCanGetDurationMilliSeconds() { |
| 60 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5ms")); |
| 61 | + assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, MILLIS)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testCanGetDurationInvalid() { |
| 66 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5foo")); |
| 67 | + assertThatThrownBy(() -> props.getDuration("foo")).isInstanceOf(ConfigurationException.class); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testGetDouble() { |
| 72 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5.23")); |
| 73 | + assertThat(props.getDouble("foo")).isEqualTo(5.23d); |
| 74 | + assertThat(props.getDouble("bar")).isNull(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testGetDoubleInvalid() { |
| 79 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5foo")); |
| 80 | + assertThatThrownBy(() -> props.getDouble("foo")).isInstanceOf(ConfigurationException.class); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetLong() { |
| 85 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "500003")); |
| 86 | + assertThat(props.getLong("foo")).isEqualTo(500003L); |
| 87 | + assertThat(props.getLong("bar")).isNull(); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testGetInt() { |
| 93 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "500003")); |
| 94 | + assertThat(props.getInt("foo")).isEqualTo(500003); |
| 95 | + assertThat(props.getInt("bar")).isNull(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testGetBoolean() { |
| 100 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "true")); |
| 101 | + assertThat(props.getBoolean("foo")).isTrue(); |
| 102 | + assertThat(props.getBoolean("bar")).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testGetList() { |
| 107 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo,bar,foobar")); |
| 108 | + assertThat(props.getList("foo")).isEqualTo(Arrays.asList("foo", "bar", "foobar")); |
| 109 | + assertThat(props.getList("bar")).isEqualTo(Collections.emptyList()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testGetMap() { |
| 114 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo=bar,foobar=noes")); |
| 115 | + Map<String, String> expected = new HashMap<>(); |
| 116 | + expected.put("foo", "bar"); |
| 117 | + expected.put("foobar", "noes"); |
| 118 | + assertThat(props.getMap("foo")).isEqualTo(expected); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testGetMapInvalid() { |
| 123 | + OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo/bar,foobar/noes")); |
| 124 | + Map<String, String> expected = new HashMap<>(); |
| 125 | + assertThatThrownBy(() -> props.getMap("foo")).isInstanceOf(ConfigurationException.class); |
| 126 | + } |
| 127 | +} |
0 commit comments