Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only return dataset with valid offers #4490

Merged
merged 2 commits into from
Sep 25, 2024
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
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
Loading