diff --git a/src/test/java/uk/gov/hmcts/opal/service/LegacyDefendantAccountServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/LegacyDefendantAccountServiceTest.java new file mode 100644 index 000000000..b8805a17b --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/service/LegacyDefendantAccountServiceTest.java @@ -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 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 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 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 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 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 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 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 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 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 ] + } + """; + } +} diff --git a/src/test/java/uk/gov/hmcts/opal/service/LegacyPartyServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/LegacyPartyServiceTest.java index 2b51bc987..436c12ca3 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/LegacyPartyServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/LegacyPartyServiceTest.java @@ -103,7 +103,7 @@ void saveParty_ErrorResponse() throws Exception { final PartyDto inputPartyDto = new PartyDto(); - String jsonBody = createJsonBody2(); + String jsonBody = createBrokenJson(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.OK); @@ -186,7 +186,7 @@ void getParty_FailureCodeResponse() throws Exception { void getParty_ErrorResponse() throws Exception { // Arrange - String jsonBody = createJsonBody2(); + String jsonBody = createBrokenJson(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.OK); @@ -237,7 +237,7 @@ private static String createJsonBody1() { """; } - private static String createJsonBody2() { + private static String createBrokenJson() { return """ { "organisation" : false,