Skip to content

Commit

Permalink
Merge pull request #23 from virtualidentityag/CARITAS-286-x-reason-ad…
Browse files Browse the repository at this point in the history
…justments

Caritas 286 x reason adjustments
  • Loading branch information
tkuzynow authored Nov 20, 2024
2 parents 4634e43 + 73c62cb commit 7af4f83
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import de.caritas.cob.userservice.api.helper.SessionListAnalyser;
import de.caritas.cob.userservice.api.service.ChatService;
import de.caritas.cob.userservice.api.service.session.SessionService;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
Expand All @@ -29,6 +32,12 @@ public class UserSessionListService {
private final @NonNull RocketChatRoomInformationProvider rocketChatRoomInformationProvider;
private final @NonNull SessionListAnalyser sessionListAnalyser;

@Value("${feature.topics.enabled}")
private boolean featureTopicsEnabled;

@Autowired(required = false)
private SessionTopicEnrichmentService sessionTopicEnrichmentService;

/**
* Returns a list of {@link UserSessionResponseDTO} for the specified user ID.
*
Expand All @@ -42,7 +51,17 @@ public List<UserSessionResponseDTO> retrieveSessionsForAuthenticatedUser(
List<UserSessionResponseDTO> sessions = sessionService.getSessionsForUserId(userId);
List<UserSessionResponseDTO> chats = chatService.getChatsForUserId(userId);

return mergeUserSessionsAndChats(sessions, chats, rocketChatCredentials);
var mergedSessions = mergeUserSessionsAndChats(sessions, chats, rocketChatCredentials);
if (featureTopicsEnabled) {
enrichSessionsWithTopics(mergedSessions);
}
return mergedSessions;
}

private void enrichSessionsWithTopics(List<UserSessionResponseDTO> mergedSessions) {
mergedSessions.stream()
.map(UserSessionResponseDTO::getSession)
.forEach(sessionTopicEnrichmentService::enrichSessionWithTopicData);
}

/**
Expand Down Expand Up @@ -193,4 +212,9 @@ private void updateUserChatValuesForAvailableLastMessage(
private boolean isRocketChatRoomSubscribedByUser(List<String> userRoomsList, String groupId) {
return nonNull(userRoomsList) && userRoomsList.contains(groupId);
}

void setSessionTopicEnrichmentService(
SessionTopicEnrichmentService sessionTopicEnrichmentService) {
this.sessionTopicEnrichmentService = sessionTopicEnrichmentService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import de.caritas.cob.userservice.api.UserServiceApplication;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand All @@ -25,6 +27,8 @@ class ActuatorControllerIT {

@Autowired private WebApplicationContext context;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

private MockMvc mockMvc;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.userservice.api.adapters.web.dto.Appointment;
import de.caritas.cob.userservice.api.config.auth.Authority.AuthorityValue;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.util.UUID;
import javax.servlet.http.Cookie;
import org.jeasy.random.EasyRandom;
Expand All @@ -18,6 +19,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
Expand All @@ -35,6 +37,7 @@ class AppointmentControllerAuthorizationIT {
private static final String CSRF_VALUE = "test";
private static final Cookie CSRF_COOKIE = new Cookie("csrfCookie", CSRF_VALUE);

@MockBean SessionTopicEnrichmentService sessionTopicEnrichmentService;
@Autowired private MockMvc mvc;

private Appointment appointment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.port.out.AppointmentRepository;
import de.caritas.cob.userservice.api.port.out.ConsultantRepository;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.lang.reflect.Method;
import java.time.Clock;
import java.time.Instant;
Expand Down Expand Up @@ -85,6 +86,8 @@ class AppointmentControllerE2EIT {

@MockBean private KeycloakConfigResolver keycloakConfigResolver;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

private Appointment appointment;

private de.caritas.cob.userservice.api.model.Appointment savedAppointment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.caritas.cob.userservice.api.conversation.facade.FinishAnonymousConversationFacade;
import de.caritas.cob.userservice.api.conversation.service.ConversationListResolver;
import de.caritas.cob.userservice.api.helper.UsernameTranscoder;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import javax.servlet.http.Cookie;
import org.jeasy.random.EasyRandom;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -67,6 +68,8 @@ class ConversationControllerAuthorizationIT {

@MockBean private FinishAnonymousConversationFacade finishAnonymousConversationFacade;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@MockBean
@SuppressWarnings("unused")
private UsernameTranscoder usernameTranscoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import de.caritas.cob.userservice.api.admin.report.service.ViolationReportGenerator;
import de.caritas.cob.userservice.api.admin.service.session.SessionAdminService;
import de.caritas.cob.userservice.api.config.auth.Authority.AuthorityValue;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.Cookie;
Expand Down Expand Up @@ -72,6 +73,8 @@ class UserAdminControllerAuthorizationIT {

@MockBean private AskerUserAdminFacade askerUserAdminFacade;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@Test
void getSessions_Should_ReturnUnauthorizedAndCallNoMethods_When_noKeycloakAuthorizationIsPresent()
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.caritas.cob.userservice.api.config.auth.IdentityConfig;
import de.caritas.cob.userservice.api.helper.AuthenticatedUser;
import de.caritas.cob.userservice.api.port.out.IdentityClient;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import de.caritas.cob.userservice.api.tenant.TenantResolverService;
import de.caritas.cob.userservice.tenantservice.generated.web.model.RestrictedTenantDTO;
import javax.servlet.http.Cookie;
Expand Down Expand Up @@ -68,6 +69,8 @@ class UserAdminControllerMultiTenancyTrueE2EIT {

@MockBean AuthenticatedUser authenticatedUser;

@MockBean SessionTopicEnrichmentService sessionTopicEnrichmentService;

@AfterEach
void reset() {
identityConfig.setDisplayNameAllowedForConsultants(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import de.caritas.cob.userservice.api.port.out.ConsultantRepository;
import de.caritas.cob.userservice.api.port.out.UserAgencyRepository;
import de.caritas.cob.userservice.api.port.out.UserRepository;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -112,6 +113,8 @@ class UserController2faE2EIT {

@MockBean private AuthenticatedUser authenticatedUser;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@MockBean
@Qualifier("keycloakRestTemplate")
private RestTemplate keycloakRestTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import de.caritas.cob.userservice.api.service.SessionDataService;
import de.caritas.cob.userservice.api.service.archive.SessionArchiveService;
import de.caritas.cob.userservice.api.service.session.SessionService;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import de.caritas.cob.userservice.api.service.user.UserAccountService;
import de.caritas.cob.userservice.api.service.user.UserService;
import java.util.Optional;
Expand Down Expand Up @@ -157,6 +158,8 @@ class UserControllerAuthorizationIT {
@MockBean private ConsultantService consultantService;
@MockBean private AskerDataProvider askerDataProvider;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@MockBean
@SuppressWarnings("unused")
private Messaging messenger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import de.caritas.cob.userservice.api.port.out.UserAgencyRepository;
import de.caritas.cob.userservice.api.port.out.UserChatRepository;
import de.caritas.cob.userservice.api.port.out.UserRepository;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import de.caritas.cob.userservice.api.testConfig.TestAgencyControllerApi;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
Expand Down Expand Up @@ -155,6 +156,8 @@ class UserControllerChatE2EIT {

@MockBean private AgencyServiceApiControllerFactory agencyServiceApiControllerFactory;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@Autowired private CacheManager cacheManager;

@MockBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import de.caritas.cob.userservice.api.port.out.SessionRepository;
import de.caritas.cob.userservice.api.port.out.UserAgencyRepository;
import de.caritas.cob.userservice.api.port.out.UserRepository;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import de.caritas.cob.userservice.api.testConfig.TestAgencyControllerApi;
import java.lang.reflect.Method;
import java.net.URI;
Expand Down Expand Up @@ -177,6 +178,8 @@ class UserControllerSessionE2EIT {

@MockBean private RocketChatCredentialsProvider rocketChatCredentialsProvider;

@MockBean private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@SuppressWarnings("unused")
@MockBean
private ConsultantDataFacade consultantDataFacade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static java.util.Objects.nonNull;
import static org.jsoup.helper.Validate.fail;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import de.caritas.cob.userservice.api.adapters.web.dto.UserSessionResponseDTO;
Expand All @@ -33,28 +34,40 @@
import de.caritas.cob.userservice.api.helper.SessionListAnalyser;
import de.caritas.cob.userservice.api.service.ChatService;
import de.caritas.cob.userservice.api.service.session.SessionService;
import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;

@ExtendWith(MockitoExtension.class)
public class UserSessionListServiceTest {
class UserSessionListServiceTest {

@InjectMocks private UserSessionListService userSessionListService;
@Mock private SessionService sessionService;
@Mock private ChatService chatService;
@Mock private RocketChatRoomInformationProvider rocketChatRoomInformationProvider;
@Mock private SessionListAnalyser sessionListAnalyser;

@Mock private SessionTopicEnrichmentService sessionTopicEnrichmentService;

@BeforeEach
public void setUp() {
ReflectionTestUtils.setField(userSessionListService, "featureTopicsEnabled", false);
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnValidSessionListWithSessionMessagesReadTrue_WhenThereAreNoUnreadMessages() {

ReflectionTestUtils.setField(userSessionListService, "featureTopicsEnabled", true);
userSessionListService.setSessionTopicEnrichmentService(sessionTopicEnrichmentService);
when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(USER_SESSION_RESPONSE_DTO_LIST);
RocketChatRoomInformation rocketChatRoomInformation =
RocketChatRoomInformation.builder()
Expand All @@ -80,10 +93,13 @@ public class UserSessionListServiceTest {
.get(0)
.getSession()
.getMessagesRead());

verify(sessionTopicEnrichmentService, Mockito.times(USER_SESSION_RESPONSE_DTO_LIST.size()))
.enrichSessionWithTopicData(Mockito.any());
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnValidSessionListWithChatMessagesReadTrue_WhenThereAreNoUnreadMessages() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
Expand All @@ -110,7 +126,7 @@ public class UserSessionListServiceTest {
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnValidSessionListWithSessionMessagesReadFalse_WhenThereAreUnreadMessages() {

when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(USER_SESSION_RESPONSE_DTO_LIST);
Expand Down Expand Up @@ -141,7 +157,7 @@ public class UserSessionListServiceTest {
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnValidSessionListWithChatMessagesReadFalse_WhenThereAreUnreadMessages() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
Expand All @@ -168,7 +184,7 @@ public class UserSessionListServiceTest {
}

@Test
public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectChatMessageDate() {
void retrieveSessionsForAuthenticatedUser_Should_SetCorrectChatMessageDate() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(Collections.emptyList());
Expand All @@ -190,7 +206,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectChatMessageDat
}

@Test
public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessageDate() {
void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessageDate() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(Collections.emptyList());
when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(USER_SESSION_RESPONSE_DTO_LIST);
Expand All @@ -211,7 +227,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessage
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnCorrectFileTypeAndImagePreviewForSession() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(Collections.emptyList());
Expand All @@ -237,8 +253,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessage
}

@Test
public void
retrieveSessionsForAuthenticatedUser_Should_ReturnCorrectFileTypeAndImagePreviewForChat() {
void retrieveSessionsForAuthenticatedUser_Should_ReturnCorrectFileTypeAndImagePreviewForChat() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(Collections.emptyList());
Expand All @@ -264,7 +279,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessage
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_ReturnEmptyList_WhenSessionAndChatListAreEmpty() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(Collections.emptyList());
Expand All @@ -278,7 +293,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_SetCorrectSessionMessage
}

@Test
public void retrieveSessionsForAuthenticatedUser_Should_MergeSessionsAndChats() {
void retrieveSessionsForAuthenticatedUser_Should_MergeSessionsAndChats() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
when(sessionService.getSessionsForUserId(USER_ID)).thenReturn(USER_SESSION_RESPONSE_DTO_LIST);
Expand Down Expand Up @@ -327,7 +342,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_MergeSessionsAndChats()
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_SetSubscribedFlagToTrue_WhenUserIsAttendeeOfAChat() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
Expand All @@ -351,7 +366,7 @@ public void retrieveSessionsForAuthenticatedUser_Should_MergeSessionsAndChats()
}

@Test
public void
void
retrieveSessionsForAuthenticatedUser_Should_SetSubscribedFlagToFalse_WhenUserIsNotAttendeeOfAChat() {

when(chatService.getChatsForUserId(USER_ID)).thenReturn(USER_CHAT_RESPONSE_DTO_LIST);
Expand Down

0 comments on commit 7af4f83

Please sign in to comment.