Skip to content

Commit

Permalink
qmap contract fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Jun 19, 2024
1 parent 7806483 commit c1f9d0a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion antora/modules/ROOT/pages/designdocs/InterviewModel.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ it should be possible to create a new tree that has some selected nodes modified

* *Navigable.* For every node in the tree we want to be able to navigate to its parent node or child nodes.

* *Immutable.* Modification of the tree (once created), should not be possible unless by means of a transformer.
* *Immutable.* Modification of the tree (once created), should not be possible.

=== GloboDiet Interview Export Semantics

Expand Down
16 changes: 16 additions & 0 deletions commons/src/main/java/dita/commons/qmap/QualifiedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,20 @@ public static Try<QualifiedMap> tryFromYamlAllowEmptyTargets(@Nullable final Dat
.mapSuccessAsNullable(dto->Dtos.fromDto(dto, Policy.EMPTY_TARGET_ALLOWED));
}

// -- CONTRACT

@Override
public boolean equals(final Object obj) {
return switch(obj) {
case QualifiedMap other -> other.internalMap.equals(this.internalMap);
default -> false;
};
}

@Override
public int hashCode() {
return internalMap.hashCode();
}


}
33 changes: 28 additions & 5 deletions commons/src/test/java/dita/commons/food/FoodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.io.DataSource;

import dita.commons.food.composition.FoodComponent;
import dita.commons.food.composition.FoodComponent.ComponentUnit;
Expand All @@ -45,20 +46,19 @@ class FoodTest {
private SemanticIdentifier blsBananaId = new SemanticIdentifier("bls", "F503100"); // Banana raw

@Test
void test() {
void qMapCreation() {

// setup food consumption
var bananaConsumption = createFoodConsumption();
var foodCompositionRepo = FoodCompositionSampler.createFoodCompositionRepository();
System.err.printf("%s%n", foodCompositionRepo.toYaml());
//debug
//System.err.printf("%s%n", foodCompositionRepo.toYaml());

final FoodComponent blsZuckerGesamt = foodCompositionRepo.componentCatalog()
.lookupEntryElseFail(new SemanticIdentifier("bls", "KMD"));


// setup nutrient mapping
final QualifiedMap qMap = new QualifiedMap(new HashMap<>());
qMap.put(new QualifiedMapEntry(bananaConsumption.foodId(), bananaConsumption.facetIds(), blsBananaId));
final QualifiedMap qMap = createQMap();

// verify lookups
assertEquals(
Expand All @@ -85,8 +85,31 @@ void test() {
fcac.quantifiedComponent(blsZuckerGesamt));
}

@Test
void qMapRoundtrip() {
// setup nutrient mapping
final QualifiedMap qMap = createQMap();
var yaml = qMap.toYaml();
var qMapAfterRoundtrip = QualifiedMap.tryFromYaml(DataSource.ofStringUtf8(yaml))
.valueAsNonNullElseFail();

//debug
System.err.printf("%s%n", yaml);

assertEquals(qMap, qMapAfterRoundtrip);
}

// -- HELPER

QualifiedMap createQMap() {
// setup food consumption
var bananaConsumption = createFoodConsumption();
// setup nutrient mapping
final QualifiedMap qMap = new QualifiedMap(new HashMap<>());
qMap.put(new QualifiedMapEntry(bananaConsumption.foodId(), bananaConsumption.facetIds(), blsBananaId));
return qMap;
}

FoodConsumption createFoodConsumption() {
var gdBanana = new SemanticIdentifier("gd", "00136"); // Banana
var gdFacetRaw = new SemanticIdentifier("gd", "0399");
Expand Down

0 comments on commit c1f9d0a

Please sign in to comment.