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

feat: Add SQL Agreement Retirement Store #1631

Merged

Conversation

bmg13
Copy link
Contributor

@bmg13 bmg13 commented Oct 17, 2024

WHAT

Adds a SQL Agreement Retirement Store and edc_agreement_retirement table in migration script.

WHY

Provide a SQL store besides the InMemory default one added in previous PR.

FURTHER NOTES

List other areas of code that have changed but are not necessarily linked to the main feature. This could be method signature changes, package declarations, bugs that were encountered and were fixed inline, etc.

In accordance with DR: #1604
Related with: #1615
Addresses eclipse-tractusx/sig-release#777

@@ -56,6 +56,7 @@ include(":edc-extensions:agreements")
include(":edc-extensions:agreements:retirement-evaluation-core")
include(":edc-extensions:agreements:retirement-evaluation-api")
include(":edc-extensions:agreements:retirement-evaluation-spi")
include("edc-extensions:agreements:retirement-evaluation-store-sql")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
include("edc-extensions:agreements:retirement-evaluation-store-sql")
include(":edc-extensions:agreements:retirement-evaluation-store-sql")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got a flashback, updated here.

return AgreementsRetirementEntry.Builder.newInstance()
.withAgreementId(resultSet.getString(statements.getIdColumn()))
.withReason(resultSet.getString(statements.getReasonColumn()))
.withAgreementRetirementDate(Long.parseLong(resultSet.getString(statements.getRetirementDateColumn())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a result.getLong() directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, good point, changed here.

private static final String NAME = "SQL Agreement Retirement Store.";

@Setting(value = "Datasource name for the SQL AgreementsRetirement store")
private static final String DATASOURCE_SETTING_NAME = "edc.datasource.agreementretirement.name";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setting was deprecated in favor of edc.sql.store.<name>.datasource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, updated to "tx.edc.sql.store.agreementretirement.datasource" since this is TX. updated here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use an edc. prefix instead of the tx. one. I sense this could lead to confusion.

CONSTRAINT contract_negotiation_lease_lease_id_fk REFERENCES edc_lease ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS edc_agreement_retirement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this here rings a bells for me. Couldn't we have just a file with the creation of the edc_agreement_retirement table?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed not needed additions I had here, but the create not exists of the edc_contract_agreement maintains because it is referenced. Maybe it can be referenced from other file, but based on what I saw here it should be added as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already created in the respective context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in here.

Copy link
Contributor

@rafaelmag110 rafaelmag110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some details but generally looks good.

if (existsById(entry.getAgreementId(), connection)) {
return StoreResult.alreadyExists(ALREADY_EXISTS_TEMPLATE.formatted(entry.getAgreementId()));
}
insert(connection, entry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the constraint on the edc_contract_agreement table, this might throw what I believe will be an uncaught exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is valid. looking into SqlQueryExecutor.execute() we can confirm the already handled and throw of EdcPersistenceException to the client, so here catching EdcPersistenceException to throw it once more seems redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't rely on fk constraint, consistency should be maintained at the service layer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that since it depends on the existence of the fk then it could be controlled here to keep consistency.
Removed in here.

@bmg13 bmg13 marked this pull request as ready for review October 17, 2024 14:00
@bmg13 bmg13 requested a review from rafaelmag110 October 17, 2024 15:00
CONSTRAINT contract_negotiation_lease_lease_id_fk REFERENCES edc_lease ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS edc_agreement_retirement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already created in the respective context

if (existsById(entry.getAgreementId(), connection)) {
return StoreResult.alreadyExists(ALREADY_EXISTS_TEMPLATE.formatted(entry.getAgreementId()));
}
insert(connection, entry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't rely on fk constraint, consistency should be maintained at the service layer

@bmg13 bmg13 requested review from wolf4ood and ndr-brt October 22, 2024 10:00
@@ -168,7 +168,7 @@ public void storeBusinessPartner(String bpn, String... groups) {
.statusCode(204);
}

public void retireProviderAgreement(String agreementId) {
public void retireProviderAgreement(String agreementId, int expectedStatusCode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of this, because it will make the test less explicit, please return directly the status code or, better, the ValidatableResponse object, on which the test can eventually add expectations or extracts body/headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, my goal was to reuse this logic and the test would have the responsability in its scope on what to expect/assert, but I see your point, reverted here.

@@ -131,6 +131,12 @@ void retireAgreement_shouldCloseTransferProcesses() {

}

@Test
@DisplayName("Verify non existing Contract Agreement is not stored in retired table.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the term "table" is really related only to the PG run of the tests, I would avoid to be that specific and only use hi level domain language, like:
void shouldFail_whenAgreementDoesNotExist()

note:

  • no need to specify retireAgreement, because this is the RetireAgreementTest class. Context is implicit
  • no need to use the @DisplayName, test name does already describe the purpose nicely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, that makes sense. changed it here, removing the @DisplayName.

@bmg13 bmg13 requested a review from ndr-brt October 22, 2024 16:08
@@ -75,4 +83,8 @@ private QuerySpec createFilterQueryByAgreementId(String agreementId) {
.build()
).build();
}

private boolean contractAgreementExists(String agreementId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single liner method used once: can be inlined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in here.

public class SqlAgreementsRetirementStoreExtension implements ServiceExtension {

protected static final String NAME = "SQL Agreement Retirement Store.";
private static final String DEFAULT_DATASOURCE_NAME = "agreementretirement";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default value is, according to line 63, DataSourceRegistry.DEFAULT_DATASOUCE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 63 was kept here since is the same approach seen in the other datasources.

@@ -183,6 +183,21 @@ public void retireProviderAgreement(String agreementId) {
.statusCode(204);
}

public void retireProviderAgreementBadRequest(String agreementId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please just have a single retireProviderAgreement method that returns either the state or the ValidatableResponse and let test explicitly add their expectations, as I pointed in the previous review

note: the name says "bad request" but the status code verified is 404

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

misunderstood, sorry, changed in here.
regarding the "note" you're right, should've been a notfound.

@@ -131,6 +131,12 @@ void retireAgreement_shouldCloseTransferProcesses() {

}

@Test
@DisplayName("Verify non existing Contract Agreement is not stored in retired table.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this @DisplayName as it's not providing any additional interesting information to the test name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was already removed in previous commit as indicated here.

@@ -131,6 +131,12 @@ void retireAgreement_shouldCloseTransferProcesses() {

}

@Test
@DisplayName("Verify non existing Contract Agreement is not stored in retired table.")
void retireAgreement_shouldFailIfTryingToRetireNonExistingId() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the correct format is eventually:
retireAgreement_shouldFail_whenAgreementDoesNotExist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, you're right, is more explicit. changed in here.

Copy link

@bmg13 bmg13 requested a review from ndr-brt October 23, 2024 08:59
@wolf4ood wolf4ood merged commit 2662c42 into eclipse-tractusx:main Oct 23, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

4 participants