Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#96: Bugfix: BigDecimal for money is now always scaled to 2 with Roun…
…dingMode.HALF_UP
- Loading branch information
Wout Schoovaerts
committed
Oct 13, 2023
1 parent
849afd9
commit 7392711
Showing
6 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/be/woutschoovaerts/mollie/serializer/MoneySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package be.woutschoovaerts.mollie.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
public class MoneySerializer extends JsonSerializer<BigDecimal> { | ||
@Override | ||
public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException { | ||
BigDecimal scaledValue = value.setScale(2, RoundingMode.HALF_UP); | ||
jsonGenerator.writeObject(scaledValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/be/woutschoovaerts/mollie/serializer/MoneySerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package be.woutschoovaerts.mollie.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class MoneySerializerTest { | ||
|
||
@Mock | ||
private JsonGenerator jsonGenerator; | ||
|
||
@InjectMocks | ||
private MoneySerializer serializer; | ||
|
||
@Captor | ||
private ArgumentCaptor<BigDecimal> bigDecimalCaptor; | ||
|
||
@Test | ||
void serialize() throws Exception { | ||
serializer.serialize(new BigDecimal("12.456"), jsonGenerator, null); | ||
|
||
verify(jsonGenerator).writeObject(bigDecimalCaptor.capture()); | ||
assertEquals(2, bigDecimalCaptor.getValue().scale()); | ||
} | ||
} |