Skip to content

Commit

Permalink
Merge pull request #41 from companieshouse/lp-218-api-transaction-int…
Browse files Browse the repository at this point in the history
…erceptor

LP-218 API transaction interceptor to get transaction
  • Loading branch information
mattch1 authored Nov 25, 2024
2 parents f021db9 + 9636c05 commit e171014
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<log4j.version>2.24.1</log4j.version>
<jib-maven-plugin.version>3.4.2</jib-maven-plugin.version>
<api-security-java.version>2.0.8</api-security-java.version>
<api-sdk-manager-java-library.version>3.0.6</api-sdk-manager-java-library.version>
</properties>

<profiles>
Expand Down Expand Up @@ -87,6 +88,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>uk.gov.companieshouse</groupId>
<artifactId>api-sdk-manager-java-library</artifactId>
<version>${api-sdk-manager-java-library.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.companieshouse</groupId>
<artifactId>api-security-java</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions spec/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
}
],
"paths": {
"/transactions/{transaction_id}/limited-partnership/partnership": {
"/transactions/{transactionId}/limited-partnership/partnership": {
"post": {
"tags": [
"Limited Partnerships"
],
"summary": "Create a Limited Partnership submission",
"parameters": [
{
"name": "transaction_id",
"name": "transactionId",
"in": "path",
"required": true,
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.companieshouse.limitedpartnershipsapi.client;

import org.springframework.stereotype.Component;
import uk.gov.companieshouse.api.ApiClient;
import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.sdk.ApiClientService;
import uk.gov.companieshouse.sdk.manager.ApiSdkManager;

import java.io.IOException;

@Component
public class ApiClientServiceImpl implements ApiClientService {


@Override
public ApiClient getApiClient() {
return ApiSdkManager.getSDK();
}

@Override
public ApiClient getApiClient(String ericPassThroughHeader) throws IOException {
return ApiSdkManager.getSDK(ericPassThroughHeader);
}

@Override
public InternalApiClient getInternalApiClient() {
return ApiSdkManager.getPrivateSDK();
}

@Override
public InternalApiClient getInternalApiClient(String passthroughHeader) throws IOException {
return ApiSdkManager.getPrivateSDK(passthroughHeader);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package uk.gov.companieshouse.limitedpartnershipsapi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import uk.gov.companieshouse.api.interceptor.TokenPermissionsInterceptor;
import uk.gov.companieshouse.api.interceptor.TransactionInterceptor;
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.CustomUserAuthenticationInterceptor;
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.LoggingInterceptor;

import static uk.gov.companieshouse.limitedpartnershipsapi.LimitedPartnershipsApiApplication.APP_NAMESPACE;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

private static final String PARTNERSHIP = "/transactions/**/partnership";
private static final String TRANSACTIONS = "/transactions/**";
private static final String FILINGS = "/private/**/filings";
private static final String PARTNERSHIP = TRANSACTIONS + "/partnership";

private final LoggingInterceptor loggingInterceptor;

private final CustomUserAuthenticationInterceptor customUserAuthenticationInterceptor;


public InterceptorConfig(LoggingInterceptor loggingInterceptor, CustomUserAuthenticationInterceptor customUserAuthenticationInterceptor) {
public InterceptorConfig(LoggingInterceptor loggingInterceptor,
CustomUserAuthenticationInterceptor customUserAuthenticationInterceptor) {
this.loggingInterceptor = loggingInterceptor;
this.customUserAuthenticationInterceptor = customUserAuthenticationInterceptor;
}
Expand All @@ -31,9 +37,16 @@ public InterceptorConfig(LoggingInterceptor loggingInterceptor, CustomUserAuthen
@Override
public void addInterceptors(@NonNull InterceptorRegistry registry) {
registry.addInterceptor(loggingInterceptor);
registry.addInterceptor(transactionInterceptor())
.addPathPatterns(FILINGS, TRANSACTIONS);
registry.addInterceptor(new TokenPermissionsInterceptor())
.addPathPatterns(PARTNERSHIP);
registry.addInterceptor(customUserAuthenticationInterceptor)
.addPathPatterns(PARTNERSHIP);
}

@Bean
public TransactionInterceptor transactionInterceptor() {
return new TransactionInterceptor(APP_NAMESPACE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipService;
Expand All @@ -18,6 +20,7 @@

import static uk.gov.companieshouse.api.util.security.EricConstants.ERIC_IDENTITY;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_REQUEST_ID_KEY;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.TRANSACTION_KEY;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_TRANSACTION_ID;

@RestController
Expand All @@ -35,11 +38,12 @@ public PartnershipController(LimitedPartnershipService limitedPartnershipService

@PostMapping
public ResponseEntity<Object> createPartnership(
@RequestAttribute(TRANSACTION_KEY) Transaction transaction,
@RequestBody LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto,
@RequestHeader(value = ERIC_REQUEST_ID_KEY) String requestId,
@RequestHeader(value = ERIC_IDENTITY) String userId) {

var transactionId = 12321123;
var transactionId = transaction.getId();
var logMap = new HashMap<String, Object>();
logMap.put(URL_PARAM_TRANSACTION_ID, transactionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ private Constants() { }
public static final String ERIC_REQUEST_ID_KEY = "X-Request-Id";

// URL path parameters
public static final String URL_PARAM_TRANSACTION_ID = "transaction_id";
public static final String URL_PARAM_TRANSACTION_ID = "transactionId";

public static final String TRANSACTION_KEY = "transaction";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import uk.gov.companieshouse.api.interceptor.TokenPermissionsInterceptor;
import uk.gov.companieshouse.api.interceptor.TransactionInterceptor;
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.CustomUserAuthenticationInterceptor;
import uk.gov.companieshouse.limitedpartnershipsapi.interceptor.LoggingInterceptor;

Expand Down Expand Up @@ -44,9 +45,10 @@ void addInterceptorsTest() {

InOrder inOrder = inOrder(interceptorRegistry, interceptorRegistration);
inOrder.verify(interceptorRegistry).addInterceptor(loggingInterceptor);
inOrder.verify(interceptorRegistry).addInterceptor(any(TransactionInterceptor.class));
inOrder.verify(interceptorRegistry).addInterceptor(any(TokenPermissionsInterceptor.class));
inOrder.verify(interceptorRegistry).addInterceptor(customUserAuthenticationInterceptor);

verify(interceptorRegistry, times(3)).addInterceptor(any());
verify(interceptorRegistry, times(4)).addInterceptor(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
Expand All @@ -27,13 +28,17 @@ class PartnershipControllerTest {
private static final String REQUEST_ID = "5346336";
private static final String USER_ID = "rjg736k791";
private static final String SUBMISSION_ID = "ABC123ABC456";
private static final String TRANSACTION_ID = "12321123";

@InjectMocks
private PartnershipController partnershipController;

@Mock
private LimitedPartnershipService limitedPartnershipService;

@Mock
private Transaction transaction;

private LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto;

@BeforeEach
Expand All @@ -51,15 +56,18 @@ void testCreatePartnership() {
eq(USER_ID)))
.thenReturn(SUBMISSION_ID);

when(transaction.getId()).thenReturn(TRANSACTION_ID);

var response = partnershipController.createPartnership(
transaction,
limitedPartnershipSubmissionDto,
REQUEST_ID,
USER_ID);

assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
var responseHeaderLocation = Objects.requireNonNull(response.getHeaders().get(HttpHeaders.LOCATION)).getFirst();
assertEquals(
String.format(URL_GET_PARTNERSHIP, 12321123, SUBMISSION_ID),
String.format(URL_GET_PARTNERSHIP, TRANSACTION_ID, SUBMISSION_ID),
responseHeaderLocation);
LimitedPartnershipSubmissionCreatedResponseDto responseBody = (LimitedPartnershipSubmissionCreatedResponseDto) response.getBody();
assert responseBody != null;
Expand All @@ -75,6 +83,7 @@ void testCreatePartnershipInternalServerError() {
.thenThrow(new RuntimeException());

var response = partnershipController.createPartnership(
transaction,
limitedPartnershipSubmissionDto,
REQUEST_ID,
USER_ID);
Expand Down

0 comments on commit e171014

Please sign in to comment.