Skip to content

Commit

Permalink
Merge pull request #27 from companieshouse/DSDN-1696
Browse files Browse the repository at this point in the history
added get endpoint and service
  • Loading branch information
YaaseenR authored Sep 12, 2023
2 parents 7ea74ea + 19ab380 commit 21e8b20
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 31 deletions.
152 changes: 142 additions & 10 deletions src/itest/java/uk/gov/companieshouse/pscdataapi/steps/PscDataSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.LOCAL_DATE;
import static uk.gov.companieshouse.pscdataapi.config.AbstractMongoConfig.mongoDBContainer;

import org.springframework.util.FileCopyUtils;
import uk.gov.companieshouse.api.company.CompanyProfile;
import uk.gov.companieshouse.api.company.Data;
import uk.gov.companieshouse.api.model.CompanyProfileDocument;
import uk.gov.companieshouse.api.psc.Individual;
import uk.gov.companieshouse.api.psc.StatementList;
import uk.gov.companieshouse.pscdataapi.config.CucumberContext;
import uk.gov.companieshouse.pscdataapi.models.PscData;
import uk.gov.companieshouse.pscdataapi.models.PscDocument;
import uk.gov.companieshouse.pscdataapi.models.*;
import uk.gov.companieshouse.pscdataapi.transform.CompanyPscTransformer;
import uk.gov.companieshouse.pscdataapi.util.FileReaderUtil;
import uk.gov.companieshouse.pscdataapi.repository.CompanyPscRepository;

import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.*;


public class PscDataSteps {
Expand All @@ -53,9 +59,12 @@ public class PscDataSteps {
@Autowired
private CompanyPscRepository companyPscRepository;

@Autowired
private CompanyPscTransformer transformer;

private final String COMPANY_NUMBER = "34777772";

private final String NOTIFICATION_ID = "ZfTs9WeeqpXTqf6dc6FZ4C0H0RQ";
private final String NOTIFICATION_ID = "ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ";

@Before
public void dbCleanUp(){
Expand Down Expand Up @@ -210,21 +219,144 @@ public void aPSCExistsForAndDelta_atDeltaAt(String deltaAt) throws Throwable {

@And("a PSC exists for {string}")
public void aPSCExistsFor(String companyNumber) throws JsonProcessingException {
String pscDataFile = FileReaderUtil.readFile("src/itest/resources/json/input/34777772.json");
String pscDataFile = FileReaderUtil.readFile("src/itest/resources/json/input/"+companyNumber+".json");
PscData pscData = objectMapper.readValue(pscDataFile, PscData.class);

PscSensitiveData pscSensitiveData = objectMapper.readValue(pscDataFile, PscSensitiveData.class);
PscDocument document = new PscDocument();


document.setId(NOTIFICATION_ID);
document.setCompanyNumber(COMPANY_NUMBER);
document.setCompanyNumber(companyNumber);
document.setPscId("ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ");
document.setDeltaAt(String.valueOf(LocalDate.now()));
pscData.setEtag("string");
pscData.setCeasedOn(LocalDate.now());
pscData.setKind("individual-person-with-significant-control");
pscData.setCountryOfResidence("United Kingdom");
DateOfBirth dateOfBirth = new DateOfBirth();
dateOfBirth.setDay(21);
dateOfBirth.setMonth(10);
dateOfBirth.setYear(1995);
pscSensitiveData.setDateOfBirth(dateOfBirth);
pscData.setName("34777772");
NameElements nameElements = new NameElements();
nameElements.setTitle("Mr");
nameElements.setForename("PHIL");
nameElements.setMiddleName("tom");
nameElements.setSurname("JONES");
pscData.setNameElements(nameElements);
Links links = new Links();
links.setSelf("/company/34777772/persons-with-significant-control/individual/ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ");
links.setStatements("string");
pscData.setLinks(links);
pscData.setNationality("British");
Address address = new Address();
address.setAddressLine1("ura_line1");
address.setAddressLine2("ura_line2");
address.setCareOf("ura_care_of");
address.setCountry("United Kingdom");
address.setLocality("Cardiff");
address.setPoBox("ura_po");
address.setPostalCode("CF2 1B6");
address.setPremises("URA");
address.setRegion("ura_region");
pscData.setAddress(address);
List<String> list = new ArrayList<>();
list.add("part-right-to-share-surplus-assets-75-to-100-percent");
pscData.setNaturesOfControl(list);
document.setData(pscData);
document.setSensitiveData(pscSensitiveData);

mongoTemplate.save(document);
assertThat(companyPscRepository.findById(NOTIFICATION_ID)).isNotEmpty();
assertThat(companyPscRepository.getPscByCompanyNumberAndId(companyNumber,"ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ")).isPresent();
}




@When("a Get request is sent for {string} and {string}")
public void aGetRequestIsSentForAnd(String companyNumber, String notification_id) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

this.contextId = "5234234234";
CucumberContext.CONTEXT.set("contextId", this.contextId);
headers.set("x-request-id", this.contextId);
headers.set("ERIC-Identity", "TEST-IDENTITY");
headers.set("ERIC-Identity-Type", "key");
headers.set("ERIC-Authorised-Key-Roles", "*");

HttpEntity<String> request = new HttpEntity<String>(null, headers);

String uri = "/company/{company_number}/persons-with-significant-control/individual/{notification_id}";
ResponseEntity<Individual> response = restTemplate.exchange(uri,
HttpMethod.GET, request, Individual.class, companyNumber, notification_id);

CucumberContext.CONTEXT.set("statusCode", response.getStatusCodeValue());
CucumberContext.CONTEXT.set("getResponseBody", response.getBody());

}

@And("the Get call response body should match {string} file")
public void theGetCallResponseBodyShouldMatchFile(String result) throws IOException, TransformerException {
String data = FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream("src/itest/resources/json/output/" + result + ".json")));
Individual expected = objectMapper.readValue(data, Individual.class);

Individual actual = CucumberContext.CONTEXT.get("getResponseBody");

assertThat(actual.getName()).isEqualTo(expected.getName());
assertThat(actual.getCountryOfResidence()).isEqualTo(expected.getCountryOfResidence());
assertThat(actual.getDateOfBirth()).isEqualTo(expected.getDateOfBirth());
assertThat(actual.getNaturesOfControl()).isEqualTo(expected.getNaturesOfControl());
}

@After
public void dbStop(){
mongoDBContainer.stop();
}

@When("a Get request is sent for {string} and {string} without ERIC headers")
public void aGetRequestIsSentForAndWithoutERICHeaders(String companyNumber, String notification_id) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

this.contextId = "5234234234";
CucumberContext.CONTEXT.set("contextId", this.contextId);
headers.set("x-request-id", this.contextId);


HttpEntity<String> request = new HttpEntity<String>(null, headers);

String uri = "/company/{company_number}/persons-with-significant-control/individual/{notification_id}";
ResponseEntity<Individual> response = restTemplate.exchange(uri,
HttpMethod.GET, request, Individual.class, companyNumber, notification_id);

CucumberContext.CONTEXT.set("statusCode", response.getStatusCodeValue());
CucumberContext.CONTEXT.set("getResponseBody", response.getBody());

}

