From 70ee94204bb4edcb0d706e64a1302ca9930c0f19 Mon Sep 17 00:00:00 2001 From: jmalek Date: Thu, 21 Nov 2024 10:30:06 +0100 Subject: [PATCH] feat(sdk): kraken-31 -if push api feature disable then throw error whe create event --- .../service/push/ApiActivityPushService.java | 28 ++++++++---- .../push/ApiActivityPushServiceTest.java | 44 +++++++++++++++++++ .../operator/core/model/AppProperty.java | 11 +++++ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushService.java b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushService.java index 6c45285a..c5c1712d 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushService.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushService.java @@ -13,6 +13,7 @@ import com.consoleconnect.kraken.operator.core.enums.EventStatusType; import com.consoleconnect.kraken.operator.core.enums.MgmtEventType; import com.consoleconnect.kraken.operator.core.exception.KrakenException; +import com.consoleconnect.kraken.operator.core.model.AppProperty; import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; import com.consoleconnect.kraken.operator.core.request.PushLogSearchRequest; import com.consoleconnect.kraken.operator.core.toolkit.Paging; @@ -24,7 +25,6 @@ import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; @@ -36,12 +36,14 @@ public class ApiActivityPushService { public static final String CREATED_AT = "createdAt"; public static final String EVENT_TYPE = "eventType"; + public static final String PUSH_API_ACTIVITY_LOGS_IS_DISABLED = + "Push api activity logs is disabled."; + public static final String THE_SAME_PARAMETERS_ALREADY_EXISTS_ERROR = + "Push event with the same parameters already exists with status 'ack' or 'in_progress'."; private final MgmtEventRepository mgmtEventRepository; private final EnvironmentService environmentService; - - @Value("${app.features.push-activity-log-external.enabled}") - private boolean pushActivityLogExternalEnabled; + private final AppProperty appProperty; public ApiRequestActivityPushResult createPushApiActivityLogInfo( CreatePushApiActivityRequest request, String userId) { @@ -56,6 +58,17 @@ public ApiRequestActivityPushResult createPushApiActivityLogInfo( } private void validateRequest(CreatePushApiActivityRequest searchRequest) { + validateIfFeatureIsEnabled(); + validatePushEventWithTheSameParameters(searchRequest); + } + + private void validateIfFeatureIsEnabled() { + if (!appProperty.getFeatures().getPushActivityLogExternal().isEnabled()) { + throw new KrakenException(400, PUSH_API_ACTIVITY_LOGS_IS_DISABLED); + } + } + + private void validatePushEventWithTheSameParameters(CreatePushApiActivityRequest searchRequest) { boolean exists = mgmtEventRepository.existsBy( List.of(EventStatusType.ACK.name(), EventStatusType.IN_PROGRESS.name()), @@ -65,9 +78,7 @@ private void validateRequest(CreatePushApiActivityRequest searchRequest) { MgmtEventType.PUSH_API_ACTIVITY_LOG.name()); if (exists) { - throw new KrakenException( - 400, - "Push event with the same parameters already exists with status 'ack' or 'in_progress'."); + throw new KrakenException(400, THE_SAME_PARAMETERS_ALREADY_EXISTS_ERROR); } } @@ -127,6 +138,7 @@ private static Specification getMgmtEventEntitySpecification( } public PushApiActivityLogEnabled isPushApiActivityLogEnabled() { - return new PushApiActivityLogEnabled(pushActivityLogExternalEnabled); + return new PushApiActivityLogEnabled( + appProperty.getFeatures().getPushActivityLogExternal().isEnabled()); } } diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushServiceTest.java b/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushServiceTest.java index 8d67b2c1..4f0df7f1 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushServiceTest.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/service/push/ApiActivityPushServiceTest.java @@ -1,8 +1,11 @@ package com.consoleconnect.kraken.operator.controller.service.push; +import static com.consoleconnect.kraken.operator.controller.service.push.ApiActivityPushService.PUSH_API_ACTIVITY_LOGS_IS_DISABLED; +import static com.consoleconnect.kraken.operator.controller.service.push.ApiActivityPushService.THE_SAME_PARAMETERS_ALREADY_EXISTS_ERROR; import static com.consoleconnect.kraken.operator.core.service.UnifiedAssetService.getSearchPageRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doReturn; import com.consoleconnect.kraken.operator.config.TestApplication; import com.consoleconnect.kraken.operator.controller.dto.push.CreatePushApiActivityRequest; @@ -12,6 +15,7 @@ import com.consoleconnect.kraken.operator.core.enums.EventStatusType; import com.consoleconnect.kraken.operator.core.enums.MgmtEventType; import com.consoleconnect.kraken.operator.core.exception.KrakenException; +import com.consoleconnect.kraken.operator.core.model.AppProperty; import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; import com.consoleconnect.kraken.operator.core.request.PushLogSearchRequest; import com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit; @@ -24,6 +28,7 @@ import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.data.domain.Sort; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -36,6 +41,7 @@ class ApiActivityPushServiceTest extends AbstractIntegrationTest { @Autowired private ApiActivityPushService sut; @Autowired private MgmtEventRepository mgmtEventRepository; @Autowired private EnvironmentService environmentService; + @SpyBean private AppProperty appProperty; @Test void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() { @@ -104,6 +110,7 @@ void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() { KrakenException.class, () -> sut.createPushApiActivityLogInfo(request, userId)); // then assertThat(krakenException.getCode()).isEqualTo(400); + assertThat(krakenException.getMessage()).isEqualTo(THE_SAME_PARAMETERS_ALREADY_EXISTS_ERROR); } @Test @@ -131,6 +138,43 @@ void givenPushApiActivityLogEnabled_whenIsPushApiActivityLogEnabled_thenReturnTr assertThat(result.isEnabled()).isTrue(); } + @Test + void givenPushApiActivityLogDisabled_whenIsPushApiActivityLogEnabled_thenReturnFalse() { + // given + givenDisabledPushActivityLogExternal(); + // when + var result = sut.isPushApiActivityLogEnabled(); + // then + assertThat(result.isEnabled()).isFalse(); + } + + @Test + void givenPushApiActivityLogDisabled_createPushApiActivityLogInfo_thenReturnsError() { + // given + givenDisabledPushActivityLogExternal(); + var env = environmentService.findAll().get(0); + var userId = "userId1"; + var endTime = ZonedDateTime.parse("2024-10-10T00:00:00+01:00").minusDays(1); + var startTime = endTime.minusDays(3); + var request = new CreatePushApiActivityRequest(startTime, endTime, env.getId()); + // when + var krakenException = + assertThrows( + KrakenException.class, () -> sut.createPushApiActivityLogInfo(request, userId)); + // then + assertThat(krakenException.getCode()).isEqualTo(400); + assertThat(krakenException.getMessage()).isEqualTo(PUSH_API_ACTIVITY_LOGS_IS_DISABLED); + } + + private void givenDisabledPushActivityLogExternal() { + AppProperty.PushActivityLogExternal pushActivityLogExternal = + new AppProperty.PushActivityLogExternal(); + pushActivityLogExternal.setEnabled(false); + AppProperty.Features appFeatures = new AppProperty.Features(); + appFeatures.setPushActivityLogExternal(pushActivityLogExternal); + doReturn(appFeatures).when(appProperty).getFeatures(); + } + private void verifyIfLogsOrderedByCreatedAtDesc(List logs) { for (int i = 0; i < logs.size() - 1; i++) { assertThat(logs.get(i).getCreatedAt().isAfter(logs.get(i + 1).getCreatedAt())).isTrue(); diff --git a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/model/AppProperty.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/model/AppProperty.java index 001fc13b..6b1fa677 100644 --- a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/model/AppProperty.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/model/AppProperty.java @@ -19,4 +19,15 @@ public class AppProperty { private Map apiSpecOrderBy = new HashMap<>(); private Map apiOrderBy = new HashMap<>(); private Map apiTargetMapperOrderBy = new HashMap<>(); + private Features features; + + @Data + public static class Features { + private PushActivityLogExternal pushActivityLogExternal; + } + + @Data + public static class PushActivityLogExternal { + private boolean enabled; + } }