Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Fix auditing when running through DatastoreTemplate.performTransaction #2604

Merged

Conversation

fpavageau
Copy link
Contributor

The new DatastoreTemplate instance should have the same
ApplicationEventPublisher as the original one, so that AuditingHandler
can be called.

Fixes #2603

Copy link
Contributor

@meltsufin meltsufin left a comment

Choose a reason for hiding this comment

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

Generally, looks good!
Feel free to add yourself as @author on the files you touched.
Thanks!!

@@ -82,6 +84,20 @@ public void testModifiedPrevProperties() {
this.datastoreTemplate.saveAll(Collections.singletonList(testEntity));
}

@Test
public void testInTransaction() {
when(datastore.runInTransaction(any()))
Copy link
Contributor

Choose a reason for hiding this comment

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

I see mock setup all done in datastoreTemplate(). Would it make sense to move it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally, I prefer keeping the specific setup where it's actually used, and Mockito could complain about unnecessary setup in the other tests, depending on its configuration. However, I've tested moving the code and it works, so Mockito is not in strict mode and the code can be arranged however you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't feel strongly either way. I'm also not sure it's good that the verification is outside the test method, but that's a problem for another time. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I think I'll leave it that way then, not specifying every possible behavior for all the tests.

I agree the verification in an Answer is brittle, which is when I first ran the test, without the added behavior on the mock, it passed: the Datastore.put() method was never called! We could extract an assertion method which would use an ArgumentCaptor to verify the call and its parameter, instead. However it's not the only occurence in the project of assertions in Answers, so just fixing this one instance probably doesn't really make sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, no need to address that here.

@codecov
Copy link

codecov bot commented Nov 30, 2020

Codecov Report

Merging #2604 (fb40156) into master (7231b7d) will decrease coverage by 7.22%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2604      +/-   ##
============================================
- Coverage     81.29%   74.06%   -7.23%     
+ Complexity     2396     2168     -228     
============================================
  Files           267      267              
  Lines          7798     7800       +2     
  Branches        808      808              
============================================
- Hits           6339     5777     -562     
- Misses         1113     1644     +531     
- Partials        346      379      +33     
Flag Coverage Δ Complexity Δ
integration ? ?
unittests 74.06% <100.00%> (-0.02%) 0.00 <1.00> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
...oud/gcp/data/datastore/core/DatastoreTemplate.java 86.34% <100.00%> (-9.24%) 128.00 <1.00> (-18.00)
...ringframework/cloud/gcp/data/firestore/AutoId.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...gcp/secretmanager/SecretManagerPropertySource.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-4.00%)
...a/spanner/repository/query/SpannerQueryMethod.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-6.00%)
...retmanager/SecretManagerPropertySourceLocator.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-2.00%)
...figure/config/GcpConfigBootstrapConfiguration.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-2.00%)
...epository/config/SpannerRepositoriesRegistrar.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...ository/config/DatastoreRepositoriesRegistrar.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...e/spanner/GcpSpannerEmulatorAutoConfiguration.java 0.00% <0.00%> (-90.91%) 0.00% <0.00%> (-2.00%)
...restore/GcpFirestoreEmulatorAutoConfiguration.java 0.00% <0.00%> (-84.85%) 0.00% <0.00%> (-4.00%)
... and 56 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7231b7d...278d09c. Read the comment docs.

The new `DatastoreTemplate` instance should have the same
`ApplicationEventPublisher` as the original one, so that `AuditingHandler`
can be called.
@fpavageau fpavageau force-pushed the bugfix-auditing-transaction branch from fb40156 to 278d09c Compare November 30, 2020 21:20
@meltsufin
Copy link
Contributor

@fpavageau BTW, no need to squash or force-push in branches/PRs because we always squash commits on merge to master. Do you want to add your name to DatastoreTemplate.java? If not, I'm happy to merge.

@fpavageau
Copy link
Contributor Author

I added my name to the test class, I guess that's enough.

@meltsufin meltsufin merged commit 1909fe1 into spring-attic:master Nov 30, 2020
@fpavageau fpavageau deleted the bugfix-auditing-transaction branch December 1, 2020 08:13
meltsufin added a commit to GoogleCloudPlatform/spring-cloud-gcp that referenced this pull request Dec 3, 2020
#2604) (#157)

The new `DatastoreTemplate` instance should have the same
`ApplicationEventPublisher` as the original one, so that `AuditingHandler`
can be called.

Port of spring-attic/spring-cloud-gcp#2604.

Co-authored-by: Frank Pavageau <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Auditing doesn't work on entities in Datastore when running through DatastoreTemplate.performTransaction()
2 participants