Skip to content

Commit

Permalink
fix: only return dataset with valid offers (#4490)
Browse files Browse the repository at this point in the history
* fix: only return dataset with valid offers

* test: add additional test
  • Loading branch information
ronjaquensel authored and ndr-brt committed Sep 25, 2024
1 parent a6138aa commit 15c8c8a
Show file tree
Hide file tree
Showing 2 changed files with 53 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 @@ -56,6 +56,7 @@
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -98,6 +99,16 @@ void search_shouldReturnOneDatasetPerAsset() {
assertThat(dataset.getProperties()).contains(entry("key", "value"));
});
}

@Test
void query_shouldNotQueryAssets_whenNoValidContractDefinition() {
when(contractDefinitionResolver.definitionsFor(any())).thenReturn(Stream.empty());

var datasets = datasetResolver.query(createParticipantAgent(), QuerySpec.none());

assertThat(datasets).isNotNull().isEmpty();
verify(assetIndex, never()).queryAssets(any());
}

@Test
void query_shouldReturnNoDataset_whenPolicyNotFound() {
Expand Down Expand Up @@ -295,6 +306,39 @@ void getById_shouldReturnNull_whenAssetNotFound() {

assertThat(dataset).isNull();
}

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

when(contractDefinitionResolver.definitionsFor(any())).thenReturn(Stream.empty());

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

assertThat(dataset).isNull();
verify(assetIndex, never()).findById(any());
}

@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 15c8c8a

Please sign in to comment.