Skip to content

Commit

Permalink
Merge pull request #4 from ministryofjustice/bugfix/ccls-2144_fix_pat…
Browse files Browse the repository at this point in the history
…ch_endpoint

bugfix(CCLS-2144) fix patch
  • Loading branch information
PhilDigitalJustice authored Apr 16, 2024
2 parents 2e91da6 + 395af8d commit f69f9c6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 41 deletions.
38 changes: 21 additions & 17 deletions assessment-api/open-api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/baseAssessmentDetail'
$ref: '#/components/schemas/patchAssessmentDetail'
parameters:
- name: 'assessment-id'
in: 'path'
Expand Down Expand Up @@ -142,23 +142,27 @@ components:
items:
$ref: "#/components/schemas/assessmentDetail"
assessmentDetail:
allOf:
- $ref: "#/components/schemas/baseAssessmentDetail"
- type: 'object'
properties:
id:
type: 'string'
entity_types:
type: array
default: [ ]
items:
$ref: '#/components/schemas/assessmentEntityTypeDetail'
audit_detail:
$ref: '#/components/schemas/auditDetail'
baseAssessmentDetail:
type: 'object'
properties:
id:
type: 'string'
provider_id:
type: 'string'
case_reference_number:
type: 'string'
name:
type: 'string'
status:
type: 'string'
entity_types:
type: array
default: [ ]
items:
$ref: '#/components/schemas/assessmentEntityTypeDetail'
audit_detail:
$ref: '#/components/schemas/auditDetail'
patchAssessmentDetail:
type: 'object'
discriminator:
propertyName: baseAssessmentDetail
properties:
provider_id:
type: 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import uk.gov.laa.ccms.caab.assessment.api.AssessmentsApi;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.BaseAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.service.AssessmentService;

