Skip to content

Commit

Permalink
Merge pull request #40 from companieshouse/feature/get-psc-details
Browse files Browse the repository at this point in the history
Feature/get psc details
  • Loading branch information
clewis1 authored Sep 18, 2024
2 parents c42eb59 + 1f8030e commit a2ce5b5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.companieshouse.pscverificationapi.service;

import uk.gov.companieshouse.api.model.psc.PscApi;
import uk.gov.companieshouse.api.model.pscverification.PscVerificationData;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.pscverificationapi.enumerations.PscType;
import uk.gov.companieshouse.pscverificationapi.exception.PscLookupServiceException;
Expand All @@ -13,12 +14,12 @@ public interface PscLookupService {
* Retrieve a PSC by ID.
*
* @param transaction the Transaction
* @param pscId the PSC Id
* @param data the psc verification data
* @param pscType the PSC Type
* @param ericPassThroughHeader includes authorisation for transaction fetch
* @return the PSC details, if found
* @throws PscLookupServiceException if the PSC was not found or an error occurred
*/
PscApi getPsc(Transaction transaction, String pscId, PscType pscType, final String ericPassThroughHeader)
PscApi getPsc(Transaction transaction, PscVerificationData data, PscType pscType, final String ericPassThroughHeader)
throws PscLookupServiceException;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package uk.gov.companieshouse.pscverificationapi.service.impl;

import java.io.IOException;
import java.text.MessageFormat;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.exception.URIValidationException;
import uk.gov.companieshouse.api.model.psc.PscApi;
import uk.gov.companieshouse.api.model.pscverification.PscVerificationData;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.api.sdk.ApiClientService;
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.pscverificationapi.enumerations.PscType;
import uk.gov.companieshouse.pscverificationapi.exception.FilingResourceNotFoundException;
import uk.gov.companieshouse.pscverificationapi.exception.FilingResourceInvalidException;
import uk.gov.companieshouse.pscverificationapi.exception.PscLookupServiceException;
import uk.gov.companieshouse.pscverificationapi.service.PscLookupService;
import uk.gov.companieshouse.pscverificationapi.utils.LogHelper;

//TODO This service is currently stubbed out to return the following responses:
// i) A valid PSC for all IDs except for:
// ii) PSC with ID '1kdaTltWeaP1EB70SSD9SLmiK5Z' - this is mocked as ceased
// ii) PSC with ID 'doesNotExist' - this is mocked as a PSC that does not exist

@Service
public class PscLookupServiceImpl implements PscLookupService {
Expand All @@ -31,28 +28,41 @@ public PscLookupServiceImpl(final ApiClientService apiClientService, Logger logg
this.logger = logger;
}

//FIXME: Remove temporary stubbing of this service
@Override
public PscApi getPsc(final Transaction transaction, final String pscId,
final PscType pscType, final String ericPassThroughHeader)
public PscApi getPsc(final Transaction transaction, final PscVerificationData data,
final PscType pscType, final String ericPassThroughHeader)
throws PscLookupServiceException {

final var logMap = LogHelper.createLogMap(transaction.getId());
PscApi psc;

//if psc already ceased
if (pscId.matches("1kdaTltWeaP1EB70SSD9SLmiK5Z")) {
throw new FilingResourceInvalidException(
MessageFormat.format("PSC is already ceased for {0}: {1} {2}", pscId,
HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()));

} else if (pscId.matches("doesNotExist")) {
throw new FilingResourceNotFoundException(
MessageFormat.format("PSC Details not found for {0}: {1} {2}", pscId,
HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()));
} else {
psc = new PscApi();
return psc;
String pscAppointmentId = data.pscAppointmentId();

try {
final var uri = "/company/"
+ data.companyNumber()
+ "/persons-with-significant-control/"
+ pscType.getValue()
+ "/"
+ pscAppointmentId;

return apiClientService.getApiClient(ericPassThroughHeader)
.pscs()
.getIndividual(uri)
.execute()
.getData();

} catch (final ApiErrorResponseException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND.value()) {
throw new FilingResourceNotFoundException(
MessageFormat.format("PSC Details not found for {0}: {1} {2}", pscAppointmentId,
e.getStatusCode(), e.getStatusMessage()), e);
}
throw new PscLookupServiceException(
MessageFormat.format("Error Retrieving PSC details for {0}: {1} {2}", pscAppointmentId,
e.getStatusCode(), e.getStatusMessage()), e);

} catch (URIValidationException | IOException e) {
throw new PscLookupServiceException(
MessageFormat.format("Error Retrieving PSC details for {0}: {1}", pscAppointmentId,
e.getMessage()), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PscExistsValidator(Map<String, String> validation, final PscLookupService
public void validate(final VerificationValidationContext validationContext) {

try {
pscLookupService.getPsc(validationContext.transaction(), validationContext.dto().pscAppointmentId(), validationContext.pscType(),
pscLookupService.getPsc(validationContext.transaction(), validationContext.dto(), validationContext.pscType(),
validationContext.passthroughHeader());
//TODO - handle - Validation should not continue if the PSC does not exist
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PscIsActiveValidator(final Map<String, String> validation, final PscLooku
public void validate(final VerificationValidationContext validationContext) {

try {
pscLookupService.getPsc(validationContext.transaction(), validationContext.dto().pscAppointmentId(), validationContext.pscType(),
pscLookupService.getPsc(validationContext.transaction(), validationContext.dto(), validationContext.pscType(),
validationContext.passthroughHeader());

} catch (FilingResourceInvalidException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package uk.gov.companieshouse.pscverificationapi.validator;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
Expand Down Expand Up @@ -61,7 +61,6 @@ void tearDown() {

@Test
void validateWhenPscExists() {
when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);

testValidator.validate(
new VerificationValidationContext(pscVerificationData, errors, transaction, pscType, passthroughHeader));
Expand All @@ -76,7 +75,7 @@ void validateWhenPscDoesNotExist() {
new String[]{null, PSC_ID}, null,
"not-exists default message");
when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);
when(pscLookupService.getPsc(transaction, PSC_ID, pscType,
when(pscLookupService.getPsc(transaction, pscVerificationData, pscType,
passthroughHeader)).thenThrow(new FilingResourceNotFoundException(
"PSC Details not found for " + PSC_ID + ": 404 Not Found", errorResponseException));
when(validation.get("psc_appointment_id-not-found")).thenReturn("not-exists default message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,30 @@ void setUp() {
passthroughHeader = "passthroughHeader";

testValidator = new PscIsActiveValidator(validation, pscLookupService);
when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);
when(pscLookupService.getPsc(transaction, PSC_ID, pscType, passthroughHeader)).thenReturn(pscApi);
when(pscLookupService.getPsc(transaction, pscVerificationData, pscType, passthroughHeader)).thenReturn(pscApi);
}

@Test
void validateWhenPscIsActive() {

when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);

testValidator.validate(
new VerificationValidationContext(pscVerificationData, errors, transaction, pscType, passthroughHeader));

assertThat(errors, is(empty()));
}


@Test
void validateWhenPscIsCeased() {

when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);

var fieldError = new FieldError("object", "psc_appointment_id", pscVerificationData.pscAppointmentId(), false,
new String[]{null, PSC_ID}, null, "is ceased default message");

when(pscVerificationData.pscAppointmentId()).thenReturn(PSC_ID);
when(pscLookupService.getPsc(transaction, PSC_ID, pscType,
passthroughHeader)).thenThrow(new FilingResourceInvalidException(
"PSC is already ceased for " + PSC_ID + ": 400 Bad Request", errorResponseException));
when(validation.get("psc-is-ceased")).thenReturn("is ceased default message");
when(pscLookupService.getPsc(transaction, pscVerificationData, pscType,
passthroughHeader)).thenThrow(new FilingResourceInvalidException(
"PSC is already ceased for " + PSC_ID + ": 400 Bad Request", errorResponseException));
when(validation.get("psc-is-ceased")).thenReturn("is ceased default message");

testValidator.validate(
new VerificationValidationContext(pscVerificationData, errors, transaction, pscType, passthroughHeader));
Expand Down

0 comments on commit a2ce5b5

Please sign in to comment.