Skip to content

Commit

Permalink
Add testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
macisamuele committed Feb 10, 2020
1 parent 4b0156e commit c81b2ab
Showing 1 changed file with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import okio.Buffer;
import org.junit.Test;

import javax.annotation.Nullable;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -105,6 +108,35 @@ public final class PolymorphicJsonAdapterFactoryTest {
assertThat(message).isNull();
}

@Test public void specifiedFallbackJsonAdapter() throws IOException {
Moshi moshi = new Moshi.Builder()
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
.withSubtype(Success.class, "success")
.withSubtype(Error.class, "error")
.withFallbackJsonAdapter(new JsonAdapter<Message>() {
@Override
public Message fromJson(JsonReader reader) throws IOException {
reader.skipValue();
return new EmptyMessage();
}

@Override
public void toJson(JsonWriter writer, @Nullable Message value) {
throw new RuntimeException("Not implemented as not needed for the test");
}
})
)
.build();
JsonAdapter<Message> adapter = moshi.adapter(Message.class);

JsonReader reader =
JsonReader.of(new Buffer().writeUtf8("{\"type\":\"data\",\"value\":\"Okay!\"}"));

Message message = adapter.fromJson(reader);
assertThat(message).isInstanceOf(EmptyMessage.class);
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}

@Test public void unregisteredSubtype() {
Moshi moshi = new Moshi.Builder()
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
Expand All @@ -125,6 +157,52 @@ public final class PolymorphicJsonAdapterFactoryTest {
}
}

@Test public void unregisteredSubtypeWithDefaultValue() {
Error fallbackError = new Error(Collections.<String, Object>emptyMap());
Moshi moshi = new Moshi.Builder()
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
.withSubtype(Success.class, "success")
.withSubtype(Error.class, "error")
.withDefaultValue(fallbackError))
.build();
JsonAdapter<Message> adapter = moshi.adapter(Message.class);

try {
adapter.toJson(new EmptyMessage());
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Expected one of [class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Success, class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Error] but found"
+ " EmptyMessage, a class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$EmptyMessage. Register"
+ " this subtype.");
}
}

@Test public void unregisteredSubtypeWithFallbackJsonAdapter() {
Moshi moshi = new Moshi.Builder()
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
.withSubtype(Success.class, "success")
.withSubtype(Error.class, "error")
.withFallbackJsonAdapter(new JsonAdapter<Message>() {
@Nullable
@Override
public Message fromJson(JsonReader reader) {
throw new RuntimeException("Not implemented as not needed for the test");
}

@Override
public void toJson(JsonWriter writer, @Nullable Message value) throws IOException {
writer.name("type").value("injected by fallbackJsonAdapter");
}
}))
.build();
JsonAdapter<Message> adapter = moshi.adapter(Message.class);

String json = adapter.toJson(new EmptyMessage());
assertThat(json).isEqualTo("{\"type\":\"injected by fallbackJsonAdapter\"}");
}

@Test public void nonStringLabelValue() throws IOException {
Moshi moshi = new Moshi.Builder()
.add(PolymorphicJsonAdapterFactory.of(Message.class, "type")
Expand Down

0 comments on commit c81b2ab

Please sign in to comment.