-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a97cb2b
commit fa25c2b
Showing
11 changed files
with
577 additions
and
3 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
core/src/main/java/com/cosium/hal_mock_mvc/InlineElementRepresentationDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.cosium.hal_mock_mvc; | ||
|
||
import com.cosium.hal_mock_mvc.template.options.InlineElementRepresentation; | ||
import com.cosium.hal_mock_mvc.template.options.MapInlineElementRepresentation; | ||
import com.cosium.hal_mock_mvc.template.options.StringInlineElementRepresentation; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.exc.MismatchedInputException; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
class InlineElementRepresentationDeserializer extends StdDeserializer<InlineElementRepresentation> { | ||
|
||
public InlineElementRepresentationDeserializer() { | ||
this(null); | ||
} | ||
|
||
protected InlineElementRepresentationDeserializer(Class<?> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public InlineElementRepresentation deserialize(JsonParser p, DeserializationContext ctxt) | ||
throws IOException { | ||
|
||
JsonToken currentToken = p.currentToken(); | ||
if (currentToken == JsonToken.START_OBJECT) { | ||
Map<String, String> map = p.readValueAs(new StringStringMap()); | ||
if (map == null) { | ||
return null; | ||
} | ||
return new MapInlineElementRepresentation(map); | ||
} | ||
if (currentToken == JsonToken.VALUE_STRING) { | ||
String value = p.readValueAs(String.class); | ||
if (value == null) { | ||
return null; | ||
} | ||
return new StringInlineElementRepresentation(value); | ||
} | ||
|
||
throw MismatchedInputException.from( | ||
p, | ||
InlineElementRepresentation.class, | ||
"%s should have been either %s or %s. But it is not." | ||
.formatted(currentToken, JsonToken.START_OBJECT, JsonToken.VALUE_STRING)); | ||
} | ||
|
||
private static class StringStringMap extends TypeReference<Map<String, String>> {} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/java/com/cosium/hal_mock_mvc/JacksonModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.cosium.hal_mock_mvc; | ||
|
||
import com.cosium.hal_mock_mvc.template.options.InlineElementRepresentation; | ||
import com.fasterxml.jackson.core.Version; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
class JacksonModule extends SimpleModule { | ||
|
||
public JacksonModule() { | ||
addDeserializer( | ||
InlineElementRepresentation.class, new InlineElementRepresentationDeserializer()); | ||
} | ||
|
||
@Override | ||
public String getModuleName() { | ||
return "com.cosium.hal_mock_mvc"; | ||
} | ||
|
||
@Override | ||
public Version version() { | ||
return Version.unknownVersion(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
core/src/main/java/com/cosium/hal_mock_mvc/template/options/InlineElementRepresentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.cosium.hal_mock_mvc.template.options; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
public sealed interface InlineElementRepresentation | ||
permits MapInlineElementRepresentation, StringInlineElementRepresentation {} |
15 changes: 15 additions & 0 deletions
15
...rc/main/java/com/cosium/hal_mock_mvc/template/options/MapInlineElementRepresentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cosium.hal_mock_mvc.template.options; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
public record MapInlineElementRepresentation(Map<String, String> map) | ||
implements InlineElementRepresentation { | ||
|
||
public MapInlineElementRepresentation(Map<String, String> map) { | ||
this.map = Optional.ofNullable(map).map(Map::copyOf).orElseGet(Map::of); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
core/src/main/java/com/cosium/hal_mock_mvc/template/options/OptionsLinkRepresentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.cosium.hal_mock_mvc.template.options; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
public class OptionsLinkRepresentation { | ||
|
||
private final String href; | ||
private final String type; | ||
private final boolean templated; | ||
|
||
@JsonCreator | ||
public OptionsLinkRepresentation( | ||
@JsonProperty("href") String href, | ||
@JsonProperty("type") String type, | ||
@JsonProperty("templated") Boolean templated) { | ||
this.href = requireNonNull(href, "Attribute 'href' is missing"); | ||
this.type = type; | ||
this.templated = Optional.ofNullable(templated).orElse(false); | ||
} | ||
|
||
public String href() { | ||
return href; | ||
} | ||
|
||
public Optional<String> type() { | ||
return Optional.ofNullable(type); | ||
} | ||
|
||
public boolean templated() { | ||
return templated; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
core/src/main/java/com/cosium/hal_mock_mvc/template/options/OptionsRepresentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.cosium.hal_mock_mvc.template.options; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
public class OptionsRepresentation { | ||
|
||
private final List<InlineElementRepresentation> inline; | ||
private final OptionsLinkRepresentation link; | ||
private final Long maxItems; | ||
private final long minItems; | ||
private final String promptField; | ||
private final List<String> selectedValues; | ||
private final String valueField; | ||
|
||
@JsonCreator | ||
public OptionsRepresentation( | ||
@JsonProperty("inline") List<InlineElementRepresentation> inline, | ||
@JsonProperty("link") OptionsLinkRepresentation link, | ||
@JsonProperty("maxItems") Long maxItems, | ||
@JsonProperty("minItems") Long minItems, | ||
@JsonProperty("promptField") String promptField, | ||
@JsonProperty("selectedValues") List<String> selectedValues, | ||
@JsonProperty("valueField") String valueField) { | ||
this.inline = Optional.ofNullable(inline).map(List::copyOf).orElseGet(List::of); | ||
this.link = link; | ||
this.maxItems = maxItems; | ||
this.minItems = Optional.ofNullable(minItems).orElse(0L); | ||
this.promptField = promptField; | ||
this.selectedValues = Optional.ofNullable(selectedValues).map(List::copyOf).orElseGet(List::of); | ||
this.valueField = valueField; | ||
} | ||
|
||
public List<InlineElementRepresentation> inline() { | ||
return inline; | ||
} | ||
|
||
public Optional<OptionsLinkRepresentation> link() { | ||
return Optional.ofNullable(link); | ||
} | ||
|
||
public Optional<Long> maxItems() { | ||
return Optional.ofNullable(maxItems); | ||
} | ||
|
||
public long minItems() { | ||
return minItems; | ||
} | ||
|
||
public Optional<String> promptField() { | ||
return Optional.ofNullable(promptField); | ||
} | ||
|
||
public List<String> selectedValues() { | ||
return selectedValues; | ||
} | ||
|
||
public Optional<String> valueField() { | ||
return Optional.ofNullable(valueField); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...main/java/com/cosium/hal_mock_mvc/template/options/StringInlineElementRepresentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.cosium.hal_mock_mvc.template.options; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* @author Réda Housni Alaoui | ||
*/ | ||
public record StringInlineElementRepresentation(String value) | ||
implements InlineElementRepresentation { | ||
|
||
public StringInlineElementRepresentation { | ||
requireNonNull(value); | ||
} | ||
} |
Oops, something went wrong.