From 6efb1c82fa108965bca4e4e12eb389dd37195718 Mon Sep 17 00:00:00 2001 From: Nicklas Lundin Date: Tue, 19 Mar 2024 13:27:58 +0100 Subject: [PATCH] fix: support for mapping a OF Number to proto (#93) fix: add support for mapping a OF Number to proto --- .../com/spotify/confidence/TypeMapper.java | 4 +- .../spotify/confidence/TypeMapperTest.java | 108 ++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/spotify/confidence/TypeMapperTest.java diff --git a/src/main/java/com/spotify/confidence/TypeMapper.java b/src/main/java/com/spotify/confidence/TypeMapper.java index 9068c3ce..17c18ac5 100644 --- a/src/main/java/com/spotify/confidence/TypeMapper.java +++ b/src/main/java/com/spotify/confidence/TypeMapper.java @@ -103,7 +103,9 @@ public static Value from(Struct struct, StructFlagSchema schema) { } public static com.google.protobuf.Value from(Value val) { - if (val.isBoolean()) { + if (val.isNumber()) { + return Values.of(val.asDouble()); + } else if (val.isBoolean()) { return Values.of(val.asBoolean()); } else if (val.isNull()) { return Values.ofNull(); diff --git a/src/test/java/com/spotify/confidence/TypeMapperTest.java b/src/test/java/com/spotify/confidence/TypeMapperTest.java new file mode 100644 index 00000000..96b388ca --- /dev/null +++ b/src/test/java/com/spotify/confidence/TypeMapperTest.java @@ -0,0 +1,108 @@ +package com.spotify.confidence; + +import static org.junit.jupiter.api.Assertions.*; + +import com.google.protobuf.util.Values; +import dev.openfeature.sdk.MutableStructure; +import dev.openfeature.sdk.Value; +import dev.openfeature.sdk.exceptions.ValueNotConvertableError; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class TypeMapperTest { + + @Test + public void testFromIntegerNumber() { + // given + final Value intValue = new Value(42); + // when + final com.google.protobuf.Value protoValue = TypeMapper.from(intValue); + // then + assertEquals(42, protoValue.getNumberValue()); + } + + @Test + public void testFromDoubleNumber() { + // given + final Value intValue = new Value(42.0); + // when + final com.google.protobuf.Value protoValue = TypeMapper.from(intValue); + // then + assertEquals(42.0, protoValue.getNumberValue()); + } + + @Test + public void testFromValueWithBoolean() { + // Create a Value with a boolean + final Value value = new Value(true); + + // Call the method under test + final com.google.protobuf.Value protoValue = TypeMapper.from(value); + + // Check the result + assertTrue(protoValue.getBoolValue()); + } + + @Test + public void testFromValueWithNull() { + // Create a Value with a null value + final Value value = new Value(); + + // Call the method under test + final com.google.protobuf.Value protoValue = TypeMapper.from(value); + + // Check the result + assertTrue(protoValue.hasNullValue()); + } + + @Test + public void testFromValueWithInstant() { + // Create a Value with an Instant + final Value value = new Value(java.time.Instant.now()); + + // Call the method under test + assertThrows(ValueNotConvertableError.class, () -> TypeMapper.from(value)); + } + + @Test + public void testFromValueWithString() { + // Create a Value with a string + final Value value = new Value("test"); + + // Call the method under test + final com.google.protobuf.Value protoValue = TypeMapper.from(value); + + // Check the result + assertEquals("test", protoValue.getStringValue()); + } + + @Test + public void testFromValueWithList() { + // Create a Value with a list + final Value value = new Value(Arrays.asList(new Value("item1"), new Value("item2"))); + + // Call the method under test + final com.google.protobuf.Value protoValue = TypeMapper.from(value); + + // Check the result + assertEquals( + Arrays.asList(Values.of("item1"), Values.of("item2")), + protoValue.getListValue().getValuesList()); + } + + @Test + public void testFromValueWithStructure() { + // Create a Value with a structure + final Map map = new HashMap<>(); + map.put("field", new Value("value")); + final Value value = new Value(new MutableStructure(map)); + + // Call the method under test + final com.google.protobuf.Value protoValue = TypeMapper.from(value); + + // Check the result + assertEquals(Values.of("value"), protoValue.getStructValue().getFieldsMap().get("field")); + } +}