Skip to content

Commit

Permalink
Merge pull request #138 from companieshouse/feature/DSND-2789-fix
Browse files Browse the repository at this point in the history
Feature/dsnd 2789 fix
  • Loading branch information
SALMANCH007 authored Jul 30, 2024
2 parents d6e69d2 + dba56c4 commit bf7e228
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.pscdataapi.api.ChsKafkaApiService;

Expand All @@ -21,4 +22,7 @@ public abstract class AbstractIntegrationTest extends AbstractMongoConfig {
@MockBean
public CompanyMetricsApiService companyMetricsApiService;

@MockBean
CompanyExemptionsApiService companyExemptionsApiService;

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.api.metrics.MetricsApi;
import uk.gov.companieshouse.api.psc.CorporateEntity;
Expand Down Expand Up @@ -83,7 +84,6 @@ public class PscDataSteps {
private ChsKafkaApiService chsKafkaApiService;
@Autowired
private CompanyPscTransformer transformer;

@InjectMocks
private CompanyPscService companyPscService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.api.converter.EnumWriteConverter;
import uk.gov.companieshouse.pscdataapi.converter.CompanyPscReadConverter;
Expand Down Expand Up @@ -54,6 +55,11 @@ public CompanyMetricsApiService companyMetricsApiService() {
return new CompanyMetricsApiService();
}

@Bean
public CompanyExemptionsApiService companyExemptionsApiService(){
return new CompanyExemptionsApiService();
}

@Bean
@Primary
public ObjectMapper objectMapper() {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/uk/gov/companieshouse/pscdataapi/models/Links.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Links {
@Field("statement")
private String statement;

@Field("exemptions")
private String exemptions;

public String getSelf() {
return self;
}
Expand All @@ -27,6 +30,10 @@ public void setStatement(String statement) {
this.statement = statement;
}

public String getExemptions() { return exemptions; }

public void setExemptions(String exemptions) { this.exemptions = exemptions; }

@Override
public String toString() {
return "Links{"
Expand All @@ -36,6 +43,9 @@ public String toString() {
+ ", statement='"
+ statement
+ '\''
+ ", exemptions='"
+ exemptions
+ '\''
+ '}';
}

Expand All @@ -49,11 +59,12 @@ public boolean equals(Object object) {
}
Links links = (Links) object;
return Objects.equals(self, links.self)
&& Objects.equals(statement, links.statement);
&& Objects.equals(statement, links.statement)
&& Objects.equals(exemptions, links.exemptions);
}

@Override
public int hashCode() {
return Objects.hash(self, statement);
return Objects.hash(self, statement, exemptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.api.exemptions.CompanyExemptions;
import uk.gov.companieshouse.api.metrics.MetricsApi;
import uk.gov.companieshouse.api.metrics.RegisterApi;
import uk.gov.companieshouse.api.metrics.RegistersApi;
Expand Down Expand Up @@ -57,16 +61,21 @@ public class CompanyPscService {
private final ChsKafkaApiService chsKafkaApiService;
private final CompanyMetricsApiService companyMetricsApiService;

@Autowired
CompanyExemptionsApiService companyExemptionsApiService;

public CompanyPscService(Logger logger,
CompanyPscTransformer transformer,
CompanyPscRepository repository,
ChsKafkaApiService chsKafkaApiService,
CompanyMetricsApiService companyMetricsApiService) {
CompanyMetricsApiService companyMetricsApiService,
CompanyExemptionsApiService companyExemptionsApiService) {
this.logger = logger;
this.transformer = transformer;
this.repository = repository;
this.chsKafkaApiService = chsKafkaApiService;
this.companyMetricsApiService = companyMetricsApiService;
this.companyExemptionsApiService = companyExemptionsApiService;
}

/**
Expand Down Expand Up @@ -601,6 +610,12 @@ private PscList createPscDocumentList(List<PscDocument> pscDocuments,
pscList.setStartIndex(startIndex);
pscList.setItems(documents);

if (hasPscExemptions(companyNumber)) {
if(hasExemptFrom(companyNumber) && !hasExemptTo(companyNumber)) {
links.setExemptions(String.format("/company/%s/exemptions", companyNumber));
}
}

companyMetrics.ifPresentOrElse(metricsApi -> {
try {
if (registerView) {
Expand Down Expand Up @@ -632,4 +647,49 @@ private PscList createPscDocumentList(List<PscDocument> pscDocuments,
return pscList;
}

private boolean hasPscExemptions(String companyNumber) {
Optional<CompanyExemptions> companyExemptions = companyExemptionsApiService.getCompanyExemptions(companyNumber);

return companyExemptions.filter(x ->
x.getExemptions() != null &&
(x.getExemptions().getPscExemptAsSharesAdmittedOnMarket()!= null ||
x.getExemptions().getPscExemptAsTradingOnEuRegulatedMarket() != null ||
x.getExemptions().getPscExemptAsTradingOnRegulatedMarket() != null ||
x.getExemptions().getPscExemptAsTradingOnUkRegulatedMarket() != null)).isPresent();
}

private boolean hasExemptFrom(String companyNumber) {
Optional<CompanyExemptions> companyExemptions = companyExemptionsApiService.getCompanyExemptions(companyNumber);

return companyExemptions
.map(exemptions -> exemptions.getExemptions())
.filter(exemptions -> exemptions != null)
.map(exemptions -> exemptions.getPscExemptAsSharesAdmittedOnMarket() != null
&& exemptions.getPscExemptAsSharesAdmittedOnMarket().getItems().get(0).getExemptFrom() != null ||
exemptions.getPscExemptAsTradingOnEuRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnEuRegulatedMarket().getItems().get(0).getExemptFrom() != null ||
exemptions.getPscExemptAsTradingOnRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnRegulatedMarket().getItems().get(0).getExemptFrom() != null ||
exemptions.getPscExemptAsTradingOnUkRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnUkRegulatedMarket().getItems().get(0).getExemptFrom() != null)
.orElse(false);
}

private boolean hasExemptTo(String companyNumber) {
Optional<CompanyExemptions> companyExemptions = companyExemptionsApiService.getCompanyExemptions(companyNumber);

return companyExemptions
.map(exemptions -> exemptions.getExemptions())
.filter(exemptions -> exemptions != null)
.map(exemptions -> exemptions.getPscExemptAsSharesAdmittedOnMarket() != null
&& exemptions.getPscExemptAsSharesAdmittedOnMarket().getItems().get(0).getExemptTo() != null ||
exemptions.getPscExemptAsTradingOnEuRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnEuRegulatedMarket().getItems().get(0).getExemptTo() != null ||
exemptions.getPscExemptAsTradingOnRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnRegulatedMarket().getItems().get(0).getExemptTo() != null ||
exemptions.getPscExemptAsTradingOnUkRegulatedMarket() != null
&& exemptions.getPscExemptAsTradingOnUkRegulatedMarket().getItems().get(0).getExemptTo() != null)
.orElse(false);
}

}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ management.endpoints.web.path-mapping.health=/healthcheck
chs.api.kafka.url=${CHS_KAFKA_API_URL:localhost}
chs.api.kafka.resource-changed.uri=${PSC_API_RESOURCE_CHANGED_URI:/private/resource-changed}
chs.api.metrics.url=${API_LOCAL_URL:localhost}
chs.api.exemptions.url=${COMPANY_EXEMPTIONS_API_LOCAL_URL:https://localhost}

spring.data.mongodb.uri=${MONGODB_URL:mongodb://mongo:27017}
spring.data.mongodb.name=company_pscs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@SpringBootTest
class PscDataApiApplicationTests {

@Test
void contextLoads() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.chskafka.ChangedResource;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.chskafka.PrivateChangedResourceHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.test.context.ActiveProfiles;

import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.chskafka.ChangedResource;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.chskafka.PrivateChangedResourceHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.companieshouse.pscdataapi.service;

import static com.mongodb.internal.connection.tlschannel.util.Util.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -29,7 +30,11 @@
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.api.api.CompanyExemptionsApiService;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.api.exemptions.CompanyExemptions;
import uk.gov.companieshouse.api.exemptions.Exemptions;
import uk.gov.companieshouse.api.exemptions.PscExemptAsTradingOnUkRegulatedMarketItem;
import uk.gov.companieshouse.api.metrics.MetricsApi;
import uk.gov.companieshouse.api.metrics.RegisterApi;
import uk.gov.companieshouse.api.metrics.RegistersApi;
Expand All @@ -50,6 +55,7 @@
import uk.gov.companieshouse.pscdataapi.exceptions.BadRequestException;
import uk.gov.companieshouse.pscdataapi.exceptions.ResourceNotFoundException;
import uk.gov.companieshouse.pscdataapi.models.Created;
import uk.gov.companieshouse.pscdataapi.models.Links;
import uk.gov.companieshouse.pscdataapi.models.PscData;
import uk.gov.companieshouse.pscdataapi.models.PscDocument;
import uk.gov.companieshouse.pscdataapi.repository.CompanyPscRepository;
Expand All @@ -74,6 +80,8 @@ class CompanyPscServiceTest {
private CompanyPscTransformer transformer;
@Mock
private ChsKafkaApiService chsKafkaApiService;
@Mock
CompanyExemptionsApiService companyExemptionsApiService;
@Captor
private ArgumentCaptor<String> dateCaptor;
@Spy
Expand All @@ -88,6 +96,9 @@ class CompanyPscServiceTest {
private OffsetDateTime date;
private OffsetDateTime laterDate;

private TestHelper testHelper;
private PscExemptAsTradingOnUkRegulatedMarketItem pscExemptAsTradingOnUkRegulatedMarketItem;

@BeforeEach
void setUp() {
date = TestHelper.createOffsetDateTime();
Expand All @@ -96,6 +107,9 @@ void setUp() {

request = TestHelper.buildBasicFullRecordPsc();
pscDocument = TestHelper.buildBasicDocument();

testHelper = new TestHelper();
pscExemptAsTradingOnUkRegulatedMarketItem = new PscExemptAsTradingOnUkRegulatedMarketItem();
}

@Test
Expand Down Expand Up @@ -716,4 +730,62 @@ void whenCompanyNotInPublicRegisterGetPSCListShouldThrow() throws ResourceNotFou
verify(repository, times(0)).getListSummaryRegisterView(COMPANY_NUMBER, 0, OffsetDateTime.parse("2020-12-20T06:00Z"), 25);
}

@Test
void pscListReturnedByCompanyNumberFromRepositoryWithExemptions() throws ResourceNotFoundException {
PscList expectedPscList = TestHelper.createPscListWithExemptions();
PscData pscData = new PscData();
pscDocument.setData(pscData);
ListSummary listSummary = new ListSummary();
Identification identification = new Identification();
identification.setPlaceRegistered("x");
identification.setCountryRegistered("x");
identification.setRegistrationNumber("x");
identification.setLegalAuthority("x");
identification.setLegalForm("x");
listSummary.setIdentification(identification);

when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER))
.thenReturn(Optional.of(TestHelper.createMetrics()));
when(repository.getPscDocumentList(anyString(), anyInt(), anyInt())).thenReturn(Optional.of(Collections.singletonList(pscDocument)));
when(transformer.transformPscDocToListSummary(pscDocument, false))
.thenReturn(listSummary);
when(companyExemptionsApiService.getCompanyExemptions(any())).thenReturn(Optional.ofNullable(testHelper.createExemptions()));

PscList PscDocumentList = service.retrievePscListSummaryFromDb(COMPANY_NUMBER, 0, false, 25);

Assertions.assertEquals(expectedPscList, PscDocumentList);
verify(repository, times(1)).getPscDocumentList(COMPANY_NUMBER, 0, 25);
}

@Test
void hasPscExemptionsReturnsTrueWhenTradingOnUkRegulatedMarket() {
PscData pscData = new PscData();
pscDocument.setData(pscData);
ListSummary listSummary = new ListSummary();
Identification identification = new Identification();
identification.setPlaceRegistered("x");
identification.setCountryRegistered("x");
identification.setRegistrationNumber("x");
identification.setLegalAuthority("x");
identification.setLegalForm("x");
listSummary.setIdentification(identification);
when(repository.getPscDocumentList(anyString(), anyInt(), anyInt())).thenReturn(Optional.of(Collections.singletonList(pscDocument)));
when(transformer.transformPscDocToListSummary(pscDocument, false))
.thenReturn(listSummary);

CompanyExemptions companyExemptions = new CompanyExemptions();
Exemptions exemptions = new Exemptions();
exemptions.setPscExemptAsTradingOnUkRegulatedMarket(pscExemptAsTradingOnUkRegulatedMarketItem);
companyExemptions.setExemptions(testHelper.getUkExemptions());
when(companyExemptionsApiService.getCompanyExemptions(any())).thenReturn(Optional.ofNullable(testHelper.createExemptions()));

PscList PscDocumentList = service.retrievePscListSummaryFromDb(COMPANY_NUMBER, 0, false, 25);

Links links = new Links();
links.setSelf("/company/" + COMPANY_NUMBER + "/persons-with-significant-control");
links.setExemptions("/company/" + COMPANY_NUMBER + "/exemptions");

assertEquals(PscDocumentList.getLinks(), links);
}

}
Loading

0 comments on commit bf7e228

Please sign in to comment.