Skip to content

Commit

Permalink
fix: only return dataset with valid offers
Browse files Browse the repository at this point in the history
  • Loading branch information
ronjaquensel committed Sep 25, 2024
1 parent 154dec9 commit 6375f9a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public DatasetResolverImpl(ContractDefinitionResolver contractDefinitionResolver
@NotNull
public Stream<Dataset> query(ParticipantAgent agent, QuerySpec querySpec) {
var contractDefinitions = contractDefinitionResolver.definitionsFor(agent).toList();
if (contractDefinitions.isEmpty()) {
return Stream.empty();
}

var assetsQuery = QuerySpec.Builder.newInstance().offset(0).limit(MAX_VALUE).filter(querySpec.getFilterExpression()).build();
return assetIndex.queryAssets(assetsQuery)
.map(asset -> toDataset(contractDefinitions, asset))
Expand All @@ -73,9 +77,14 @@ public Stream<Dataset> query(ParticipantAgent agent, QuerySpec querySpec) {
@Override
public Dataset getById(ParticipantAgent agent, String id) {
var contractDefinitions = contractDefinitionResolver.definitionsFor(agent).toList();
if (contractDefinitions.isEmpty()) {
return null;
}

return Optional.of(id)
.map(assetIndex::findById)
.map(asset -> toDataset(contractDefinitions, asset))
.filter(Dataset::hasOffers)
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,39 @@ void getById_shouldReturnNull_whenAssetNotFound() {

assertThat(dataset).isNull();
}

@Test
void getById_shouldReturnNull_whenNoValidContractDefinition() {
var participantAgent = createParticipantAgent();

when(contractDefinitionResolver.definitionsFor(any())).thenReturn(Stream.empty());
when(assetIndex.findById(any())).thenReturn(createAsset("datasetId").build());

var dataset = datasetResolver.getById(participantAgent, "datasetId");

assertThat(dataset).isNull();
}

@Test
void getById_shouldReturnNull_whenNoValidContractDefinitionForAsset() {
var assetId = "assetId";
var participantAgent = createParticipantAgent();

when(contractDefinitionResolver.definitionsFor(any())).thenReturn(Stream.of(
contractDefinitionBuilder("definition")
.assetsSelectorCriterion(Criterion.Builder.newInstance()
.operandRight(EDC_NAMESPACE + "id")
.operator("=")
.operandLeft("a-different-asset")
.build())
.build()
));
when(assetIndex.findById(any())).thenReturn(createAsset(assetId).build());

var dataset = datasetResolver.getById(participantAgent, assetId);

assertThat(dataset).isNull();
}

private ContractDefinition.Builder contractDefinitionBuilder(String id) {
return ContractDefinition.Builder.newInstance()
Expand Down

0 comments on commit 6375f9a

Please sign in to comment.