|
1 | 1 | package com.sap.ai.sdk.orchestration; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
3 | 4 | import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
4 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
5 | 8 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 10 | import com.fasterxml.jackson.databind.json.JsonMapper; |
7 | 11 | import com.fasterxml.jackson.databind.module.SimpleModule; |
8 | 12 | import java.util.List; |
9 | 13 | import lombok.SneakyThrows; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
10 | 15 | import org.junit.jupiter.api.Test; |
11 | 16 |
|
12 | 17 | class PolymorphicFallbackDeserializerTest { |
13 | 18 |
|
14 | | - @SneakyThrows |
15 | | - @Test |
16 | | - void testValueTypeResolutionFailure() { |
| 19 | + private static String candidateAJson; |
| 20 | + private static String candidateBJson; |
17 | 21 |
|
18 | | - interface AbstractParent {} |
19 | | - |
20 | | - class KnownCandidate implements AbstractParent { |
21 | | - String content; |
22 | | - } |
23 | | - |
24 | | - class UnknownCandidate implements AbstractParent { |
25 | | - List<String> content; |
26 | | - } |
| 22 | + @BeforeEach |
| 23 | + void setUp() { |
| 24 | + candidateAJson = |
| 25 | + """ |
| 26 | + { |
| 27 | + "content": "I am candidate A" |
| 28 | + } |
| 29 | + """; |
27 | 30 |
|
28 | | - final String unknownCandidateJson = |
| 31 | + candidateBJson = |
29 | 32 | """ |
30 | 33 | { |
31 | 34 | "content": [ |
32 | | - "Sentence one", |
33 | | - "Sentence two" |
| 35 | + "I am candidate B" |
34 | 36 | ] |
35 | 37 | } |
36 | 38 | """; |
| 39 | + } |
| 40 | + |
| 41 | + @SneakyThrows |
| 42 | + @Test |
| 43 | + void testValueTypesResolutionSuccess() { |
37 | 44 |
|
| 45 | + final var fullCandidateList = List.of(CandidateA.class, CandidateB.class); |
38 | 46 | final var module = |
39 | 47 | new SimpleModule() |
40 | 48 | .addDeserializer( |
41 | 49 | AbstractParent.class, |
42 | 50 | PolymorphicFallbackDeserializer.fromCandidates( |
43 | | - AbstractParent.class, List.of(KnownCandidate.class))); |
| 51 | + AbstractParent.class, fullCandidateList)); |
44 | 52 |
|
45 | 53 | final var mapper = new JsonMapper().registerModule(module); |
46 | 54 |
|
47 | | - assertThatThrownBy(() -> mapper.readValue(unknownCandidateJson, AbstractParent.class)) |
| 55 | + final var candidateA = mapper.readValue(candidateAJson, AbstractParent.class); |
| 56 | + assertThat(candidateA).isInstanceOf(CandidateA.class); |
| 57 | + |
| 58 | + final var candidateB = mapper.readValue(candidateBJson, AbstractParent.class); |
| 59 | + assertThat(candidateB).isInstanceOf(CandidateB.class); |
| 60 | + } |
| 61 | + |
| 62 | + @SneakyThrows |
| 63 | + @Test |
| 64 | + void testValueTypeResolutionFailure() { |
| 65 | + |
| 66 | + final List<Class<? extends AbstractParent>> incompleteCandidateList = List.of(CandidateA.class); |
| 67 | + |
| 68 | + final var moduleWithoutCandidateB = |
| 69 | + new SimpleModule() |
| 70 | + .addDeserializer( |
| 71 | + AbstractParent.class, |
| 72 | + PolymorphicFallbackDeserializer.fromCandidates( |
| 73 | + AbstractParent.class, incompleteCandidateList)); |
| 74 | + |
| 75 | + final var mapper = new JsonMapper().registerModule(moduleWithoutCandidateB); |
| 76 | + |
| 77 | + assertThatThrownBy(() -> mapper.readValue(candidateBJson, AbstractParent.class)) |
48 | 78 | .isInstanceOf(JsonMappingException.class) |
49 | 79 | .hasMessageContaining( |
50 | 80 | "PolymorphicFallbackDeserializer failed to deserialize " |
51 | 81 | + AbstractParent.class.getName() |
52 | 82 | + ". Attempted candidates: " |
53 | | - + List.of(KnownCandidate.class.getName())); |
| 83 | + + List.of(CandidateA.class.getName())); |
| 84 | + } |
| 85 | + |
| 86 | + @SneakyThrows |
| 87 | + @Test |
| 88 | + void testSubtypeAnnotationSuccess() { |
| 89 | + |
| 90 | + final var module = |
| 91 | + new SimpleModule() |
| 92 | + .addDeserializer( |
| 93 | + AbstractParent.class, |
| 94 | + PolymorphicFallbackDeserializer.fromJsonSubTypes(AbstractParent.class)); |
| 95 | + |
| 96 | + ObjectMapper mapper = new ObjectMapper().registerModule(module); |
| 97 | + |
| 98 | + final var candidate = mapper.readValue(candidateAJson, AbstractParent.class); |
| 99 | + assertThat(candidate).isInstanceOf(CandidateA.class); |
| 100 | + } |
| 101 | + |
| 102 | + @JsonSubTypes({ |
| 103 | + @JsonSubTypes.Type(value = CandidateA.class), |
| 104 | + @JsonSubTypes.Type(value = CandidateB.class) |
| 105 | + }) |
| 106 | + interface AbstractParent {} |
| 107 | + |
| 108 | + static class CandidateA implements AbstractParent { |
| 109 | + @JsonProperty("content") |
| 110 | + String content; |
| 111 | + } |
| 112 | + |
| 113 | + static class CandidateB implements AbstractParent { |
| 114 | + @JsonProperty("content") |
| 115 | + List<String> content; |
54 | 116 | } |
55 | 117 |
|
56 | 118 | @Test |
|
0 commit comments