Skip to content

Commit

Permalink
test: create query outside of thrown-by-assertions
Browse files Browse the repository at this point in the history
Fixes some code smells reported by SonarCloud

RISDEV-4322
  • Loading branch information
andreasphil committed Aug 27, 2024
1 parent 033b1de commit e5ef5e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,21 @@ private List<TextualMod> getPassiveModsAmendingByOrAt(
}

private Predicate<Article> createPassiveModFilter(final List<TextualMod> mods) {
return article -> {
// If we filter by amendedAt or amendedBy: Those properties are found
// in the passive modifications we already collected above. What's left
// now is to only return the articles that are going to be modified by
// those passive modifications.
return mods.stream()
.map(TextualMod::getDestinationHref)
.flatMap(Optional::stream)
.map(Href::getEId)
.flatMap(Optional::stream)
.anyMatch(
destinationEid ->
// Modifications can be either on the article itself or anywhere
// inside the article, hence the "contains" rather than exact
// matching.
destinationEid.contains(article.getEid().orElseThrow()));
};
return article ->
// If we filter by amendedAt or amendedBy: Those properties are found
// in the passive modifications we already collected above. What's left
// now is to only return the articles that are going to be modified by
// those passive modifications.
mods.stream()
.map(TextualMod::getDestinationHref)
.flatMap(Optional::stream)
.map(Href::getEId)
.flatMap(Optional::stream)
.anyMatch(
destinationEid ->
// Modifications can be either on the article itself or anywhere
// inside the article, hence the "contains" rather than exact
// matching.
destinationEid.contains(article.getEid().orElseThrow()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ void throwsIfNormNotFound() {
// given
var eli = "eli/bund/DOES_NOT_EXIST/2000/s1/1970-01-01/1/deu/regelungstext-1";
var eid = "meta-1";
var query = new LoadArticleHtmlUseCase.Query(eli, eid);
when(loadNormPort.loadNorm(new LoadNormPort.Command(eli))).thenReturn(Optional.empty());

// when
assertThatThrownBy(
() -> articleService.loadArticleHtml(new LoadArticleHtmlUseCase.Query(eli, eid)))
assertThatThrownBy(() -> articleService.loadArticleHtml(query))

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

// when
assertThatThrownBy(
() -> articleService.loadArticleHtml(new LoadArticleHtmlUseCase.Query(eli, eid)))
assertThatThrownBy(() -> articleService.loadArticleHtml(query))

// then
.isInstanceOf(ArticleNotFoundException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ void itCallsLoadNormAndReturnsNorm() {
void itThrowsWhenNormIsNotFound() {
// Given
var eli = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1";
var query = new LoadNormUseCase.Query(eli);
when(loadNormPort.loadNorm(any())).thenReturn(Optional.empty());

// When
assertThatThrownBy(() -> service.loadNorm(new LoadNormUseCase.Query(eli)))
assertThatThrownBy(() -> service.loadNorm(query))

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

// When
assertThatThrownBy(
() ->
service.loadSpecificArticlesXmlFromNorm(
new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel")))
assertThatThrownBy(() -> service.loadSpecificArticlesXmlFromNorm(query))

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

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

// When
assertThatThrownBy(
() ->
service.loadSpecificArticlesXmlFromNorm(
new LoadSpecificArticlesXmlFromNormUseCase.Query(eli, "geltungszeitregel")))
assertThatThrownBy(() -> service.loadSpecificArticlesXmlFromNorm(query))

// Then
.isInstanceOf(
Expand Down

0 comments on commit e5ef5e1

Please sign in to comment.