diff --git a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenceControllerIntegrationTest.java similarity index 54% rename from src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerIntegrationTest.java rename to src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenceControllerIntegrationTest.java index 5f1016fdf..9f68a1684 100644 --- a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/OffenceControllerIntegrationTest.java @@ -9,10 +9,11 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; +import uk.gov.hmcts.opal.controllers.OffenceController; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.opal.OffenseService; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.service.opal.OffenceService; import static java.util.Collections.singletonList; import static org.mockito.ArgumentMatchers.any; @@ -24,75 +25,73 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest -@ContextConfiguration(classes = OffenseController.class) +@ContextConfiguration(classes = OffenceController.class) @ActiveProfiles({"integration"}) -class OffenseControllerIntegrationTest { +class OffenceControllerIntegrationTest { @Autowired MockMvc mockMvc; @MockBean - @Qualifier("offenseService") - OffenseService offenseService; + @Qualifier("offenceService") + OffenceService offenceService; @Test - void testGetOffenseById() throws Exception { - OffenseEntity offenseEntity = createOffenseEntity(); + void testGetOffenceById() throws Exception { + OffenceEntity offenceEntity = createOffenceEntity(); - when(offenseService.getOffense((short)1)).thenReturn(offenseEntity); + when(offenceService.getOffence((short)1)).thenReturn(offenceEntity); - mockMvc.perform(get("/api/offense/1")) + mockMvc.perform(get("/api/offence/1")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.offenseId").value(1)) + .andExpect(jsonPath("$.offenceId").value(1)) .andExpect(jsonPath("$.cjsCode").value("cjs-code")) - .andExpect(jsonPath("$.offenseTitle").value("Title of Offense")) - .andExpect(jsonPath("$.offenseTitleCy").value("Title of Offense CY")); + .andExpect(jsonPath("$.offenceTitle").value("Title of Offence")) + .andExpect(jsonPath("$.offenceTitleCy").value("Title of Offence CY")); } @Test - void testGetOffenseById_WhenOffenseDoesNotExist() throws Exception { - when(offenseService.getOffense((short)2)).thenReturn(null); + void testGetOffenceById_WhenOffenceDoesNotExist() throws Exception { + when(offenceService.getOffence((short)2)).thenReturn(null); - mockMvc.perform(get("/api/offense/2")) + mockMvc.perform(get("/api/offence/2")) .andExpect(status().isNoContent()); } @Test - void testPostOffensesSearch() throws Exception { - OffenseEntity offenseEntity = createOffenseEntity(); + void testPostOffencesSearch() throws Exception { + OffenceEntity offenceEntity = createOffenceEntity(); - when(offenseService.searchOffenses(any(OffenseSearchDto.class))).thenReturn(singletonList(offenseEntity)); + when(offenceService.searchOffences(any(OffenceSearchDto.class))).thenReturn(singletonList(offenceEntity)); - mockMvc.perform(post("/api/offense/search") + mockMvc.perform(post("/api/offence/search") .contentType(MediaType.APPLICATION_JSON) .content("{\"criteria\":\"value\"}")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$[0].offenseId").value(1)) + .andExpect(jsonPath("$[0].offenceId").value(1)) .andExpect(jsonPath("$[0].cjsCode").value("cjs-code")) - .andExpect(jsonPath("$[0].offenseTitle").value("Title of Offense")) - .andExpect(jsonPath("$[0].offenseTitleCy").value("Title of Offense CY")); + .andExpect(jsonPath("$[0].offenceTitle").value("Title of Offence")) + .andExpect(jsonPath("$[0].offenceTitleCy").value("Title of Offence CY")); } @Test - void testPostOffensesSearch_WhenOffenseDoesNotExist() throws Exception { - // when(offenseService.getOffense(2L)).thenReturn(null); - - mockMvc.perform(post("/api/offense/search") + void testPostOffencesSearch_WhenOffenceDoesNotExist() throws Exception { + mockMvc.perform(post("/api/offence/search") .contentType(MediaType.APPLICATION_JSON) .content("{\"criteria\":\"2\"}")) .andExpect(status().isNoContent()); } - private OffenseEntity createOffenseEntity() { - return OffenseEntity.builder() - .offenseId((short)1) + private OffenceEntity createOffenceEntity() { + return OffenceEntity.builder() + .offenceId(1L) .cjsCode("cjs-code") .businessUnit(BusinessUnitEntity.builder().build()) - .offenseTitle("Title of Offense") - .offenseTitleCy("Title of Offense CY") + .offenceTitle("Title of Offence") + .offenceTitleCy("Title of Offence CY") .build(); } } diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/OffenceController.java b/src/main/java/uk/gov/hmcts/opal/controllers/OffenceController.java new file mode 100644 index 000000000..b9915b880 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/controllers/OffenceController.java @@ -0,0 +1,74 @@ +package uk.gov.hmcts.opal.controllers; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import uk.gov.hmcts.opal.dto.reference.OffenceReferenceDataResults; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.entity.projection.OffenceReferenceData; +import uk.gov.hmcts.opal.service.OffenceServiceInterface; +import uk.gov.hmcts.opal.service.opal.OffenceService; + +import java.util.List; +import java.util.Optional; + +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; + + +@RestController +@RequestMapping("/api/offence") +@Slf4j(topic = "OffenceController") +@Tag(name = "Offence Controller") +public class OffenceController { + + private final OffenceServiceInterface offenceService; + private final OffenceService opalOffenceService; + + public OffenceController(@Qualifier("offenceService") OffenceServiceInterface offenceService, + OffenceService opalOffenceService) { + this.offenceService = offenceService; + this.opalOffenceService = opalOffenceService; + } + + @GetMapping(value = "/{offenceId}") + @Operation(summary = "Returns the Offence for the given offenceId.") + public ResponseEntity getOffenceById(@PathVariable Long offenceId) { + + log.info(":GET:getOffenceById: offenceId: {}", offenceId); + + OffenceEntity response = offenceService.getOffence(offenceId); + + return buildResponse(response); + } + + @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE) + @Operation(summary = "Searches Offences based upon criteria in request body") + public ResponseEntity> postOffencesSearch(@RequestBody OffenceSearchDto criteria) { + log.info(":POST:postOffencesSearch: query: \n{}", criteria); + + List response = offenceService.searchOffences(criteria); + + return buildResponse(response); + } + + @GetMapping(value = {"/ref-data", "/ref-data/", "/ref-data/{filter}"}) + @Operation(summary = "Returns Offences as reference data with an option filter applied") + public ResponseEntity getOffenceRefData(@PathVariable Optional filter) { + log.info(":GET:getOffenceRefData: query: \n{}", filter); + + List refData = opalOffenceService.getReferenceData(filter); + + log.info(":GET:getOffenceRefData: offences reference data count: {}", refData.size()); + return ResponseEntity.ok(OffenceReferenceDataResults.builder().refData(refData).build()); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/develop/OffenseController.java b/src/main/java/uk/gov/hmcts/opal/controllers/develop/OffenseController.java deleted file mode 100644 index fe2ce9a51..000000000 --- a/src/main/java/uk/gov/hmcts/opal/controllers/develop/OffenseController.java +++ /dev/null @@ -1,58 +0,0 @@ -package uk.gov.hmcts.opal.controllers.develop; - -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.OffenseServiceInterface; - -import java.util.List; - -import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; - - -@RestController -@RequestMapping("/api/offense") -@Slf4j(topic = "OffenseController") -@Tag(name = "Offense Controller") -public class OffenseController { - - private final OffenseServiceInterface offenseService; - - public OffenseController(@Qualifier("offenseService") OffenseServiceInterface offenseService) { - this.offenseService = offenseService; - } - - @GetMapping(value = "/{offenseId}") - @Operation(summary = "Returns the Offense for the given offenseId.") - public ResponseEntity getOffenseById(@PathVariable Short offenseId) { - - log.info(":GET:getOffenseById: offenseId: {}", offenseId); - - OffenseEntity response = offenseService.getOffense(offenseId); - - return buildResponse(response); - } - - @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "Searches Offenses based upon criteria in request body") - public ResponseEntity> postOffensesSearch(@RequestBody OffenseSearchDto criteria) { - log.info(":POST:postOffensesSearch: query: \n{}", criteria); - - List response = offenseService.searchOffenses(criteria); - - return buildResponse(response); - } - - -} diff --git a/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java b/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java new file mode 100644 index 000000000..94ffb2151 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java @@ -0,0 +1,33 @@ +package uk.gov.hmcts.opal.dto.reference; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.entity.projection.OffenceReferenceData; + +import java.util.List; +import java.util.Optional; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class OffenceReferenceDataResults { + private Integer count; + private List refData; + + public static class OffenceReferenceDataResultsBuilder { + public OffenceReferenceDataResults.OffenceReferenceDataResultsBuilder refData( + List refData) { + this.refData = refData; + this.count(Optional.ofNullable(refData).map(List::size).orElse(0)); + return this; + } + + private OffenceReferenceDataResults.OffenceReferenceDataResultsBuilder count(Integer count) { + this.count = count; + return this; + } + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/dto/search/OffenseSearchDto.java b/src/main/java/uk/gov/hmcts/opal/dto/search/OffenceSearchDto.java similarity index 78% rename from src/main/java/uk/gov/hmcts/opal/dto/search/OffenseSearchDto.java rename to src/main/java/uk/gov/hmcts/opal/dto/search/OffenceSearchDto.java index d3c99f77b..bc2d40df0 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/search/OffenseSearchDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/search/OffenceSearchDto.java @@ -6,9 +6,9 @@ @Data @Builder -public class OffenseSearchDto implements ToJsonString { +public class OffenceSearchDto implements ToJsonString { - private String offenseId; + private String offenceId; private String cjsCode; private String businessUnitId; private String businessUnitName; diff --git a/src/main/java/uk/gov/hmcts/opal/entity/OffenseEntity.java b/src/main/java/uk/gov/hmcts/opal/entity/OffenceEntity.java similarity index 55% rename from src/main/java/uk/gov/hmcts/opal/entity/OffenseEntity.java rename to src/main/java/uk/gov/hmcts/opal/entity/OffenceEntity.java index ce438225e..526d872b4 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/OffenseEntity.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/OffenceEntity.java @@ -12,37 +12,51 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.SequenceGenerator; import jakarta.persistence.Table; +import jakarta.persistence.Temporal; +import jakarta.persistence.TemporalType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; + @Entity -@Table(name = "offenses") +@Table(name = "offences") @Data @NoArgsConstructor @AllArgsConstructor @Builder @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) -@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "offenseId") -public class OffenseEntity { +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "offenceId") +public class OffenceEntity { @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "offense_id_seq_generator") - @SequenceGenerator(name = "offense_id_seq_generator", sequenceName = "offense_id_seq", allocationSize = 1) - @Column(name = "offense_id", nullable = false) - private Short offenseId; + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "offence_id_seq_generator") + @SequenceGenerator(name = "offence_id_seq_generator", sequenceName = "offence_id_seq", allocationSize = 1) + @Column(name = "offence_id", nullable = false) + private Long offenceId; @Column(name = "cjs_code", length = 10, nullable = false) private String cjsCode; @ManyToOne - @JoinColumn(name = "business_unit_id", updatable = false) + @JoinColumn(name = "business_unit_id") private BusinessUnitEntity businessUnit; - @Column(name = "offense_title", length = 120, nullable = false) - private String offenseTitle; + @Column(name = "offence_title", length = 120) + private String offenceTitle; + + @Column(name = "offence_title_cy", length = 120) + private String offenceTitleCy; + + @Column(name = "date_used_to") + @Temporal(TemporalType.TIMESTAMP) + private LocalDateTime dateUsedTo; + + @Column(name = "offence_oas") + private String offenceOas; - @Column(name = "offense_title_cy", length = 120, nullable = false) - private String offenseTitleCy; + @Column(name = "offence_oas_cy") + private String offenceOasCy; } diff --git a/src/main/java/uk/gov/hmcts/opal/entity/projection/OffenceReferenceData.java b/src/main/java/uk/gov/hmcts/opal/entity/projection/OffenceReferenceData.java new file mode 100644 index 000000000..d86513d5b --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/entity/projection/OffenceReferenceData.java @@ -0,0 +1,12 @@ +package uk.gov.hmcts.opal.entity.projection; + +public interface OffenceReferenceData { + + Long getOffenceId(); + + String getCjsCode(); + + String getOffenceTitle(); + + String getOffenceTitleCy(); +} diff --git a/src/main/java/uk/gov/hmcts/opal/repository/OffenseRepository.java b/src/main/java/uk/gov/hmcts/opal/repository/OffenceRepository.java similarity index 58% rename from src/main/java/uk/gov/hmcts/opal/repository/OffenseRepository.java rename to src/main/java/uk/gov/hmcts/opal/repository/OffenceRepository.java index faa71f830..7fbba85c4 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/OffenseRepository.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/OffenceRepository.java @@ -3,9 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; -import uk.gov.hmcts.opal.entity.OffenseEntity; +import uk.gov.hmcts.opal.entity.OffenceEntity; @Repository -public interface OffenseRepository extends JpaRepository, - JpaSpecificationExecutor { +public interface OffenceRepository extends JpaRepository, + JpaSpecificationExecutor { } diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenceSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenceSpecs.java new file mode 100644 index 000000000..8b660caab --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenceSpecs.java @@ -0,0 +1,85 @@ +package uk.gov.hmcts.opal.repository.jpa; + +import jakarta.persistence.criteria.From; +import jakarta.persistence.criteria.Join; +import org.springframework.data.jpa.domain.Specification; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.BusinessUnitEntity; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.entity.OffenceEntity_; + +import java.time.LocalDateTime; +import java.util.Optional; + +import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.equalsBusinessUnitIdPredicate; +import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.likeBusinessUnitNamePredicate; + +public class OffenceSpecs extends EntitySpecs { + + public Specification findBySearchCriteria(OffenceSearchDto criteria) { + return Specification.allOf(specificationList( + notBlank(criteria.getOffenceId()).map(OffenceSpecs::equalsOffenceId), + notBlank(criteria.getCjsCode()).map(OffenceSpecs::likeCjsCode), + numericShort(criteria.getBusinessUnitId()).map(OffenceSpecs::equalsBusinessUnitId), + notBlank(criteria.getBusinessUnitName()).map(OffenceSpecs::likeBusinessUnitName), + notBlank(criteria.getOffenceTitle()).map(OffenceSpecs::likeOffenceTitle), + notBlank(criteria.getOffenceTitleCy()).map(OffenceSpecs::likeOffenceTitleCy) + )); + } + + public Specification referenceDataFilter(Optional filter) { + return Specification.allOf(specificationList( + Optional.of(LocalDateTime.now()).map(OffenceSpecs::usedToDateGreaterThenEqualToDate), + filter.filter(s -> !s.isBlank()).map(OffenceSpecs::likeAnyOffence) + )); + } + + public static Specification equalsOffenceId(String offenceId) { + return (root, query, builder) -> builder.equal(root.get(OffenceEntity_.offenceId), offenceId); + } + + public static Specification equalsBusinessUnitId(Short businessUnitId) { + return (root, query, builder) -> + equalsBusinessUnitIdPredicate(joinBusinessUnit(root), builder, businessUnitId); + } + + public static Specification likeBusinessUnitName(String businessUnitName) { + return (root, query, builder) -> + likeBusinessUnitNamePredicate(joinBusinessUnit(root), builder, businessUnitName); + } + + public static Specification likeCjsCode(String cjsCode) { + return (root, query, builder) -> + likeWildcardPredicate(root.get(OffenceEntity_.cjsCode), builder, cjsCode); + } + + public static Specification likeOffenceTitle(String offenceTitle) { + return (root, query, builder) -> + likeWildcardPredicate(root.get(OffenceEntity_.offenceTitle), builder, offenceTitle); + } + + public static Specification likeOffenceTitleCy(String offenceTitleCy) { + return (root, query, builder) -> + likeWildcardPredicate(root.get(OffenceEntity_.offenceTitleCy), builder, offenceTitleCy); + } + + public static Specification usedToDateGreaterThenEqualToDate(LocalDateTime expiryDate) { + return (root, query, builder) -> builder.or( + builder.isNull(root.get(OffenceEntity_.dateUsedTo)), + builder.greaterThanOrEqualTo(root.get(OffenceEntity_.dateUsedTo), expiryDate) + ); + } + + public static Specification likeAnyOffence(String filter) { + return Specification.anyOf( + likeCjsCode(filter), + likeOffenceTitle(filter), + likeOffenceTitleCy(filter) + ); + } + + public static Join joinBusinessUnit(From from) { + return from.join(OffenceEntity_.businessUnit); + } + +} diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenseSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenseSpecs.java deleted file mode 100644 index a58bd4da2..000000000 --- a/src/main/java/uk/gov/hmcts/opal/repository/jpa/OffenseSpecs.java +++ /dev/null @@ -1,59 +0,0 @@ -package uk.gov.hmcts.opal.repository.jpa; - -import jakarta.persistence.criteria.From; -import jakarta.persistence.criteria.Join; -import org.springframework.data.jpa.domain.Specification; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.BusinessUnitEntity; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.entity.OffenseEntity_; - -import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.equalsBusinessUnitIdPredicate; -import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.likeBusinessUnitNamePredicate; - -public class OffenseSpecs extends EntitySpecs { - - public Specification findBySearchCriteria(OffenseSearchDto criteria) { - return Specification.allOf(specificationList( - notBlank(criteria.getOffenseId()).map(OffenseSpecs::equalsOffenseId), - notBlank(criteria.getCjsCode()).map(OffenseSpecs::likeCjsCode), - numericShort(criteria.getBusinessUnitId()).map(OffenseSpecs::equalsBusinessUnitId), - notBlank(criteria.getBusinessUnitName()).map(OffenseSpecs::likeBusinessUnitName), - notBlank(criteria.getOffenceTitle()).map(OffenseSpecs::likeOffenseTitle), - notBlank(criteria.getOffenceTitleCy()).map(OffenseSpecs::likeOffenseTitleCy) - )); - } - - public static Specification equalsOffenseId(String offenseId) { - return (root, query, builder) -> builder.equal(root.get(OffenseEntity_.offenseId), offenseId); - } - - public static Specification equalsBusinessUnitId(Short businessUnitId) { - return (root, query, builder) -> - equalsBusinessUnitIdPredicate(joinBusinessUnit(root), builder, businessUnitId); - } - - public static Specification likeBusinessUnitName(String businessUnitName) { - return (root, query, builder) -> - likeBusinessUnitNamePredicate(joinBusinessUnit(root), builder, businessUnitName); - } - - public static Specification likeCjsCode(String cjsCode) { - return (root, query, builder) -> - likeWildcardPredicate(root.get(OffenseEntity_.cjsCode), builder, cjsCode); - } - - public static Specification likeOffenseTitle(String offenseTitle) { - return (root, query, builder) -> - likeWildcardPredicate(root.get(OffenseEntity_.offenseTitle), builder, offenseTitle); - } - - public static Specification likeOffenseTitleCy(String offenseTitleCy) { - return (root, query, builder) -> - likeWildcardPredicate(root.get(OffenseEntity_.offenseTitleCy), builder, offenseTitleCy); - } - - public static Join joinBusinessUnit(From from) { - return from.join(OffenseEntity_.businessUnit); - } -} diff --git a/src/main/java/uk/gov/hmcts/opal/service/OffenceServiceInterface.java b/src/main/java/uk/gov/hmcts/opal/service/OffenceServiceInterface.java new file mode 100644 index 000000000..b143a77c2 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/service/OffenceServiceInterface.java @@ -0,0 +1,13 @@ +package uk.gov.hmcts.opal.service; + +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; + +import java.util.List; + +public interface OffenceServiceInterface { + + OffenceEntity getOffence(long offenceId); + + List searchOffences(OffenceSearchDto criteria); +} diff --git a/src/main/java/uk/gov/hmcts/opal/service/OffenseServiceInterface.java b/src/main/java/uk/gov/hmcts/opal/service/OffenseServiceInterface.java deleted file mode 100644 index 46487f49a..000000000 --- a/src/main/java/uk/gov/hmcts/opal/service/OffenseServiceInterface.java +++ /dev/null @@ -1,13 +0,0 @@ -package uk.gov.hmcts.opal.service; - -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; - -import java.util.List; - -public interface OffenseServiceInterface { - - OffenseEntity getOffense(short offenseId); - - List searchOffenses(OffenseSearchDto criteria); -} diff --git a/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseService.java b/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceService.java similarity index 58% rename from src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseService.java rename to src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceService.java index 2f2a32c5f..72b61b38c 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceService.java @@ -5,17 +5,17 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestClient; import uk.gov.hmcts.opal.config.properties.LegacyGatewayProperties; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.OffenseServiceInterface; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.service.OffenceServiceInterface; import java.util.List; @Service -@Slf4j(topic = "LegacyOffenseService") -public class LegacyOffenseService extends LegacyService implements OffenseServiceInterface { +@Slf4j(topic = "LegacyOffenceService") +public class LegacyOffenceService extends LegacyService implements OffenceServiceInterface { - public LegacyOffenseService(LegacyGatewayProperties legacyGatewayProperties, RestClient restClient) { + public LegacyOffenceService(LegacyGatewayProperties legacyGatewayProperties, RestClient restClient) { super(legacyGatewayProperties, restClient); } @@ -25,12 +25,12 @@ public Logger getLog() { } @Override - public OffenseEntity getOffense(short offenseId) { + public OffenceEntity getOffence(long offenceId) { throw new LegacyGatewayResponseException("Not Yet Implemented"); } @Override - public List searchOffenses(OffenseSearchDto criteria) { + public List searchOffences(OffenceSearchDto criteria) { throw new LegacyGatewayResponseException("Not Yet Implemented"); } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/OffenceService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/OffenceService.java new file mode 100644 index 000000000..47227a741 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/OffenceService.java @@ -0,0 +1,57 @@ +package uk.gov.hmcts.opal.service.opal; + + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.entity.OffenceEntity_; +import uk.gov.hmcts.opal.entity.projection.OffenceReferenceData; +import uk.gov.hmcts.opal.repository.OffenceRepository; +import uk.gov.hmcts.opal.repository.jpa.OffenceSpecs; +import uk.gov.hmcts.opal.service.OffenceServiceInterface; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Qualifier("offenceService") +public class OffenceService implements OffenceServiceInterface { + + private final OffenceRepository offenceRepository; + + private final OffenceSpecs specs = new OffenceSpecs(); + + @Override + public OffenceEntity getOffence(long offenceId) { + return offenceRepository.getReferenceById(offenceId); + } + + @Override + public List searchOffences(OffenceSearchDto criteria) { + Page page = offenceRepository + .findBy(specs.findBySearchCriteria(criteria), + ffq -> ffq.page(Pageable.unpaged())); + + return page.getContent(); + } + + public List getReferenceData(Optional filter) { + + Sort codeSort = Sort.by(Sort.Direction.ASC, OffenceEntity_.CJS_CODE); + + Page page = offenceRepository + .findBy(specs.referenceDataFilter(filter), + ffq -> ffq + .sortBy(codeSort) + .as(OffenceReferenceData.class) + .page(Pageable.unpaged())); + + return page.getContent(); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/OffenseService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/OffenseService.java deleted file mode 100644 index c672f1fb7..000000000 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/OffenseService.java +++ /dev/null @@ -1,40 +0,0 @@ -package uk.gov.hmcts.opal.service.opal; - - -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.repository.OffenseRepository; -import uk.gov.hmcts.opal.repository.jpa.OffenseSpecs; -import uk.gov.hmcts.opal.service.OffenseServiceInterface; - -import java.util.List; - -@Service -@RequiredArgsConstructor -@Qualifier("offenseService") -public class OffenseService implements OffenseServiceInterface { - - private final OffenseRepository offenseRepository; - - private final OffenseSpecs specs = new OffenseSpecs(); - - @Override - public OffenseEntity getOffense(short offenseId) { - return offenseRepository.getReferenceById(offenseId); - } - - @Override - public List searchOffenses(OffenseSearchDto criteria) { - Page page = offenseRepository - .findBy(specs.findBySearchCriteria(criteria), - ffq -> ffq.page(Pageable.unpaged())); - - return page.getContent(); - } - -} diff --git a/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxy.java b/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxy.java new file mode 100644 index 000000000..a86fc5d51 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxy.java @@ -0,0 +1,37 @@ +package uk.gov.hmcts.opal.service.proxy; + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.service.DynamicConfigService; +import uk.gov.hmcts.opal.service.OffenceServiceInterface; +import uk.gov.hmcts.opal.service.legacy.LegacyOffenceService; +import uk.gov.hmcts.opal.service.opal.OffenceService; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Qualifier("offenceServiceProxy") +public class OffenceServiceProxy implements OffenceServiceInterface, ProxyInterface { + + private final OffenceService opalOffenceService; + private final LegacyOffenceService legacyOffenceService; + private final DynamicConfigService dynamicConfigService; + + private OffenceServiceInterface getCurrentModeService() { + return isLegacyMode(dynamicConfigService) ? legacyOffenceService : opalOffenceService; + } + + @Override + public OffenceEntity getOffence(long offenceId) { + return getCurrentModeService().getOffence(offenceId); + } + + @Override + public List searchOffences(OffenceSearchDto criteria) { + return getCurrentModeService().searchOffences(criteria); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxy.java b/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxy.java deleted file mode 100644 index ef9efc19b..000000000 --- a/src/main/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxy.java +++ /dev/null @@ -1,37 +0,0 @@ -package uk.gov.hmcts.opal.service.proxy; - -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.DynamicConfigService; -import uk.gov.hmcts.opal.service.OffenseServiceInterface; -import uk.gov.hmcts.opal.service.legacy.LegacyOffenseService; -import uk.gov.hmcts.opal.service.opal.OffenseService; - -import java.util.List; - -@Service -@RequiredArgsConstructor -@Qualifier("offenseServiceProxy") -public class OffenseServiceProxy implements OffenseServiceInterface, ProxyInterface { - - private final OffenseService opalOffenseService; - private final LegacyOffenseService legacyOffenseService; - private final DynamicConfigService dynamicConfigService; - - private OffenseServiceInterface getCurrentModeService() { - return isLegacyMode(dynamicConfigService) ? legacyOffenseService : opalOffenseService; - } - - @Override - public OffenseEntity getOffense(short offenseId) { - return getCurrentModeService().getOffense(offenseId); - } - - @Override - public List searchOffenses(OffenseSearchDto criteria) { - return getCurrentModeService().searchOffenses(criteria); - } -} diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/OffenceControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/OffenceControllerTest.java new file mode 100644 index 000000000..e1eb357f3 --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/controllers/OffenceControllerTest.java @@ -0,0 +1,113 @@ +package uk.gov.hmcts.opal.controllers; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import uk.gov.hmcts.opal.dto.reference.OffenceReferenceDataResults; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.entity.projection.OffenceReferenceData; +import uk.gov.hmcts.opal.service.opal.OffenceService; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class OffenceControllerTest { + + @Mock + private OffenceService offenceService; + + @InjectMocks + private OffenceController offenceController; + + @Test + void testGetOffence_Success() { + // Arrange + OffenceEntity entity = OffenceEntity.builder().build(); + + when(offenceService.getOffence(any(Long.class))).thenReturn(entity); + + // Act + ResponseEntity response = offenceController.getOffenceById(1L); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(entity, response.getBody()); + verify(offenceService, times(1)).getOffence(any(Long.class)); + } + + @Test + void testSearchOffences_Success() { + // Arrange + OffenceEntity entity = OffenceEntity.builder().build(); + List offenceList = List.of(entity); + + when(offenceService.searchOffences(any())).thenReturn(offenceList); + + // Act + OffenceSearchDto searchDto = OffenceSearchDto.builder().build(); + ResponseEntity> response = offenceController.postOffencesSearch(searchDto); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(offenceList, response.getBody()); + verify(offenceService, times(1)).searchOffences(any()); + } + + @Test + void testGetOffencesRefData_Success() { + // Arrange + OffenceReferenceData entity = createOffenceReferenceData(); + List offenceList = List.of(entity); + + when(offenceService.getReferenceData(any())).thenReturn(offenceList); + + // Act + Optional filter = Optional.empty(); + ResponseEntity response = offenceController.getOffenceRefData(filter); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + OffenceReferenceDataResults refDataResults = response.getBody(); + assertEquals(1, refDataResults.getCount()); + assertEquals(offenceList, refDataResults.getRefData()); + verify(offenceService, times(1)).getReferenceData(any()); + } + + private OffenceReferenceData createOffenceReferenceData() { + return new OffenceReferenceData() { + @Override + public Long getOffenceId() { + return 1L; + } + + @Override + public String getCjsCode() { + return "TH123456"; + } + + @Override + public String getOffenceTitle() { + return "Thief of Time"; + } + + @Override + public String getOffenceTitleCy() { + return null; + } + }; + + } + +} diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerTest.java deleted file mode 100644 index 888ec3655..000000000 --- a/src/test/java/uk/gov/hmcts/opal/controllers/develop/OffenseControllerTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package uk.gov.hmcts.opal.controllers.develop; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.opal.OffenseService; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class OffenseControllerTest { - - @Mock - private OffenseService offenseService; - - @InjectMocks - private OffenseController offenseController; - - @Test - void testGetOffense_Success() { - // Arrange - OffenseEntity entity = OffenseEntity.builder().build(); - - when(offenseService.getOffense(any(Short.class))).thenReturn(entity); - - // Act - ResponseEntity response = offenseController.getOffenseById((short)1); - - // Assert - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(entity, response.getBody()); - verify(offenseService, times(1)).getOffense(any(Short.class)); - } - - @Test - void testSearchOffenses_Success() { - // Arrange - OffenseEntity entity = OffenseEntity.builder().build(); - List offenseList = List.of(entity); - - when(offenseService.searchOffenses(any())).thenReturn(offenseList); - - // Act - OffenseSearchDto searchDto = OffenseSearchDto.builder().build(); - ResponseEntity> response = offenseController.postOffensesSearch(searchDto); - - // Assert - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(offenseList, response.getBody()); - verify(offenseService, times(1)).searchOffenses(any()); - } - -} diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceServiceTest.java similarity index 66% rename from src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseServiceTest.java rename to src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceServiceTest.java index a8538b3dd..527a68fbb 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenseServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyOffenceServiceTest.java @@ -6,50 +6,50 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @ExtendWith(MockitoExtension.class) -class LegacyOffenseServiceTest extends LegacyTestsBase { +class LegacyOffenceServiceTest extends LegacyTestsBase { @Mock private Logger log; @InjectMocks - private LegacyOffenseService legacyOffenseService; + private LegacyOffenceService legacyOffenceService; @Test - void testGetOffense() { + void testGetOffence() { // Arrange - OffenseEntity offenseEntity = OffenseEntity.builder().build(); + OffenceEntity offenceEntity = OffenceEntity.builder().build(); // Act LegacyGatewayResponseException exception = assertThrows( LegacyGatewayResponseException.class, - () -> legacyOffenseService.getOffense((short)1) + () -> legacyOffenceService.getOffence((short)1) ); // Assert - assertNotNull(legacyOffenseService.getLog()); + assertNotNull(legacyOffenceService.getLog()); assertNotNull(exception); assertEquals(NOT_YET_IMPLEMENTED, exception.getMessage()); } @Test - void testSearchOffenses() { + void testSearchOffences() { // Arrange - OffenseSearchDto criteria = OffenseSearchDto.builder().build(); + OffenceSearchDto criteria = OffenceSearchDto.builder().build(); // Act LegacyGatewayResponseException exception = assertThrows( LegacyGatewayResponseException.class, - () -> legacyOffenseService.searchOffenses(criteria) + () -> legacyOffenceService.searchOffences(criteria) ); // Assert diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/OffenceServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/OffenceServiceTest.java new file mode 100644 index 000000000..39adc364e --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/service/opal/OffenceServiceTest.java @@ -0,0 +1,99 @@ +package uk.gov.hmcts.opal.service.opal; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.repository.query.FluentQuery; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.entity.projection.OffenceReferenceData; +import uk.gov.hmcts.opal.repository.OffenceRepository; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class OffenceServiceTest { + + @Mock + private OffenceRepository offenceRepository; + + @InjectMocks + private OffenceService offenceService; + + @Test + void testGetOffence() { + // Arrange + + OffenceEntity offenceEntity = OffenceEntity.builder().build(); + when(offenceRepository.getReferenceById(any())).thenReturn(offenceEntity); + + // Act + OffenceEntity result = offenceService.getOffence((short)1); + + // Assert + assertNotNull(result); + + } + + @SuppressWarnings("unchecked") + @Test + void testSearchOffences() { + // Arrange + FluentQuery.FetchableFluentQuery ffq = Mockito.mock(FluentQuery.FetchableFluentQuery.class); + + OffenceEntity offenceEntity = OffenceEntity.builder().build(); + Page mockPage = new PageImpl<>(List.of(offenceEntity), Pageable.unpaged(), 999L); + when(offenceRepository.findBy(any(Specification.class), any())).thenAnswer(iom -> { + iom.getArgument(1, Function.class).apply(ffq); + return mockPage; + }); + + // Act + List result = offenceService.searchOffences(OffenceSearchDto.builder().build()); + + // Assert + assertEquals(List.of(offenceEntity), result); + + } + + @SuppressWarnings("unchecked") + @Test + void testOffencesReferenceData() { + // Arrange + FluentQuery.FetchableFluentQuery ffq = Mockito.mock(FluentQuery.FetchableFluentQuery.class); + when(ffq.as(any())).thenReturn(ffq); + when(ffq.sortBy(any())).thenReturn(ffq); + + OffenceEntity offenceEntity = OffenceEntity.builder().build(); + Page mockPage = new PageImpl<>(List.of(offenceEntity), Pageable.unpaged(), 999L); + when(offenceRepository.findBy(any(Specification.class), any())).thenAnswer(iom -> { + iom.getArgument(1, Function.class).apply(ffq); + return mockPage; + }); + + // Act + List result = offenceService.getReferenceData(Optional.empty()); + + // Assert + assertEquals(List.of(offenceEntity), result); + + } + + + + +} diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/OffenseServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/OffenseServiceTest.java deleted file mode 100644 index 4f4af9492..000000000 --- a/src/test/java/uk/gov/hmcts/opal/service/opal/OffenseServiceTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package uk.gov.hmcts.opal.service.opal; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.data.repository.query.FluentQuery; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.repository.OffenseRepository; - -import java.util.List; -import java.util.function.Function; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class OffenseServiceTest { - - @Mock - private OffenseRepository offenseRepository; - - @InjectMocks - private OffenseService offenseService; - - @Test - void testGetOffense() { - // Arrange - - OffenseEntity offenseEntity = OffenseEntity.builder().build(); - when(offenseRepository.getReferenceById(any())).thenReturn(offenseEntity); - - // Act - OffenseEntity result = offenseService.getOffense((short)1); - - // Assert - assertNotNull(result); - - } - - @SuppressWarnings("unchecked") - @Test - void testSearchOffenses() { - // Arrange - FluentQuery.FetchableFluentQuery ffq = Mockito.mock(FluentQuery.FetchableFluentQuery.class); - - OffenseEntity offenseEntity = OffenseEntity.builder().build(); - Page mockPage = new PageImpl<>(List.of(offenseEntity), Pageable.unpaged(), 999L); - when(offenseRepository.findBy(any(Specification.class), any())).thenAnswer(iom -> { - iom.getArgument(1, Function.class).apply(ffq); - return mockPage; - }); - - // Act - List result = offenseService.searchOffenses(OffenseSearchDto.builder().build()); - - // Assert - assertEquals(List.of(offenseEntity), result); - - } - - -} diff --git a/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxyTest.java b/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxyTest.java new file mode 100644 index 000000000..bfc78de89 --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenceServiceProxyTest.java @@ -0,0 +1,97 @@ +package uk.gov.hmcts.opal.service.proxy; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import uk.gov.hmcts.opal.dto.search.OffenceSearchDto; +import uk.gov.hmcts.opal.entity.OffenceEntity; +import uk.gov.hmcts.opal.service.OffenceServiceInterface; +import uk.gov.hmcts.opal.service.legacy.LegacyOffenceService; +import uk.gov.hmcts.opal.service.opal.OffenceService; + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +class OffenceServiceProxyTest extends ProxyTestsBase { + + private AutoCloseable closeable; + + @Mock + private OffenceService opalService; + + @Mock + private LegacyOffenceService legacyService; + + @InjectMocks + private OffenceServiceProxy offenceServiceProxy; + + @BeforeEach + void setUp() { + closeable = MockitoAnnotations.openMocks(this); + } + + @AfterEach + void tearDown() throws Exception { + closeable.close(); + } + + void testMode(OffenceServiceInterface targetService, OffenceServiceInterface otherService) { + testGetOffence(targetService, otherService); + testSearchOffences(targetService, otherService); + } + + void testGetOffence(OffenceServiceInterface targetService, OffenceServiceInterface otherService) { + // Given: a OffenceEntity is returned from the target service + OffenceEntity entity = OffenceEntity.builder().build(); + when(targetService.getOffence(anyLong())).thenReturn(entity); + + // When: getOffence is called on the proxy + OffenceEntity offenceResult = offenceServiceProxy.getOffence(1L); + + // Then: target service should be used, and the returned offence should be as expected + verify(targetService).getOffence((short)1); + verifyNoInteractions(otherService); + Assertions.assertEquals(entity, offenceResult); + } + + void testSearchOffences(OffenceServiceInterface targetService, OffenceServiceInterface otherService) { + // Given: an offences list result is returned from the target service + OffenceEntity entity = OffenceEntity.builder().build(); + List offencesList = List.of(entity); + when(targetService.searchOffences(any())).thenReturn(offencesList); + + // When: searchOffences is called on the proxy + OffenceSearchDto criteria = OffenceSearchDto.builder().build(); + List listResult = offenceServiceProxy.searchOffences(criteria); + + // Then: target service should be used, and the returned list should be as expected + verify(targetService).searchOffences(criteria); + verifyNoInteractions(otherService); + Assertions.assertEquals(offencesList, listResult); + } + + @Test + void shouldUseOpalOffenceServiceWhenModeIsNotLegacy() { + // Given: app mode is set + setMode(OPAL); + // Then: the target service is called, but the other service is not + testMode(opalService, legacyService); + } + + @Test + void shouldUseLegacyOffenceServiceWhenModeIsLegacy() { + // Given: app mode is set + setMode(LEGACY); + // Then: the target service is called, but the other service is not + testMode(legacyService, opalService); + } +} diff --git a/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxyTest.java b/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxyTest.java deleted file mode 100644 index baac71f2f..000000000 --- a/src/test/java/uk/gov/hmcts/opal/service/proxy/OffenseServiceProxyTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package uk.gov.hmcts.opal.service.proxy; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import uk.gov.hmcts.opal.dto.search.OffenseSearchDto; -import uk.gov.hmcts.opal.entity.OffenseEntity; -import uk.gov.hmcts.opal.service.OffenseServiceInterface; -import uk.gov.hmcts.opal.service.legacy.LegacyOffenseService; -import uk.gov.hmcts.opal.service.opal.OffenseService; - -import java.util.List; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyShort; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; - -class OffenseServiceProxyTest extends ProxyTestsBase { - - private AutoCloseable closeable; - - @Mock - private OffenseService opalService; - - @Mock - private LegacyOffenseService legacyService; - - @InjectMocks - private OffenseServiceProxy offenseServiceProxy; - - @BeforeEach - void setUp() { - closeable = MockitoAnnotations.openMocks(this); - } - - @AfterEach - void tearDown() throws Exception { - closeable.close(); - } - - void testMode(OffenseServiceInterface targetService, OffenseServiceInterface otherService) { - testGetOffense(targetService, otherService); - testSearchOffenses(targetService, otherService); - } - - void testGetOffense(OffenseServiceInterface targetService, OffenseServiceInterface otherService) { - // Given: a OffenseEntity is returned from the target service - OffenseEntity entity = OffenseEntity.builder().build(); - when(targetService.getOffense(anyShort())).thenReturn(entity); - - // When: getOffense is called on the proxy - OffenseEntity offenseResult = offenseServiceProxy.getOffense((short)1); - - // Then: target service should be used, and the returned offense should be as expected - verify(targetService).getOffense((short)1); - verifyNoInteractions(otherService); - Assertions.assertEquals(entity, offenseResult); - } - - void testSearchOffenses(OffenseServiceInterface targetService, OffenseServiceInterface otherService) { - // Given: a offenses list result is returned from the target service - OffenseEntity entity = OffenseEntity.builder().build(); - List offensesList = List.of(entity); - when(targetService.searchOffenses(any())).thenReturn(offensesList); - - // When: searchOffenses is called on the proxy - OffenseSearchDto criteria = OffenseSearchDto.builder().build(); - List listResult = offenseServiceProxy.searchOffenses(criteria); - - // Then: target service should be used, and the returned list should be as expected - verify(targetService).searchOffenses(criteria); - verifyNoInteractions(otherService); - Assertions.assertEquals(offensesList, listResult); - } - - @Test - void shouldUseOpalOffenseServiceWhenModeIsNotLegacy() { - // Given: app mode is set - setMode(OPAL); - // Then: the target service is called, but the other service is not - testMode(opalService, legacyService); - } - - @Test - void shouldUseLegacyOffenseServiceWhenModeIsLegacy() { - // Given: app mode is set - setMode(LEGACY); - // Then: the target service is called, but the other service is not - testMode(legacyService, opalService); - } -}