Skip to content

Commit

Permalink
feat(sdk): kraken-247 - reject the push request if 0 records to push (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete authored Dec 10, 2024
1 parent 13c256f commit 165fcdb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.consoleconnect.kraken.operator.controller.service.push;

import static com.consoleconnect.kraken.operator.controller.service.statistics.ApiActivityStatisticsService.CALL_SEQ;
import static com.consoleconnect.kraken.operator.controller.service.statistics.ApiActivityStatisticsService.CALL_SEQ_ZERO;
import static com.consoleconnect.kraken.operator.controller.service.statistics.ApiActivityStatisticsService.ENV;
import static com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit.fromJson;
import static com.consoleconnect.kraken.operator.core.toolkit.JsonToolkit.toJson;

Expand All @@ -9,11 +12,13 @@
import com.consoleconnect.kraken.operator.controller.dto.push.PushApiActivityLogHistory;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.service.EnvironmentService;
import com.consoleconnect.kraken.operator.core.entity.ApiActivityLogEntity;
import com.consoleconnect.kraken.operator.core.entity.MgmtEventEntity;
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.ApiActivityLogRepository;
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 @@ -40,10 +45,13 @@ public class ApiActivityPushService {
"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'.";
public static final String NO_API_ACTIVITIES_FOUND =
"No API activities found for the provided request parameters. Please verify your input and try again.";

private final MgmtEventRepository mgmtEventRepository;
private final EnvironmentService environmentService;
private final AppProperty appProperty;
private final ApiActivityLogRepository apiActivityLogRepository;

public ApiRequestActivityPushResult createPushApiActivityLogInfo(
CreatePushApiActivityRequest request, String userId) {
Expand All @@ -60,6 +68,27 @@ public ApiRequestActivityPushResult createPushApiActivityLogInfo(
private void validateRequest(CreatePushApiActivityRequest searchRequest) {
validateIfFeatureIsEnabled();
validatePushEventWithTheSameParameters(searchRequest);
validateIfTimeRangeContainsAtLeastOneApiActivityLog(searchRequest);
}

private void validateIfTimeRangeContainsAtLeastOneApiActivityLog(
CreatePushApiActivityRequest searchRequest) {
Specification<ApiActivityLogEntity> spec =
(root, query, criteriaBuilder) -> {
var predicateList = new ArrayList<Predicate>();
predicateList.add(criteriaBuilder.equal(root.get(ENV), searchRequest.getEnvId()));
predicateList.add(criteriaBuilder.equal(root.get(CALL_SEQ), CALL_SEQ_ZERO));
predicateList.add(
criteriaBuilder.greaterThanOrEqualTo(
root.get(CREATED_AT), searchRequest.getStartTime()));
predicateList.add(
criteriaBuilder.lessThanOrEqualTo(root.get(CREATED_AT), searchRequest.getEndTime()));
return query.where(predicateList.toArray(new Predicate[0])).getRestriction();
};
long count = apiActivityLogRepository.count(spec);
if (count < 1) {
throw new KrakenException(400, NO_API_ACTIVITIES_FOUND);
}
}

private void validateIfFeatureIsEnabled() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.consoleconnect.kraken.operator.controller.service.push;

import static com.consoleconnect.kraken.operator.controller.service.push.ApiActivityPushService.NO_API_ACTIVITIES_FOUND;
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;
Expand All @@ -12,10 +13,12 @@
import com.consoleconnect.kraken.operator.controller.dto.push.PushApiActivityLogHistory;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.service.EnvironmentService;
import com.consoleconnect.kraken.operator.core.entity.ApiActivityLogEntity;
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.ApiActivityLogRepository;
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 @@ -26,6 +29,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
Expand All @@ -42,6 +46,7 @@ class ApiActivityPushServiceTest extends AbstractIntegrationTest {
@Autowired private MgmtEventRepository mgmtEventRepository;
@Autowired private EnvironmentService environmentService;
@SpyBean private AppProperty appProperty;
@SpyBean private ApiActivityLogRepository apiActivityLogRepository;

@Test
void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() {
Expand All @@ -50,6 +55,7 @@ void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() {
var userId = "userId1";
var endTime = ZonedDateTime.parse("2024-10-10T00:00:00+01:00");
var startTime = endTime.minusDays(3);
givenApiActivityLogs(endTime, env);
var request = new CreatePushApiActivityRequest(startTime, endTime, env.getId());
// when
var created = sut.createPushApiActivityLogInfo(request, userId);
Expand Down Expand Up @@ -78,6 +84,7 @@ void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() {
var userId = "userId1";
var endTime = ZonedDateTime.parse("2024-10-10T00:00:00+01:00").minusDays(1);
var startTime = endTime.minusDays(3);
givenApiActivityLogs(endTime, env);
var request = new CreatePushApiActivityRequest(startTime, endTime, env.getId());
sut.createPushApiActivityLogInfo(request, userId);
// when
Expand All @@ -96,6 +103,7 @@ void givenApiLogSearchParam_whenCreatePushApiActivityLogInfo_thenSaveEvent() {
var userId = "userId1";
var endTime = ZonedDateTime.parse("2024-10-10T00:00:00+01:00").minusDays(2);
var startTime = endTime.minusDays(3);
givenApiActivityLogs(endTime, env);
var request = new CreatePushApiActivityRequest(startTime, endTime, env.getId());
var pushApiActivityLogInfo = sut.createPushApiActivityLogInfo(request, userId);
var mgmtEventEntity =
Expand Down Expand Up @@ -149,7 +157,7 @@ void givenPushApiActivityLogDisabled_whenIsPushApiActivityLogEnabled_thenReturnF
}

@Test
void givenPushApiActivityLogDisabled_createPushApiActivityLogInfo_thenReturnsError() {
void givenPushApiActivityLogDisabled_whenCreatePushApiActivityLogInfo_thenReturnsError() {
// given
givenDisabledPushActivityLogExternal();
var env = environmentService.findAll().get(0);
Expand All @@ -166,6 +174,23 @@ void givenPushApiActivityLogDisabled_createPushApiActivityLogInfo_thenReturnsErr
assertThat(krakenException.getMessage()).isEqualTo(PUSH_API_ACTIVITY_LOGS_IS_DISABLED);
}

@Test
void givenNoApiActivityLogsToPush_whenCreatePushApiActivityLogInfo_thenReturnsError() {
// given
var env = environmentService.findAll().get(0);
var userId = "userId1";
var endTime = ZonedDateTime.parse("2022-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(NO_API_ACTIVITIES_FOUND);
}

private void givenDisabledPushActivityLogExternal() {
AppProperty.PushActivityLogExternal pushActivityLogExternal =
new AppProperty.PushActivityLogExternal();
Expand All @@ -185,6 +210,7 @@ private void givenPushApiActivityLogs() {
var env = environmentService.findAll().get(0);
for (int i = 1; i < 4; i++) {
var request = pushApiActivityRequest(env, i);
givenApiActivityLogs(request.getEndTime(), env);
sut.createPushApiActivityLogInfo(request, "userId1");
}
}
Expand All @@ -200,4 +226,16 @@ private String toUtcString(ZonedDateTime zonedDateTime) {
.withZoneSameInstant(ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}

private void givenApiActivityLogs(ZonedDateTime endTime, Environment env) {
var entity = new ApiActivityLogEntity();
entity.setEnv(env.getId());
entity.setCallSeq(0);
entity.setCreatedAt(endTime.minusHours(1));
entity.setMethod("POST");
entity.setPath("/path");
entity.setUri("/uri");
entity.setRequestId(UUID.randomUUID().toString());
apiActivityLogRepository.save(entity);
}
}

0 comments on commit 165fcdb

Please sign in to comment.