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
2 changes: 1 addition & 1 deletion .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
java-version: ${{ matrix.java }}
distribution: 'temurin'
- name: Build with Maven
run: mvn -B install -Dgpg.skip=true --file pom.xml
run: mvn clean -B install -Dgpg.skip=true --file pom.xml
- name: Upload surefire artifact
uses: actions/upload-artifact@v4
if: failure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import tech.illuin.wombat.core.activity.commons.validation.ValidTimeRange;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -12,6 +13,7 @@
import java.util.Collections;
import java.util.List;

@ValidTimeRange
public record TimeRange(
@JsonProperty("start") Instant start,
@JsonProperty("end") Instant end,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.illuin.wombat.core.activity.commons.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import tech.illuin.wombat.core.activity.commons.TimeRange;

public class TimeRangeValidator implements ConstraintValidator<ValidTimeRange, TimeRange>
{
@Override
public boolean isValid(TimeRange value, ConstraintValidatorContext context)
{
if (value == null)
return true;
return value.start() != null && value.end() != null && value.start().isBefore(value.end());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.illuin.wombat.core.activity.commons.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = TimeRangeValidator.class)
@Documented
public @interface ValidTimeRange
{
String message() default "TimeRange must have non-null start and end, and start must be before end";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tech.illuin.wombat.core.asset.profile.AssetProfile;
import tech.illuin.wombat.core.asset.type.AssetType;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
public interface Asset
{
@NotNull @Valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,44 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import tech.illuin.wombat.core.asset.type.AssetType;

import java.util.Objects;

/**
* Identifies an asset, and names its {@link AssetType}. Embedded in an {@link Asset}, its {@code type} is the asset's
* own {@code type} property, which also selects the asset's class.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record AssetIdentity(
@NotBlank @JsonProperty("id") String id,
@NotBlank @JsonProperty("environmentId") @JsonAlias("environment-id") String environmentId,
@NotBlank @JsonProperty("name") String name
@NotBlank @JsonProperty("name") String name,
@NotBlank @JsonProperty("type") String assetType
) {
public AssetIdentity
{
Objects.requireNonNull(id, "id must not be null");
Objects.requireNonNull(environmentId, "environmentId must not be null");
Objects.requireNonNull(name, "name must not be null");
Objects.requireNonNull(assetType, "assetType must not be null");
if (id.isBlank())
throw new IllegalArgumentException("id must not be blank");
if (environmentId.isBlank())
throw new IllegalArgumentException("environmentId must not be blank");
if (name.isBlank())
throw new IllegalArgumentException("name must not be blank");
if (assetType.isBlank())
throw new IllegalArgumentException("assetType must not be blank");
}

public static AssetIdentity of(String id, String environmentId, String name)
public static AssetIdentity of(String id, String environmentId, String name, String assetType)
{
return new AssetIdentity(id, environmentId, name);
return new AssetIdentity(id, environmentId, name, assetType);
}

public static AssetIdentity of(String id, String environmentId, String name, AssetType assetType)
{
return new AssetIdentity(id, environmentId, name, assetType.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface SimulatedAsset extends Asset
@Override
default AssetIdentity identity()
{
return AssetIdentity.of("simulated-asset", "simulated-environment", "simulated-asset");
return AssetIdentity.of("simulated-asset", "simulated-environment", "simulated-asset", this.type());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package tech.illuin.wombat.core.source.data;

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

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "family")
@JsonSubTypes({
@JsonSubTypes.Type(value = KubernetesData.class, name = "KUBERNETES_CONTAINER"),
@JsonSubTypes.Type(value = LLMData.class, name = "LLM"),
})
public interface MetricData
{
String serviceId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private record SourceableAsset(String assetId) implements Asset, Monitorable
@Override
public AssetIdentity identity()
{
return AssetIdentity.of(this.assetId, "env", this.assetId);
return AssetIdentity.of(this.assetId, "env", this.assetId, this.type());
}

@Override
Expand Down Expand Up @@ -218,7 +218,7 @@ private record ModeledAsset(String assetId) implements Asset
@Override
public AssetIdentity identity()
{
return AssetIdentity.of(this.assetId, "env", this.assetId);
return AssetIdentity.of(this.assetId, "env", this.assetId, this.type());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tech.illuin.wombat.core.activity.commons.validation;

import org.junit.jupiter.api.Test;
import tech.illuin.wombat.core.activity.commons.TimeRange;

import java.time.Instant;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class TimeRangeValidatorTest
{
private final TimeRangeValidator validator = new TimeRangeValidator();

@Test
void nullRangeIsValid()
{
assertTrue(this.validator.isValid(null, null));
}

@Test
void missingEndpointsAreInvalid()
{
Instant instant = Instant.parse("2026-01-01T00:00:00Z");
TimeRange missingStart = mock(TimeRange.class);
when(missingStart.end()).thenReturn(instant);
TimeRange missingEnd = mock(TimeRange.class);
when(missingEnd.start()).thenReturn(instant);

assertFalse(this.validator.isValid(missingStart, null));
assertFalse(this.validator.isValid(missingEnd, null));
}

@Test
void equalOrReversedEndpointsAreInvalid()
{
Instant instant = Instant.parse("2026-01-01T00:00:00Z");

assertFalse(this.validator.isValid(new TimeRange(instant, instant), null));
assertFalse(this.validator.isValid(new TimeRange(instant.plusSeconds(1), instant), null));
}

@Test
void increasingEndpointsAreValid()
{
Instant start = Instant.parse("2026-01-01T00:00:00Z");

assertTrue(this.validator.isValid(new TimeRange(start, start.plusSeconds(1)), null));
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,68 @@
package tech.illuin.wombat.core.asset;

import org.junit.jupiter.api.Test;
import tech.illuin.wombat.core.asset.type.ActivityRegime;
import tech.illuin.wombat.core.asset.type.AssetType;
import tech.illuin.wombat.core.asset.type.ServiceFamily;

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

class AssetIdentityTest
{
private static final AssetType TYPE = AssetType.of("tech.illuin", "wombat-module", "llm-static", ActivityRegime.MODELED, ServiceFamily.LLM);
private static final String TYPE_NAME = TYPE.name();

@Test
void of_constructsValidAssetIdentity()
{
AssetIdentity identity = AssetIdentity.of("asset-1", "env-prod", "my-asset");
AssetIdentity identity = AssetIdentity.of("asset-1", "env-prod", "my-asset", TYPE_NAME);
assertEquals("asset-1", identity.id());
assertEquals("env-prod", identity.environmentId());
assertEquals("my-asset", identity.name());
assertEquals(TYPE_NAME, identity.assetType());
}

@Test
void of_namesTheGivenAssetType()
{
assertEquals(AssetIdentity.of("asset-1", "env-prod", "my-asset", TYPE_NAME), AssetIdentity.of("asset-1", "env-prod", "my-asset", TYPE));
}

@Test
void equalityAndHashCode_matchByValues()
{
AssetIdentity id1 = AssetIdentity.of("asset-1", "env-prod", "my-asset");
AssetIdentity id2 = new AssetIdentity("asset-1", "env-prod", "my-asset");
AssetIdentity id3 = AssetIdentity.of("asset-2", "env-prod", "my-asset");
AssetIdentity id4 = AssetIdentity.of("asset-1", "env-dev", "my-asset");
AssetIdentity id5 = AssetIdentity.of("asset-1", "env-prod", "other-asset");
AssetIdentity id1 = AssetIdentity.of("asset-1", "env-prod", "my-asset", TYPE_NAME);
AssetIdentity id2 = new AssetIdentity("asset-1", "env-prod", "my-asset", TYPE_NAME);
AssetIdentity id3 = AssetIdentity.of("asset-2", "env-prod", "my-asset", TYPE_NAME);
AssetIdentity id4 = AssetIdentity.of("asset-1", "env-dev", "my-asset", TYPE_NAME);
AssetIdentity id5 = AssetIdentity.of("asset-1", "env-prod", "other-asset", TYPE_NAME);
AssetIdentity id6 = AssetIdentity.of("asset-1", "env-prod", "my-asset", "tech.illuin.wombat-module.other");

assertEquals(id1, id2);
assertEquals(id1.hashCode(), id2.hashCode());
assertNotEquals(id1, id3);
assertNotEquals(id1, id4);
assertNotEquals(id1, id5);
assertNotEquals(id1, id6);
}

@Test
void constructor_rejectsNullOrBlankFields()
{
assertThrows(NullPointerException.class, () -> new AssetIdentity(null, "env-prod", "my-asset"));
assertThrows(NullPointerException.class, () -> new AssetIdentity("asset-1", null, "my-asset"));
assertThrows(NullPointerException.class, () -> new AssetIdentity("asset-1", "env-prod", null));

assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("", "env-prod", "my-asset"));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity(" ", "env-prod", "my-asset"));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "", "my-asset"));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", " ", "my-asset"));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", ""));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", " "));
assertThrows(NullPointerException.class, () -> new AssetIdentity(null, "env-prod", "my-asset", TYPE_NAME));
assertThrows(NullPointerException.class, () -> new AssetIdentity("asset-1", null, "my-asset", TYPE_NAME));
assertThrows(NullPointerException.class, () -> new AssetIdentity("asset-1", "env-prod", null, TYPE_NAME));
assertThrows(NullPointerException.class, () -> new AssetIdentity("asset-1", "env-prod", "my-asset", null));

assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("", "env-prod", "my-asset", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity(" ", "env-prod", "my-asset", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "", "my-asset", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", " ", "my-asset", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", "", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", " ", TYPE_NAME));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", "my-asset", ""));
assertThrows(IllegalArgumentException.class, () -> new AssetIdentity("asset-1", "env-prod", "my-asset", " "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private record TestAsset(AssetType type, AssetProfile profile) implements Asset
@Override
public AssetIdentity identity()
{
return AssetIdentity.of("asset", "env", "Asset");
return AssetIdentity.of("asset", "env", "Asset", this.type());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private record TestAsset(AssetType type, AssetProfile profile) implements Asset
@Override
public AssetIdentity identity()
{
return AssetIdentity.of("asset", "env", "Asset");
return AssetIdentity.of("asset", "env", "Asset", this.type());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private record TestAsset(String assetId, AssetType type, Period period) implemen
@Override
public AssetIdentity identity()
{
return AssetIdentity.of(this.assetId, "env", this.assetId);
return AssetIdentity.of(this.assetId, "env", this.assetId, this.type());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tech.illuin.wombat.core.source.data;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import tech.illuin.wombat.core.activity.commons.Country;
import tech.illuin.wombat.core.asset.profile.LLMProvider;

import java.util.List;

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

class MetricDataTest
{
private static final ObjectMapper MAPPER = new ObjectMapper();

private static final KubernetesData KUBERNETES = new KubernetesData("api", "cluster", "namespace", "pod", 100.0, 2048.0);
private static final LLMData LLM = new LLMData("chat", LLMProvider.mistralai, "mistral-large-latest", Country.ofCode("FRA").orElseThrow(), 120L, 1L);

@Test
void metricData_isTaggedWithItsFamily()
{
JsonNode kubernetes = MAPPER.valueToTree(KUBERNETES);

assertEquals("KUBERNETES_CONTAINER", kubernetes.get("family").asText());
assertEquals("LLM", MAPPER.<JsonNode>valueToTree(LLM).get("family").asText());
assertFalse(kubernetes.has("serviceFamily"));
}

@Test
void metricData_roundTripsToItsVariant() throws Exception
{
for (MetricData data : List.of(KUBERNETES, LLM))
assertEquals(data, MAPPER.readValue(MAPPER.writeValueAsString(data), MetricData.class));
}

@Test
void familyNames_matchTheServiceFamilyOfEachVariant()
{
for (JsonSubTypes.Type type : MetricData.class.getAnnotation(JsonSubTypes.class).value())
{
MetricData sample = List.of(KUBERNETES, LLM).stream().filter(type.value()::isInstance).findFirst().orElseThrow();
assertEquals(sample.serviceFamily().name(), type.name());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class KubernetesMapperTest
{
private static final AssetType TYPE_KUBERNETES = AssetType.of("tech.illuin", "wombat-module", "kubernetes-prometheus", ActivityRegime.MEASURED, ServiceFamily.KUBERNETES_CONTAINER);
private static final AssetIdentity IDENTITY = AssetIdentity.of("asset-1", "env-1", "Asset 1");
private static final AssetIdentity IDENTITY = AssetIdentity.of("asset-1", "env-1", "Asset 1", TYPE_KUBERNETES);

private final KubernetesMapper mapper = new KubernetesMapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class LLMMapperTest
{
private static final AssetType TYPE_LLM = AssetType.of("tech.illuin", "wombat-module", "llm-prometheus", ActivityRegime.MEASURED, ServiceFamily.LLM);
private static final AssetIdentity IDENTITY = AssetIdentity.of("asset-1", "env-1", "Asset 1");
private static final AssetIdentity IDENTITY = AssetIdentity.of("asset-1", "env-1", "Asset 1", TYPE_LLM);

private final LLMMapper mapper = new LLMMapper();

Expand Down
Loading
Loading