Skip to content

Commit e5ef5e1

Browse files
committed
test: create query outside of thrown-by-assertions
Fixes some code smells reported by SonarCloud RISDEV-4322
1 parent 033b1de commit e5ef5e1

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

backend/src/main/java/de/bund/digitalservice/ris/norms/application/service/ArticleService.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,21 @@ private List<TextualMod> getPassiveModsAmendingByOrAt(
113113
}
114114

115115
private Predicate<Article> createPassiveModFilter(final List<TextualMod> mods) {
116-
return article -> {
117-
// If we filter by amendedAt or amendedBy: Those properties are found
118-
// in the passive modifications we already collected above. What's left
119-
// now is to only return the articles that are going to be modified by
120-
// those passive modifications.
121-
return mods.stream()
122-
.map(TextualMod::getDestinationHref)
123-
.flatMap(Optional::stream)
124-
.map(Href::getEId)
125-
.flatMap(Optional::stream)
126-
.anyMatch(
127-
destinationEid ->
128-
// Modifications can be either on the article itself or anywhere
129-
// inside the article, hence the "contains" rather than exact
130-
// matching.
131-
destinationEid.contains(article.getEid().orElseThrow()));
132-
};
116+
return article ->
117+
// If we filter by amendedAt or amendedBy: Those properties are found
118+
// in the passive modifications we already collected above. What's left
119+
// now is to only return the articles that are going to be modified by
120+
// those passive modifications.
121+
mods.stream()
122+
.map(TextualMod::getDestinationHref)
123+
.flatMap(Optional::stream)
124+
.map(Href::getEId)
125+
.flatMap(Optional::stream)
126+
.anyMatch(
127+
destinationEid ->
128+
// Modifications can be either on the article itself or anywhere
129+
// inside the article, hence the "contains" rather than exact
130+
// matching.
131+
destinationEid.contains(article.getEid().orElseThrow()));
133132
}
134133
}

backend/src/test/java/de/bund/digitalservice/ris/norms/application/service/ArticleServiceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ void throwsIfNormNotFound() {
4949
// given
5050
var eli = "eli/bund/DOES_NOT_EXIST/2000/s1/1970-01-01/1/deu/regelungstext-1";
5151
var eid = "meta-1";
52+
var query = new LoadArticleHtmlUseCase.Query(eli, eid);
5253
when(loadNormPort.loadNorm(new LoadNormPort.Command(eli))).thenReturn(Optional.empty());
5354

5455
// when
55-
assertThatThrownBy(
56-
() -> articleService.loadArticleHtml(new LoadArticleHtmlUseCase.Query(eli, eid)))
56+
assertThatThrownBy(() -> articleService.loadArticleHtml(query))
5757

5858
// then
5959
.isInstanceOf(NormNotFoundException.class);
@@ -65,12 +65,12 @@ void throwsIfArticleNotFound() {
6565
var norm = NormFixtures.loadFromDisk("SimpleNorm.xml");
6666
var eli = "eli/bund/DOES_NOT_EXIST/2000/s1/1970-01-01/1/deu/regelungstext-1";
6767
var eid = "NOT_IN_NORM";
68+
var query = new LoadArticleHtmlUseCase.Query(eli, eid);
6869
when(loadNormPort.loadNorm(new LoadNormPort.Command(eli))).thenReturn(Optional.of(norm));
6970
when(loadNormPort.loadNorm(new LoadNormPort.Command(eli))).thenReturn(Optional.of(norm));
7071

7172
// when
72-
assertThatThrownBy(
73-
() -> articleService.loadArticleHtml(new LoadArticleHtmlUseCase.Query(eli, eid)))
73+
assertThatThrownBy(() -> articleService.loadArticleHtml(query))
7474

7575
// then
7676
.isInstanceOf(ArticleNotFoundException.class);

backend/src/test/java/de/bund/digitalservice/ris/norms/application/service/NormServiceTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ void itCallsLoadNormAndReturnsNorm() {
8585
void itThrowsWhenNormIsNotFound() {
8686
// Given
8787
var eli = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1";
88+
var query = new LoadNormUseCase.Query(eli);
8889
when(loadNormPort.loadNorm(any())).thenReturn(Optional.empty());
8990

9091
// When
91-
assertThatThrownBy(() -> service.loadNorm(new LoadNormUseCase.Query(eli)))
92+
assertThatThrownBy(() -> service.loadNorm(query))
9293

9394
// Then
9495
.isInstanceOf(NormNotFoundException.class);
@@ -532,13 +533,11 @@ void loadAllArticles() {
532533
void itCallsLoadNormAndThrowsIfNotFound() {
533534
// Given
534535
var eli = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1";
536+
var query = new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel");
535537
when(loadNormPort.loadNorm(any())).thenReturn(Optional.empty());
536538

537539
// When
538-
assertThatThrownBy(
539-
() ->
540-
service.loadSpecificArticlesXmlFromNorm(
541-
new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel")))
540+
assertThatThrownBy(() -> service.loadSpecificArticlesXmlFromNorm(query))
542541

543542
// Then
544543
.isInstanceOf(NormNotFoundException.class);
@@ -595,6 +594,7 @@ void loadSpecificArticles() {
595594
void itThrowsWhenNoArticlesOfTypeAreFoundInNorm() {
596595
// Given
597596
var eli = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1";
597+
var query = new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel");
598598

599599
var norm =
600600
Norm.builder()
@@ -619,10 +619,7 @@ void itThrowsWhenNoArticlesOfTypeAreFoundInNorm() {
619619
when(loadNormPort.loadNorm(any())).thenReturn(Optional.of(norm));
620620

621621
// When
622-
assertThatThrownBy(
623-
() ->
624-
service.loadSpecificArticlesXmlFromNorm(
625-
new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel")))
622+
assertThatThrownBy(() -> service.loadSpecificArticlesXmlFromNorm(query))
626623

627624
// Then
628625
.isInstanceOf(

0 commit comments

Comments
 (0)