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: add missing transaction block on CatalogService #3680

Merged
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 @@ -159,7 +159,8 @@ public CatalogService catalogService() {

@Provider
public CatalogProtocolService catalogProtocolService(ServiceExtensionContext context) {
return new CatalogProtocolServiceImpl(datasetResolver, participantAgentService, dataServiceRegistry, identityService, monitor, context.getParticipantId());
return new CatalogProtocolServiceImpl(datasetResolver, participantAgentService, dataServiceRegistry,
identityService, monitor, context.getParticipantId(), transactionContext);
}

@Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.eclipse.edc.spi.iam.TokenRepresentation;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.result.ServiceResult;
import org.eclipse.edc.transaction.spi.TransactionContext;
import org.jetbrains.annotations.NotNull;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;

public class CatalogProtocolServiceImpl extends BaseProtocolService implements CatalogProtocolService {
Expand All @@ -40,50 +40,53 @@ public class CatalogProtocolServiceImpl extends BaseProtocolService implements C
private final ParticipantAgentService participantAgentService;
private final DataServiceRegistry dataServiceRegistry;
private final String participantId;
private final TransactionContext transactionContext;

public CatalogProtocolServiceImpl(DatasetResolver datasetResolver,
ParticipantAgentService participantAgentService,
DataServiceRegistry dataServiceRegistry,
IdentityService identityService,
Monitor monitor,
String participantId) {
String participantId,
TransactionContext transactionContext) {
super(identityService, monitor);
this.datasetResolver = datasetResolver;
this.participantAgentService = participantAgentService;
this.dataServiceRegistry = dataServiceRegistry;
this.participantId = participantId;
this.transactionContext = transactionContext;
}

@Override
@NotNull
public ServiceResult<Catalog> getCatalog(CatalogRequestMessage message, TokenRepresentation tokenRepresentation) {
return verifyToken(tokenRepresentation).map(claimToken -> {
var agent = participantAgentService.createFor(claimToken);
return transactionContext.execute(() -> verifyToken(tokenRepresentation)
.map(participantAgentService::createFor)
.map(agent -> {
try (var datasets = datasetResolver.query(agent, message.getQuerySpec())) {
var dataServices = dataServiceRegistry.getDataServices();

try (var datasets = datasetResolver.query(agent, message.getQuerySpec())) {
var dataServices = dataServiceRegistry.getDataServices();

return Catalog.Builder.newInstance()
.dataServices(dataServices)
.datasets(datasets.collect(toList()))
.property(EDC_NAMESPACE + PARTICIPANT_ID_PROPERTY_KEY, participantId)
.build();
}
});
return Catalog.Builder.newInstance()
.dataServices(dataServices)
.datasets(datasets.toList())
.property(EDC_NAMESPACE + PARTICIPANT_ID_PROPERTY_KEY, participantId)
.build();
}
})
);
}

@Override
public @NotNull ServiceResult<Dataset> getDataset(String datasetId, TokenRepresentation tokenRepresentation) {
return verifyToken(tokenRepresentation).compose(claimToken -> {
var agent = participantAgentService.createFor(claimToken);

var dataset = datasetResolver.getById(agent, datasetId);

if (dataset == null) {
return ServiceResult.notFound(format("Dataset %s does not exist", datasetId));
}
return transactionContext.execute(() -> verifyToken(tokenRepresentation)
.map(participantAgentService::createFor)
.map(agent -> datasetResolver.getById(agent, datasetId))
.compose(dataset -> {
if (dataset == null) {
return ServiceResult.notFound(format("Dataset %s does not exist", datasetId));
}

return ServiceResult.success(dataset);
});
return ServiceResult.success(dataset);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.result.ServiceFailure;
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
import org.eclipse.edc.transaction.spi.TransactionContext;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -43,16 +45,21 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class CatalogProtocolServiceImplTest {

private final DatasetResolver datasetResolver = mock(DatasetResolver.class);
private final ParticipantAgentService participantAgentService = mock(ParticipantAgentService.class);
private final DataServiceRegistry dataServiceRegistry = mock(DataServiceRegistry.class);
private final DatasetResolver datasetResolver = mock();
private final ParticipantAgentService participantAgentService = mock();
private final DataServiceRegistry dataServiceRegistry = mock();
private final IdentityService identityService = mock();
private final CatalogProtocolServiceImpl service = new CatalogProtocolServiceImpl(datasetResolver, participantAgentService, dataServiceRegistry, identityService, mock(), "participantId");
private final TransactionContext transactionContext = spy(new NoopTransactionContext());

private final CatalogProtocolServiceImpl service = new CatalogProtocolServiceImpl(datasetResolver,
participantAgentService, dataServiceRegistry, identityService, mock(), "participantId",
transactionContext);

@Test
void getCatalog_shouldReturnCatalogWithConnectorDataServiceAndItsDataset() {
Expand All @@ -76,6 +83,7 @@ void getCatalog_shouldReturnCatalogWithConnectorDataServiceAndItsDataset() {
});
verify(datasetResolver).query(eq(participantAgent), eq(querySpec));
verify(participantAgentService).createFor(token);
verify(transactionContext).execute(any(TransactionContext.ResultTransactionBlock.class));
}

@Test
Expand Down Expand Up @@ -108,6 +116,7 @@ void getDataset_shouldReturnDataset() {
assertThat(result).isSucceeded().isEqualTo(dataset);
verify(participantAgentService).createFor(claimToken);
verify(datasetResolver).getById(participantAgent, "datasetId");
verify(transactionContext).execute(any(TransactionContext.ResultTransactionBlock.class));
}

@Test
Expand Down
Loading