/**
Expand Down Expand Up @@ -62,16 +62,16 @@ public ResponseEntity<AssessmentDetails> getAssessments(
*
* @param assessmentId The ID of the assessment to update.
* @param caabUserLoginId The CAAB user login ID performing the update.
* @param baseAssessmentDetail The details to update the assessment with.
* @param patch The details to update the assessment with.
* @return ResponseEntity with the status of the update operation.
*/
@Override
public ResponseEntity<AssessmentDetail> updateAssessment(
final Long assessmentId,
final String caabUserLoginId,
final BaseAssessmentDetail baseAssessmentDetail) {
final PatchAssessmentDetail patch) {

assessmentService.updateAssessment(assessmentId, baseAssessmentDetail);
assessmentService.updateAssessment(assessmentId, patch);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;
import org.mapstruct.BeanMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -17,7 +16,7 @@
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentEntityDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentEntityTypeDetail;
import uk.gov.laa.ccms.caab.assessment.model.BaseAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;

/**
* Mapper for mapping between assessment entities and models.
Expand Down Expand Up @@ -50,12 +49,16 @@ public interface AssessmentMapper {
@Mapping(target = "type", source = "attributeType")
AssessmentAttributeDetail toAssessmentAttributeDetail(OpaAttribute opaAttribute);

@InheritConfiguration(name = "toOpaSession")
@Mapping(target = "assessment", source = "name")
@Mapping(target = "ownerId", source = "providerId")
@Mapping(target = "targetId", source = "caseReferenceNumber")
@Mapping(target = "auditTrail", ignore = true)
@Mapping(target = "opaListEntities", ignore = true)
@Mapping(target = "id", ignore = true)
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void mapIntoOpaSession(
@MappingTarget OpaSession opaSession,
AssessmentDetail assessmentDetail);
PatchAssessmentDetail patch);

/**
* Maps a list of {@link OpaSession} entities to an {@link AssessmentDetails} model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import uk.gov.laa.ccms.caab.assessment.mapper.AssessmentMapper;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.BaseAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.repository.OpaSessionRepository;

/**
Expand Down Expand Up @@ -57,20 +57,20 @@ public AssessmentDetail getAssessment(Long assessmentId) {
* Updates an assessment's details in the database.
*
* @param assessmentId The ID of the assessment to update.
* @param baseAssessmentDetail The new details to be applied to the assessment.
* @param patch The new details to be applied to the assessment.
* @throws ApplicationException if the assessment with the specified ID
* does not exist.
*/
@Transactional
public void updateAssessment(
final Long assessmentId,
final BaseAssessmentDetail baseAssessmentDetail) {
final PatchAssessmentDetail patch) {
OpaSession opaSession = opaSessionRepository.findById(assessmentId)
.orElseThrow(() -> new ApplicationException(
String.format("Assessment with id %s not found", assessmentId),
HttpStatus.NOT_FOUND));

assessmentMapper.mapIntoOpaSession(opaSession, (AssessmentDetail) baseAssessmentDetail);
assessmentMapper.mapIntoOpaSession(opaSession, patch);
opaSessionRepository.save(opaSession);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import uk.gov.laa.ccms.caab.assessment.model.AssessmentRelationshipDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentRelationshipTargetDetail;
import uk.gov.laa.ccms.caab.assessment.model.AuditDetail;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;

@ExtendWith(MockitoExtension.class)
class AssessmentMapperTest {
Expand Down Expand Up @@ -326,30 +327,28 @@ void testOpaRelationshipListToAssessmentRelationshipDetailListWithNonNullList()
@Test
void mapIntoOpaSession_WithFullDetail_MapsAllFields() {
OpaSession opaSession = new OpaSession();
AssessmentDetail assessmentDetail = new AssessmentDetail()
PatchAssessmentDetail patch = new PatchAssessmentDetail()
.name("Assessment Name")
.providerId("Provider123")
.caseReferenceNumber("CaseRef456")
.id("789")
.status("Completed");

assessmentMapper.mapIntoOpaSession(opaSession, assessmentDetail);
assessmentMapper.mapIntoOpaSession(opaSession, patch);

assertEquals("Assessment Name", opaSession.getAssessment());
assertEquals("Provider123", opaSession.getOwnerId());
assertEquals("CaseRef456", opaSession.getTargetId());
assertEquals(789L, opaSession.getId());
assertEquals("Completed", opaSession.getStatus());
}

@Test
void mapIntoOpaSession_WithPartialDetail_MapsNonNullFields() {
OpaSession opaSession = new OpaSession();
AssessmentDetail assessmentDetail = new AssessmentDetail()
PatchAssessmentDetail patch = new PatchAssessmentDetail()
.name("Partial Name")
.caseReferenceNumber("PartialCaseRef123");

assessmentMapper.mapIntoOpaSession(opaSession, assessmentDetail);
assessmentMapper.mapIntoOpaSession(opaSession, patch);

assertEquals("Partial Name", opaSession.getAssessment());
assertNull(opaSession.getOwnerId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import uk.gov.laa.ccms.caab.assessment.mapper.AssessmentMapper;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.repository.OpaSessionRepository;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -102,33 +103,33 @@ void testGetAssessmentNotFound() {
@Test
void testUpdateAssessmentSuccess() {
final Long assessmentId = 1L;
final AssessmentDetail baseAssessmentDetail = new AssessmentDetail();
final PatchAssessmentDetail patch = new PatchAssessmentDetail();
final OpaSession opaSession = new OpaSession();

when(opaSessionRepository.findById(assessmentId)).thenReturn(Optional.of(opaSession));

assessmentService.updateAssessment(assessmentId, baseAssessmentDetail);
assessmentService.updateAssessment(assessmentId, patch);

verify(opaSessionRepository).findById(assessmentId);
verify(assessmentMapper).mapIntoOpaSession(opaSession, baseAssessmentDetail);
verify(assessmentMapper).mapIntoOpaSession(opaSession, patch);
verify(opaSessionRepository).save(opaSession);
}

@Test
void testUpdateAssessmentNotFound() {
final Long assessmentId = 1L;
final AssessmentDetail baseAssessmentDetail = new AssessmentDetail();
final PatchAssessmentDetail patch = new PatchAssessmentDetail();

when(opaSessionRepository.findById(assessmentId)).thenReturn(Optional.empty());

ApplicationException thrown = assertThrows(ApplicationException.class, () -> {
assessmentService.updateAssessment(assessmentId, baseAssessmentDetail);
assessmentService.updateAssessment(assessmentId, patch);
});

assertEquals(HttpStatus.NOT_FOUND, thrown.getHttpStatus());
assertTrue(thrown.getMessage().contains("Assessment with id " + assessmentId + " not found"));
verify(opaSessionRepository).findById(assessmentId);
verify(assessmentMapper, never()).mapIntoOpaSession(any(OpaSession.class), any(AssessmentDetail.class));
verify(assessmentMapper, never()).mapIntoOpaSession(any(OpaSession.class), any(PatchAssessmentDetail.class));
verify(opaSessionRepository, never()).save(any(OpaSession.class));
}

Expand Down

0 comments on commit f69f9c6

Please sign in to comment.