Skip to content

Commit 72a2d98

Browse files
committed
Added unit tests
1 parent 4ee5d4f commit 72a2d98

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

src/main/java/org/radarbase/output/path/FormattedPathFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ open class FormattedPathFactory : RecordPathFactory() {
8686
): String = sanitizeId(key.get("sourceId"), "unknown-source")
8787

8888
companion object {
89-
private const val DEFAULT_FORMAT = "\${projectId}/\${userId}/\${topic}/\${filename}"
90-
private const val DEFAULT_FORMAT_PLUGINS = "fixed time key value"
89+
internal const val DEFAULT_FORMAT = "\${projectId}/\${userId}/\${topic}/\${filename}"
90+
internal const val DEFAULT_FORMAT_PLUGINS = "fixed time key value"
9191
private val logger = LoggerFactory.getLogger(FormattedPathFactory::class.java)
9292

9393
private fun String.toPathFormatterPlugin(): PathFormatterPlugin? = when (this) {

src/main/java/org/radarbase/output/path/ValuePathFormatterPlugin.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.radarbase.output.path
22

3+
import org.apache.avro.AvroRuntimeException
34
import org.apache.avro.generic.GenericRecord
45
import org.radarbase.output.path.RecordPathFactory.Companion.sanitizeId
6+
import org.slf4j.LoggerFactory
57

68
class ValuePathFormatterPlugin : PathFormatterPlugin() {
79
override val prefix: String = "value"
@@ -18,8 +20,19 @@ class ValuePathFormatterPlugin : PathFormatterPlugin() {
1820

1921
companion object {
2022
fun GenericRecord.lookup(index: List<String>): Any? =
21-
index.fold<String, Any?>(this) { r, item ->
22-
r?.let { (it as? GenericRecord)?.get(item) }
23+
index.fold<String, Any?>(this) { record, item ->
24+
record
25+
?.let { it as? GenericRecord }
26+
?.let {
27+
try {
28+
it.get(item)
29+
} catch (ex: AvroRuntimeException) {
30+
logger.warn("Unknown field {} in record using index {}", item, index)
31+
null
32+
}
33+
}
2334
}
35+
36+
private val logger = LoggerFactory.getLogger(ValuePathFormatterPlugin::class.java)
2437
}
2538
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)