-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from companieshouse/feature/company-validator
Add company validator
- Loading branch information
Showing
7 changed files
with
242 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...va/uk/gov/companieshouse/pscverificationapi/exception/CompanyProfileServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/uk/gov/companieshouse/pscverificationapi/service/CompanyProfileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...java/uk/gov/companieshouse/pscverificationapi/service/impl/CompanyProfileServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/uk/gov/companieshouse/pscverificationapi/validator/CompanyValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/uk/gov/companieshouse/pscverificationapi/validator/CompanyValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |