libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

JsonTest.kt (3087B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2023 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 import kotlinx.serialization.Serializable
     21 import kotlinx.serialization.encodeToString
     22 import kotlinx.serialization.json.Json
     23 import org.junit.Test
     24 import tech.libeufin.bank.CreditDebitInfo
     25 import tech.libeufin.common.RelativeTime
     26 import tech.libeufin.common.TalerAmount
     27 import tech.libeufin.common.TalerTimestamp
     28 import java.time.Duration
     29 import java.time.Instant
     30 import java.time.temporal.ChronoUnit
     31 
     32 @Serializable
     33 data class MyJsonType(
     34     val content: String,
     35     val n: Int
     36 )
     37 
     38 // Running (de)serialization, only checking that no exceptions are raised.
     39 class JsonTest {
     40     @Test
     41     fun serializationTest() {
     42         Json.encodeToString(MyJsonType("Lorem Ipsum", 3))
     43     }
     44     @Test
     45     fun deserializationTest() {
     46         val serialized = """
     47             {"content": "Lorem Ipsum", "n": 3}
     48         """.trimIndent()
     49         Json.decodeFromString<MyJsonType>(serialized)
     50     }
     51 
     52     /**
     53      * Testing the custom absolute and relative time serializers.
     54      */
     55     @Test
     56     fun timeSerializers() {
     57         // from JSON to time types
     58         assert(Json.decodeFromString<RelativeTime>("{\"d_us\": 3}").duration.toNanos() == 3000L)
     59         assert(Json.decodeFromString<RelativeTime>("{\"d_us\": \"forever\"}").duration == ChronoUnit.FOREVER.duration)
     60         assert(Json.decodeFromString<TalerTimestamp>("{\"t_s\": 3}").instant == Instant.ofEpochSecond(3))
     61         assert(Json.decodeFromString<TalerTimestamp>("{\"t_s\": \"never\"}").instant == Instant.MAX)
     62 
     63         // from time types to JSON
     64         val oneDay = RelativeTime(Duration.of(1, ChronoUnit.DAYS))
     65         val oneDaySerial = Json.encodeToString(oneDay)
     66         assert(Json.decodeFromString<RelativeTime>(oneDaySerial).duration == oneDay.duration)
     67         val forever = RelativeTime(ChronoUnit.FOREVER.duration)
     68         val foreverSerial = Json.encodeToString(forever)
     69         assert(Json.decodeFromString<RelativeTime>(foreverSerial).duration == forever.duration)
     70     }
     71 
     72     @Test
     73     fun enumSerializer() {
     74         assert("\"credit\"" == Json.encodeToString(CreditDebitInfo.credit))
     75         assert("\"debit\"" == Json.encodeToString(CreditDebitInfo.debit))
     76     }
     77 
     78     // Testing JSON <--> TalerAmount
     79     @Test
     80     fun amountSerializer() {
     81         val amt = Json.decodeFromString<TalerAmount>("\"KUDOS:4.4\"")
     82         assert(Json.encodeToString(amt) == "\"KUDOS:4.4\"")
     83     }
     84 }