-
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 #65 from companieshouse/feature/DSND-2086
Feature/dsnd 2086
- Loading branch information
Showing
17 changed files
with
362 additions
and
10 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
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
41 changes: 41 additions & 0 deletions
41
src/main/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspect.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,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(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/uk/gov/companieshouse/pscdataapi/api/StreamEvents.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,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 { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/uk/gov/companieshouse/pscdataapi/config/FeatureFlags.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,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; | ||
} | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -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} |
79 changes: 79 additions & 0 deletions
79
...ompanieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagDisabledITest.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,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(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagEnabledITest.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,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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectTest.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,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"); | ||
} | ||
} |
Oops, something went wrong.