add more serialization support for various libraries
[storage-units.git] / storage-units-eclipselink / src / test / java / wtf / metio / storageunits / eclipselink / StorageUnitConverterTest.java
blob057ab6a98369ee0f10581cc58734be2ac66a00aa
1 /*
2 * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package wtf.metio.storageunits.eclipselink;
9 import java.math.BigInteger;
10 import java.util.stream.Stream;
11 import org.eclipse.persistence.internal.helper.DatabaseField;
12 import org.eclipse.persistence.mappings.DatabaseMapping;
13 import org.junit.jupiter.api.Assertions;
14 import org.junit.jupiter.api.DynamicTest;
15 import org.junit.jupiter.api.TestFactory;
16 import org.mockito.BDDMockito;
17 import org.mockito.Mockito;
18 import wtf.metio.storageunits.model.StorageUnits;
20 class StorageUnitConverterTest {
22 private Stream<AbstractStorageUnitConverter> converters() {
23 return Stream.of(new BinaryStorageUnitConverter(), new DecimalStorageUnitConverter());
26 @TestFactory
27 Stream<DynamicTest> shouldNotBeMutable() {
28 return converters()
29 .map(converter -> DynamicTest.dynamicTest("should not be mutable",
30 () -> Assertions.assertFalse(converter.isMutable())));
33 @TestFactory
34 Stream<DynamicTest> shouldConvertUnitToBigInteger() {
35 return converters()
36 .map(converter -> converter.convertObjectValueToDataValue(StorageUnits.kilobyte(1), null))
37 .map(dataValue -> DynamicTest.dynamicTest("should convert storage unit to big integer",
38 () -> Assertions.assertEquals(BigInteger.valueOf(1000L), dataValue)));
41 @TestFactory
42 Stream<DynamicTest> shouldConvertBigIntegerToUnit() {
43 return converters()
44 .map(converter -> converter.convertDataValueToObjectValue(BigInteger.valueOf(2000L), null))
45 .map(objectValue -> DynamicTest.dynamicTest("should convert big integer to storage unit",
46 () -> Assertions.assertEquals(StorageUnits.kilobyte(2), objectValue)));
49 @TestFactory
50 Stream<DynamicTest> shouldInitializeAsBigInt() {
51 return converters()
52 .map(converter -> DynamicTest.dynamicTest("should initialize as big integer",
53 () -> {
54 final var mapping = Mockito.mock(DatabaseMapping.class);
55 final var field = new DatabaseField();
56 BDDMockito.given(mapping.getField()).willReturn(field);
58 converter.initialize(mapping, null);
60 Assertions.assertEquals(java.sql.Types.BIGINT, field.getSqlType());
61 }));