Skip to content

Commit

Permalink
story(ccls-2178): Add Application delete endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
porritta committed Jul 17, 2024
1 parent d85ff49 commit fdfcafd
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 20 deletions.
32 changes: 32 additions & 0 deletions caab-api/open-api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,38 @@ paths:
description: 'Not found'
'500':
description: 'Internal server error'
delete:
tags:
- applications
summary: 'Remove an application'
operationId: 'removeApplication'
parameters:
- name: 'id'
in: 'path'
required: true
schema:
type: 'integer'
format: 'int64'
example: '1234567890'
- name: 'Caab-User-Login-Id'
in: header
required: true
schema:
type: 'string'
example: '[email protected]'
responses:
'204':
description: 'No Content'
'400':
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
description: 'Internal server error'
/applications/clients/{client-reference-id}:
patch:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void testSaveApplication_opponent(){
assertEquals(1, fetchedApplication.getOpponents().size());

uk.gov.laa.ccms.caab.api.entity.Opponent fetchedOpponent =
fetchedApplication.getOpponents().get(0);
fetchedApplication.getOpponents().getFirst();

assertNotNull(fetchedOpponent.getAddress());
assertEquals(builtOpponent.getAmendment(), fetchedOpponent.getAmendment());
Expand Down Expand Up @@ -360,6 +360,46 @@ public void testGetApplication_noData() {
assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus());
}

@Test
@Sql(scripts = {
"/sql/application_insert.sql",
"/sql/linked_cases_insert.sql",
"/sql/opponent_insert.sql",
"/sql/case_outcome_insert.sql",
"/sql/prior_authority_insert.sql",
"/sql/proceeding_insert.sql",
"/sql/scope_limitation_insert.sql"
})
public void testRemoveApplication_applicationExists_removesSuccessfully() {
Long applicationId = 41L;

applicationService.removeApplication(applicationId);

}

@Test
@Sql(scripts = {
"/sql/application_insert.sql",
"/sql/linked_cases_insert.sql",
"/sql/opponent_insert.sql",
"/sql/case_outcome_insert.sql",
"/sql/prior_authority_insert.sql",
"/sql/proceeding_insert.sql",
"/sql/scope_limitation_insert.sql"
})
public void testRemoveApplication_applicationNotExists_throwsException() {
Long applicationId = 999L;

// Use assertThrows to check if the method throws the expected exception
CaabApiException exception = assertThrows(CaabApiException.class,
() ->applicationService.removeApplication(applicationId));

assertEquals(
String.format("Application with id: %s not found", applicationId),
exception.getMessage());
assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus());
}

