Skip to content

Commit

Permalink
Merge pull request #65 from companieshouse/feature/DSND-2086
Browse files Browse the repository at this point in the history
Feature/dsnd 2086
  • Loading branch information
maddytjCH authored Dec 19, 2023
2 parents 2021637 + 2ad9f95 commit faf2806
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 10 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
<private-api-sdk-java.version>2.0.326</private-api-sdk-java.version>
<data-sync-api-sdk-java.version>1.0.4</data-sync-api-sdk-java.version>
<api-security-java.version>0.4.1</api-security-java.version>
<api-sdk-manager-java-library.version>1.0.4</api-sdk-manager-java-library.version>
<api-sdk-manager-java-library.version>1.0.8</api-sdk-manager-java-library.version>
<api-helper-java-library.version>1.4.5</api-helper-java-library.version>
<sdk-manager-java.version>1.5.17</sdk-manager-java.version>

<!-- tests -->
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
Expand Down Expand Up @@ -171,6 +172,16 @@
<artifactId>kafka-models</artifactId>
<version>${kafka-models.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -219,8 +230,9 @@
<skipTests>${skip.unit.tests}</skipTests>
<environmentVariables>
<CHS_API_KEY>key</CHS_API_KEY>
<API_URL>g9yZIA81Zo9J46Kzp3JPbfld6kOqxR47EAYqXbRV</API_URL>
<API_URL>key</API_URL>
<PAYMENTS_API_URL>key</PAYMENTS_API_URL>
<DOCUMENT_API_LOCAL_URL>document_api_local_url</DOCUMENT_API_LOCAL_URL>
</environmentVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ChsKafkaApiService {
* @param notificationId mongo id
* @return passes request to api response handling
*/

@StreamEvents
public ApiResponse<Void> invokeChsKafkaApi(String contextId, String companyNumber,
String notificationId, String kind) {
internalApiClient.setBasePath(chsKafkaApiUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.gov.companieshouse.pscdataapi.api;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.pscdataapi.config.FeatureFlags;

@Aspect
@Component
@ConditionalOnProperty(prefix = "feature", name = "seeding_collection_enabled")
public class ResourceChangedApiServiceAspect {

private final FeatureFlags featureFlags;
private final Logger logger;

public ResourceChangedApiServiceAspect(FeatureFlags featureFlags, Logger logger) {
this.featureFlags = featureFlags;
this.logger = logger;
}

/**
* Feature flag check.
* @param proceedingJoinPoint the proceeding join point.
* @return returns an object.
* @throws Throwable throws something.
*/
@Around("@annotation(StreamEvents)")
public Object invokeChsKafkaApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

if (featureFlags.isStreamHookDisabled()) {
logger.debug("Stream hook disabled; not publishing change to chs-kafka-api");
return null;
} else {
logger.debug("Stream hook enabled; publishing change to chs-kafka-api");
return proceedingJoinPoint.proceed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.companieshouse.pscdataapi.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StreamEvents {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.gov.companieshouse.pscdataapi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class FeatureFlags {

private final boolean streamHookDisabled;

public FeatureFlags(
@Value("${feature.seeding_collection_enabled}") boolean streamHookDisabled) {
this.streamHookDisabled = streamHookDisabled;
}

public boolean isStreamHookDisabled() {
return streamHookDisabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import uk.gov.companieshouse.api.psc.Identification;



@JsonInclude(JsonInclude.Include.NON_NULL)
@Document(collection = "delta_company_pscs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.data.mongodb.repository.Aggregation;
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import uk.gov.companieshouse.pscdataapi.repository.CompanyPscRepository;
import uk.gov.companieshouse.pscdataapi.transform.CompanyPscTransformer;


@Service
public class CompanyPscService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import uk.gov.companieshouse.pscdataapi.models.Updated;
import uk.gov.companieshouse.pscdataapi.util.PscTransformationHelper;


@Component
public class CompanyPscTransformer {

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
management.endpoints.web.base-path=/psc-data-api
management.endpoints.web.path-mapping.health=/healthcheck
chs.api.kafka.uri=/private/resource-changed
chs.api.kafka.kind=someKind
chs.api.kafka.kind=someKind
feature.seeding_collection_enabled=${SEEDING_COLLECTION_ENABLED:false}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package uk.gov.companieshouse.pscdataapi.api;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
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.chskafka.ChangedResource;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.chskafka.PrivateChangedResourceHandler;
import uk.gov.companieshouse.api.handler.chskafka.request.PrivateChangedResourcePost;
import uk.gov.companieshouse.api.http.HttpClient;
import uk.gov.companieshouse.api.model.ApiResponse;
import uk.gov.companieshouse.api.sdk.ApiClientService;
import uk.gov.companieshouse.pscdataapi.util.TestHelper;

@SpringBootTest
class ResourceChangedApiServiceAspectFeatureFlagDisabledITest {

@InjectMocks
private ChsKafkaApiService chsKafkaApiService;

@MockBean
private ApiClientService apiClientService;

@Mock
private InternalApiClient internalApiClient;

@Mock
private ChangedResource changedResource;
@Mock
private PrivateChangedResourceHandler privateChangedResourceHandler;
@Mock
private PrivateChangedResourcePost changedResourcePost;
@Mock
private ApiResponse<Void> response;
@Mock
private HttpClient httpClient;

@MockBean
private ChsKafkaApiService mapper;

private TestHelper testHelper;

@Captor
ArgumentCaptor<ChangedResource> changedResourceCaptor;



@BeforeEach
void setup() {
when(internalApiClient.getHttpClient()).thenReturn(httpClient);
testHelper = new TestHelper();
}

@Test
void testThatKafkaApiShouldBeCalledWhenFeatureFlagDisabled()
throws ApiErrorResponseException {

when(internalApiClient.privateChangedResourceHandler()).thenReturn(
privateChangedResourceHandler);
when(privateChangedResourceHandler.postChangedResource(Mockito.any(), Mockito.any())).thenReturn(
changedResourcePost);
when(changedResourcePost.execute()).thenReturn(response);

ApiResponse<?> apiResponse = chsKafkaApiService.invokeChsKafkaApi(TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "kind");

Assertions.assertThat(apiResponse).isNotNull();

verify(internalApiClient).privateChangedResourceHandler();
verify(privateChangedResourceHandler,times(1)).postChangedResource(Mockito.any(), changedResourceCaptor.capture());
verify(changedResourcePost, times(1)).execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package uk.gov.companieshouse.pscdataapi.api;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.chskafka.ChangedResource;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.chskafka.PrivateChangedResourceHandler;
import uk.gov.companieshouse.api.handler.chskafka.request.PrivateChangedResourcePost;
import uk.gov.companieshouse.api.http.HttpClient;
import uk.gov.companieshouse.api.model.ApiResponse;
import uk.gov.companieshouse.api.request.RequestExecutor;
import uk.gov.companieshouse.api.sdk.ApiClientService;
import uk.gov.companieshouse.pscdataapi.exceptions.ServiceUnavailableException;
import uk.gov.companieshouse.pscdataapi.util.TestHelper;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

@SpringBootTest
@ActiveProfiles("feature_flag_enabled")
class ResourceChangedApiServiceAspectFeatureFlagEnabledITest {
@Autowired
private ChsKafkaApiService chsKafkaApiService;

@MockBean
private ApiClientService apiClientService;

@Mock
private InternalApiClient internalApiClient;
@Mock
private PrivateChangedResourceHandler privateChangedResourceHandler;
@Mock
private PrivateChangedResourcePost changedResourcePost;
@Mock
private ApiResponse<Void> response;
@Mock
private HttpClient httpClient;

@Mock
private ChangedResource changedResource;

private String url = "testurl";
@Mock
private RequestExecutor requestExecutor;
private TestHelper testHelper;

@BeforeEach
void setup() {

testHelper = new TestHelper();
}
@Test
void testThatAspectShouldNotProceedWhenFeatureFlagEnabled() throws ServiceUnavailableException, ApiErrorResponseException {

when(internalApiClient.privateChangedResourceHandler()).thenReturn(
privateChangedResourceHandler);
when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(
changedResourcePost);
when(changedResourcePost.execute()).thenReturn(response);

chsKafkaApiService.invokeChsKafkaApi(TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "kind");

verifyNoInteractions(apiClientService);
verifyNoInteractions(internalApiClient);
verifyNoInteractions(privateChangedResourceHandler);
verifyNoInteractions(changedResourcePost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package uk.gov.companieshouse.pscdataapi.api;

import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.logging.Logger;
import uk.gov.companieshouse.pscdataapi.config.FeatureFlags;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.*;


@ExtendWith(MockitoExtension.class)
class ResourceChangedApiServiceAspectTest {

@InjectMocks
private ResourceChangedApiServiceAspect resourceChangedApiServiceAspect;
@Mock
private ProceedingJoinPoint proceedingJoinPoint;
@Mock
private Object object;
@Mock
private Logger logger;
@Mock
private FeatureFlags featureFlags;

@Test
void testAspectDoesNotProceedWhenFlagEnabled() throws Throwable {
//given
when(featureFlags.isStreamHookDisabled()).thenReturn(true);
// when
Object actual = resourceChangedApiServiceAspect.invokeChsKafkaApi(proceedingJoinPoint);

// then
assertNull(actual);
verifyNoInteractions(proceedingJoinPoint);
verify(logger).debug("Stream hook disabled; not publishing change to chs-kafka-api");
}

@Test
void testAspectProceedsWhenFlagDisabled() throws Throwable {
//given
when(proceedingJoinPoint.proceed()).thenReturn(object);
//when
Object actual = resourceChangedApiServiceAspect.invokeChsKafkaApi(proceedingJoinPoint);
//then
assertSame(object, actual);
verify(proceedingJoinPoint).proceed();
verify(logger).debug("Stream hook enabled; publishing change to chs-kafka-api");
}
}
Loading

0 comments on commit faf2806

Please sign in to comment.