Skip to content

Commit

Permalink
Bugfix(CCLS-2164) filter assessments on multiple assessment types
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilDigitalJustice committed Apr 29, 2024
1 parent df99f52 commit 04047bb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
23 changes: 12 additions & 11 deletions assessment-api/open-api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,18 @@ components:
name: name
in: query
schema:
type: string
enum:
- 'billingAssessment'
- 'billingAssessment_PREPOP'
- 'poaAssessment'
- 'poaAssessment_PREPOP'
- 'meansAssessment'
- 'meansAssessment_PREPOP'
- 'meritsAssessment'
- 'meritsAssessment_PREPOP'
example: 'meansAssessment'
type: 'array'
items:
type: 'string'
enum:
- 'billingAssessment'
- 'billingAssessment_PREPOP'
- 'poaAssessment'
- 'poaAssessment_PREPOP'
- 'meansAssessment'
- 'meansAssessment_PREPOP'
- 'meritsAssessment'
- 'meritsAssessment_PREPOP'

requiredUserLoginId:
name: 'Caab-User-Login-Id'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.laa.ccms.caab.assessment.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -43,18 +44,18 @@ public ResponseEntity<AssessmentDetail> getAssessment(
*/
@Override
public ResponseEntity<AssessmentDetails> getAssessments(
final String name,
final List<String> name,
final String providerId,
final String caseReferenceNumber,
final String status) {


AssessmentDetail criteria = new AssessmentDetail()
.providerId(providerId)
.caseReferenceNumber(caseReferenceNumber)
.name(name)
.status(status);

return ResponseEntity.ok(assessmentService.getAssessments(criteria));
return ResponseEntity.ok(assessmentService.getAssessments(criteria, name));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.laa.ccms.caab.assessment.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import uk.gov.laa.ccms.caab.assessment.entity.OpaSession;

Expand All @@ -13,6 +14,7 @@
* Spring Data JPA.
*/
@Repository
public interface OpaSessionRepository extends JpaRepository<OpaSession, Long> {
public interface OpaSessionRepository extends JpaRepository<OpaSession, Long>,
JpaSpecificationExecutor<OpaSession> {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package uk.gov.laa.ccms.caab.assessment.service;

import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import uk.gov.laa.ccms.caab.assessment.entity.OpaSession;
Expand Down Expand Up @@ -31,9 +36,13 @@ public class AssessmentService {
* @param criteria the criteria to filter the assessments
* @return the list of assessments
*/
public AssessmentDetails getAssessments(AssessmentDetail criteria) {
public AssessmentDetails getAssessments(
final AssessmentDetail criteria,
final List<String> names) {
OpaSession example = assessmentMapper.toOpaSession(criteria);
List<OpaSession> opaSessions = opaSessionRepository.findAll(Example.of(example));

List<OpaSession> opaSessions =
opaSessionRepository.findAll(buildQuerySpecification(Example.of(example), names));

return assessmentMapper.toAssessmentDetails(opaSessions);
}
Expand Down Expand Up @@ -74,6 +83,32 @@ public void updateAssessment(
opaSessionRepository.save(opaSession);
}

/**
* Constructs a query specification based on a provided example and a list of names.
*
* @param assessment the example of OpaSession to filter query results.
* @param names a list of names to include in the query; may be null or empty.
* @return the Specification object that constructs the predicate for querying.
*/
private Specification<OpaSession> buildQuerySpecification(
final Example<OpaSession> assessment,
final List<String> names) {
return (root, query, builder) -> {
List<Predicate> predicates = new ArrayList<>();

if (names != null && !names.isEmpty()) {
predicates.add(root.get("assessment").in(names));
}

Predicate examplePredicate = QueryByExamplePredicateBuilder
.getPredicate(root, builder, assessment);

if (examplePredicate != null) {
predicates.add(examplePredicate);
}

return builder.and(predicates.toArray(new Predicate[0]));
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand Down Expand Up @@ -71,11 +73,12 @@ public void getAssessments() throws Exception {
AssessmentDetail criteria = new AssessmentDetail()
.providerId(providerId)
.caseReferenceNumber(caseReferenceNumber)
.name(name)
.status(status);

List<String> names = new ArrayList<>(List.of(name));

AssessmentDetail assessment = new AssessmentDetail();
when(assessmentService.getAssessments(criteria))
when(assessmentService.getAssessments(criteria, names))
.thenReturn(new AssessmentDetails().addContentItem(assessment));

this.mockMvc.perform(get("/assessments")
Expand All @@ -85,7 +88,7 @@ public void getAssessments() throws Exception {
.param("status", status))
.andExpect(status().isOk());

verify(assessmentService).getAssessments(criteria);
verify(assessmentService).getAssessments(criteria, names);
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package uk.gov.laa.ccms.caab.assessment.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import uk.gov.laa.ccms.caab.assessment.entity.OpaSession;
import uk.gov.laa.ccms.caab.assessment.exception.ApplicationException;
Expand Down Expand Up @@ -46,22 +50,23 @@ void testGetAssessments() {
AssessmentDetail criteria = new AssessmentDetail()
.providerId(providerId)
.caseReferenceNumber(caseReferenceNumber)
.name(name)
.status(status);

List<String> names = new ArrayList<>(List.of(name));

OpaSession session = new OpaSession();

when(assessmentMapper.toOpaSession(criteria))
.thenReturn(session);
when(opaSessionRepository.findAll(Example.of(session)))
when(opaSessionRepository.findAll(any(Specification.class)))
.thenReturn(List.of(session));
when(assessmentMapper.toAssessmentDetails(List.of(session)))
.thenReturn(new AssessmentDetails());

assessmentService.getAssessments(criteria);
assessmentService.getAssessments(criteria, names);

verify(assessmentMapper).toOpaSession(criteria);
verify(opaSessionRepository).findAll(Example.of(session));
verify(opaSessionRepository).findAll(any(Specification.class));
verify(assessmentMapper).toAssessmentDetails(List.of(session));
}

Expand Down

0 comments on commit 04047bb

Please sign in to comment.