From 64842b5f9b18a89b404c12760b28163ed599edad Mon Sep 17 00:00:00 2001 From: jayanta2018 Date: Mon, 16 Oct 2023 13:27:27 +0100 Subject: [PATCH 01/13] feat: end point for exceptions --- .../controller/ConnectionController.java | 20 +++++++++++++++++++ .../connection/dto/ExceptionRecordDto.java | 3 +++ .../repository/ExceptionRepository.java | 5 ++++- .../connection/service/ExceptionService.java | 17 ++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index 551ea8b9..016c67f2 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -31,9 +31,12 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; import java.util.Objects; import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -45,6 +48,7 @@ import org.springframework.web.bind.annotation.RestController; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.exception.ConnectionQueryException; @@ -53,6 +57,7 @@ import uk.nhs.hee.tis.revalidation.connection.service.DisconnectedElasticSearchService; import uk.nhs.hee.tis.revalidation.connection.service.DiscrepanciesElasticSearchService; import uk.nhs.hee.tis.revalidation.connection.service.ElasticsearchQueryHelper; +import uk.nhs.hee.tis.revalidation.connection.service.ExceptionService; @Slf4j @RestController @@ -81,6 +86,9 @@ public class ConnectionController { @Autowired private DisconnectedElasticSearchService disconnectedElasticSearchService; + @Autowired + private ExceptionService exceptionService; + /** * POST /connections/add : Add a new connection. * @@ -306,4 +314,16 @@ public ResponseEntity getSummaryDisconnected( return ResponseEntity.ok(connectionSummaryDto); } + + /** + * GET /exceptions/today : get list of exceptions. + * + * @return the list of ExceptionRecordDto + */ + @GetMapping("/exceptions/today") + public List getListOfExceptions() { + + DateTime today = DateTime.now();// need to go to the service method + return exceptionService.getExceptionLogForToday(today); + } } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java index e0644354..76d8d25e 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java @@ -21,6 +21,7 @@ package uk.nhs.hee.tis.revalidation.connection.dto; +import java.time.LocalDateTime; import lombok.Builder; import lombok.Data; @@ -30,4 +31,6 @@ public class ExceptionRecordDto { private String gmcId; private String exceptionMessage; + private LocalDateTime timestamp; + private String admin; } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java index 83abb80b..89d379ec 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java @@ -21,11 +21,14 @@ package uk.nhs.hee.tis.revalidation.connection.repository; +import java.util.List; +import org.joda.time.DateTime; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; @Repository public interface ExceptionRepository extends MongoRepository { - +//find by current admin + List findByTimestamp(DateTime time); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 042d7426..5f111ce5 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -90,6 +91,22 @@ public ExceptionResponseDto getExceptionLog(final ExceptionRequestDto requestDto .build(); } + /** + * Get today's exception log + * @param today request by date today + */ + public List getExceptionLogForToday(DateTime today){ + final var todaysExceptions = repository.findByTimestamp(today); + return todaysExceptions.stream().map(exceptionLog -> { + return ExceptionRecordDto.builder() + .gmcId(exceptionLog.getGmcId()) + .exceptionMessage(exceptionLog.getErrorMessage()) + .timestamp(exceptionLog.getTimestamp()) + .admin(exceptionLog.getAdmin()) + .build(); + }).collect(toList()); + } + /** * Get exception log hashmap. */ From 0ba001edb2ff04bdb25cfe6f4aad3b56b1f23f81 Mon Sep 17 00:00:00 2001 From: jayanta2018 Date: Tue, 17 Oct 2023 11:00:37 +0100 Subject: [PATCH 02/13] test: add test and clean code --- .../controller/ConnectionController.java | 10 +++-- .../connection/dto/ExceptionRecordDto.java | 1 + .../connection/entity/ExceptionLog.java | 6 +++ .../repository/ExceptionRepository.java | 6 +-- .../connection/service/ExceptionService.java | 9 +++-- .../controller/ConnectionControllerTest.java | 37 +++++++++++++++++++ .../service/ExceptionServiceTest.java | 32 +++++++++++++++- 7 files changed, 88 insertions(+), 13 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index 016c67f2..5af16b4c 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -320,10 +320,12 @@ public ResponseEntity getSummaryDisconnected( * * @return the list of ExceptionRecordDto */ - @GetMapping("/exceptions/today") - public List getListOfExceptions() { + @GetMapping("/exceptions/today/{admin}") + public ResponseEntity> getListOfExceptions(@PathVariable("admin") final String admin) { + + log.info("Received request to fetch exceptions for admin: {}", admin); + final var exceptions = exceptionService.getExceptionLogs(admin); + return ResponseEntity.ok().body(exceptions); - DateTime today = DateTime.now();// need to go to the service method - return exceptionService.getExceptionLogForToday(today); } } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java index 76d8d25e..8416472f 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java @@ -25,6 +25,7 @@ import lombok.Builder; import lombok.Data; + @Data @Builder public class ExceptionRecordDto { diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java index 128f4506..82e43a65 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java @@ -22,6 +22,9 @@ package uk.nhs.hee.tis.revalidation.connection.entity; import java.time.LocalDateTime; +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -37,6 +40,9 @@ public class ExceptionLog { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private int _id; private String gmcId; private String errorMessage; private LocalDateTime timestamp; diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java index 89d379ec..513da17d 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java @@ -21,14 +21,14 @@ package uk.nhs.hee.tis.revalidation.connection.repository; +import java.time.LocalDateTime; import java.util.List; -import org.joda.time.DateTime; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; @Repository public interface ExceptionRepository extends MongoRepository { -//find by current admin - List findByTimestamp(DateTime time); + + List findByTimestampAndAdmin(LocalDateTime timestamp, String admin); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 5f111ce5..327b8331 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -28,11 +28,11 @@ import static org.springframework.data.domain.Sort.Direction.DESC; import static org.springframework.data.domain.Sort.by; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; -import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -93,10 +93,11 @@ public ExceptionResponseDto getExceptionLog(final ExceptionRequestDto requestDto /** * Get today's exception log - * @param today request by date today + * @param admin request by date today */ - public List getExceptionLogForToday(DateTime today){ - final var todaysExceptions = repository.findByTimestamp(today); + public List getExceptionLogs(String admin){ + LocalDateTime today = LocalDateTime.now(); + final var todaysExceptions = repository.findByTimestampAndAdmin(today, admin); return todaysExceptions.stream().map(exceptionLog -> { return ExceptionRecordDto.builder() .gmcId(exceptionLog.getGmcId()) diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index b6e411aa..f9275628 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -53,6 +53,7 @@ import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionInfoDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; import uk.nhs.hee.tis.revalidation.connection.dto.DoctorInfoDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.entity.ConnectionRequestType; @@ -60,6 +61,8 @@ import uk.nhs.hee.tis.revalidation.connection.service.ConnectionService; import uk.nhs.hee.tis.revalidation.connection.service.DisconnectedElasticSearchService; import uk.nhs.hee.tis.revalidation.connection.service.DiscrepanciesElasticSearchService; +import uk.nhs.hee.tis.revalidation.connection.service.ExceptionService; + @WebMvcTest(ConnectionController.class) class ConnectionControllerTest { @@ -86,6 +89,9 @@ class ConnectionControllerTest { private ConnectedElasticSearchService connectedElasticSearchService; @MockBean private DisconnectedElasticSearchService disconnectedElasticSearchService; + @MockBean + private ExceptionService exceptionService; + private String changeReason; private String designatedBodyCode; private String gmcId; @@ -116,6 +122,8 @@ class ConnectionControllerTest { private String programmeOwner2; private String exceptionReason1; private String exceptionReason2; + private String admin; + private LocalDateTime today; @BeforeEach public void setup() { @@ -151,6 +159,7 @@ public void setup() { programmeOwner2 = faker.lorem().characters(20); exceptionReason1 = faker.lorem().characters(20); exceptionReason2 = faker.lorem().characters(20); + today = LocalDateTime.now(); } @Test @@ -417,6 +426,22 @@ void shouldReturnDisconnectedTraineeDoctorsInformationDesc() throws Exception { jsonPath("$.connections.[*].tcsPersonId").value(hasItem(personId1.intValue()))); } + + @Test + void shouldReturnAllExceptionsForAnAdmin() throws Exception { + final var exceptionRecordDtoList = buildExceptionRecordDtoList(); + + when(exceptionService.getExceptionLogs(admin)).thenReturn(exceptionRecordDtoList); + + mockMvc.perform(get("/api/connections/exceptions/today/{admin}", admin)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId))) + .andExpect(jsonPath("$.[*].exceptionMessage").value(hasItem(exceptionReason1))) + //.andExpect(jsonPath("$.[*].timestamp").value(hasItem("year=2023"))) + .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))); + + } + private ConnectionDto prepareConnectionDto() { final ConnectionDto connectionDto = new ConnectionDto(); final ConnectionHistoryDto connectionHistory = ConnectionHistoryDto.builder() @@ -476,4 +501,16 @@ private List buildDoctorsForDbList() { .build(); return of(doctor1, doctor2); } + + private List buildExceptionRecordDtoList() { + final var record1 = ExceptionRecordDto.builder() + .gmcId(gmcId).exceptionMessage(exceptionReason1) + .timestamp(today).admin(admin) + .build(); + final var record2 = ExceptionRecordDto.builder() + .gmcId(gmcRef2).exceptionMessage(exceptionReason2) + .timestamp(today).admin(admin) + .build(); + return of(record1, record2); + } } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index 3b7191e8..3ad89dfc 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -21,9 +21,10 @@ package uk.nhs.hee.tis.revalidation.connection.service; +import static java.util.List.of; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -35,6 +36,7 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; +import org.joda.time.DateTime; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -64,18 +66,24 @@ class ExceptionServiceTest { @Mock private ExceptionLog exceptionLog; - private String gmcId; + private String gmcId, gmcId2; private String responseCode; private LocalDateTime localDateTime; private List exceptionLogList; private String admin; + private String exceptionReason1, exceptionReason2; + private LocalDateTime dateTime; @BeforeEach public void setup() { gmcId = faker.number().digits(8); + gmcId2 = faker.number().digits(8); responseCode = "0"; localDateTime = LocalDateTime.now(); admin = "admin"; + exceptionReason1 = faker.letterify("aa"); + exceptionReason2 = faker.letterify("bb"); + dateTime = LocalDateTime.now(); initializeExceptionLogList(); } @@ -103,6 +111,14 @@ void shouldReturnLogsMap() { assertThat(result.get(gmcId), is(responseCode)); } + @Test + void shouldReturnExceptionLogs() { + when(repository.findByTimestampAndAdmin(dateTime, admin)).thenReturn(buildExceptionLogList()); + + final var result = exceptionService.getExceptionLogs(admin); + assertThat(result.get(0).getGmcId(), is(gmcId)); + } + private void initializeExceptionLogList() { ExceptionLog exceptionLog = ExceptionLog.builder() .gmcId(gmcId) @@ -113,4 +129,16 @@ private void initializeExceptionLogList() { exceptionLogList = new ArrayList<>(); exceptionLogList.add(exceptionLog); } + + private List buildExceptionLogList() { + final var record1 = ExceptionLog.builder() + .gmcId(gmcId).errorMessage(exceptionReason1) + .timestamp(dateTime).admin(admin) + .build(); + final var record2 = ExceptionLog.builder() + .gmcId(gmcId2).errorMessage(exceptionReason2) + .timestamp(dateTime).admin(admin) + .build(); + return of(record1, record2); + } } From 531c20d7c0f3ce816da5dd5347361da88496dc67 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Tue, 17 Oct 2023 12:14:21 +0100 Subject: [PATCH 03/13] fix: fix tests and use localdate for searches, localdateTime for saves for exceptionlogs TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../controller/ConnectionController.java | 3 -- .../repository/ExceptionRepository.java | 4 +- .../connection/service/ExceptionService.java | 54 ++----------------- .../controller/ConnectionControllerTest.java | 1 + .../service/ExceptionServiceTest.java | 40 +++----------- 5 files changed, 14 insertions(+), 88 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index 5af16b4c..dba02449 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -31,12 +31,9 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; -import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.List; import java.util.Objects; import lombok.extern.slf4j.Slf4j; -import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java index 513da17d..84b8784c 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java @@ -21,7 +21,7 @@ package uk.nhs.hee.tis.revalidation.connection.repository; -import java.time.LocalDateTime; +import java.time.LocalDate; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; @@ -30,5 +30,5 @@ @Repository public interface ExceptionRepository extends MongoRepository { - List findByTimestampAndAdmin(LocalDateTime timestamp, String admin); + List findByTimestampAndAdmin(LocalDate timestamp, String admin); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 327b8331..aaeef75a 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -23,22 +23,14 @@ import static java.time.LocalDateTime.now; import static java.util.stream.Collectors.toList; -import static org.springframework.data.domain.PageRequest.of; -import static org.springframework.data.domain.Sort.Direction.ASC; -import static org.springframework.data.domain.Sort.Direction.DESC; -import static org.springframework.data.domain.Sort.by; -import java.time.LocalDateTime; -import java.util.HashMap; +import java.time.LocalDate; import java.util.List; -import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRequestDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionResponseDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @@ -73,30 +65,11 @@ public void removeExceptionLog(final String gmcId) { } /** - * Get exception log. - * - * @param requestDto request for getting exception log - */ - public ExceptionResponseDto getExceptionLog(final ExceptionRequestDto requestDto) { - final var direction = "asc".equalsIgnoreCase(requestDto.getSortOrder()) ? ASC : DESC; - final var pageableAndSortable = of(requestDto.getPageNumber(), 20, - by(direction, requestDto.getSortColumn())); - - final var exceptionLogPage = repository.findAll(pageableAndSortable); - final var exceptionLogs = exceptionLogPage.get().collect(toList()); - return ExceptionResponseDto.builder() - .totalPages(exceptionLogPage.getTotalPages()) - .totalResults(exceptionLogPage.getTotalElements()) - .exceptionRecord(buildExceptionRecords(exceptionLogs)) - .build(); - } - - /** - * Get today's exception log + * Get today's exception logs for a specific admin * @param admin request by date today */ public List getExceptionLogs(String admin){ - LocalDateTime today = LocalDateTime.now(); + LocalDate today = LocalDate.now(); final var todaysExceptions = repository.findByTimestampAndAdmin(today, admin); return todaysExceptions.stream().map(exceptionLog -> { return ExceptionRecordDto.builder() @@ -107,25 +80,4 @@ public List getExceptionLogs(String admin){ .build(); }).collect(toList()); } - - /** - * Get exception log hashmap. - */ - public Map getExceptionsMap() { - Map exceptionsMap = new HashMap<>(); - List exceptionLogList = repository.findAll(); - for (ExceptionLog exceptionLog: exceptionLogList) { - exceptionsMap.put(exceptionLog.getGmcId(), exceptionLog.getErrorMessage()); - } - return exceptionsMap; - } - - private List buildExceptionRecords(final List exceptionLogs) { - return exceptionLogs.stream().map(exception -> { - return ExceptionRecordDto.builder() - .gmcId(exception.getGmcId()) - .exceptionMessage(exception.getErrorMessage()) - .build(); - }).collect(toList()); - } } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index f9275628..0cc5e8dc 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -141,6 +141,7 @@ public void setup() { requestTime = LocalDateTime.now().minusDays(-1); responseCode = faker.number().digits(5); + admin = faker.internet().emailAddress(); personId1 = (long) faker.hashCode(); personId2 = (long) faker.hashCode(); gmcRef1 = faker.number().digits(8); diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index 3ad89dfc..49e43882 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -23,29 +23,26 @@ import static java.util.List.of; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.springframework.data.domain.Sort.Direction.ASC; -import static org.springframework.data.domain.Sort.by; import com.github.javafaker.Faker; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; -import java.util.stream.Stream; -import org.joda.time.DateTime; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRequestDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @@ -60,12 +57,6 @@ class ExceptionServiceTest { @Mock private ExceptionRepository repository; - @Mock - private Page page; - - @Mock - private ExceptionLog exceptionLog; - private String gmcId, gmcId2; private String responseCode; private LocalDateTime localDateTime; @@ -73,6 +64,8 @@ class ExceptionServiceTest { private String admin; private String exceptionReason1, exceptionReason2; private LocalDateTime dateTime; + @Captor + private ArgumentCaptor localDateCaptor; @BeforeEach public void setup() { @@ -93,29 +86,12 @@ void shouldCreateExceptionLog() { verify(repository).save(any(ExceptionLog.class)); } - @Test - void shouldGetExceptionLog() { - final var exceptionRequestDto = ExceptionRequestDto.builder().pageNumber(1) - .sortOrder("asc").sortColumn("gmcId").build(); - final var pageableAndSortable = PageRequest.of(1, 20, by(ASC, "gmcId")); - when(repository.findAll(pageableAndSortable)).thenReturn(page); - when(page.get()).thenReturn(Stream.of(exceptionLog)); - final var exceptionResponseDto = exceptionService.getExceptionLog(exceptionRequestDto); - assertThat(exceptionResponseDto.getExceptionRecord(), hasSize(1)); - } - - @Test - void shouldReturnLogsMap() { - when(repository.findAll()).thenReturn(exceptionLogList); - final var result = exceptionService.getExceptionsMap(); - assertThat(result.get(gmcId), is(responseCode)); - } - @Test void shouldReturnExceptionLogs() { - when(repository.findByTimestampAndAdmin(dateTime, admin)).thenReturn(buildExceptionLogList()); + when(repository.findByTimestampAndAdmin(localDateCaptor.capture(), eq(admin))).thenReturn(buildExceptionLogList()); final var result = exceptionService.getExceptionLogs(admin); + assertThat(localDateCaptor.getValue(), is(LocalDate.now())); //request is for today's logs assertThat(result.get(0).getGmcId(), is(gmcId)); } From 7333c8ee193a1b5806d7eb2cab9c8827553c7c65 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Tue, 17 Oct 2023 13:05:52 +0100 Subject: [PATCH 04/13] refactor: use localdateTime for search and consistency, use between mongorepository method TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../repository/ExceptionRepository.java | 5 ++--- .../connection/service/ExceptionService.java | 8 ++++++-- .../service/ExceptionServiceTest.java | 17 +++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java index 84b8784c..41598f78 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java @@ -21,7 +21,7 @@ package uk.nhs.hee.tis.revalidation.connection.repository; -import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; @@ -29,6 +29,5 @@ @Repository public interface ExceptionRepository extends MongoRepository { - - List findByTimestampAndAdmin(LocalDate timestamp, String admin); + List findByAdminAndTimestampBetween(final String admin, final LocalDateTime start, final LocalDateTime end); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index aaeef75a..1905d465 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -25,6 +25,7 @@ import static java.util.stream.Collectors.toList; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -69,8 +70,10 @@ public void removeExceptionLog(final String gmcId) { * @param admin request by date today */ public List getExceptionLogs(String admin){ - LocalDate today = LocalDate.now(); - final var todaysExceptions = repository.findByTimestampAndAdmin(today, admin); + LocalDateTime today = LocalDate.now().atStartOfDay(); + LocalDateTime tomorrow = today.plusDays(1); + + final var todaysExceptions = repository.findByAdminAndTimestampBetween(admin, today, tomorrow); return todaysExceptions.stream().map(exceptionLog -> { return ExceptionRecordDto.builder() .gmcId(exceptionLog.getGmcId()) @@ -80,4 +83,5 @@ public List getExceptionLogs(String admin){ .build(); }).collect(toList()); } + } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index 49e43882..a01f914e 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -23,9 +23,10 @@ import static java.util.List.of; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -37,12 +38,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.data.domain.Page; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @@ -64,8 +62,6 @@ class ExceptionServiceTest { private String admin; private String exceptionReason1, exceptionReason2; private LocalDateTime dateTime; - @Captor - private ArgumentCaptor localDateCaptor; @BeforeEach public void setup() { @@ -88,11 +84,16 @@ void shouldCreateExceptionLog() { @Test void shouldReturnExceptionLogs() { - when(repository.findByTimestampAndAdmin(localDateCaptor.capture(), eq(admin))).thenReturn(buildExceptionLogList()); + LocalDateTime todayStart = LocalDate.now().atStartOfDay(); + LocalDateTime tomorrowStart = todayStart.plusDays(1); + + when(repository.findByAdminAndTimestampBetween(admin, todayStart, tomorrowStart)).thenReturn(buildExceptionLogList()); final var result = exceptionService.getExceptionLogs(admin); - assertThat(localDateCaptor.getValue(), is(LocalDate.now())); //request is for today's logs assertThat(result.get(0).getGmcId(), is(gmcId)); + assertThat(result.get(0).getAdmin(), is(admin)); + assertThat(result.get(0).getTimestamp(), greaterThan(todayStart)); + assertThat(result.get(0).getTimestamp(), lessThan(tomorrowStart)); } private void initializeExceptionLogList() { From 4d39a901feac05cbba715753a9165b4e8230d89c Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Tue, 17 Oct 2023 14:55:32 +0100 Subject: [PATCH 05/13] fix: fixed autoincrement id so not bypassing spring magic, some general tidying up TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../controller/ConnectionController.java | 6 +++--- ...tionRecordDto.java => ExceptionLogDto.java} | 8 ++++++-- .../connection/dto/ExceptionResponseDto.java | 2 +- .../connection/entity/ExceptionLog.java | 8 +------- .../connection/service/ConnectionService.java | 2 -- .../connection/service/ExceptionService.java | 18 ++++++++++-------- src/main/resources/application.yml | 2 +- .../controller/ConnectionControllerTest.java | 17 ++++++++--------- 8 files changed, 30 insertions(+), 33 deletions(-) rename src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/{ExceptionRecordDto.java => ExceptionLogDto.java} (89%) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index dba02449..ff6b01e9 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -45,7 +45,7 @@ import org.springframework.web.bind.annotation.RestController; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.exception.ConnectionQueryException; @@ -315,10 +315,10 @@ public ResponseEntity getSummaryDisconnected( /** * GET /exceptions/today : get list of exceptions. * - * @return the list of ExceptionRecordDto + * @return the list of ExceptionLogDto */ @GetMapping("/exceptions/today/{admin}") - public ResponseEntity> getListOfExceptions(@PathVariable("admin") final String admin) { + public ResponseEntity> getListOfExceptions(@PathVariable("admin") final String admin) { log.info("Received request to fetch exceptions for admin: {}", admin); final var exceptions = exceptionService.getExceptionLogs(admin); diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionLogDto.java similarity index 89% rename from src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java rename to src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionLogDto.java index 8416472f..e60dbc00 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionRecordDto.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionLogDto.java @@ -22,16 +22,20 @@ package uk.nhs.hee.tis.revalidation.connection.dto; import java.time.LocalDateTime; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; @Data @Builder -public class ExceptionRecordDto { +@AllArgsConstructor +@NoArgsConstructor +public class ExceptionLogDto { private String gmcId; - private String exceptionMessage; + private String errorMessage; private LocalDateTime timestamp; private String admin; } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionResponseDto.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionResponseDto.java index 3728fdce..105114f8 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionResponseDto.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/dto/ExceptionResponseDto.java @@ -32,5 +32,5 @@ public class ExceptionResponseDto { private long countTotal; private long totalPages; private long totalResults; - private List exceptionRecord; + private List exceptionRecord; } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java index 82e43a65..ff8751f9 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/entity/ExceptionLog.java @@ -22,9 +22,6 @@ package uk.nhs.hee.tis.revalidation.connection.entity; import java.time.LocalDateTime; -import javax.persistence.Column; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -38,11 +35,8 @@ @Builder @Document(collection = "exceptionLogs") public class ExceptionLog { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id") - private int _id; + private String id; private String gmcId; private String errorMessage; private LocalDateTime timestamp; diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ConnectionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ConnectionService.java index be8155e5..17534447 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ConnectionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ConnectionService.java @@ -31,8 +31,6 @@ import static uk.nhs.hee.tis.revalidation.connection.entity.GmcResponseCode.fromCode; import java.time.LocalDate; -import java.time.ZoneId; -import java.util.Date; import java.util.List; import java.util.Optional; import java.util.UUID; diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 1905d465..ede8b45d 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -31,7 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @@ -47,10 +47,11 @@ public class ExceptionService { * Create exception log. * * @param gmcId gmcId of trainees where there are some issue when updating connection - * @param exceptionMessage response code that response from gmc + * @param errorMessage response code that response from gmc */ - public void createExceptionLog(final String gmcId, final String exceptionMessage, String admin) { - final var exceptionLog = ExceptionLog.builder().gmcId(gmcId).errorMessage(exceptionMessage) + public void createExceptionLog(final String gmcId, final String errorMessage, String admin) { + + final var exceptionLog = ExceptionLog.builder().gmcId(gmcId).errorMessage(errorMessage) .timestamp(now()).admin(admin).build(); repository.save(exceptionLog); @@ -67,17 +68,18 @@ public void removeExceptionLog(final String gmcId) { /** * Get today's exception logs for a specific admin - * @param admin request by date today + * + * @param admin request by date today */ - public List getExceptionLogs(String admin){ + public List getExceptionLogs(String admin) { LocalDateTime today = LocalDate.now().atStartOfDay(); LocalDateTime tomorrow = today.plusDays(1); final var todaysExceptions = repository.findByAdminAndTimestampBetween(admin, today, tomorrow); return todaysExceptions.stream().map(exceptionLog -> { - return ExceptionRecordDto.builder() + return ExceptionLogDto.builder() .gmcId(exceptionLog.getGmcId()) - .exceptionMessage(exceptionLog.getErrorMessage()) + .errorMessage(exceptionLog.getErrorMessage()) .timestamp(exceptionLog.getTimestamp()) .admin(exceptionLog.getAdmin()) .build(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 10d06727..21c7bba4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -17,7 +17,7 @@ spring: uris: ${ES_URLS:http://localhost:9200} data: mongodb: - uri: mongodb://${MONGODB_USERNAME:root}:${MONGODB_PASSWORD:password}@${MONGODB_HOST:localhost}:${MONGODB_PORT:27017}/${MONGODB_DATABASE:revalidation}?authSource=admin&authMechanism=SCRAM-SHA-1&replicaSet=rs0&readPreference=secondaryPreferred + uri: mongodb://${MONGODB_USERNAME:root}:${MONGODB_PASSWORD:password}@${MONGODB_HOST:localhost}:${MONGODB_PORT:27017}/${MONGODB_DATABASE:revalidation}?authSource=admin&authMechanism=SCRAM-SHA-1 server: port: 8090 diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index 0cc5e8dc..6d14a98c 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -53,7 +53,7 @@ import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionInfoDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; import uk.nhs.hee.tis.revalidation.connection.dto.DoctorInfoDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionRecordDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.entity.ConnectionRequestType; @@ -429,7 +429,7 @@ void shouldReturnDisconnectedTraineeDoctorsInformationDesc() throws Exception { @Test - void shouldReturnAllExceptionsForAnAdmin() throws Exception { + void shouldReturnAllTodayExceptionsForAnAdmin() throws Exception { final var exceptionRecordDtoList = buildExceptionRecordDtoList(); when(exceptionService.getExceptionLogs(admin)).thenReturn(exceptionRecordDtoList); @@ -437,8 +437,7 @@ void shouldReturnAllExceptionsForAnAdmin() throws Exception { mockMvc.perform(get("/api/connections/exceptions/today/{admin}", admin)) .andExpect(status().isOk()) .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId))) - .andExpect(jsonPath("$.[*].exceptionMessage").value(hasItem(exceptionReason1))) - //.andExpect(jsonPath("$.[*].timestamp").value(hasItem("year=2023"))) + .andExpect(jsonPath("$.[*].errorMessage").value(hasItem(exceptionReason1))) .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))); } @@ -503,13 +502,13 @@ private List buildDoctorsForDbList() { return of(doctor1, doctor2); } - private List buildExceptionRecordDtoList() { - final var record1 = ExceptionRecordDto.builder() - .gmcId(gmcId).exceptionMessage(exceptionReason1) + private List buildExceptionRecordDtoList() { + final var record1 = ExceptionLogDto.builder() + .gmcId(gmcId).errorMessage(exceptionReason1) .timestamp(today).admin(admin) .build(); - final var record2 = ExceptionRecordDto.builder() - .gmcId(gmcRef2).exceptionMessage(exceptionReason2) + final var record2 = ExceptionLogDto.builder() + .gmcId(gmcRef2).errorMessage(exceptionReason2) .timestamp(today).admin(admin) .build(); return of(record1, record2); From 90c331cc3a2c5c444712350802cf1dda69238af8 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Tue, 17 Oct 2023 15:08:02 +0100 Subject: [PATCH 06/13] refactor: use request body param instead of url param TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../connection/controller/ConnectionController.java | 8 +++++--- .../connection/controller/ConnectionControllerTest.java | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index ff6b01e9..3c40fd9e 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -70,6 +70,7 @@ public class ConnectionController { private static final String SEARCH_QUERY = "searchQuery"; private static final String EMPTY_STRING = ""; private static final String PROGRAMME_NAME = "programmeName"; + private static final String ADMIN = "admin"; @Autowired private ConnectionService connectionService; @@ -317,11 +318,12 @@ public ResponseEntity getSummaryDisconnected( * * @return the list of ExceptionLogDto */ - @GetMapping("/exceptions/today/{admin}") - public ResponseEntity> getListOfExceptions(@PathVariable("admin") final String admin) { + @GetMapping("/exceptions/today") + public ResponseEntity> getListOfExceptions( + @RequestParam(name = ADMIN) final String admin) { log.info("Received request to fetch exceptions for admin: {}", admin); - final var exceptions = exceptionService.getExceptionLogs(admin); + final var exceptions = exceptionService.getExceptionLogs(admin); return ResponseEntity.ok().body(exceptions); } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index 6d14a98c..f0aa7d89 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -434,7 +434,8 @@ void shouldReturnAllTodayExceptionsForAnAdmin() throws Exception { when(exceptionService.getExceptionLogs(admin)).thenReturn(exceptionRecordDtoList); - mockMvc.perform(get("/api/connections/exceptions/today/{admin}", admin)) + mockMvc.perform(get("/api/connections/exceptions/today") + .param("admin", admin)) .andExpect(status().isOk()) .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId))) .andExpect(jsonPath("$.[*].errorMessage").value(hasItem(exceptionReason1))) From fb9f4fac78d07c621882c47a76c05ce161bfe7c2 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Wed, 18 Oct 2023 09:27:50 +0100 Subject: [PATCH 07/13] fix: restore mongodb configuration TIS21-5172:Add a GET endpoint to get all recent exceptions --- src/main/resources/application.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 21c7bba4..10d06727 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -17,7 +17,7 @@ spring: uris: ${ES_URLS:http://localhost:9200} data: mongodb: - uri: mongodb://${MONGODB_USERNAME:root}:${MONGODB_PASSWORD:password}@${MONGODB_HOST:localhost}:${MONGODB_PORT:27017}/${MONGODB_DATABASE:revalidation}?authSource=admin&authMechanism=SCRAM-SHA-1 + uri: mongodb://${MONGODB_USERNAME:root}:${MONGODB_PASSWORD:password}@${MONGODB_HOST:localhost}:${MONGODB_PORT:27017}/${MONGODB_DATABASE:revalidation}?authSource=admin&authMechanism=SCRAM-SHA-1&replicaSet=rs0&readPreference=secondaryPreferred server: port: 8090 From 8dce541c2877153da59ead8f5e1135c8479715d8 Mon Sep 17 00:00:00 2001 From: jayanta2018 Date: Wed, 18 Oct 2023 15:46:27 +0100 Subject: [PATCH 08/13] refactor: replace builder by mapper --- .../connection/mapper/ConnectionInfoMapper.java | 5 +++++ .../connection/service/ExceptionService.java | 15 ++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java index 86f6b74b..b32f84e8 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java @@ -25,9 +25,11 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionInfoDto; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.CurrentConnectionsView; import uk.nhs.hee.tis.revalidation.connection.entity.DisconnectedView; import uk.nhs.hee.tis.revalidation.connection.entity.DiscrepanciesView; +import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.entity.MasterDoctorView; @Mapper(componentModel = "spring") @@ -73,4 +75,7 @@ List currentConnectionsToConnectionInfoDtos( @Mapping(target = "membershipEndDate", source = "programmeMembershipEndDate") @Mapping(target = "id", ignore = true) MasterDoctorView dtoToMaster(ConnectionInfoDto connectionInfoDto); + + @Mapping(target = "", source = "id", ignore = true) + List exceptionLogsToExceptionLogDtos(List exceptionLog); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index ede8b45d..99e91600 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -22,17 +22,18 @@ package uk.nhs.hee.tis.revalidation.connection.service; import static java.time.LocalDateTime.now; -import static java.util.stream.Collectors.toList; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import lombok.extern.slf4j.Slf4j; +import org.mapstruct.factory.Mappers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; +import uk.nhs.hee.tis.revalidation.connection.mapper.ConnectionInfoMapper; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @Slf4j @@ -43,6 +44,9 @@ public class ExceptionService { @Autowired private ExceptionRepository repository; + private final ConnectionInfoMapper exceptionLogMapper = Mappers + .getMapper(ConnectionInfoMapper.class); + /** * Create exception log. * @@ -76,14 +80,7 @@ public List getExceptionLogs(String admin) { LocalDateTime tomorrow = today.plusDays(1); final var todaysExceptions = repository.findByAdminAndTimestampBetween(admin, today, tomorrow); - return todaysExceptions.stream().map(exceptionLog -> { - return ExceptionLogDto.builder() - .gmcId(exceptionLog.getGmcId()) - .errorMessage(exceptionLog.getErrorMessage()) - .timestamp(exceptionLog.getTimestamp()) - .admin(exceptionLog.getAdmin()) - .build(); - }).collect(toList()); + return exceptionLogMapper.exceptionLogsToExceptionLogDtos(todaysExceptions); } } From 43f110846da25963097b72aad123b609038f9f65 Mon Sep 17 00:00:00 2001 From: jayanta2018 Date: Thu, 19 Oct 2023 10:34:16 +0100 Subject: [PATCH 09/13] fix: fix sonar code smell --- .../connection/repository/ExceptionRepository.java | 4 +++- .../connection/service/ExceptionService.java | 2 +- .../connection/service/ExceptionServiceTest.java | 9 ++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java index 41598f78..1b1b245a 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/repository/ExceptionRepository.java @@ -29,5 +29,7 @@ @Repository public interface ExceptionRepository extends MongoRepository { - List findByAdminAndTimestampBetween(final String admin, final LocalDateTime start, final LocalDateTime end); + + List findByAdminAndTimestampBetween(final String admin, final LocalDateTime start, + final LocalDateTime end); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 99e91600..f73adb90 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -71,7 +71,7 @@ public void removeExceptionLog(final String gmcId) { } /** - * Get today's exception logs for a specific admin + * Get today's exception logs for a specific admin. * * @param admin request by date today */ diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index a01f914e..0c2c2052 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -55,12 +55,14 @@ class ExceptionServiceTest { @Mock private ExceptionRepository repository; - private String gmcId, gmcId2; + private String gmcId; + private String gmcId2; private String responseCode; private LocalDateTime localDateTime; private List exceptionLogList; private String admin; - private String exceptionReason1, exceptionReason2; + private String exceptionReason1; + private String exceptionReason2; private LocalDateTime dateTime; @BeforeEach @@ -87,7 +89,8 @@ void shouldReturnExceptionLogs() { LocalDateTime todayStart = LocalDate.now().atStartOfDay(); LocalDateTime tomorrowStart = todayStart.plusDays(1); - when(repository.findByAdminAndTimestampBetween(admin, todayStart, tomorrowStart)).thenReturn(buildExceptionLogList()); + when(repository.findByAdminAndTimestampBetween(admin, todayStart, tomorrowStart)) + .thenReturn(buildExceptionLogList()); final var result = exceptionService.getExceptionLogs(admin); assertThat(result.get(0).getGmcId(), is(gmcId)); From 99de07ad011fd2aafe8c85b11bb8212ab032ccae Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Thu, 19 Oct 2023 13:01:10 +0100 Subject: [PATCH 10/13] refactor: better naming, new controller class for exceptionLogs, dedicated mapper --- .../controller/ConnectionController.java | 19 +---- .../controller/ExceptionLogController.java | 40 ++++++++++ .../mapper/ConnectionInfoMapper.java | 2 - .../connection/mapper/ExceptionLogMapper.java | 15 ++++ .../connection/service/ExceptionService.java | 7 +- .../controller/ConnectionControllerTest.java | 38 +-------- .../ExceptionLogControllerTest.java | 78 +++++++++++++++++++ .../service/ExceptionServiceTest.java | 2 +- 8 files changed, 141 insertions(+), 60 deletions(-) create mode 100644 src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java create mode 100644 src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java create mode 100644 src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index 3c40fd9e..56b29bf0 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -45,7 +45,6 @@ import org.springframework.web.bind.annotation.RestController; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.exception.ConnectionQueryException; @@ -70,7 +69,7 @@ public class ConnectionController { private static final String SEARCH_QUERY = "searchQuery"; private static final String EMPTY_STRING = ""; private static final String PROGRAMME_NAME = "programmeName"; - private static final String ADMIN = "admin"; + @Autowired private ConnectionService connectionService; @@ -84,8 +83,6 @@ public class ConnectionController { @Autowired private DisconnectedElasticSearchService disconnectedElasticSearchService; - @Autowired - private ExceptionService exceptionService; /** * POST /connections/add : Add a new connection. @@ -313,18 +310,4 @@ public ResponseEntity getSummaryDisconnected( return ResponseEntity.ok(connectionSummaryDto); } - /** - * GET /exceptions/today : get list of exceptions. - * - * @return the list of ExceptionLogDto - */ - @GetMapping("/exceptions/today") - public ResponseEntity> getListOfExceptions( - @RequestParam(name = ADMIN) final String admin) { - - log.info("Received request to fetch exceptions for admin: {}", admin); - final var exceptions = exceptionService.getExceptionLogs(admin); - return ResponseEntity.ok().body(exceptions); - - } } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java new file mode 100644 index 00000000..b15bd5d6 --- /dev/null +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java @@ -0,0 +1,40 @@ +package uk.nhs.hee.tis.revalidation.connection.controller; + +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; +import uk.nhs.hee.tis.revalidation.connection.service.ExceptionService; + +@Slf4j +@RestController +@RequestMapping("/api/exceptionLog") +public class ExceptionLogController { + private ExceptionService exceptionService; + + public ExceptionLogController(ExceptionService exceptionService) { + this.exceptionService = exceptionService; + } + + private static final String ADMIN = "admin"; + + /** + * GET /exceptions/today : get list of exceptions. + * + * @return the list of ExceptionLogDto + */ + @GetMapping("/today") + public ResponseEntity> getListOfConnectionExceptionsFromToday( + @RequestParam(name = ADMIN) final String admin) { + + log.info("Received request to fetch exceptions for admin: {}", admin); + final var exceptions = exceptionService.getConnectionExceptionLogsFromToday(admin); + return ResponseEntity.ok().body(exceptions); + + } + +} diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java index b32f84e8..f8c191e8 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java @@ -76,6 +76,4 @@ List currentConnectionsToConnectionInfoDtos( @Mapping(target = "id", ignore = true) MasterDoctorView dtoToMaster(ConnectionInfoDto connectionInfoDto); - @Mapping(target = "", source = "id", ignore = true) - List exceptionLogsToExceptionLogDtos(List exceptionLog); } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java new file mode 100644 index 00000000..84b4cfd7 --- /dev/null +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java @@ -0,0 +1,15 @@ +package uk.nhs.hee.tis.revalidation.connection.mapper; + +import java.util.List; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; +import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; + +@Mapper(componentModel = "spring") +public interface ExceptionLogMapper { + + @Mapping(target = "id", ignore = true) + List exceptionLogsToExceptionLogDtos(List exceptionLog); + +} diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index f73adb90..39af40ca 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -34,6 +34,7 @@ import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.mapper.ConnectionInfoMapper; +import uk.nhs.hee.tis.revalidation.connection.mapper.ExceptionLogMapper; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @Slf4j @@ -44,8 +45,8 @@ public class ExceptionService { @Autowired private ExceptionRepository repository; - private final ConnectionInfoMapper exceptionLogMapper = Mappers - .getMapper(ConnectionInfoMapper.class); + private final ExceptionLogMapper exceptionLogMapper = Mappers + .getMapper(ExceptionLogMapper.class); /** * Create exception log. @@ -75,7 +76,7 @@ public void removeExceptionLog(final String gmcId) { * * @param admin request by date today */ - public List getExceptionLogs(String admin) { + public List getConnectionExceptionLogsFromToday(String admin) { LocalDateTime today = LocalDate.now().atStartOfDay(); LocalDateTime tomorrow = today.plusDays(1); diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index f0aa7d89..7f201047 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -53,7 +53,6 @@ import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionInfoDto; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionSummaryDto; import uk.nhs.hee.tis.revalidation.connection.dto.DoctorInfoDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionDto; import uk.nhs.hee.tis.revalidation.connection.dto.UpdateConnectionResponseDto; import uk.nhs.hee.tis.revalidation.connection.entity.ConnectionRequestType; @@ -61,7 +60,6 @@ import uk.nhs.hee.tis.revalidation.connection.service.ConnectionService; import uk.nhs.hee.tis.revalidation.connection.service.DisconnectedElasticSearchService; import uk.nhs.hee.tis.revalidation.connection.service.DiscrepanciesElasticSearchService; -import uk.nhs.hee.tis.revalidation.connection.service.ExceptionService; @WebMvcTest(ConnectionController.class) @@ -89,8 +87,6 @@ class ConnectionControllerTest { private ConnectedElasticSearchService connectedElasticSearchService; @MockBean private DisconnectedElasticSearchService disconnectedElasticSearchService; - @MockBean - private ExceptionService exceptionService; private String changeReason; private String designatedBodyCode; @@ -122,8 +118,7 @@ class ConnectionControllerTest { private String programmeOwner2; private String exceptionReason1; private String exceptionReason2; - private String admin; - private LocalDateTime today; + @BeforeEach public void setup() { @@ -141,7 +136,6 @@ public void setup() { requestTime = LocalDateTime.now().minusDays(-1); responseCode = faker.number().digits(5); - admin = faker.internet().emailAddress(); personId1 = (long) faker.hashCode(); personId2 = (long) faker.hashCode(); gmcRef1 = faker.number().digits(8); @@ -160,7 +154,6 @@ public void setup() { programmeOwner2 = faker.lorem().characters(20); exceptionReason1 = faker.lorem().characters(20); exceptionReason2 = faker.lorem().characters(20); - today = LocalDateTime.now(); } @Test @@ -426,23 +419,6 @@ void shouldReturnDisconnectedTraineeDoctorsInformationDesc() throws Exception { .andExpect( jsonPath("$.connections.[*].tcsPersonId").value(hasItem(personId1.intValue()))); } - - - @Test - void shouldReturnAllTodayExceptionsForAnAdmin() throws Exception { - final var exceptionRecordDtoList = buildExceptionRecordDtoList(); - - when(exceptionService.getExceptionLogs(admin)).thenReturn(exceptionRecordDtoList); - - mockMvc.perform(get("/api/connections/exceptions/today") - .param("admin", admin)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId))) - .andExpect(jsonPath("$.[*].errorMessage").value(hasItem(exceptionReason1))) - .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))); - - } - private ConnectionDto prepareConnectionDto() { final ConnectionDto connectionDto = new ConnectionDto(); final ConnectionHistoryDto connectionHistory = ConnectionHistoryDto.builder() @@ -503,15 +479,5 @@ private List buildDoctorsForDbList() { return of(doctor1, doctor2); } - private List buildExceptionRecordDtoList() { - final var record1 = ExceptionLogDto.builder() - .gmcId(gmcId).errorMessage(exceptionReason1) - .timestamp(today).admin(admin) - .build(); - final var record2 = ExceptionLogDto.builder() - .gmcId(gmcRef2).errorMessage(exceptionReason2) - .timestamp(today).admin(admin) - .build(); - return of(record1, record2); - } + } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java new file mode 100644 index 00000000..5c2f45ba --- /dev/null +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java @@ -0,0 +1,78 @@ +package uk.nhs.hee.tis.revalidation.connection.controller; + +import static java.util.List.of; +import static org.hamcrest.Matchers.hasItem; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.github.javafaker.Faker; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; +import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; +import uk.nhs.hee.tis.revalidation.connection.service.ExceptionService; + +@WebMvcTest(ExceptionLogController.class) +class ExceptionLogControllerTest { + private final Faker faker = new Faker(); + private String admin; + private LocalDateTime today; + private String gmcId1; + private String gmcId2; + private String exceptionReason1; + private String exceptionReason2; + @Autowired + private MockMvc mockMvc; + @MockBean + private ExceptionService exceptionService; + @InjectMocks + private ExceptionLogController exceptionLogController; + + @BeforeEach + public void setup() { + + admin = faker.internet().emailAddress(); + today = LocalDateTime.now(); + gmcId1 = faker.number().digits(8); + gmcId2 = faker.number().digits(8); + exceptionReason1 = faker.lorem().characters(20); + exceptionReason2 = faker.lorem().characters(20); + } + + @Test + void shouldReturnAllExceptionsFromTodayForAnAdmin() throws Exception { + final var exceptionRecordDtoList = buildExceptionRecordDtoList(); + + when(exceptionService.getConnectionExceptionLogsFromToday(admin)).thenReturn(exceptionRecordDtoList); + + mockMvc.perform(get("/api/exceptionLog/today") + .param("admin", admin)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId1))) + .andExpect(jsonPath("$.[*].errorMessage").value(hasItem(exceptionReason1))) + .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))) + .andExpect(jsonPath("$.[*].gmcId").value(hasItem(gmcId2))) + .andExpect(jsonPath("$.[*].errorMessage").value(hasItem(exceptionReason2))) + .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))); + + } + private List buildExceptionRecordDtoList() { + final var record1 = ExceptionLogDto.builder() + .gmcId(gmcId1).errorMessage(exceptionReason1) + .timestamp(today).admin(admin) + .build(); + final var record2 = ExceptionLogDto.builder() + .gmcId(gmcId2).errorMessage(exceptionReason2) + .timestamp(today).admin(admin) + .build(); + return of(record1, record2); + } +} diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index 0c2c2052..1637985f 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -92,7 +92,7 @@ void shouldReturnExceptionLogs() { when(repository.findByAdminAndTimestampBetween(admin, todayStart, tomorrowStart)) .thenReturn(buildExceptionLogList()); - final var result = exceptionService.getExceptionLogs(admin); + final var result = exceptionService.getConnectionExceptionLogsFromToday(admin); assertThat(result.get(0).getGmcId(), is(gmcId)); assertThat(result.get(0).getAdmin(), is(admin)); assertThat(result.get(0).getTimestamp(), greaterThan(todayStart)); From 2655738af51fbe72c80aa5cb42e19370b10bc231 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Thu, 19 Oct 2023 13:08:43 +0100 Subject: [PATCH 11/13] chore: code smells TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../revalidation/connection/mapper/ConnectionInfoMapper.java | 2 -- .../revalidation/connection/service/ExceptionService.java | 1 - .../connection/controller/ConnectionControllerTest.java | 1 + .../connection/controller/ExceptionLogControllerTest.java | 5 ++++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java index f8c191e8..071a0d35 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java @@ -25,11 +25,9 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; import uk.nhs.hee.tis.revalidation.connection.dto.ConnectionInfoDto; -import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.CurrentConnectionsView; import uk.nhs.hee.tis.revalidation.connection.entity.DisconnectedView; import uk.nhs.hee.tis.revalidation.connection.entity.DiscrepanciesView; -import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; import uk.nhs.hee.tis.revalidation.connection.entity.MasterDoctorView; @Mapper(componentModel = "spring") diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 39af40ca..94b716bb 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -33,7 +33,6 @@ import org.springframework.transaction.annotation.Transactional; import uk.nhs.hee.tis.revalidation.connection.dto.ExceptionLogDto; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; -import uk.nhs.hee.tis.revalidation.connection.mapper.ConnectionInfoMapper; import uk.nhs.hee.tis.revalidation.connection.mapper.ExceptionLogMapper; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index 7f201047..c9eaf1c8 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -419,6 +419,7 @@ void shouldReturnDisconnectedTraineeDoctorsInformationDesc() throws Exception { .andExpect( jsonPath("$.connections.[*].tcsPersonId").value(hasItem(personId1.intValue()))); } + private ConnectionDto prepareConnectionDto() { final ConnectionDto connectionDto = new ConnectionDto(); final ConnectionHistoryDto connectionHistory = ConnectionHistoryDto.builder() diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java index 5c2f45ba..794c12f5 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogControllerTest.java @@ -22,6 +22,7 @@ @WebMvcTest(ExceptionLogController.class) class ExceptionLogControllerTest { + private final Faker faker = new Faker(); private String admin; private LocalDateTime today; @@ -51,7 +52,8 @@ public void setup() { void shouldReturnAllExceptionsFromTodayForAnAdmin() throws Exception { final var exceptionRecordDtoList = buildExceptionRecordDtoList(); - when(exceptionService.getConnectionExceptionLogsFromToday(admin)).thenReturn(exceptionRecordDtoList); + when(exceptionService.getConnectionExceptionLogsFromToday(admin)).thenReturn( + exceptionRecordDtoList); mockMvc.perform(get("/api/exceptionLog/today") .param("admin", admin)) @@ -64,6 +66,7 @@ void shouldReturnAllExceptionsFromTodayForAnAdmin() throws Exception { .andExpect(jsonPath("$.[*].admin").value(hasItem(admin))); } + private List buildExceptionRecordDtoList() { final var record1 = ExceptionLogDto.builder() .gmcId(gmcId1).errorMessage(exceptionReason1) From 8204a0775a9e516b84a380b1acbf62635788345f Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Thu, 19 Oct 2023 14:41:55 +0100 Subject: [PATCH 12/13] chore: cleanup blank lines, method definitions and mapper injection TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../connection/controller/ConnectionController.java | 3 --- .../connection/controller/ExceptionLogController.java | 4 +--- .../connection/mapper/ConnectionInfoMapper.java | 1 - .../revalidation/connection/mapper/ExceptionLogMapper.java | 1 - .../revalidation/connection/service/ExceptionService.java | 7 ++++--- .../connection/controller/ConnectionControllerTest.java | 2 -- .../connection/service/ExceptionServiceTest.java | 6 ++++++ 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java index 56b29bf0..341fa82b 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionController.java @@ -70,7 +70,6 @@ public class ConnectionController { private static final String EMPTY_STRING = ""; private static final String PROGRAMME_NAME = "programmeName"; - @Autowired private ConnectionService connectionService; @@ -83,7 +82,6 @@ public class ConnectionController { @Autowired private DisconnectedElasticSearchService disconnectedElasticSearchService; - /** * POST /connections/add : Add a new connection. * @@ -309,5 +307,4 @@ public ResponseEntity getSummaryDisconnected( return ResponseEntity.ok(connectionSummaryDto); } - } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java index b15bd5d6..be45a658 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/controller/ExceptionLogController.java @@ -23,7 +23,7 @@ public ExceptionLogController(ExceptionService exceptionService) { private static final String ADMIN = "admin"; /** - * GET /exceptions/today : get list of exceptions. + * GET /exceptions/today : get list of exceptions from today for an admin. * * @return the list of ExceptionLogDto */ @@ -34,7 +34,5 @@ public ResponseEntity> getListOfConnectionExceptionsFromTo log.info("Received request to fetch exceptions for admin: {}", admin); final var exceptions = exceptionService.getConnectionExceptionLogsFromToday(admin); return ResponseEntity.ok().body(exceptions); - } - } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java index 071a0d35..86f6b74b 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ConnectionInfoMapper.java @@ -73,5 +73,4 @@ List currentConnectionsToConnectionInfoDtos( @Mapping(target = "membershipEndDate", source = "programmeMembershipEndDate") @Mapping(target = "id", ignore = true) MasterDoctorView dtoToMaster(ConnectionInfoDto connectionInfoDto); - } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java index 84b4cfd7..b53c0e83 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/mapper/ExceptionLogMapper.java @@ -11,5 +11,4 @@ public interface ExceptionLogMapper { @Mapping(target = "id", ignore = true) List exceptionLogsToExceptionLogDtos(List exceptionLog); - } diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 94b716bb..416a3f48 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -44,14 +44,15 @@ public class ExceptionService { @Autowired private ExceptionRepository repository; - private final ExceptionLogMapper exceptionLogMapper = Mappers - .getMapper(ExceptionLogMapper.class); + @Autowired + private ExceptionLogMapper exceptionLogMapper; /** * Create exception log. * * @param gmcId gmcId of trainees where there are some issue when updating connection * @param errorMessage response code that response from gmc + * @param admin the admin that generated the exception log */ public void createExceptionLog(final String gmcId, final String errorMessage, String admin) { @@ -73,7 +74,7 @@ public void removeExceptionLog(final String gmcId) { /** * Get today's exception logs for a specific admin. * - * @param admin request by date today + * @param admin the admin that generated the exception log */ public List getConnectionExceptionLogsFromToday(String admin) { LocalDateTime today = LocalDate.now().atStartOfDay(); diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java index c9eaf1c8..11e2591b 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/controller/ConnectionControllerTest.java @@ -479,6 +479,4 @@ private List buildDoctorsForDbList() { .build(); return of(doctor1, doctor2); } - - } diff --git a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java index 1637985f..03a1553e 100644 --- a/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java +++ b/src/test/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionServiceTest.java @@ -40,8 +40,11 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import uk.nhs.hee.tis.revalidation.connection.entity.ExceptionLog; +import uk.nhs.hee.tis.revalidation.connection.mapper.ExceptionLogMapper; +import uk.nhs.hee.tis.revalidation.connection.mapper.ExceptionLogMapperImpl; import uk.nhs.hee.tis.revalidation.connection.repository.ExceptionRepository; @ExtendWith(MockitoExtension.class) @@ -55,6 +58,9 @@ class ExceptionServiceTest { @Mock private ExceptionRepository repository; + @Spy + private ExceptionLogMapper mapper = new ExceptionLogMapperImpl(); + private String gmcId; private String gmcId2; private String responseCode; From b132d97efe5016aab14aaa02c9d754c73680b537 Mon Sep 17 00:00:00 2001 From: Cai Willis Date: Thu, 19 Oct 2023 14:48:52 +0100 Subject: [PATCH 13/13] chore: code smells TIS21-5172:Add a GET endpoint to get all recent exceptions --- .../tis/revalidation/connection/service/ExceptionService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java index 416a3f48..ba9a24f9 100644 --- a/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java +++ b/src/main/java/uk/nhs/hee/tis/revalidation/connection/service/ExceptionService.java @@ -27,7 +27,6 @@ import java.time.LocalDateTime; import java.util.List; import lombok.extern.slf4j.Slf4j; -import org.mapstruct.factory.Mappers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;