Skip to content

Commit

Permalink
feat(sdk): kraken-31 -if push api feature disable then throw error wh…
Browse files Browse the repository at this point in the history
…e create event
  • Loading branch information
jaroslawmalekcodete committed Nov 21, 2024
1 parent 8d190f4 commit 70ee942
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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()),
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -127,6 +138,7 @@ private static Specification<MgmtEventEntity> getMgmtEventEntitySpecification(
}

public PushApiActivityLogEnabled isPushApiActivityLogEnabled() {
return new PushApiActivityLogEnabled(pushActivityLogExternalEnabled);
return new PushApiActivityLogEnabled(
appProperty.getFeatures().getPushActivityLogExternal().isEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<PushApiActivityLogHistory> logs) {
for (int i = 0; i < logs.size() - 1; i++) {
assertThat(logs.get(i).getCreatedAt().isAfter(logs.get(i + 1).getCreatedAt())).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public class AppProperty {
private Map<String, String> apiSpecOrderBy = new HashMap<>();
private Map<String, String> apiOrderBy = new HashMap<>();
private Map<String, String> apiTargetMapperOrderBy = new HashMap<>();
private Features features;

@Data
public static class Features {
private PushActivityLogExternal pushActivityLogExternal;
}

@Data
public static class PushActivityLogExternal {
private boolean enabled;
}
}

0 comments on commit 70ee942

Please sign in to comment.