Skip to content

Commit

Permalink
Add LoadArticlesPort
Browse files Browse the repository at this point in the history
RISDEV-0000
  • Loading branch information
VictorDelCampo committed Mar 2, 2024
1 parent 455aebc commit e6c09e0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.ArticleDto;
import de.bund.digitalservice.ris.norms.domain.entity.Article;
import java.util.Optional;

/** Mapper class for converting between {@link ArticleDto} and {@link Article}. */
public class ArticleMapper {
Expand Down Expand Up @@ -35,7 +36,8 @@ public static ArticleDto mapToDto(final Article article) {
.enumeration(article.getEnumeration())
.eid(article.getEid())
.title(article.getTitle())
.targetLawDto(TargetLawMapper.mapToDto(article.getTargetLaw()))
.targetLawDto(
Optional.ofNullable(article.getTargetLaw()).map(TargetLawMapper::mapToDto).orElse(null))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package de.bund.digitalservice.ris.norms.adapter.output.database.service;

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.AmendingLawDto;
import de.bund.digitalservice.ris.norms.adapter.output.database.mapper.AmendingLawMapper;
import de.bund.digitalservice.ris.norms.adapter.output.database.mapper.ArticleMapper;
import de.bund.digitalservice.ris.norms.adapter.output.database.repository.AmendingLawRepository;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAllAmendingLawsPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAmendingLawPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadArticlesPort;
import de.bund.digitalservice.ris.norms.domain.entity.AmendingLaw;
import de.bund.digitalservice.ris.norms.domain.entity.Article;
import jakarta.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
Expand All @@ -16,7 +21,7 @@
* Spring context.
*/
@Service
public class DBService implements LoadAmendingLawPort, LoadAllAmendingLawsPort {
public class DBService implements LoadAmendingLawPort, LoadAllAmendingLawsPort, LoadArticlesPort {

private final AmendingLawRepository amendingLawRepository;

Expand All @@ -26,7 +31,7 @@ public DBService(AmendingLawRepository amendingLawRepository) {

@Override
@Transactional
public Optional<AmendingLaw> loadAmendingLawByEli(Command command) {
public Optional<AmendingLaw> loadAmendingLawByEli(LoadAmendingLawPort.Command command) {
return amendingLawRepository
.findByEli(command.eli())
.map(AmendingLawMapper::mapToDomainWithArticles);
Expand All @@ -38,4 +43,15 @@ public List<AmendingLaw> loadAllAmendingLaws() {
.map(AmendingLawMapper::mapToDomain)
.toList();
}

@Override
public List<Article> loadArticlesByAmendingLaw(LoadArticlesPort.Command command) {
final Optional<AmendingLawDto> amendingLawDtoOptional =
amendingLawRepository.findByEli(command.eli());
return amendingLawDtoOptional
.map(
amendingLawDto ->
amendingLawDto.getArticleDtos().stream().map(ArticleMapper::mapToDomain).toList())
.orElse(Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.bund.digitalservice.ris.norms.application.port.output;

import de.bund.digitalservice.ris.norms.domain.entity.Article;
import java.util.List;

/** Port interface for loading all articles of a given amending law from the storage. */
public interface LoadArticlesPort {

/**
* Loads all {@link Article}s available in the system of a given amending law.
*
* @param command The command specifying the ELI to identify the amending law of the articles to
* be loaded.
* @return A {@link List} of {@link Article}, which may be empty if no articles are found.
*/
List<Article> loadArticlesByAmendingLaw(final Command command);

/**
* A record representing the command for loading articles. The command includes the ELI (European
* Legislation Identifier) to identify the amending law.
*
* @param eli The ELI (European Legislation Identifier) used to identify the amending law in the
* command.
*/
record Command(String eli) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import de.bund.digitalservice.ris.norms.application.port.input.LoadTargetLawUseCase;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAllAmendingLawsPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAmendingLawPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadArticlesPort;
import de.bund.digitalservice.ris.norms.domain.entity.AmendingLaw;
import de.bund.digitalservice.ris.norms.domain.entity.Article;
import de.bund.digitalservice.ris.norms.domain.entity.TargetLaw;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
Expand All @@ -30,6 +30,7 @@ public class AmendingLawService

private final LoadAmendingLawPort loadAmendingLawPort;
private final LoadAllAmendingLawsPort loadAllAmendingLawsPort;
private final LoadArticlesPort loadArticlesPort;

/**
* Constructs a new {@link AmendingLawService} instance.
Expand All @@ -38,9 +39,12 @@ public class AmendingLawService
* @param loadAllAmendingLawsPort The port for loading all amending laws.
*/
public AmendingLawService(
LoadAmendingLawPort loadAmendingLawPort, LoadAllAmendingLawsPort loadAllAmendingLawsPort) {
LoadAmendingLawPort loadAmendingLawPort,
LoadAllAmendingLawsPort loadAllAmendingLawsPort,
LoadArticlesPort loadArticlesPort) {
this.loadAmendingLawPort = loadAmendingLawPort;
this.loadAllAmendingLawsPort = loadAllAmendingLawsPort;
this.loadArticlesPort = loadArticlesPort;
}

@Override
Expand All @@ -60,7 +64,7 @@ public Optional<Article> loadArticle(final LoadArticleUseCase.Query query) {

@Override
public List<Article> loadArticlesOfAmendingLaw(final LoadArticlesUseCase.Query query) {
return Collections.emptyList();
return loadArticlesPort.loadArticlesByAmendingLaw(new LoadArticlesPort.Command(query.eli()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.bund.digitalservice.ris.norms.application.port.input.LoadAmendingLawUseCase;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAllAmendingLawsPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadAmendingLawPort;
import de.bund.digitalservice.ris.norms.application.port.output.LoadArticlesPort;
import de.bund.digitalservice.ris.norms.domain.entity.AmendingLaw;
import java.time.LocalDate;
import java.util.List;
Expand All @@ -20,15 +21,19 @@

class AmendingLawServiceTest {

final LoadAmendingLawPort loadAmendingLawAdapter = mock(LoadAmendingLawPort.class);
final LoadAllAmendingLawsPort loadAllAmendingLawsAdapter = mock(LoadAllAmendingLawsPort.class);

final LoadArticlesPort loadArticlesPort = mock(LoadArticlesPort.class);

final AmendingLawService service =
new AmendingLawService(loadAmendingLawAdapter, loadAllAmendingLawsAdapter, loadArticlesPort);

@Test
void itCallsLoadAmendingLawByEliUsingInputQueryEli() {
// Given
final String eli = "someEli";
final LoadAmendingLawPort loadAmendingLawAdapter = mock(LoadAmendingLawPort.class);
final LoadAllAmendingLawsPort loadAllAmendingLawsAdapter = mock(LoadAllAmendingLawsPort.class);

final AmendingLawService service =
new AmendingLawService(loadAmendingLawAdapter, loadAllAmendingLawsAdapter);
final LoadAmendingLawUseCase.Query query = new LoadAmendingLawUseCase.Query(eli);
when(loadAmendingLawAdapter.loadAmendingLawByEli(any())).thenReturn(Optional.empty());

Expand All @@ -44,11 +49,7 @@ void itCallsLoadAmendingLawByEliUsingInputQueryEli() {
void canLoadAmendingLawByEliIfAdapterFindsOne() {
// Given
final String eli = "someEli";
final LoadAmendingLawPort loadAmendingLawAdapter = mock(LoadAmendingLawPort.class);
final LoadAllAmendingLawsPort loadAllAmendingLawAdapter = mock(LoadAllAmendingLawsPort.class);

final AmendingLawService service =
new AmendingLawService(loadAmendingLawAdapter, loadAllAmendingLawAdapter);
final LoadAmendingLawUseCase.Query query = new LoadAmendingLawUseCase.Query(eli);

final String printAnnouncementGazette = "someGazette";
Expand Down Expand Up @@ -82,11 +83,7 @@ void canLoadAmendingLawByEliIfAdapterFindsOne() {
void canNotLoadAmendingLawByEliIfAdapterDoesNotFindOne() {
// Given
final String eli = "someEli";
final LoadAmendingLawPort loadAmendingLawAdapter = mock(LoadAmendingLawPort.class);
final LoadAllAmendingLawsPort loadAllAmendingLawsAdapter = mock(LoadAllAmendingLawsPort.class);

final AmendingLawService service =
new AmendingLawService(loadAmendingLawAdapter, loadAllAmendingLawsAdapter);
final LoadAmendingLawUseCase.Query query = new LoadAmendingLawUseCase.Query(eli);
when(loadAmendingLawAdapter.loadAmendingLawByEli(any())).thenReturn(Optional.empty());

Expand All @@ -100,11 +97,6 @@ void canNotLoadAmendingLawByEliIfAdapterDoesNotFindOne() {
@Test
void canLoadAllAmendingLaws() {
// Given
final LoadAmendingLawPort loadAmendingLawAdapter = mock(LoadAmendingLawPort.class);
final LoadAllAmendingLawsPort loadAllAmendingLawsAdapter = mock(LoadAllAmendingLawsPort.class);
final AmendingLawService service =
new AmendingLawService(loadAmendingLawAdapter, loadAllAmendingLawsAdapter);

final String eli = "eli/bund/bgbl-1/1953/s225/2017-03-15/1/deu/regelungstext-1";
final String printAnnouncementGazette = "someGazette";
final LocalDate publicationDate = LocalDate.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void itCallsAmendingLawServiceAndReturnsAmendingLaw() throws Exception {
final String digitalAnnouncementMedium = "medium123";
final String digitalAnnouncementEdition = "edition123";
final String title = "title";
final String xml = "<test></test>";

// When
final AmendingLaw amendingLaw =
Expand All @@ -49,6 +50,7 @@ void itCallsAmendingLawServiceAndReturnsAmendingLaw() throws Exception {
.digitalAnnouncementMedium(digitalAnnouncementMedium)
.digitalAnnouncementEdition(digitalAnnouncementEdition)
.title(title)
.xml(xml)
.build();
amendingLawRepository.save(AmendingLawMapper.mapToDto(amendingLaw));

Expand Down Expand Up @@ -78,6 +80,7 @@ void itLoadsAllAmendingLawsAndReturnsSuccessfully() throws Exception {
final String digitalAnnouncementMedium = "medium123";
final String digitalAnnouncementEdition = "edition123";
final String title1 = "title1";
final String xml1 = "<test>1</test>";

final String eli2 = "eli2";
final String printAnnouncementGazette2 = "someGazette2";
Expand All @@ -86,6 +89,7 @@ void itLoadsAllAmendingLawsAndReturnsSuccessfully() throws Exception {
final String digitalAnnouncementMedium2 = "medium1232";
final String digitalAnnouncementEdition2 = "edition1232";
final String title2 = "title2";
final String xml2 = "<test>2</test>";

final AmendingLaw amendingLaw1 =
AmendingLaw.builder()
Expand All @@ -96,6 +100,7 @@ void itLoadsAllAmendingLawsAndReturnsSuccessfully() throws Exception {
.digitalAnnouncementMedium(digitalAnnouncementMedium)
.digitalAnnouncementEdition(digitalAnnouncementEdition)
.title(title1)
.xml(xml1)
.build();

final AmendingLaw amendingLaw2 =
Expand All @@ -107,6 +112,7 @@ void itLoadsAllAmendingLawsAndReturnsSuccessfully() throws Exception {
.digitalAnnouncementMedium(digitalAnnouncementMedium2)
.digitalAnnouncementEdition(digitalAnnouncementEdition2)
.title(title2)
.xml(xml2)
.build();

amendingLawRepository.saveAll(
Expand Down

0 comments on commit e6c09e0

Please sign in to comment.