|
| 1 | +package org.radarbase.output.path |
| 2 | + |
| 3 | +import org.hamcrest.MatcherAssert.assertThat |
| 4 | +import org.hamcrest.Matchers.equalTo |
| 5 | +import org.junit.jupiter.api.Assertions.assertThrows |
| 6 | +import org.junit.jupiter.api.BeforeEach |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import org.radarcns.kafka.ObservationKey |
| 9 | +import org.radarcns.monitor.application.ApplicationServerStatus |
| 10 | +import org.radarcns.monitor.application.ServerStatus |
| 11 | +import java.nio.file.Paths |
| 12 | +import java.time.Instant |
| 13 | +import java.time.ZoneOffset |
| 14 | +import java.time.format.DateTimeFormatter |
| 15 | + |
| 16 | +internal class PathFormatterTest { |
| 17 | + lateinit var params: PathFormatParameters |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + fun setupRecord() { |
| 21 | + val defaultTimeFormat = DateTimeFormatter.ofPattern("YYYYMMdd_HH'00'") |
| 22 | + .withZone(ZoneOffset.UTC) |
| 23 | + |
| 24 | + params = PathFormatParameters( |
| 25 | + topic = "my_topic", |
| 26 | + key = ObservationKey( |
| 27 | + "p", |
| 28 | + "u", |
| 29 | + "s", |
| 30 | + ), |
| 31 | + value = ApplicationServerStatus( |
| 32 | + 1.0, |
| 33 | + ServerStatus.CONNECTED, |
| 34 | + "1.1.1.1", |
| 35 | + ), |
| 36 | + time = Instant.ofEpochMilli(1000), |
| 37 | + attempt = 0, |
| 38 | + extension = ".csv", |
| 39 | + computeTimeBin = { t -> if (t != null) defaultTimeFormat.format(t) else "unknown-time" } |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testDefaultPath() { |
| 45 | + val formatter = PathFormatter( |
| 46 | + format = FormattedPathFactory.Companion.DEFAULT_FORMAT, |
| 47 | + plugins = listOf( |
| 48 | + FixedPathFormatterPlugin(), |
| 49 | + TimePathFormatterPlugin(), |
| 50 | + KeyPathFormatterPlugin(), |
| 51 | + ValuePathFormatterPlugin(), |
| 52 | + ) |
| 53 | + ) |
| 54 | + assertThat(formatter.format(params), equalTo(Paths.get("p/u/my_topic/19700101_0000.csv"))) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun testDefaultPathFewerPlugins() { |
| 59 | + val formatter = PathFormatter( |
| 60 | + format = FormattedPathFactory.Companion.DEFAULT_FORMAT, |
| 61 | + plugins = listOf( |
| 62 | + FixedPathFormatterPlugin(), |
| 63 | + ) |
| 64 | + ) |
| 65 | + assertThat(formatter.format(params), equalTo(Paths.get("p/u/my_topic/19700101_0000.csv"))) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun testDefaultPathNoTime() { |
| 70 | + val formatter = PathFormatter( |
| 71 | + format = FormattedPathFactory.Companion.DEFAULT_FORMAT, |
| 72 | + plugins = listOf( |
| 73 | + FixedPathFormatterPlugin(), |
| 74 | + ) |
| 75 | + ) |
| 76 | + assertThat(formatter.format(params.copy(time = null)), equalTo(Paths.get("p/u/my_topic/unknown-time.csv"))) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testDefaultPathWrongPlugins() { |
| 81 | + assertThrows(IllegalArgumentException::class.java) { |
| 82 | + PathFormatter( |
| 83 | + format = FormattedPathFactory.Companion.DEFAULT_FORMAT, |
| 84 | + plugins = listOf( |
| 85 | + TimePathFormatterPlugin(), |
| 86 | + KeyPathFormatterPlugin(), |
| 87 | + ValuePathFormatterPlugin(), |
| 88 | + ) |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun testCorrectTimeFormatPlugins() { |
| 95 | + val formatter = PathFormatter( |
| 96 | + format = "\${topic}/\${time:YYYY-MM-dd_HH:mm:ss}\${attempt}\${extension}", |
| 97 | + plugins = listOf( |
| 98 | + FixedPathFormatterPlugin(), |
| 99 | + TimePathFormatterPlugin(), |
| 100 | + ), |
| 101 | + ) |
| 102 | + assertThat(formatter.format(params), equalTo(Paths.get("my_topic/1970-01-01_000001.csv"))) |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun testBadTimeFormatPlugins() { |
| 107 | + assertThrows(IllegalArgumentException::class.java) { |
| 108 | + PathFormatter( |
| 109 | + format = "\${topic}/\${time:VVV}\${attempt}\${extension}", |
| 110 | + plugins = listOf( |
| 111 | + FixedPathFormatterPlugin(), |
| 112 | + TimePathFormatterPlugin(), |
| 113 | + ) |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun testCorrectKeyFormat() { |
| 120 | + val formatter = PathFormatter( |
| 121 | + format = "\${topic}/\${key:projectId}\${attempt}\${extension}", |
| 122 | + plugins = listOf( |
| 123 | + FixedPathFormatterPlugin(), |
| 124 | + KeyPathFormatterPlugin(), |
| 125 | + ) |
| 126 | + ) |
| 127 | + |
| 128 | + assertThat(formatter.format(params), equalTo(Paths.get("my_topic/p.csv"))) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun testUnknownKeyFormat() { |
| 133 | + val formatter = PathFormatter( |
| 134 | + format = "\${topic}/\${key:doesNotExist}\${attempt}\${extension}", |
| 135 | + plugins = listOf( |
| 136 | + FixedPathFormatterPlugin(), |
| 137 | + KeyPathFormatterPlugin(), |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + assertThat(formatter.format(params), equalTo(Paths.get("my_topic/unknown-key.csv"))) |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun testCorrectValueFormat() { |
| 146 | + val formatter = PathFormatter( |
| 147 | + format = "\${topic}/\${value:serverStatus}\${attempt}\${extension}", |
| 148 | + plugins = listOf( |
| 149 | + FixedPathFormatterPlugin(), |
| 150 | + ValuePathFormatterPlugin(), |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | + assertThat(formatter.format(params), equalTo(Paths.get("my_topic/CONNECTED.csv"))) |
| 155 | + } |
| 156 | +} |
0 commit comments