@Test
@Sql(scripts = "/sql/application_insert.sql")
public void testGetApplicationType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public ResponseEntity<Void> updateApplication(
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Override
public ResponseEntity<Void> removeApplication(
final Long id,
final String caabUserLoginId) {
applicationService.removeApplication(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

//correspondence address

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -307,5 +310,11 @@ public class Opponent implements Serializable {
@JoinColumn(name = "FK_OUTCOME")
private CaseOutcome caseOutcome;

/**
* The liable parties associated with this opponent.
*/
@OneToMany(fetch = FetchType.LAZY, mappedBy = "opponent",
cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<LiableParty> liableParties;

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ public void updateApplication(
applicationRepository.save(application);
}

/**
* Remove an application.
*
* @param applicationId the TDS id for the application.
* @throws uk.gov.laa.ccms.caab.api.exception.CaabApiException If the application
* with the specified ID is not found.
*/
@Transactional
public void removeApplication(final Long applicationId) {
if (applicationRepository.existsById(applicationId)) {
applicationRepository.deleteById(applicationId);
} else {
throw new CaabApiException(
String.format("Application with id: %s not found", applicationId),
HttpStatus.NOT_FOUND);
}
}

/**
* Updates a client's information in the application repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ private Opponent getOpponentEntity(Integer id) {
*/
@Transactional
public void removeCaseOutcome(final Long caseOutcomeId) {
caseOutcomeRepository.findById(caseOutcomeId)
.ifPresentOrElse(this::deleteCaseOutcome, () -> {
throw new CaabApiException(
String.format("CaseOutcome with id: %s not found", caseOutcomeId),
HttpStatus.NOT_FOUND);
});
if (caseOutcomeRepository.existsById(caseOutcomeId)) {
caseOutcomeRepository.deleteById(caseOutcomeId);
} else {
throw new CaabApiException(
String.format("CaseOutcome with id: %s not found", caseOutcomeId),
HttpStatus.NOT_FOUND);
}

}

/**
Expand All @@ -160,16 +162,7 @@ public void removeCaseOutcomes(
caseOutcome.setLscCaseReference(caseReferenceNumber);
caseOutcome.setProviderId(providerId);

caseOutcomeRepository.findAll(Example.of(caseOutcome))
.forEach(this::deleteCaseOutcome);
}

private void deleteCaseOutcome(final CaseOutcome caseOutcomeEntity) {
// Clear the association between CaseOutcome and Opponent.
Optional.ofNullable(caseOutcomeEntity.getOpponents())
.ifPresent(opponents -> opponents.forEach(opponent -> opponent.setCaseOutcome(null)));

caseOutcomeRepository.delete(caseOutcomeEntity);
caseOutcomeRepository.deleteAll(caseOutcomeRepository.findAll(Example.of(caseOutcome)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down Expand Up @@ -128,6 +129,20 @@ public void updateApplication() throws Exception {
verify(applicationService).updateApplication(id, applicationDetail);
}

@Test
public void removeApplication() throws Exception {
Long id = 1L;
String caabUserLoginId = "testUserLoginId";

doNothing().when(applicationService).removeApplication(id);

this.mockMvc.perform(delete("/applications/{id}", id)
.header("Caab-User-Login-Id", caabUserLoginId))
.andExpect(status().isNoContent());

verify(applicationService).removeApplication(id);
}


@Test
public void getApplicationType() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ void updateApplication_whenApplicationNotExists_throwsException() {
assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus());
}

@Test
void removeApplication_whenApplicationNotExists_throwsException() {
Long applicationId = 1L;

when(applicationRepository.existsById(applicationId)).thenReturn(Boolean.FALSE);

CaabApiException exception = assertThrows(CaabApiException.class,
() -> applicationService.removeApplication(applicationId));

assertEquals("Application with id: " + applicationId + " not found", exception.getMessage());
assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus());
}

@Test
void removeApplication_whenApplicationExists_applicationDeleted() {
Long applicationId = 1L;

when(applicationRepository.existsById(applicationId)).thenReturn(Boolean.TRUE);

applicationService.removeApplication(applicationId);

verify(applicationRepository).deleteById(applicationId);
}


@Test
void updateClient_updatesClientInformation() {
BaseClientDetail baseClient = new BaseClientDetail();
Expand Down Expand Up @@ -499,7 +524,7 @@ void getProceedingsForApplication_WhenExists_ReturnsProceedings() {
verify(applicationMapper, times(application.getProceedings().size())).toProceedingModel(any());

assertFalse(result.isEmpty());
assertEquals(proceedingModel, result.get(0));
assertEquals(proceedingModel, result.getFirst());
}

@Test
Expand Down Expand Up @@ -566,7 +591,7 @@ void getPriorAuthoritiesForApplication_WhenExists_ReturnsPriorAuthorities() {
verify(applicationMapper, times(application.getPriorAuthorities().size())).toPriorAuthorityModel(any());

assertFalse(result.isEmpty());
assertEquals(priorAuthorityModel, result.get(0));
assertEquals(priorAuthorityModel, result.getFirst());
}

@Test
Expand Down Expand Up @@ -695,7 +720,7 @@ void getOpponentsForApplication_WhenExists_ReturnsOpponents() {
verify(applicationMapper, times(application.getOpponents().size())).toOpponentModel(any());

assertFalse(result.isEmpty());
assertEquals(opponentModel, result.get(0));
assertEquals(opponentModel, result.getFirst());
}

@Test
Expand Down

0 comments on commit fdfcafd

Please sign in to comment.