Skip to content

Commit

Permalink
WIP deserialization test for omitted id field
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Aug 22, 2023
1 parent 1db0d6e commit 180ca79
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.vena.bosk.TestEntityBuilder;
import io.vena.bosk.annotations.DerivedRecord;
import io.vena.bosk.annotations.DeserializationPath;
import io.vena.bosk.annotations.ReferencePath;
import io.vena.bosk.exceptions.InvalidTypeException;
import io.vena.bosk.exceptions.MalformedPathException;
import io.vena.bosk.exceptions.ParameterUnboundException;
Expand Down Expand Up @@ -55,6 +56,7 @@
import static io.vena.bosk.AbstractBoskTest.TestEnum.OK;
import static io.vena.bosk.ListingEntry.LISTING_ENTRY;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
Expand All @@ -72,6 +74,12 @@ class JacksonPluginTest extends AbstractBoskTest {
private ObjectMapper boskMapper;
private CatalogReference<TestEntity> entitiesRef;
private Reference<TestEntity> parentRef;
private Refs refs;

public interface Refs {
@ReferencePath("/entities/-entity-") Reference<TestEntity> entity(Identifier entity);
@ReferencePath("/entities/-entity-/implicitRefs") Reference<ImplicitRefs> implicitRefs(Identifier entity);
}

/**
* Not configured by JacksonPlugin. Only for checking the properties of the generated JSON.
Expand All @@ -92,6 +100,7 @@ void setUpJackson() throws Exception {
boskMapper = new ObjectMapper()
.registerModule(jacksonPlugin.moduleFor(bosk))
.enable(INDENT_OUTPUT);
refs = bosk.buildReferences(Refs.class);
}

@Test
Expand Down Expand Up @@ -467,6 +476,39 @@ public static class DeserializationPathContainer implements StateTreeNode {
ImplicitRefs secondField;
}

@Test
void deserializationPathMissingID_filledInFromContext() throws InvalidTypeException, JsonProcessingException {
Map<String, Object> plainObject = new LinkedHashMap<>();
plainObject.put("firstField", emptyMap());
plainObject.put("secondField", emptyMap());

String json = plainMapper.writeValueAsString(plainObject);

Identifier id123 = Identifier.from("123");
Identifier id456 = Identifier.from("456");
DeserializationPathContainer expected = new DeserializationPathContainer(
new ImplicitRefs(id123,
refs.implicitRefs(id123),
refs.entity(id123),
refs.implicitRefs(id123),
refs.entity(id123)),
new ImplicitRefs(id456,
refs.implicitRefs(id456),
refs.entity(id456),
refs.implicitRefs(id456),
refs.entity(id456))
);

BindingEnvironment env = BindingEnvironment.empty().builder()
.bind("entity1", id123)
.bind("entity2", Identifier.from("456"))
.build();
try (DeserializationScope scope = jacksonPlugin.overlayScope(env)) {
Object actual = boskObjectFor(plainObject, new TypeReference<DeserializationPathContainer>() {}, Path.empty());
assertEquals(expected, actual);
}
}

private TestEntity makeEntityWithOptionalString(Optional<String> optionalString) throws InvalidTypeException {
CatalogReference<TestEntity> catalogRef = entitiesRef;
Identifier entityID = Identifier.unique("testOptional");
Expand Down

0 comments on commit 180ca79

Please sign in to comment.