Skip to content

Commit 45219aa

Browse files
committed
1 parent ba73681 commit 45219aa

File tree

2 files changed

+43
-0
lines changed
  • src
    • main/kotlin/com/github/mgramin/sqlboot/model/resourcetype
    • test/kotlin/com/github/mgramin/sqlboot/model/resourcetype

2 files changed

+43
-0
lines changed

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/Metadata.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.github.mgramin.sqlboot.model.resourcetype
22

33
import com.fasterxml.jackson.annotation.JsonAutoDetect
44
import com.google.gson.Gson
5+
import com.google.gson.JsonElement
6+
import com.google.gson.JsonObject
57
import com.google.gson.reflect.TypeToken
68
import java.util.HashMap
79

@@ -44,6 +46,18 @@ data class Metadata(
4446

4547
fun properties(): Map<String, Any> = properties
4648

49+
/**
50+
* Get as JSON
51+
*/
52+
fun toJson(): JsonObject {
53+
val jsonObject = JsonObject()
54+
val toJson: JsonElement = Gson().toJsonTree (properties)
55+
jsonObject.addProperty("name", name)
56+
jsonObject.addProperty("description", description)
57+
jsonObject.add("properties", toJson)
58+
return jsonObject
59+
}
60+
4761
override fun compareTo(other: Metadata): Int = if (name > other.name()) -1 else 1
4862

4963
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.mgramin.sqlboot.model.resourcetype
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class MetadataTest {
7+
8+
@Test
9+
fun name() = assertEquals("name", metadata().name())
10+
11+
@Test
12+
fun description() = assertEquals("""{ "key_1": "value_1" }""", metadata().description())
13+
14+
@Test
15+
fun properties() = assertEquals(mapOf("key_1" to "value_1", "key" to "name"), metadata().properties())
16+
17+
@Test
18+
fun compareTo() {
19+
assertEquals(1, metadata().compareTo(metadata()))
20+
}
21+
22+
@Test
23+
fun toJson() = assertEquals(
24+
"""{"name":"name","description":"{ \"key_1\": \"value_1\" }","properties":{"key_1":"value_1","key":"name"}}""",
25+
metadata().toJson().toString())
26+
27+
private fun metadata() = Metadata("name", "String", """{ "key_1": "value_1" }""")
28+
29+
}

0 commit comments

Comments
 (0)