Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package tech.illuin.wombat.core.source.data;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import tech.illuin.wombat.core.asset.type.ServiceFamily;

import java.util.Objects;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "family",
defaultImpl = KubernetesData.class
)
public record KubernetesData(
String serviceId,
String cluster,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package tech.illuin.wombat.core.source.data;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import tech.illuin.wombat.core.activity.commons.Country;
import tech.illuin.wombat.core.asset.profile.LLMProvider;
import tech.illuin.wombat.core.asset.type.ServiceFamily;

import java.util.Objects;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "family",
defaultImpl = LLMData.class
)
public record LLMData(
String serviceId,
LLMProvider provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import tech.illuin.wombat.core.activity.commons.Country;
import tech.illuin.wombat.core.asset.profile.LLMProvider;
Expand All @@ -11,6 +14,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

class MetricDataTest
{
Expand Down Expand Up @@ -45,4 +49,54 @@ void familyNames_matchTheServiceFamilyOfEachVariant()
assertEquals(sample.serviceFamily().name(), type.name());
}
}

@Test
void metricData_deserializesDirectlyToItsConcreteType() throws Exception
{
assertEquals(KUBERNETES, MAPPER.readValue(MAPPER.writeValueAsString(KUBERNETES), KubernetesData.class));
assertEquals(LLM, MAPPER.readValue(MAPPER.writeValueAsString(LLM), LLMData.class));
}

@Test
void metricData_deserializesLegacyPayloadWithoutFamilyToConcreteType() throws Exception
{
for (MetricData data : List.of(KUBERNETES, LLM))
{
ObjectNode tree = MAPPER.valueToTree(data);
tree.remove("family");

assertEquals(data, MAPPER.readValue(tree.toString(), data.getClass()));
}
}

@Test
void metricData_rejectsPolymorphicDeserializationWithoutFamily()
{
for (MetricData data : List.of(KUBERNETES, LLM))
{
ObjectNode tree = MAPPER.valueToTree(data);
tree.remove("family");
String untaggedJson = tree.toString();

assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(untaggedJson, MetricData.class));
}
}

@Test
void metricData_rejectsUnknownOrMismatchedFamily() throws Exception
{
String unknownFamily = """
{"family":"UNKNOWN","serviceId":"api"}
""";

assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(unknownFamily, MetricData.class));
assertThrows(ValueInstantiationException.class, () -> MAPPER.readValue(unknownFamily, KubernetesData.class));
assertThrows(ValueInstantiationException.class, () -> MAPPER.readValue(unknownFamily, LLMData.class));

String llmJson = MAPPER.writeValueAsString(LLM);
String k8sJson = MAPPER.writeValueAsString(KUBERNETES);

assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(llmJson, KubernetesData.class));
assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(k8sJson, LLMData.class));
}
}
Loading