Skip to content

Commit

Permalink
CCMSPUI-530 Finished controller and service tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie Briggs <jamie.briggs@digital.justice.gov.uk>
  • Loading branch information
Jamie Briggs committed Feb 6, 2025
1 parent 5639fed commit 018abbc
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.laa.ccms.data.api.NotificationsApi;
import uk.gov.laa.ccms.data.entity.NotificationInfo;
import uk.gov.laa.ccms.data.model.Notification;
import uk.gov.laa.ccms.data.model.NotificationSummary;
import uk.gov.laa.ccms.data.model.Notifications;
import uk.gov.laa.ccms.data.repository.NotificationRepository;
import uk.gov.laa.ccms.data.service.NotificationService;

/**
Expand All @@ -24,43 +22,44 @@
* <p>This class implements the {@link NotificationsApi} interface and provides
* endpoints for retrieving notification summaries for users.</p>
*
* @author Jamie Briggs
* @see NotificationsApi
* @see NotificationService
* @author Jamie Briggs
*/
@Slf4j
@RestController
@RequiredArgsConstructor
public class NotificationsController implements NotificationsApi {

private final NotificationService notificationService;
private final NotificationRepository tempRepo;


@Override
public ResponseEntity<Notification> getNotification(Long notificationId) {
Optional<NotificationInfo> tempNotification = tempRepo.findById(notificationId);

log.info(tempNotification.get().getSubject());
return ResponseEntity.ok(new Notification().subject(tempNotification.get().getSubject()));
return notificationService.getNotification(notificationId).map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

/**
* Retrieves a list of notifications based on various search criteria.
*
* @param providerId the provider ID to filter notifications
* @param caseReferenceNumber the case reference number to filter notifications
* @param providerId the provider ID to filter notifications
* @param caseReferenceNumber the case reference number to filter notifications
* @param providerCaseReference the provider-specific case reference to filter notifications
* @param assignedToUserId the user ID to filter notifications assigned to a specific user
* @param clientSurname the client's surname to filter notifications for a specific client
* @param feeEarnerId the ID of the fee earner to filter notifications associated with them
* @param includeClosed a flag to indicate whether to include closed notifications in the results
* @param notificationType the type of notifications to filter by
* @param dateFrom the starting date to filter notifications by a specific date range
* @param dateTo the ending date to filter notifications by a specific date range
* @param pageable the pagination and sorting information for the result set
* @return a {@code ResponseEntity} containing the retrieved list of notifications if found,
* or a {@code ResponseEntity} with HTTP status 404 if no notifications are found
* @param assignedToUserId the user ID to filter notifications assigned to a specific user
* @param clientSurname the client's surname to filter notifications for a specific
* client
* @param feeEarnerId the ID of the fee earner to filter notifications associated with
* them
* @param includeClosed a flag to indicate whether to include closed notifications in the
* results
* @param notificationType the type of notifications to filter by
* @param dateFrom the starting date to filter notifications by a specific date
* range
* @param dateTo the ending date to filter notifications by a specific date range
* @param pageable the pagination and sorting information for the result set
* @return a {@code ResponseEntity} containing the retrieved list of notifications if found, or a
* {@code ResponseEntity} with HTTP status 404 if no notifications are found
*/
@Override
public ResponseEntity<Notifications> getNotifications(Long providerId,
Expand All @@ -86,8 +85,8 @@ public ResponseEntity<Notifications> getNotifications(Long providerId,
* Retrieves a summary of user notifications for the specified login ID.
*
* @param loginId the login ID of the user for whom the notification summary is to be retrieved
* @return a {@code ResponseEntity} containing the {@code NotificationSummary} if found,
* or a {@code ResponseEntity} with HTTP status 404 if no summary is available
* @return a {@code ResponseEntity} containing the {@code NotificationSummary} if found, or a
* {@code ResponseEntity} with HTTP status 404 if no summary is available
*/
@Override
public ResponseEntity<NotificationSummary> getUserNotificationSummary(String loginId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import org.springframework.transaction.annotation.Transactional;
import uk.gov.laa.ccms.data.entity.NotificationCount;
import uk.gov.laa.ccms.data.entity.NotificationInfo;
import uk.gov.laa.ccms.data.mapper.NotificationMapper;
import uk.gov.laa.ccms.data.mapper.NotificationSummaryMapper;
import uk.gov.laa.ccms.data.mapper.NotificationsMapper;
import uk.gov.laa.ccms.data.model.Notification;
import uk.gov.laa.ccms.data.model.NotificationSummary;
import uk.gov.laa.ccms.data.model.Notifications;
import uk.gov.laa.ccms.data.repository.NotificationCountRepository;
import uk.gov.laa.ccms.data.repository.NotificationRepository;
import uk.gov.laa.ccms.data.repository.NotificationSearchRepository;

/**
Expand All @@ -33,9 +36,11 @@
public class NotificationService {

private final NotificationCountRepository notificationCountRepository;
private final NotificationSummaryMapper notificationSummaryMapper;
private final NotificationSearchRepository notificationSearchRepository;
private final NotificationRepository notificationRepository;
private final NotificationSummaryMapper notificationSummaryMapper;
private final NotificationsMapper notificationsMapper;
private final NotificationMapper notificationMapper;
private final UserService userService;

/**
Expand Down Expand Up @@ -72,7 +77,7 @@ public Optional<NotificationSummary> getUserNotificationSummary(String userId) {
* @param pageable the pageable to describe the requested pagination format.
* @return a paginated list of notifications.
*/
public Optional<Notifications> getNotifications(long providerId,
public Optional<Notifications> getNotifications(final long providerId,
String caseReferenceNumber,
String providerCaseReference, String assignedToUserId, String clientSurname,
Integer feeEarnerId, boolean includeClosed, String notificationType, LocalDate dateFrom,
Expand All @@ -91,4 +96,16 @@ public Optional<Notifications> getNotifications(long providerId,
Notifications notifications = notificationsMapper.mapToNotificationsList(byAssignedTo);
return Optional.ofNullable(notifications);
}

/**
* Retrieves a notification by its unique identifier.
*
* @param notificationId the unique identifier of the notification to retrieve
* @return an Optional containing the Notification object if found, or an
* empty Optional if not found
*/
public Optional<Notification> getNotification(final long notificationId) {
return notificationRepository.findById(notificationId).map(
notificationMapper::mapToNotification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import uk.gov.laa.ccms.data.model.Notification;
import uk.gov.laa.ccms.data.model.NotificationInfo;
import uk.gov.laa.ccms.data.model.NotificationSummary;
import uk.gov.laa.ccms.data.model.Notifications;
Expand Down Expand Up @@ -117,4 +118,38 @@ void getNotifications_badRequest() throws Exception {
.andDo(print())
.andExpect(status().isBadRequest());
}

@Test
@DisplayName("getNotification: Returns data")
void getNotification_returnsData() throws Exception {
//Given
Notification expected = new Notification().notificationId("123");
when(notificationService.getNotification(123L)).thenReturn(Optional.of(expected));
// Then
String jsonContent = objectMapper.writeValueAsString(expected);
this.mockMvc.perform(get("/notifications/123"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json(jsonContent));
}

@Test
@DisplayName("getNotification: Not found")
void getNotification_notFound() throws Exception {
//Given
// Then
this.mockMvc.perform(get("/notifications/123"))
.andDo(print())
.andExpect(status().isNotFound());
}

@Test
@DisplayName("getNotification: Bad request")
void getNotification_badRequest() throws Exception {
//Given
// Then
this.mockMvc.perform(get("/notifications/abc"))
.andDo(print())
.andExpect(status().isBadRequest());
}
}

This file was deleted.

Loading

0 comments on commit 018abbc

Please sign in to comment.