Skip to content

Commit 313d986

Browse files
authored
#2312 add RowDataUtils (#2314)
* #2312 add RowDataUtils * #2312 add java test * #2312 add java test * #2312 rename
1 parent 7cfd545 commit 313d986

3 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.finos.vuu.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.finos.vuu.core.table.RowData;
7+
import org.finos.vuu.core.table.RowWithData;
8+
import org.finos.vuu.core.table.util.RowDataUtils;
9+
import org.junit.jupiter.api.Test;
10+
import scala.runtime.RichLong;
11+
12+
import java.util.Map;
13+
14+
class RowDataUtilsJavaTest {
15+
Map<String, Object> data = Map.of(
16+
"long", 123L,
17+
"Long", Long.valueOf(456L),
18+
"RichLong", new RichLong(789L)
19+
);
20+
RowData rowData = new RowWithData("myKey", ScalaCollectionConverter.toScala(data));
21+
22+
@Test
23+
void shouldReturnLongWhenValueIsPrimitiveLong() {
24+
long result = RowDataUtils.getRequiredLong(rowData, "long");
25+
assertEquals(123L, result);
26+
}
27+
28+
@Test
29+
void shouldReturnLongWhenValueIsLong() {
30+
long result = RowDataUtils.getRequiredLong(rowData, "Long");
31+
assertEquals(456L, result);
32+
}
33+
34+
@Test
35+
void shouldReturnLongWhenValueIsRichLong() {
36+
long result = RowDataUtils.getRequiredLong(rowData, "RichLong");
37+
assertEquals(789L, result);
38+
}
39+
40+
@Test
41+
void shouldThrowExceptionWhenValueIsNull() {
42+
assertThrows(
43+
RowDataUtils.RowDataException.class,
44+
() -> RowDataUtils.getRequiredLong(rowData, "dummy")
45+
);
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.finos.vuu.core.table.util
2+
3+
import org.finos.vuu.core.table.RowData
4+
5+
import scala.runtime.RichLong
6+
7+
object RowDataUtils {
8+
9+
def getRequiredLong(rowData: RowData, columnName: String): Long = {
10+
safeGet(rowData, columnName) match {
11+
case value: RichLong => value.longValue
12+
case value: Long => value
13+
case _ => throw new RowDataException(s"$columnName is not present or is not a Long")
14+
}
15+
}
16+
17+
def getRequiredString(rowData: RowData, columnName: String): String = {
18+
safeGet(rowData, columnName) match {
19+
case value: String => value
20+
case _ => throw new RowDataException(s"$columnName is not present or is not a String")
21+
}
22+
}
23+
24+
def getString(rowData: RowData, columnName: String): String = {
25+
safeGet(rowData, columnName) match {
26+
case value: String => value
27+
case _ => null
28+
}
29+
}
30+
31+
private def safeGet(rowData: RowData, columnName: String): Any = {
32+
if (rowData != null && columnName != null) {
33+
return rowData.get(columnName) match {
34+
case option: Option[_] => option.orNull
35+
case value => value
36+
}
37+
}
38+
null
39+
}
40+
41+
final class RowDataException(message: String)
42+
extends RuntimeException
43+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.finos.vuu.core.table.util
2+
3+
import org.finos.vuu.core.table.RowWithData
4+
import org.scalatest.featurespec.AnyFeatureSpec
5+
import org.scalatest.matchers.should.Matchers
6+
7+
class RowDataUtilsTest extends AnyFeatureSpec with Matchers {
8+
private val rowData = RowWithData("myKey", Map(
9+
"key" -> "myKey",
10+
"id" -> 123L,
11+
"someId" -> Some(123L),
12+
"name" -> "user1",
13+
"someName" -> Some("user1"),
14+
"noneCol" -> None
15+
))
16+
17+
Feature("getRequiredLong") {
18+
19+
Scenario("getRequiredLong should throw when rowData is null") {
20+
an[RowDataUtils.RowDataException] should be thrownBy {
21+
RowDataUtils.getRequiredLong(null, "any")
22+
}
23+
}
24+
25+
Scenario("getRequiredLong should throw when columnName is null") {
26+
an[RowDataUtils.RowDataException] should be thrownBy {
27+
RowDataUtils.getRequiredLong(rowData, null)
28+
}
29+
}
30+
31+
Scenario("getRequiredLong should return a Long") {
32+
RowDataUtils.getRequiredLong(rowData, "id") shouldBe 123L
33+
}
34+
35+
Scenario("getRequiredLong should unwrap Some(Long)") {
36+
RowDataUtils.getRequiredLong(rowData, "someId") shouldBe 123L
37+
}
38+
39+
Scenario("getRequiredLong should throw when column is not in RowData") {
40+
an[RowDataUtils.RowDataException] should be thrownBy {
41+
RowDataUtils.getRequiredLong(rowData, "dummy")
42+
}
43+
}
44+
45+
Scenario("getRequiredLong should throw when value is None") {
46+
an[RowDataUtils.RowDataException] should be thrownBy {
47+
RowDataUtils.getRequiredLong(rowData, "noneCol")
48+
}
49+
}
50+
51+
Scenario("getRequiredLong should throw when value is not a Long") {
52+
an[RowDataUtils.RowDataException] should be thrownBy {
53+
RowDataUtils.getRequiredLong(rowData, "name")
54+
}
55+
}
56+
}
57+
58+
Feature("getRequiredString") {
59+
60+
Scenario("getRequiredString should throw when rowData is null") {
61+
an[RowDataUtils.RowDataException] should be thrownBy {
62+
RowDataUtils.getRequiredString(null, "any")
63+
}
64+
}
65+
66+
Scenario("getRequiredString should throw when columnName is null") {
67+
an[RowDataUtils.RowDataException] should be thrownBy {
68+
RowDataUtils.getRequiredString(rowData, null)
69+
}
70+
}
71+
72+
Scenario("getRequiredString should return a String") {
73+
RowDataUtils.getRequiredString(rowData, "name") shouldBe "user1"
74+
}
75+
76+
Scenario("getRequiredString should unwrap Some(String)") {
77+
RowDataUtils.getRequiredString(rowData, "someName") shouldBe "user1"
78+
}
79+
80+
Scenario("getRequiredString should throw when column is not in RowData") {
81+
an[RowDataUtils.RowDataException] should be thrownBy {
82+
RowDataUtils.getRequiredString(rowData, "dummy")
83+
}
84+
}
85+
86+
Scenario("getRequiredString should throw when value is None") {
87+
an[RowDataUtils.RowDataException] should be thrownBy {
88+
RowDataUtils.getRequiredString(rowData, "noneCol")
89+
}
90+
}
91+
92+
Scenario("getRequiredString should throw when value is not a String") {
93+
an[RowDataUtils.RowDataException] should be thrownBy {
94+
RowDataUtils.getRequiredString(rowData, "id")
95+
}
96+
}
97+
}
98+
99+
Feature("getString") {
100+
101+
Scenario("getString should return null when rowData is null") {
102+
RowDataUtils.getString(null, "any") shouldBe null
103+
}
104+
105+
Scenario("getString should return null when columnName is null") {
106+
RowDataUtils.getString(rowData, null) shouldBe null
107+
}
108+
109+
Scenario("getString should return a String") {
110+
RowDataUtils.getString(rowData, "name") shouldBe "user1"
111+
}
112+
113+
Scenario("getString should unwrap Some(String)") {
114+
RowDataUtils.getString(rowData, "someName") shouldBe "user1"
115+
}
116+
117+
Scenario("getString should return null when column is not in RowData") {
118+
RowDataUtils.getString(rowData, "dummy") shouldBe null
119+
}
120+
121+
Scenario("getString should return null when value is None") {
122+
RowDataUtils.getString(rowData, "noneCol") shouldBe null
123+
}
124+
125+
Scenario("getString should return null when value is not a String") {
126+
RowDataUtils.getString(rowData, "id") shouldBe null
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)