Skip to content

Commit

Permalink
Release 1.2.1 merge to Main (#631)
Browse files Browse the repository at this point in the history
* Fix Sep31Transaction.refunds Gson serialization exception (#630)

* Trigger workflow commit
  • Loading branch information
lijamie98 authored Oct 19, 2022
1 parent 92f941d commit 39f74a5
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.2.1
* Fix gson serialization error of the Refunds object. [#626](https://github.com/stellar/java-stellar-anchor-sdk/issues/626)

## 1.2.0
* Add Stellar observer retries with exponential back-off timer [#607](https://github.com/stellar/java-stellar-anchor-sdk/pull/607)
* Add health check endpoint to the Stellar observer [#602](https://github.com/stellar/java-stellar-anchor-sdk/pull/602)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ subprojects {

allprojects {
group = "org.stellar.anchor-sdk"
version = "1.2.0"
version = "1.2.1"

tasks.jar {
manifest {
Expand Down
38 changes: 32 additions & 6 deletions core/src/test/kotlin/org/stellar/anchor/sep10/Sep10ServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import io.mockk.*
import io.mockk.impl.annotations.MockK
import java.io.IOException
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import java.util.stream.Stream
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -80,12 +84,8 @@ internal class Sep10ServiceTest {
private lateinit var sep10Service: Sep10Service
private val clientKeyPair = KeyPair.random()
private val clientDomainKeyPair = KeyPair.random()
private val httpClient: OkHttpClient =
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.MINUTES)
.readTimeout(10, TimeUnit.MINUTES)
.writeTimeout(10, TimeUnit.MINUTES)
.build()

private lateinit var httpClient: OkHttpClient

@BeforeEach
fun setUp() {
Expand All @@ -108,6 +108,8 @@ internal class Sep10ServiceTest {

this.jwtService = spyk(JwtService(appConfig))
this.sep10Service = Sep10Service(appConfig, sep10Config, horizon, jwtService)

this.httpClient = `create httpClient that trust all certificates`()
}

@AfterEach
Expand All @@ -116,6 +118,30 @@ internal class Sep10ServiceTest {
unmockkAll()
}

fun `create httpClient that trust all certificates`(): OkHttpClient {
val trustAllCerts =
arrayOf<TrustManager>(
object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}

override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
}
)

// Install the all-trusting trust manager
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
return OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.MINUTES)
.readTimeout(10, TimeUnit.MINUTES)
.writeTimeout(10, TimeUnit.MINUTES)
.sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
.hostnameVerifier { _, _ -> true }
.build()
}

@Test
fun `test the challenge with existent account, multisig, and client domain`() {
// 1 ------ Create Test Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Sep31TransactionTest {
}

@Test
fun test_toPlatformApiGetTransactionResponse() {
fun `test PlatformApiGetTransactionResponse correctness`() {
val wantRefunds: Refund =
Refund.builder()
.amountRefunded(Amount("90.0000", fiatUSD))
Expand Down Expand Up @@ -196,7 +196,7 @@ class Sep31TransactionTest {
}

@Test
fun test_toSep31GetTransactionResponse() {
fun `test Sep31GetTransactionResponse correctness`() {
val refunds =
Sep31GetTransactionResponse.Refunds.builder()
.amountRefunded("90.0000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import lombok.Data;
import org.stellar.anchor.sep31.RefundPayment;

public class JdbcSep31RefundPayment {
@Data
public static class JdbcRefundPayment implements RefundPayment {
String Id;
String amount;
String fee;
}
@Data
public class JdbcSep31RefundPayment implements RefundPayment {
String id;
String amount;
String fee;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.stellar.anchor.platform.data;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.stellar.anchor.sep31.RefundPayment;
Expand All @@ -15,5 +16,28 @@ public class JdbcSep31Refunds implements Refunds {
String amountFee;

@SerializedName("payments")
List<RefundPayment> refundPayments;
List<JdbcSep31RefundPayment> refundPayments;

@Override
public List<RefundPayment> getRefundPayments() {
if (refundPayments == null) return null;
// getPayments() is made for Gson serialization.
List<RefundPayment> payments = new ArrayList<>(refundPayments.size());
for (JdbcSep31RefundPayment refundPayment : refundPayments) {
payments.add(refundPayment);
}
return payments;
}

@Override
public void setRefundPayments(List<RefundPayment> refundPayments) {
this.refundPayments = new ArrayList<>(refundPayments.size());
for (RefundPayment rp : refundPayments) {
if (rp instanceof JdbcSep31RefundPayment)
this.refundPayments.add((JdbcSep31RefundPayment) rp);
else
throw new ClassCastException(
String.format("Error casting %s to JdbcSep31RefundPayment", rp.getClass()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public String getRefundsJson() {

public void setRefundsJson(String refundsJson) {
if (refundsJson != null) {
this.refunds = gson.fromJson(refundsJson, Refunds.class);
this.refunds = gson.fromJson(refundsJson, JdbcSep31Refunds.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Refunds newRefunds() {

@Override
public RefundPayment newRefundPayment() {
return new JdbcSep31RefundPayment.JdbcRefundPayment();
return new JdbcSep31RefundPayment();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.stellar.anchor.platform.data

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert

class JdbcSep31TransactionTest {
private val refundsJsonNoRefundPayment =
"""
{
"amount_refunded": "10",
"amount_fee": "5",
"refundPayments": null
}
""".trimIndent()

private val refundsJsonWithRefundPayment =
"""
{
"amount_refunded": "10",
"amount_fee": "5",
"payments": [
{
"id": "1",
"amount": "5",
"fee": "1"
},
{
"id": "2",
"amount": "5",
"fee": "4"
}
]
}
""".trimIndent()

@Test
fun `test JdbcSep31Transaction refunds Json conversion`() {
val txn = JdbcSep31Transaction()
txn.refundsJson = refundsJsonNoRefundPayment
// strict is set to false because refundPayments is omitted in txn.refundsJson when it is set to
// null.
JSONAssert.assertEquals(txn.refundsJson, refundsJsonNoRefundPayment, false)
assertEquals("10", txn.refunds.amountRefunded)
assertEquals("5", txn.refunds.amountFee)
assertNull(txn.refunds.refundPayments)

txn.refundsJson = refundsJsonWithRefundPayment
JSONAssert.assertEquals(txn.refundsJson, refundsJsonWithRefundPayment, true)
assertEquals("10", txn.refunds.amountRefunded)
assertEquals("5", txn.refunds.amountFee)
assertEquals(2, txn.refunds.refundPayments.size)
assertEquals("1", txn.refunds.refundPayments[0].id)
assertEquals("5", txn.refunds.refundPayments[0].amount)
assertEquals("1", txn.refunds.refundPayments[0].fee)
assertEquals("2", txn.refunds.refundPayments[1].id)
assertEquals("5", txn.refunds.refundPayments[1].amount)
assertEquals("4", txn.refunds.refundPayments[1].fee)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package org.stellar.anchor.platform.service
import io.mockk.*
import io.mockk.impl.annotations.MockK
import java.time.Instant
import org.junit.jupiter.api.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource
import org.stellar.anchor.api.exception.AnchorException
Expand All @@ -19,7 +22,7 @@ import org.stellar.anchor.api.shared.RefundPayment
import org.stellar.anchor.asset.AssetService
import org.stellar.anchor.asset.ResourceJsonAssetService
import org.stellar.anchor.event.models.TransactionEvent
import org.stellar.anchor.platform.data.JdbcSep31RefundPayment.JdbcRefundPayment
import org.stellar.anchor.platform.data.JdbcSep31RefundPayment
import org.stellar.anchor.platform.data.JdbcSep31Refunds
import org.stellar.anchor.platform.data.JdbcSep31Transaction
import org.stellar.anchor.sep31.*
Expand Down Expand Up @@ -78,7 +81,7 @@ class TransactionServiceTest {
// Mock the store
every { sep31TransactionStore.newTransaction() } returns JdbcSep31Transaction()
every { sep31TransactionStore.newRefunds() } returns JdbcSep31Refunds()
every { sep31TransactionStore.newRefundPayment() } answers { JdbcRefundPayment() }
every { sep31TransactionStore.newRefundPayment() } answers { JdbcSep31RefundPayment() }

// mock time
val mockStartedAt = Instant.now().minusSeconds(180)
Expand Down

0 comments on commit 39f74a5

Please sign in to comment.