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) #4491

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
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ maven/mavencentral/net.javacrumbs.json-unit/json-unit-core/2.36.0, Apache-2.0, a
maven/mavencentral/net.minidev/accessors-smart/2.4.7, Apache-2.0, approved, #7515
maven/mavencentral/net.minidev/json-smart/2.4.7, Apache-2.0, approved, #3288
maven/mavencentral/net.sf.jopt-simple/jopt-simple/5.0.4, MIT, approved, CQ13174
maven/mavencentral/net.sf.saxon/Saxon-HE/12.5, MPL-2.0-no-copyleft-exception AND (LicenseRef-scancode-proprietary-license AND MPL-2.0-no-copyleft-exception) AND (MPL-2.0-no-copyleft-exception AND X11) AND (MIT AND MPL-2.0-no-copyleft-exception) AND (MPL-1.0 AND MPL-2.0-no-copyleft-exception) AND (Apache-2.0 AND MPL-2.0-no-copyleft-exception) AND MPL-1.0, restricted, #16061
maven/mavencentral/net.sf.saxon/Saxon-HE/12.5, W3C-19980720 AND MPL-2.0 AND MPL-1.0, approved, #16061
maven/mavencentral/org.antlr/antlr4-runtime/4.13.2, BSD-3-Clause, approved, #10767
maven/mavencentral/org.apache.commons/commons-compress/1.24.0, Apache-2.0 AND BSD-3-Clause AND bzip2-1.0.6 AND LicenseRef-Public-Domain, approved, #10368
maven/mavencentral/org.apache.commons/commons-digester3/3.2, Apache-2.0, approved, clearlydefined
Expand Down
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