@When("a Get request has been sent for {string} and {string}")
public void aGetRequestHasBeenSentForAnd(String companyNumber, String notification_id) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

this.contextId = "5234234234";
CucumberContext.CONTEXT.set("contextId", this.contextId);
headers.set("x-request-id", this.contextId);
headers.set("ERIC-Identity", "TEST-IDENTITY");
headers.set("ERIC-Identity-Type", "key");
headers.set("ERIC-Authorised-Key-Roles", "*");

HttpEntity<String> request = new HttpEntity<String>(null, headers);

String uri = "/company/{company_number}/persons-with-significant-control/individual/{notification_id}";
ResponseEntity<Individual> response = restTemplate.exchange(uri,
HttpMethod.GET, request, Individual.class, companyNumber, notification_id);

CucumberContext.CONTEXT.set("statusCode", response.getStatusCodeValue());
}
}
35 changes: 35 additions & 0 deletions src/itest/resources/features/get_Individual.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Feature: Get individual

Scenario Outline: Get individual successfully
Given Psc data api service is running
And a PSC exists for "<company_number>"
When a Get request is sent for "<company_number>" and "<notificationId>"
And the Get call response body should match "<result>" file
Then I should receive 200 status code

Examples:
| company_number | notificationId | result |
| 34777772 | ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ | individual_get_request_result |



Scenario Outline: Get Individual when sending get request without Eric headers

Given Psc data api service is running
And a PSC exists for "<company_number>"
When a Get request is sent for "<company_number>" and "<notificationId>" without ERIC headers
Then I should receive 401 status code

Examples:
| company_number | notificationId |
| 34777772 | ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ |

Scenario Outline: Get PSC unsuccessfully - PSC resource does not exist
Given a PSC does not exist for "<company_number>"
When a Get request has been sent for "<company_number>" and "<notificationId>"
Then I should receive 404 status code

Examples:
| company_number | notificationId |
| 34777772 | ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ |

3 changes: 2 additions & 1 deletion src/itest/resources/features/psc_data.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Feature: Process Psc Data Requests

Given Psc data api service is running
When I send a PUT request with payload "<data>" file with notification id "<notificationId>"
Then I should receive 201 status code
And a record exists with id "<notificationId>"
Then I should receive 201 status code


