Skip to content

Commit

Permalink
Update MissionModelTests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdailis committed Oct 18, 2023
1 parent 0f86a5b commit 7167780
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import javax.json.Json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -72,8 +73,8 @@ private ArrayList<ResourceType> expectedResourceTypesBanananation(){
resourceTypes.add(new ResourceType("/flag/conflicted", VALUE_SCHEMA_BOOLEAN));
resourceTypes.add(new ResourceType(
"/fruit",
new ValueSchemaStruct(Map.of("rate", VALUE_SCHEMA_REAL, "initial", VALUE_SCHEMA_REAL))));
resourceTypes.add(new ResourceType("/peel", VALUE_SCHEMA_REAL));
new ValueSchemaMeta(Map.of("unit", Json.createValue("bananas")), new ValueSchemaStruct(Map.of("rate", VALUE_SCHEMA_REAL, "initial", VALUE_SCHEMA_REAL)))));
resourceTypes.add(new ResourceType("/peel", new ValueSchemaMeta(Map.of("unit", Json.createValue("kg")), VALUE_SCHEMA_REAL)));
resourceTypes.add(new ResourceType("/plant", VALUE_SCHEMA_INT));
resourceTypes.add(new ResourceType("/producer", VALUE_SCHEMA_STRING));
return resourceTypes;
Expand All @@ -91,7 +92,7 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
"glutenFree", new Parameter(2, VALUE_SCHEMA_BOOLEAN),
"temperature", new Parameter(0, VALUE_SCHEMA_REAL))));
activityTypes.add(new ActivityType("BananaNap", Map.of()));
activityTypes.add(new ActivityType("BiteBanana", Map.of("biteSize", new Parameter(0, VALUE_SCHEMA_REAL))));
activityTypes.add(new ActivityType("BiteBanana", Map.of("biteSize", new Parameter(0, new ValueSchemaMeta(Map.of("unit", Json.createValue("m"), "label", Json.createObjectBuilder().add("value", Json.createValue("Specifies the size of bite to take")).build()), VALUE_SCHEMA_REAL)))));
activityTypes.add(new ActivityType("ChangeProducer", Map.of("producer", new Parameter(0, VALUE_SCHEMA_STRING))));
activityTypes.add(new ActivityType("child", Map.of("counter", new Parameter(0, VALUE_SCHEMA_INT))));
activityTypes.add(new ActivityType("ControllableDurationActivity", Map.of("duration", new Parameter(0, VALUE_SCHEMA_DURATION))));
Expand Down Expand Up @@ -247,8 +248,8 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
entry("byteArray", new Parameter(19,new ValueSchemaSeries(VALUE_SCHEMA_INT))),
entry("charArray", new Parameter(23, new ValueSchemaSeries(VALUE_SCHEMA_STRING))),
entry("doubleMap", new Parameter(43,new ValueSchemaSeries(new ValueSchemaStruct(Map.of(
"key", VALUE_SCHEMA_REAL,
"value", VALUE_SCHEMA_REAL))))),
"key", new ValueSchemaMeta(Map.of("unit", Json.createValue("m")), VALUE_SCHEMA_REAL),
"value", new ValueSchemaMeta(Map.of("unit", Json.createValue("m")), VALUE_SCHEMA_REAL)))))),
entry("floatList", new Parameter(35, new ValueSchemaSeries(VALUE_SCHEMA_REAL))),
entry("longArray", new Parameter(22, new ValueSchemaSeries(VALUE_SCHEMA_INT))),
entry("obnoxious", new Parameter(57, new ValueSchemaSeries(new ValueSchemaSeries(new ValueSchemaStruct(Map.of(
Expand All @@ -265,7 +266,7 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
"value", VALUE_SCHEMA_BOOLEAN))))),
entry("boxedFloat", new Parameter(9, VALUE_SCHEMA_REAL)),
entry("boxedShort", new Parameter(11, VALUE_SCHEMA_INT)),
entry("doubleList", new Parameter(34, new ValueSchemaSeries(VALUE_SCHEMA_REAL))),
entry("doubleList", new Parameter(34, new ValueSchemaSeries(new ValueSchemaMeta(Map.of("unit", Json.createValue("m")), VALUE_SCHEMA_REAL)))),
entry("floatArray", new Parameter(18, new ValueSchemaSeries(VALUE_SCHEMA_REAL))),
entry("shortArray", new Parameter(20, new ValueSchemaSeries(VALUE_SCHEMA_INT))),
entry("stringList", new Parameter(42, new ValueSchemaSeries(VALUE_SCHEMA_STRING))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,28 +18,34 @@ public sealed interface ValueSchema {
ValueSchemaString VALUE_SCHEMA_STRING = new ValueSchemaString();

static ValueSchema fromJSON(JsonObject json) {
switch (json.getString("type")) {
case "boolean" -> {return VALUE_SCHEMA_BOOLEAN;}
case "duration" -> {return VALUE_SCHEMA_DURATION;}
case "int" -> {return VALUE_SCHEMA_INT;}
case "path" -> {return VALUE_SCHEMA_PATH;}
case "real" -> {return VALUE_SCHEMA_REAL;}
case "series" -> {return new ValueSchemaSeries(ValueSchema.fromJSON(json.getJsonObject("items")));}
case "string" -> {return VALUE_SCHEMA_STRING;}
final var result = switch (json.getString("type")) {
case "boolean" -> VALUE_SCHEMA_BOOLEAN;
case "duration" -> VALUE_SCHEMA_DURATION;
case "int" -> VALUE_SCHEMA_INT;
case "path" -> VALUE_SCHEMA_PATH;
case "real" -> VALUE_SCHEMA_REAL;
case "series" -> new ValueSchemaSeries(ValueSchema.fromJSON(json.getJsonObject("items")));
case "string" -> VALUE_SCHEMA_STRING;
case "struct" -> {
final var items = new HashMap<String, ValueSchema>();
final var itemsJson = json.getJsonObject("items");
for(final var item : itemsJson.keySet()){
items.put(item, ValueSchema.fromJSON(itemsJson.getJsonObject(item)));
}
return new ValueSchemaStruct(items);
yield new ValueSchemaStruct(items);
}
case "variant" -> {
final var variants = json.getJsonArray("variants")
.getValuesAs((JsonObject v) -> new Variant(v.getString("key"), v.getString("label")));
return new ValueSchemaVariant(variants);
yield new ValueSchemaVariant(variants);
}
default -> throw new IllegalArgumentException("Cannot determine ValueSchema from JSON");
};
if (json.containsKey("metadata")) {
final var metadata = new HashMap<>(json.getJsonObject("metadata"));
return new ValueSchemaMeta(metadata, result);
} else {
return result;
}
}

Expand Down Expand Up @@ -132,6 +140,16 @@ public JsonObject toJson() {
.build();
}
}

record ValueSchemaMeta(Map<String, JsonValue> metadata, ValueSchema target) implements ValueSchema {
@Override
public JsonObject toJson() {
final var builder = Json.createObjectBuilder(target.toJson());
metadata.forEach(builder::add);
return builder.build();
}
}

record Variant(String key, String label) {
public JsonObject toJson() {
return Json.createObjectBuilder().add("key", key).add("label", label).build();
Expand Down

0 comments on commit 7167780

Please sign in to comment.