Skip to content

Commit

Permalink
feat: CARITAS-286
Browse files Browse the repository at this point in the history
* add unit tests for new class TopicEnrichmentService
  • Loading branch information
Leandro13Silva13 committed Oct 28, 2024
1 parent 210dc42 commit aeba3ee
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -24,11 +25,23 @@ public List<AgencyTopicsDTO> enrichTopicIdsWithTopicData(List<Integer> topicIds)
log.debug("Enriching topic ids with topics short titles");
var availableTopics = topicService.getAllTopics();
log.debug("Enriching topic ids list with size: {} ", topicIds.size());
log.debug("Available topics list has size: {} ", availableTopics.size());
logAvailableTopicsList(availableTopics);
return enrichTopicIds(availableTopics, topicIds);
}

private static void logAvailableTopicsList(List<TopicDTO> availableTopics) {
if (availableTopics == null) {
log.debug("Available topics list is null ");
} else {
log.debug("Available topics list has size: {} ", availableTopics.size());
}
}

private List<AgencyTopicsDTO> enrichTopicIds(List<TopicDTO> availableTopics, List<Integer> topicIds) {

if (availableTopics == null) {
return new ArrayList<>();
}
// Create a map of availableTopics to quickly access TopicDTO by id
Map<Long, String> topicMap = availableTopics.stream()
.collect(Collectors.toMap(TopicDTO::getId, TopicDTO::getName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package de.caritas.cob.agencyservice.api.service;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;

@ExtendWith(MockitoExtension.class)
class TopicEnrichmentServiceTest {

@InjectMocks
TopicEnrichmentService topicEnrichmentService;

@Mock
TopicService topicService;

@Test
void enrichTopicIdsWithTopicData_Should_EnrichAgencyWithTopicDataFromTopicService() {
// given
when(topicService.getAllTopics()).thenReturn(
newArrayList(new TopicDTO().id(1L).name("first topic").description("desc"),
new TopicDTO().id(2L).name("second topic").description("desc")));
List<Integer> topicIds = newArrayList(1, 2);

// when
var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds);

// then
assertThat(result).isNotEmpty();
assertThat(result)
.extracting(AgencyTopicsDTO::getName)
.contains("first topic", "second topic");
}


@Test
void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfNoTopicsAreDefined() {
// given
when(topicService.getAllTopics()).thenReturn(
newArrayList());
List<Integer> topicIds = newArrayList(1, 2);

// when
var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds);

// then
assertThat(result).isEmpty();
}


@Test
void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfTopicsListIsNull() {
// given
when(topicService.getAllTopics()).thenReturn(
null);
List<Integer> topicIds = newArrayList(1, 2);

// when
var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds);

// then
assertThat(result).isEmpty();
}

@Test
void enrichTopicIdsWithTopicData_Should_ReturnEmptyListIfTopicIdDoNotMatch() {
// given
when(topicService.getAllTopics()).thenReturn(
newArrayList(new TopicDTO().id(3L).name("third topic").description("desc"),
new TopicDTO().id(4L).name("fourth topic").description("desc")));
List<Integer> topicIds = newArrayList(1, 2);

// when
var result = topicEnrichmentService.enrichTopicIdsWithTopicData(topicIds);

// then
assertThat(result).isEmpty();
}
}

0 comments on commit aeba3ee

Please sign in to comment.