|
| 1 | +package de.gesellix.docker.remote.api.core; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Disabled; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import com.squareup.moshi.Json; |
| 13 | +import com.squareup.moshi.JsonAdapter; |
| 14 | +import com.squareup.moshi.JsonDataException; |
| 15 | +import com.squareup.moshi.Moshi; |
| 16 | +import com.squareup.moshi.Types; |
| 17 | + |
| 18 | +import de.gesellix.docker.remote.api.ChangeType; |
| 19 | +import de.gesellix.docker.remote.api.ContainerState; |
| 20 | + |
| 21 | +class NullIfEmptyEnumAdapterFactoryTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + @Disabled("not sure if this should work as expected by the test") |
| 25 | + public void shouldReadNullEnumValues() throws IOException { |
| 26 | + TestWrapper<ContainerState.Status> result = deserialize( |
| 27 | + ContainerState.Status.class, |
| 28 | + null); |
| 29 | + assertNull(result.getProperty()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void shouldReadEmptyStringEnumValuesAsNull() throws IOException { |
| 34 | + TestWrapper<ContainerState.Status> result = deserialize( |
| 35 | + ContainerState.Status.class, |
| 36 | + "\"\""); |
| 37 | + assertNull(result.getProperty()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldFailWhenReadingInvalidEnumValues() { |
| 42 | + JsonDataException thrown = assertThrows(JsonDataException.class, |
| 43 | + () -> deserialize(ContainerState.Status.class, "\"foobar\"")); |
| 44 | + assertEquals("Expected one of [created, running, paused, restarting, removing, exited, dead] but was foobar at path $", thrown.getMessage()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void shouldReadStringEnumValues() throws IOException { |
| 49 | + TestWrapper<ContainerState.Status> result = deserialize( |
| 50 | + ContainerState.Status.class, |
| 51 | + "\"" + ContainerState.Status.Dead.getValue() + "\""); |
| 52 | + assertEquals(ContainerState.Status.Dead, result.getProperty()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void shouldReadIntegerEnumValuesFromString() throws IOException { |
| 57 | + TestWrapper<ChangeType> result = deserialize( |
| 58 | + ChangeType.class, |
| 59 | + "\"" + ChangeType.T1.getValue() + "\""); |
| 60 | + assertEquals(ChangeType.T1, result.getProperty()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldReadIntegerEnumValuesFromNumber() throws IOException { |
| 65 | + TestWrapper<ChangeType> result = deserialize( |
| 66 | + ChangeType.class, |
| 67 | + ChangeType.T1.getValue()); |
| 68 | + assertEquals(ChangeType.T1, result.getProperty()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void shouldReadDoubleEnumValuesFromString() throws IOException { |
| 73 | + TestWrapper<TestEnum> result = deserialize( |
| 74 | + TestEnum.class, |
| 75 | + "\"" + TestEnum.V1_2.getValue() + "\""); |
| 76 | + assertEquals(TestEnum.V1_2, result.getProperty()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Disabled("not sure if this should work as expected by the test") |
| 81 | + public void shouldReadDoubleEnumValuesFromNumber() throws IOException { |
| 82 | + TestWrapper<TestEnum> result = deserialize( |
| 83 | + TestEnum.class, |
| 84 | + TestEnum.V1_2.getValue()); |
| 85 | + assertEquals(TestEnum.V1_2, result.getProperty()); |
| 86 | + } |
| 87 | + |
| 88 | + private <T> TestWrapper<T> deserialize(Class<T> enumType, Object serializedValue) throws IOException { |
| 89 | + JsonAdapter<TestWrapper<T>> moshi = new Moshi.Builder() |
| 90 | + .add(new NullIfEmptyEnumAdapterFactory()) |
| 91 | + .add(new TestWrapperAdapter<T>()) |
| 92 | + .build() |
| 93 | + .adapter(Types.newParameterizedType(TestWrapper.class, enumType)); |
| 94 | + |
| 95 | + String serialized = "{ \"property\" : " + serializedValue + " }"; |
| 96 | + System.out.println("Deserializing '" + serialized + "'"); |
| 97 | + return moshi.fromJson(serialized); |
| 98 | + } |
| 99 | + |
| 100 | + enum TestEnum { |
| 101 | + @Json(name = "1.1") |
| 102 | + V1_1("1.1"), |
| 103 | + @Json(name = "1.2") |
| 104 | + V1_2("1.2"); |
| 105 | + |
| 106 | + private final String value; |
| 107 | + |
| 108 | + TestEnum(String value) { |
| 109 | + this.value = value; |
| 110 | + } |
| 111 | + |
| 112 | + public String getValue() { |
| 113 | + return value; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments