Skip to content

Commit

Permalink
remove escaping of json, write tests to check stringified json and no…
Browse files Browse the repository at this point in the history
…n stringified json, add changelog
  • Loading branch information
nullpointer0x00 committed Nov 9, 2023
1 parent e1673a4 commit dddd95a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements
* Add aggregation of orderbooks for hash historical pricing [#496](https://github.com/provenance-io/explorer-service/issues/496)

### Bug Fixes
* Fix parsing of ibc json packet [#501](https://github.com/provenance-io/explorer-service/issues/501)

## [v5.6.0](https://github.com/provenance-io/explorer-service/releases/tag/v5.6.0) - 2023-07-05
### Release Name: Niels Peter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fun BlockOuterClass.Block.height() = this.header.height.toInt()
fun Long.get24HrBlockHeight(avgBlockTime: BigDecimal) =
BigDecimal(24 * 60 * 60).divide(avgBlockTime, 0, RoundingMode.HALF_UP).let { this - it.toInt() }

fun String.toObjectNode() = OBJECT_MAPPER.readValue(StringEscapeUtils.unescapeJson(this), ObjectNode::class.java)
fun String.toObjectNode() = OBJECT_MAPPER.readValue(this, ObjectNode::class.java)
fun Any?.stringify() = if (this == null) null else OBJECT_MAPPER.writeValueAsString(this)

fun List<BigDecimal>.average() = this.fold(BigDecimal.ZERO, BigDecimal::add)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package io.provenance.explorer.domain.extensions

import org.junit.jupiter.api.Tag

Check failure on line 3 in service/src/test/kotlin/io/provenance/explorer/domain/extensions/ExtenstionsKtTest.kt

View workflow job for this annotation

GitHub Actions / ktlint

Imports must be ordered in lexicographic order without any empty lines in-between with "java", "javax", "kotlin" and aliases in the end
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import org.apache.commons.lang3.StringEscapeUtils
import org.junit.jupiter.api.Assertions.assertEquals

class ExtensionsKtTest {

@Test
fun toObjectNode() {
// // Given an input JSON string
// val inputJson = "{\"key\":\"value\"}"
//
// // Create an ObjectMapper for testing
// val objectMapper = ObjectMapper()
//
// // Escape the input JSON
// val escapedInput = StringEscapeUtils.escapeJson(inputJson)
//
// // Convert the escaped JSON to an ObjectNode
// val resultObjectNode = escapedInput.toObjectNode(objectMapper)
//
// // Create an expected ObjectNode
// val expectedObjectNode = objectMapper.createObjectNode()
// expectedObjectNode.put("key", "value")
//
// // Assert that the result ObjectNode is equal to the expected ObjectNode
// assertEquals(expectedObjectNode, resultObjectNode)
@Tag("junit-jupiter")
fun testToObjectNode() {
val inputJsonWithJsonStrObj = "{\"amount\":\"1\",\"denom\":\"psa.3zlqy2ecncvalbycokxnoh.stock\",\"memo\":\"{\\\"marker\\\":{\\\"transfer-auths\\\":[\\\"tp19zf8q9swrsspkdljumwh04zjac4nkfvju6ehl9\\\",\\\"tp1tk6fqws0su7fzp090edrauaa756mdyjfdw0507\\\",\\\"tp1a53udazy8ayufvy0s434pfwjcedzqv34vfvvyc\\\"],\\\"allow-force-transfer\\\":false}}\",\"receiver\":\"tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3\",\"sender\":\"tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3\"}"
val inputFormatedJsonWithJsonStrObj = "{\n" +
" \"amount\": \"1\",\n" +
" \"denom\": \"psa.3zlqy2ecncvalbycokxnoh.stock\",\n" +
" \"memo\": \"{\\\"marker\\\":{\\\"transfer-auths\\\":[\\\"tp19zf8q9swrsspkdljumwh04zjac4nkfvju6ehl9\\\",\\\"tp1tk6fqws0su7fzp090edrauaa756mdyjfdw0507\\\",\\\"tp1a53udazy8ayufvy0s434pfwjcedzqv34vfvvyc\\\"],\\\"allow-force-transfer\\\":false}}\",\n" +
" \"receiver\": \"tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3\",\n" +
" \"sender\": \"tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3\"\n" +
"}"

val tests = mapOf(
"test input with string that has json string as value for memo" to inputJsonWithJsonStrObj,
"test input with formatted json and json string as value for memo" to inputFormatedJsonWithJsonStrObj
)

for ((testname, json) in tests) {
val actualJsonObj = json.toObjectNode()
assertEquals("tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3", actualJsonObj.get("receiver").asText(), testname)
assertEquals("tp12wyy028sd3yf3j0z950fq5p3zvzgpzgds3dqp3", actualJsonObj.get("sender").asText(), testname)
assertEquals("1", actualJsonObj.get("amount").asText(), testname)
assertEquals("psa.3zlqy2ecncvalbycokxnoh.stock", actualJsonObj.get("denom").asText(), testname)
assertEquals("{\"marker\":{\"transfer-auths\":[\"tp19zf8q9swrsspkdljumwh04zjac4nkfvju6ehl9\",\"tp1tk6fqws0su7fzp090edrauaa756mdyjfdw0507\",\"tp1a53udazy8ayufvy0s434pfwjcedzqv34vfvvyc\"],\"allow-force-transfer\":false}}", actualJsonObj.get("memo").asText(), testname)
}
}
}
}

0 comments on commit dddd95a

Please sign in to comment.