Skip to content

Commit

Permalink
Merge pull request #44 from companieshouse/feature/company-validator
Browse files Browse the repository at this point in the history
Add company validator
  • Loading branch information
eedwards0 authored Sep 27, 2024
2 parents ba48598 + f8120be commit 7ed0243
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import uk.gov.companieshouse.pscverificationapi.enumerations.PscType;
import uk.gov.companieshouse.pscverificationapi.validator.CompanyValidator;
import uk.gov.companieshouse.pscverificationapi.validator.PscIdProvidedValidator;
import uk.gov.companieshouse.pscverificationapi.validator.ValidationChainEnable;
import uk.gov.companieshouse.pscverificationapi.validator.VerificationValidationChain;
Expand All @@ -16,17 +17,22 @@ public class ValidatorConfig {
//TODO add a 2nd validation chain for the RLE journey if required

@Bean
public ValidationChainEnable verificationValidationEnable(final PscIdProvidedValidator pscIdProvidedValidator, final PscExistsValidator pscExistsValidator, final PscIsActiveValidator pscIsActiveValidator) {
createValidationChain(pscIdProvidedValidator, pscExistsValidator, pscIsActiveValidator);
public ValidationChainEnable verificationValidationEnable(final PscIdProvidedValidator pscIdProvidedValidator,
final PscExistsValidator pscExistsValidator, final PscIsActiveValidator pscIsActiveValidator,
final CompanyValidator companyValidator) {

createValidationChain(pscIdProvidedValidator, pscExistsValidator, pscIsActiveValidator, companyValidator);

return new VerificationValidationChain(PscType.INDIVIDUAL, pscIdProvidedValidator);
}

private static void createValidationChain(final PscIdProvidedValidator pscIdProvidedValidator,
final PscExistsValidator pscExistsValidator, final PscIsActiveValidator pscIsActiveValidator) {
pscIdProvidedValidator.setNext(pscExistsValidator);
final PscExistsValidator pscExistsValidator, final PscIsActiveValidator pscIsActiveValidator,
final CompanyValidator companyValidator) {

pscIdProvidedValidator.setNext(pscExistsValidator);
pscExistsValidator.setNext(pscIsActiveValidator);
pscIsActiveValidator.setNext(companyValidator);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.companieshouse.pscverificationapi.exception;

/**
* Company Profile not found or external query failed.
*/
public class CompanyProfileServiceException extends RuntimeException {
public CompanyProfileServiceException(final String s, final Exception e) {
super(s, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.gov.companieshouse.pscverificationapi.service;

import uk.gov.companieshouse.api.model.company.CompanyProfileApi;
import uk.gov.companieshouse.api.model.pscverification.PscVerificationData;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.pscverificationapi.exception.CompanyProfileServiceException;

/**
* The company profile service layer responsible for
* retrieving company profile data from Company Profile API.
*/
public interface CompanyProfileService {

/**
* Query the company profile service for a given transaction.
*
* @param transaction the transaction
* @param dto the psc verification data
* @param ericPassThroughHeader includes authorisation details
* @return the company profile if found
* @throws CompanyProfileServiceException if not found or an error occurred
*/
CompanyProfileApi getCompanyProfile(final Transaction transaction, final PscVerificationData dto, final String ericPassThroughHeader)
throws CompanyProfileServiceException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package uk.gov.companieshouse.pscverificationapi.service.impl;

import org.springframework.stereotype.Service;
import uk.gov.companieshouse.api.handler.exception.URIValidationException;
import uk.gov.companieshouse.api.model.company.CompanyProfileApi;
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.exception.CompanyProfileServiceException;
import uk.gov.companieshouse.pscverificationapi.service.CompanyProfileService;
import uk.gov.companieshouse.pscverificationapi.utils.LogHelper;

import java.io.IOException;

@Service
public class CompanyProfileServiceImpl implements CompanyProfileService {

private final ApiClientService apiClientService;
private final Logger logger;

public CompanyProfileServiceImpl(ApiClientService apiClientService, Logger logger) {
this.apiClientService = apiClientService;
this.logger = logger;
}

@Override
public CompanyProfileApi getCompanyProfile(final Transaction transaction, final PscVerificationData dto,
final String ericPassThroughHeader)
throws CompanyProfileServiceException {

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

try {
final String uri = "/company/" + dto.companyNumber();
final CompanyProfileApi companyProfile = apiClientService.getApiClient(ericPassThroughHeader)
.company()
.get(uri)
.execute()
.getData();
logMap.put("company_number", dto.companyNumber());
logMap.put("company_name", companyProfile.getCompanyName());
logger.debugContext(transaction.getId(), "Retrieved company profile details", logMap);
return companyProfile;
}
catch (final URIValidationException | IOException e) {
throw new CompanyProfileServiceException("Error Retrieving company profile " + dto.companyNumber(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.gov.companieshouse.pscverificationapi.validator;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.validation.FieldError;
import uk.gov.companieshouse.api.model.company.CompanyProfileApi;
import uk.gov.companieshouse.pscverificationapi.service.CompanyProfileService;

import java.util.List;
import java.util.Map;

@Component
public class CompanyValidator extends BaseVerificationValidator implements
VerificationValidator {

private final CompanyProfileService companyProfileService;
private final Map<String, List<String>> company;

public CompanyValidator(final CompanyProfileService companyProfileService,
@Qualifier(value = "validation") Map<String, String> validation,
@Qualifier(value = "company") Map<String, List<String>> company) {
super(validation);
this.companyProfileService = companyProfileService;
this.validation = validation;
this.company = company;
}

/**
* Validates if the company type is allowed.
*
* @param validationContext the validation context
*/
@Override
public void validate(final VerificationValidationContext validationContext) {

CompanyProfileApi companyProfile = companyProfileService.getCompanyProfile(validationContext.transaction(),
validationContext.dto(), validationContext.passthroughHeader());

if (companyProfile != null && !company.get("type-allowed").contains(companyProfile.getType())) {

validationContext.errors().add(
new FieldError("object", "type", companyProfile.getType(), false,
new String[]{null, "data.type"}, null, validation.get("company-type-not-allowed")));
}

super.validate(validationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.pscverificationapi.enumerations.PscType;
import uk.gov.companieshouse.pscverificationapi.validator.CompanyValidator;
import uk.gov.companieshouse.pscverificationapi.validator.PscExistsValidator;
import uk.gov.companieshouse.pscverificationapi.validator.PscIdProvidedValidator;
import uk.gov.companieshouse.pscverificationapi.validator.PscIsActiveValidator;
Expand All @@ -26,6 +27,8 @@ class ValidatorConfigTest {
private PscExistsValidator pscExistsValidator;
@Mock
private PscIsActiveValidator pscIsActiveValidator;
@Mock
private CompanyValidator companyValidator;

@BeforeEach
void setUp() {
Expand All @@ -34,11 +37,13 @@ void setUp() {

@Test
void verificationValidationEnable() {
final var valid = testConfig.verificationValidationEnable(pscIdProvidedValidator, pscExistsValidator, pscIsActiveValidator);
final var valid = testConfig.verificationValidationEnable(pscIdProvidedValidator, pscExistsValidator,
pscIsActiveValidator, companyValidator);

assertThat(valid.pscType(), is(PscType.INDIVIDUAL));
assertThat(valid.first(), is(pscIdProvidedValidator));
verify(pscExistsValidator, times(1)).setNext(pscIsActiveValidator);
verify(pscIsActiveValidator, times(1)).setNext(companyValidator);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.gov.companieshouse.pscverificationapi.validator;

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.validation.FieldError;
import uk.gov.companieshouse.api.model.company.CompanyProfileApi;
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.service.CompanyProfileService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

@ExtendWith(MockitoExtension.class)
class CompanyValidatorTest {

@Mock
private CompanyProfileApi companyProfile;
@Mock
private CompanyProfileService companyProfileService;
@Mock
private Map<String, String> validation;
@Mock
private Map<String, List<String>> company;
@Mock
private PscVerificationData pscVerificationData;
@Mock
private Transaction transaction;

CompanyValidator testValidator;
private PscType pscType;
private List<FieldError> errors;
private String passthroughHeader;

private static final List<String> companyTypeList = new ArrayList<>();

@BeforeEach
void setUp() {

errors = new ArrayList<>();
pscType = PscType.INDIVIDUAL;
passthroughHeader = "passthroughHeader";

testValidator = new CompanyValidator(companyProfileService, validation, company);
}

@AfterEach
void tearDown() {
}

@Test
void validateWhenPscExists() {

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

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

}

@Test
void validateWhenCompanyTypeNotAllowed() {

var fieldError = new FieldError("object", "type", companyProfile.getType(), false,
new String[]{null, "data.type"}, null, "type not allowed default message");

when(companyProfileService.getCompanyProfile(transaction, pscVerificationData, passthroughHeader)).thenReturn(companyProfile);
when(companyProfile.getType()).thenReturn(null);
when(company.get("type-allowed")).thenReturn(companyTypeList);
when(validation.get("company-type-not-allowed")).thenReturn("type not allowed default message");

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

assertThat(errors.stream().findFirst().orElseThrow(), equalTo(fieldError));
assertThat(errors, IsIterableContainingInOrder.contains(fieldError));
}

}

0 comments on commit 7ed0243

Please sign in to comment.