Skip to content

Commit

Permalink
Add unit tests for LegacyDefendantAccountService
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Jan 15, 2024
1 parent 3c08f74 commit 2484dec
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package uk.gov.hmcts.opal.service;

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 org.springframework.web.client.RestTemplate;
import uk.gov.hmcts.opal.dto.AccountEnquiryDto;
import uk.gov.hmcts.opal.dto.AccountSearchDto;
import uk.gov.hmcts.opal.dto.AccountSearchResultsDto;
import uk.gov.hmcts.opal.dto.ToJsonString;
import uk.gov.hmcts.opal.entity.DefendantAccountEntity;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class LegacyDefendantAccountServiceTest {

@Mock
private RestTemplate restTemplate;

@InjectMocks
private LegacyDefendantAccountService legacyDefendantAccountService;

@Test
@SuppressWarnings("unchecked")
void putDefendantAccount_SuccessfulResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountEntity.class), any(Class.class)))
.thenReturn(successfulResponseEntity);

// Act
DefendantAccountEntity resultPartyDto = legacyDefendantAccountService.putDefendantAccount(inputAccountEntity);

// Assert
assertEquals(expectedAccountEntity, resultPartyDto);
}

@Test
@SuppressWarnings("unchecked")
void putDefendantAccount_FailureBodyResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
null, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountEntity.class), any(Class.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
DefendantAccountEntity resultPartyDto = legacyDefendantAccountService.putDefendantAccount(inputAccountEntity);

// Assert

assertNull(resultPartyDto);
}

@Test
@SuppressWarnings("unchecked")
void putDefendantAccount_FailureCodeResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.INTERNAL_SERVER_ERROR);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountEntity.class), any(Class.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
DefendantAccountEntity resultPartyDto = legacyDefendantAccountService.putDefendantAccount(inputAccountEntity);

// Assert

assertNull(resultPartyDto);
}

@Test
@SuppressWarnings("unchecked")
void putDefendantAccount_ErrorResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = createBrokenJson();

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountEntity.class), any(Class.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
DefendantAccountEntity resultPartyDto = legacyDefendantAccountService.putDefendantAccount(inputAccountEntity);

// Assert

assertNull(resultPartyDto);
}

@Test
@SuppressWarnings("unchecked")
void getParty_SuccessfulResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.getForEntity(any(String.class), any(Class.class), any(AccountEnquiryDto.class)))
.thenReturn(successfulResponseEntity);

// Act
AccountEnquiryDto enquiry = AccountEnquiryDto.builder().build();
DefendantAccountEntity resultAccountEntity = legacyDefendantAccountService.getDefendantAccount(enquiry);

// Assert
assertEquals(expectedAccountEntity, resultAccountEntity);
}

@Test
@SuppressWarnings("unchecked")
void getParty_FailureBodyResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();


ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
null, HttpStatus.OK);
when(restTemplate.getForEntity(any(String.class), any(Class.class), any(AccountEnquiryDto.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
AccountEnquiryDto enquiry = AccountEnquiryDto.builder().build();
DefendantAccountEntity resultAccountEntity = legacyDefendantAccountService.getDefendantAccount(enquiry);

// Assert

assertNull(resultAccountEntity);
}

@Test
@SuppressWarnings("unchecked")
void getParty_FailureCodeResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();


String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.INTERNAL_SERVER_ERROR);
when(restTemplate.getForEntity(any(String.class), any(Class.class), any(AccountEnquiryDto.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
AccountEnquiryDto enquiry = AccountEnquiryDto.builder().build();
DefendantAccountEntity resultAccountEntity = legacyDefendantAccountService.getDefendantAccount(enquiry);

// Assert

assertNull(resultAccountEntity);
}

@Test
@SuppressWarnings("unchecked")
void getParty_ErrorResponse() throws Exception {
// Arrange

String jsonBody = createBrokenJson();

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.OK);
when(restTemplate.getForEntity(any(String.class), any(Class.class), any(AccountEnquiryDto.class)))
.thenReturn(unsuccessfulResponseEntity);

// Act
AccountEnquiryDto enquiry = AccountEnquiryDto.builder().build();
DefendantAccountEntity resultAccountEntity = legacyDefendantAccountService.getDefendantAccount(enquiry);

// Assert

assertNull(resultAccountEntity);
}

@Test
@SuppressWarnings("unchecked")
void searchForParty_SuccessfulResponse() throws Exception {
// Arrange
AccountSearchResultsDto resultsDto = AccountSearchResultsDto.builder().totalCount(9L).build();
String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(resultsDto);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(AccountSearchDto.class), any(Class.class)))
.thenReturn(successfulResponseEntity);
// Act
AccountSearchResultsDto searchResultsDto = legacyDefendantAccountService
.searchDefendantAccounts(AccountSearchDto.builder().build());

// Assert
assertEquals(9L, searchResultsDto.getTotalCount());
}


private static String createBrokenJson() {
return """
{
"organisation" : false,
"organisationName" : null,
"surname" : "Smith",
"forenames" : "John James",
"initials" : "JJ",
"title" : "Mr",
"FOOBAR 1" : "22 Acacia Avenue",
"FOOBAR 2" : "Hammersmith",
"FOOBAR 3" : "Birmingham",
"FOOBAR 4" : "Cornwall",
"FOOBAR 5" : "Scotland",
"postcode" : "SN15 9TT",
"accountType" : "TFO",
"dateOfBirth" : [ 2001, 8, 16 ],
"age" : 21,
"niNumber" : "FF22446688",
"lastChangedDate" : [ 2023, 12, 5, 15, 45 ]
}
""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void saveParty_ErrorResponse() throws Exception {
final PartyDto inputPartyDto = new PartyDto();


String jsonBody = createJsonBody2();
String jsonBody = createBrokenJson();

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.OK);
Expand Down Expand Up @@ -186,7 +186,7 @@ void getParty_FailureCodeResponse() throws Exception {
void getParty_ErrorResponse() throws Exception {
// Arrange

String jsonBody = createJsonBody2();
String jsonBody = createBrokenJson();

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.OK);
Expand Down Expand Up @@ -237,7 +237,7 @@ private static String createJsonBody1() {
""";
}

private static String createJsonBody2() {
private static String createBrokenJson() {
return """
{
"organisation" : false,
Expand Down

0 comments on commit 2484dec

Please sign in to comment.