Skip to content

Commit

Permalink
Fixed serialization of SingleChoiceControlDataDto
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jul 3, 2024
1 parent ebfcabf commit 5929251
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ public abstract class SingleChoiceControlDataDto implements FormControlDataDto {
@JsonCreator
@Nonnull
public static SingleChoiceControlDataDto get(@JsonProperty(PropertyNames.CONTROL) @Nonnull SingleChoiceControlDescriptor descriptor,
@JsonProperty(PropertyNames.CHOICE) @Nullable PrimitiveFormControlDataDto choice,
@JsonProperty(PropertyNames.VALUE) @Nullable PrimitiveFormControlDataDto choice,
@JsonProperty(PropertyNames.DEPTH) int depth) {
return new AutoValue_SingleChoiceControlDataDto(depth, descriptor, choice);
}

@Nonnull
@JsonProperty(PropertyNames.CONTROL)
public abstract SingleChoiceControlDescriptor getDescriptor();

@JsonProperty(PropertyNames.CONTROL)
@JsonProperty(PropertyNames.VALUE)
@Nullable
protected abstract PrimitiveFormControlDataDto getChoiceInternal();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package edu.stanford.protege.webprotege.forms.data;

import com.google.common.collect.*;
import edu.stanford.protege.webprotege.entity.IRIData;
import edu.stanford.protege.webprotege.forms.field.*;
import edu.stanford.protege.webprotege.jackson.WebProtegeJacksonApplication;
import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.model.IRI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.*;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Import;

import java.io.*;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2024-07-03
*/
@JsonTest
@AutoConfigureJsonTesters
@Import(WebProtegeJacksonApplication.class)
public class SingleChoiceControlDataDtoTest {

@Autowired
private JacksonTester<SingleChoiceControlDataDto> tester;

@Test
void shouldSerialize() throws IOException {
var data = SingleChoiceControlDataDto.get(SingleChoiceControlDescriptor.get(
SingleChoiceControlType.COMBO_BOX,
FixedChoiceListSourceDescriptor.get(ImmutableList.of())),
PrimitiveFormControlDataDto.get(IRIData.get(IRI.create(
"http://example.org/A"), ImmutableMap.of())),
3);
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathStringValue("['@type']", "SingleChoiceControlDataDto");
assertThat(written).hasJsonPathValue("control");
assertThat(written).hasJsonPathValue("value");
assertThat(written).hasJsonPathValue("depth");

}

@Test
void shouldDeserialize() throws IOException {
var expected = SingleChoiceControlDataDto.get(SingleChoiceControlDescriptor.get(
SingleChoiceControlType.COMBO_BOX,
FixedChoiceListSourceDescriptor.get(ImmutableList.of())),
PrimitiveFormControlDataDto.get(IRIData.get(IRI.create(
"http://example.org/A"), ImmutableMap.of())),
3);
var json = """
{"@type":"SingleChoiceControlDataDto","depth":3,"control":{"@type":"SINGLE_CHOICE","widgetType":"ComboBox","choicesSource":{"type":"Fixed","choices":[]},"defaultChoice":null},"value":{"type":"IriFormControlDataDto","iri":{"@type":"IRIData","iri":"http://example.org/A"}}}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject()).isEqualTo(expected);
}
}

0 comments on commit 5929251

Please sign in to comment.