Examples:
| notificationId | data |
Expand Down
37 changes: 37 additions & 0 deletions src/itest/resources/json/output/individual_get_request_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"address": {
"address_line_1" : "ura_line1",
"address_line_2" : "ura_line2",
"care_of" : "ura_care_of",
"country" : "United Kingdom",
"locality" : "Cardiff",
"po_box" : "ura_po",
"postal_code" : "CF2 1B6",
"premises" : "URA",
"region" : "ura_region"
},
"ceased_on": "2021-11-02T08:47:45",
"country_of_residence": "United Kingdom",
"date_of_birth": {
"day": 21,
"month": 10,
"year": 1995
},
"etag": "string",
"kind": "individual-person-with-significant-control",
"links": {
"self": "/company/34777772/persons-with-significant-control/individual/ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ",
"statement": "string"
},
"name": "34777772",
"name_elements": {
"forename": "PHIL",
"other_forenames": "tom",
"surname": "JONES",
"title": "Mr"
},
"nationality": "British",
"natures_of_control": [
"part-right-to-share-surplus-assets-75-to-100-percent"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public MongoClient mongoClient() {
return MongoClients.create(mongoClientSettings);
}

/** Mongo Mapping. */
@Bean
@Override
public MongoMappingContext mongoMappingContext(MongoCustomConversions customConversions) {
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setFieldNamingStrategy(new SnakeCaseFieldNamingStrategy());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.gov.companieshouse.pscdataapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -12,6 +14,7 @@
import org.springframework.web.bind.annotation.RestController;
import uk.gov.companieshouse.api.exception.ResourceNotFoundException;
import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi;
import uk.gov.companieshouse.api.psc.Individual;
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.logging.LoggerFactory;
import uk.gov.companieshouse.pscdataapi.exceptions.ServiceUnavailableException;
Expand All @@ -20,8 +23,8 @@

@RestController
@RequestMapping(
path = "/company/{company_number}/persons-with-significant-control/"
+ "{notification_id}/full_record", produces = "application/json")
path = "/company/{company_number}/persons-with-significant-control/",
produces = "application/json")
public class CompanyPscController {

@Autowired
Expand All @@ -35,7 +38,7 @@ public class CompanyPscController {
* @param request request payload.
* @return response.
* */
@PutMapping(consumes = "application/json")
@PutMapping(path = "{notification_id}/full_record", consumes = "application/json")
public ResponseEntity<Void> submitPscData(@RequestHeader("x-request-id") String contextId,
@RequestBody final FullRecordCompanyPSCApi request) {
try {
Expand All @@ -54,7 +57,7 @@ public ResponseEntity<Void> submitPscData(@RequestHeader("x-request-id") String
* @param companyNumber The number of the company
* @return ResponseEntity
*/
@DeleteMapping
@DeleteMapping(path = "{notification_id}/full_record")
public ResponseEntity<Void> deletePscData(
@PathVariable("company_number") String companyNumber,
@PathVariable("notification_id") String notificationId) {
Expand All @@ -73,4 +76,33 @@ public ResponseEntity<Void> deletePscData(
return ResponseEntity.internalServerError().build();
}
}

/**
* Get the data object for given company profile number.
*
* @param companyNumber The number of the company
* @return ResponseEntity
*/
@GetMapping("individual/{notification_id}")
public ResponseEntity<Individual> getIndividualPscData(
@PathVariable("company_number") String companyNumber,
@PathVariable("notification_id") String notificationId) {
LOGGER.info(String.format("Getting PSC data with company number %s", companyNumber));
try {

LOGGER.info(String.format(
"Retrieving PSC with company number %s",
companyNumber));
Individual individual = pscService.getIndividualPsc(companyNumber , notificationId);
return new ResponseEntity<>(individual, HttpStatus.OK);
} catch (ResourceNotFoundException resourceNotFoundException) {
LOGGER.error(resourceNotFoundException.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (DataAccessException exception) {
LOGGER.error(exception.getMessage());
return ResponseEntity.internalServerError().build();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import uk.gov.companieshouse.api.model.PscStatementDocument;
import uk.gov.companieshouse.pscdataapi.models.PscDocument;

public interface CompanyPscRepository extends MongoRepository<PscDocument, String> {
Expand All @@ -15,4 +14,5 @@ public interface CompanyPscRepository extends MongoRepository<PscDocument, Strin
@Query("{'company_number' : ?0, '_id' : ?1}")
Optional<PscDocument> getPscByCompanyNumberAndId(String companyNumber, String notificationId);


}
Loading

0 comments on commit 21e8b20

Please sign in to comment.