From 20316e5645c1bec65f7f224b2263c9d281d75feb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Ho=C3=9F?= Date: Wed, 10 Aug 2016 23:57:48 +0200 Subject: [PATCH] fix #11 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Sebastian Hoß --- pom.xml | 1 + storage-units-jackson/pom.xml | 2 +- storage-units-mongodb/bnd.bnd | 10 ++ .../pom.xml | 24 ++--- .../mongodb/AbstractStorageUnitCodec.java | 48 +++++++++ .../mongodb/BinaryStorageUnitCodec.java | 26 +++++ .../mongodb/CommonStorageUnitCodec.java | 26 +++++ .../mongodb/DecimalStorageUnitCodec.java | 26 +++++ .../storage_unit/mongodb/package-info.java | 12 +++ .../storage_unit/mongodb/StorageUnitCodecTest.java | 114 +++++++++++++++++++++ 10 files changed, 276 insertions(+), 13 deletions(-) create mode 100644 storage-units-mongodb/bnd.bnd copy {storage-units-jackson => storage-units-mongodb}/pom.xml (83%) create mode 100644 storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/AbstractStorageUnitCodec.java create mode 100644 storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/BinaryStorageUnitCodec.java create mode 100644 storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/CommonStorageUnitCodec.java create mode 100644 storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/DecimalStorageUnitCodec.java create mode 100644 storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/package-info.java create mode 100644 storage-units-mongodb/src/test/java/de/xn__ho_hia/storage_unit/mongodb/StorageUnitCodecTest.java diff --git a/pom.xml b/pom.xml index 2bb69a5..3d18632 100755 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ storage-units storage-units-jackson + storage-units-mongodb diff --git a/storage-units-jackson/pom.xml b/storage-units-jackson/pom.xml index 8f93e2b..70575c3 100644 --- a/storage-units-jackson/pom.xml +++ b/storage-units-jackson/pom.xml @@ -30,7 +30,7 @@ - Storage Units :: Jackson + Storage Units :: Serialization :: Jackson diff --git a/storage-units-mongodb/bnd.bnd b/storage-units-mongodb/bnd.bnd new file mode 100644 index 0000000..10146dc --- /dev/null +++ b/storage-units-mongodb/bnd.bnd @@ -0,0 +1,10 @@ +Bundle-License: Creative Commons Zero +Bundle-Name: ${project.artifactId} +Bundle-Description: ${project.description} +Bundle-DocURL: ${project.url} +Bundle-Version: ${project.version} +Export-Package: de.xn__ho_hia.storage_unit.mongodb +Import-Package: \ + de.xn__ho_hia.quality.null_analysis,\ + de.xn__ho_hia.quality.suppression,\ + * diff --git a/storage-units-jackson/pom.xml b/storage-units-mongodb/pom.xml similarity index 83% copy from storage-units-jackson/pom.xml copy to storage-units-mongodb/pom.xml index 8f93e2b..8cbe04c 100644 --- a/storage-units-jackson/pom.xml +++ b/storage-units-mongodb/pom.xml @@ -24,19 +24,18 @@ - storage-units-jackson + storage-units-mongodb - Storage Units :: Jackson + Storage Units :: Serialization :: MongoDB de.xn--ho-hia.quality suppress-warnings - test de.xn--ho-hia.quality @@ -48,19 +47,20 @@ ${project.version} - com.fasterxml.jackson.core - jackson-core - 2.7.5 + org.mongodb + mongodb-driver + 3.3.0 - com.fasterxml.jackson.core - jackson-databind - 2.7.5 + org.mockito + mockito-core + test - com.fasterxml.jackson.core - jackson-annotations - 2.7.5 + org.jooq + jool + 0.9.11 + test \ No newline at end of file diff --git a/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/AbstractStorageUnitCodec.java b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/AbstractStorageUnitCodec.java new file mode 100644 index 0000000..9f1b632 --- /dev/null +++ b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/AbstractStorageUnitCodec.java @@ -0,0 +1,48 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.mongodb; + +import java.math.BigInteger; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.Codec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.quality.suppression.CompilerWarnings; +import de.xn__ho_hia.storage_unit.StorageUnit; + +/** + * Abstract implementation of a MongoDB {@link Codec} for {@link StorageUnit StorageUnits}. + * + * @see MongoDB Codec documentation + */ +abstract class AbstractStorageUnitCodec implements Codec> { + + @Override + public final void encode(final BsonWriter writer, final StorageUnit value, final EncoderContext encoderContext) { + writer.writeString(value.inByte().toString()); + } + + @Override + @SuppressWarnings(CompilerWarnings.UNCHECKED) + public final Class> getEncoderClass() { + return (Class>) (Object) StorageUnit.class; + } + + @Override + public final StorageUnit decode(final BsonReader reader, final DecoderContext decoderContext) { + final String bsonValue = reader.readString(); + final BigInteger value = new BigInteger(bsonValue); + return convertToStorageUnit(value); + } + + protected abstract StorageUnit convertToStorageUnit(@NonNull BigInteger value); + +} diff --git a/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/BinaryStorageUnitCodec.java b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/BinaryStorageUnitCodec.java new file mode 100644 index 0000000..aa0efec --- /dev/null +++ b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/BinaryStorageUnitCodec.java @@ -0,0 +1,26 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.mongodb; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * + */ +public final class BinaryStorageUnitCodec extends AbstractStorageUnitCodec { + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.binaryValueOf(value); + } + +} diff --git a/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/CommonStorageUnitCodec.java b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/CommonStorageUnitCodec.java new file mode 100644 index 0000000..176b9f6 --- /dev/null +++ b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/CommonStorageUnitCodec.java @@ -0,0 +1,26 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.mongodb; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * + */ +public final class CommonStorageUnitCodec extends AbstractStorageUnitCodec { + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.commonValueOf(value); + } + +} diff --git a/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/DecimalStorageUnitCodec.java b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/DecimalStorageUnitCodec.java new file mode 100644 index 0000000..a61aae2 --- /dev/null +++ b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/DecimalStorageUnitCodec.java @@ -0,0 +1,26 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.mongodb; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * + */ +public final class DecimalStorageUnitCodec extends AbstractStorageUnitCodec { + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.decimalValueOf(value); + } + +} diff --git a/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/package-info.java b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/package-info.java new file mode 100644 index 0000000..51501d1 --- /dev/null +++ b/storage-units-mongodb/src/main/java/de/xn__ho_hia/storage_unit/mongodb/package-info.java @@ -0,0 +1,12 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +/** + * Serialization support for MongoDB. + * + * @see MongoDB Homepage + */ +package de.xn__ho_hia.storage_unit.mongodb; diff --git a/storage-units-mongodb/src/test/java/de/xn__ho_hia/storage_unit/mongodb/StorageUnitCodecTest.java b/storage-units-mongodb/src/test/java/de/xn__ho_hia/storage_unit/mongodb/StorageUnitCodecTest.java new file mode 100644 index 0000000..9808a1e --- /dev/null +++ b/storage-units-mongodb/src/test/java/de/xn__ho_hia/storage_unit/mongodb/StorageUnitCodecTest.java @@ -0,0 +1,114 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.mongodb; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; +import org.jooq.lambda.tuple.Tuple2; +import org.junit.Assert; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.Mockito; + +import de.xn__ho_hia.quality.suppression.CompilerWarnings; +import de.xn__ho_hia.storage_unit.CommonKilobyte; +import de.xn__ho_hia.storage_unit.Kibibyte; +import de.xn__ho_hia.storage_unit.Kilobyte; +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * + */ +@RunWith(Theories.class) +@SuppressWarnings(CompilerWarnings.STATIC_METHOD) +public class StorageUnitCodecTest { + + /** + * @return Suppliers to create a storage-unit codec. + */ + @DataPoints("supplier") + public static final List, Class>> suppliers() { + final List, Class>> suppliers = new ArrayList<>(); + suppliers.add(new Tuple2<>(BinaryStorageUnitCodec::new, Kibibyte.class)); + suppliers.add(new Tuple2<>(CommonStorageUnitCodec::new, CommonKilobyte.class)); + suppliers.add(new Tuple2<>(DecimalStorageUnitCodec::new, Kilobyte.class)); + return suppliers; + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldEncodeStorageUnitClass( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final AbstractStorageUnitCodec codec = supplier.v1.get(); + + // when + final Class> encoderClass = codec.getEncoderClass(); + + // then + Assert.assertNotNull(encoderClass); + Assert.assertEquals(StorageUnit.class, encoderClass); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + @SuppressWarnings(CompilerWarnings.NLS) + public void shouldEncodeStorageUnit( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final AbstractStorageUnitCodec codec = supplier.v1.get(); + final BsonWriter writer = Mockito.mock(BsonWriter.class); + final EncoderContext context = EncoderContext.builder().build(); + final StorageUnit value = StorageUnits.kilobyte(1L); + + // when + codec.encode(writer, value, context); + + // then + Mockito.verify(writer).writeString("1000"); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + @SuppressWarnings(CompilerWarnings.NLS) + public void shouldDecodeStorageUnit( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final AbstractStorageUnitCodec codec = supplier.v1.get(); + final BsonReader reader = Mockito.mock(BsonReader.class); + final DecoderContext context = DecoderContext.builder().build(); + BDDMockito.given(reader.readString()).willReturn("2000"); + + // when + final StorageUnit value = codec.decode(reader, context); + + // then + Assert.assertNotNull(value); + Assert.assertEquals(supplier.v2, value.getClass()); + } + +} -- 2.11.4.GIT