Skip to content

Commit

Permalink
Descriptive test names (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 13, 2024
1 parent f7469dd commit 7f68047
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 236 deletions.
12 changes: 6 additions & 6 deletions src/test/java/io/vavr/jackson/datatype/CharSeqTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.vavr.jackson.datatype;

import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
Expand All @@ -11,10 +9,12 @@

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertThrows;

class CharSeqTest extends BaseTest {

@Test
void test1() throws IOException {
void shouldSerializeAndDeserializeCharSeq() throws IOException {
ObjectWriter writer = mapper().writer();
CharSeq src = CharSeq.of("abc");
String json = writer.writeValueAsString(src);
Expand All @@ -24,7 +24,7 @@ void test1() throws IOException {
}

@Test
void test2() throws IOException {
void shouldSerializeAndDeserializeWrappedCharSeqAsObject() throws IOException {
ObjectMapper mapper = mapper().addMixIn(CharSeq.class, WrapperObject.class);
CharSeq src = CharSeq.of("abc");
String plainJson = mapper().writeValueAsString(src);
Expand All @@ -35,7 +35,7 @@ void test2() throws IOException {
}

@Test
void test3() throws IOException {
void shouldSerializeAndDeserializeWrappedCharSeqAsArray() throws IOException {
ObjectMapper mapper = mapper().addMixIn(CharSeq.class, WrapperArray.class);
CharSeq src = CharSeq.of("abc");
String plainJson = mapper().writeValueAsString(src);
Expand All @@ -46,7 +46,7 @@ void test3() throws IOException {
}

@Test
void test4() {
void shouldThrowExceptionWhenDeserializingInvalidCharSeq() {
assertThrows(JsonMappingException.class, () -> mapper().readValue("42", CharSeq.class));
}
}
105 changes: 53 additions & 52 deletions src/test/java/io/vavr/jackson/datatype/EitherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.scala.DefaultScalaModule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;

import io.vavr.Tuple;
import io.vavr.collection.HashSet;
import io.vavr.collection.List;
import io.vavr.collection.Set;
import io.vavr.control.Either;
import io.vavr.control.Option;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;

import static io.vavr.control.Option.none;
import static io.vavr.control.Option.some;

public class EitherTest extends BaseTest {

@SuppressWarnings("unchecked")
@Test
void testWrapperObject() throws IOException {
void shouldSerializeAndDeserializeWrappedEitherObject() throws IOException {
ObjectMapper mapper = mapper().addMixIn(Either.class, WrapperObject.class);
Either<Integer, Integer> src = Either.right(1);
String plainJson = mapper().writeValueAsString(src);
Expand All @@ -37,117 +34,102 @@ void testWrapperObject() throws IOException {
Assertions.assertEquals(src, restored);
}

@SuppressWarnings("unchecked")
@Test
void test1() throws IOException {
void shouldSerializeAndDeserializeLeftEither() throws IOException {
Either<String, Integer> left = Either.left("left");
String json = mapper().writer().writeValueAsString(left);
Either<String, Integer> restored = mapper().readValue(json, Either.class);
Assertions.assertEquals(left, restored);
}

@SuppressWarnings("unchecked")
@Test
void test2() throws IOException {
void shouldSerializeAndDeserializeRightEither() throws IOException {
Either<String, Integer> right = Either.right(1);
String json = mapper().writer().writeValueAsString(right);
Either<String, Integer> restored = mapper().readValue(json, Either.class);
Assertions.assertEquals(right, restored);
}

@Test
void test3() throws IOException {
void shouldThrowExceptionWhenInvalidJsonHasExtraRightValue() throws IOException {
Assertions.assertThrows(JsonMappingException.class, () -> {
String json = "[\"right\", 2, 3]";
mapper().readValue(json, Either.class);
});
}

@Test
void test4() throws IOException {
void shouldThrowExceptionWhenInvalidJsonHasMissingRightValue() throws IOException {
Assertions.assertThrows(JsonMappingException.class, () -> {
String json = "[\"right\"]";
mapper().readValue(json, Either.class);
});
}

@Test
void test5() throws IOException {
void shouldThrowExceptionWhenInvalidJsonHasMalformedLeftType() throws IOException {
Assertions.assertThrows(JsonMappingException.class, () -> {
String json = "[\"lEft\", 42]";
mapper().readValue(json, Either.class);
});
}

@Test
void test6() throws IOException {
void shouldThrowExceptionWhenInvalidJsonHasNonStringLeftOrRight() throws IOException {
Assertions.assertThrows(JsonMappingException.class, () -> {
String json = "[42, 42]";
mapper().readValue(json, Either.class);
});
}

@SuppressWarnings("unchecked")
@Test
void test7() throws IOException {
void shouldSerializeAndDeserializeLeftEitherWithGenericTypes() throws IOException {
Either<List<Integer>, Set<Double>> either = Either.left(List.of(42));
String json = mapper().writer().writeValueAsString(either);
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {});
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {
});
Assertions.assertEquals(either, restored);
}

@SuppressWarnings("unchecked")
@Test
void test8() throws IOException {
void shouldSerializeAndDeserializeRightEitherWithGenericTypes() throws IOException {
Either<List<Integer>, Set<Double>> either = Either.right(HashSet.of(42.0));
String json = mapper().writer().writeValueAsString(either);
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {});
Either<List<Integer>, Set<Double>> restored = mapper().readValue(json, new TypeReference<Either<List<Integer>, Set<Double>>>() {
});
Assertions.assertEquals(either, restored);
}

@SuppressWarnings("unchecked")
@Test
void testNullSerialization() throws IOException {
void shouldHandleNullInLeftEitherDuringSerialization() throws IOException {
Either<String, Integer> left = Either.left(null);
String leftJson = mapper().writer().writeValueAsString(left);
Either<String, Integer> restoredLeft = mapper().readValue(leftJson, Either.class);
Assertions.assertEquals(left, restoredLeft);
}

@Test
void shouldHandleNullInRightEitherDuringSerialization() throws IOException {
Either<String, Integer> right = Either.left(null);
String rightJson = mapper().writer().writeValueAsString(right);
Either<String, Integer> restoredRight = mapper().readValue(rightJson, Either.class);
Assertions.assertEquals(right, restoredRight);
}

@Test
void testWithOption() throws IOException {
TypeReference<Either<Option<String>, Option<String>>> typeReference = new TypeReference<Either<Option<String>, Option<String>>>() {};
void shouldSerializeAndDeserializeEitherWithOption() throws IOException {
TypeReference<Either<Option<String>, Option<String>>> typeReference = new TypeReference<Either<Option<String>, Option<String>>>() {
};
verifySerialization(typeReference, List.of(
Tuple.of(Either.left(none()), genJsonList("left", null)),
Tuple.of(Either.right(none()), genJsonList("right", null)),
Tuple.of(Either.left(some("value")), genJsonList("left", "value")),
Tuple.of(Either.right(some("value")), genJsonList("right", "value"))
Tuple.of(Either.left(none()), genJsonList("left", null)),
Tuple.of(Either.right(none()), genJsonList("right", null)),
Tuple.of(Either.left(some("value")), genJsonList("left", "value")),
Tuple.of(Either.right(some("value")), genJsonList("right", "value"))
));
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT,
property = "type")
@JsonTypeName("card")
private static class TestSerialize {
public String type = "hello";
}

private static class Left {
public Either<TestSerialize, TestSerialize> f = Either.left(new TestSerialize());
}

private static class Right {
public Either<TestSerialize, TestSerialize> f = Either.right(new TestSerialize());
}

@Test
void testJsonTypeInfo() throws IOException {
void shouldSerializeAndDeserializeCustomTypeWithJsonTypeInfo() throws IOException {
String javaUtilValue;
javaUtilValue = mapper().writeValueAsString(new Left());
Assertions.assertEquals("{\"f\":[\"left\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue);
Expand All @@ -160,19 +142,38 @@ void testJsonTypeInfo() throws IOException {
}

@Test
void testDeserializingScalaEither() throws IOException {
void shouldSerializeAndDeserializeScalaEither() throws IOException {
final scala.util.Either<String, BigInteger> left = scala.util.Left.apply("test");
final scala.util.Either<String, BigInteger> right = scala.util.Right.apply(BigInteger.ONE);
final ObjectMapper mapper = mapper().registerModule(new DefaultScalaModule());

final String serializedLeft = mapper.writeValueAsString(left);
final Either<String, BigInteger> deserializedLeft =
mapper.readValue(serializedLeft, new TypeReference<Either<String, BigInteger>>() { });
mapper.readValue(serializedLeft, new TypeReference<Either<String, BigInteger>>() {
});
Assertions.assertEquals("test", deserializedLeft.getLeft());

final String serializedRight = mapper.writeValueAsString(right);
final Either<String, BigInteger> deserializedRight =
mapper.readValue(serializedRight, new TypeReference<Either<String, BigInteger>>() { });
mapper.readValue(serializedRight, new TypeReference<Either<String, BigInteger>>() {
});
Assertions.assertEquals(BigInteger.ONE, deserializedRight.get());
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT,
property = "type")
@JsonTypeName("card")
private static class TestSerialize {
public String type = "hello";
}

private static class Left {
public Either<TestSerialize, TestSerialize> f = Either.left(new TestSerialize());
}

private static class Right {
public Either<TestSerialize, TestSerialize> f = Either.right(new TestSerialize());
}
}
Loading

0 comments on commit 7f68047

Please sign in to comment.