Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(sdk): kraken-247 - reject the push request if 0 records to push" #315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 @@ -12,13 +9,11 @@
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 @@ -45,13 +40,10 @@ 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 @@ -68,27 +60,6 @@ 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,6 +1,5 @@
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 @@ -13,12 +12,10 @@
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 @@ -29,7 +26,6 @@
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 @@ -46,7 +42,6 @@ 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 @@ -55,7 +50,6 @@ 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 @@ -84,7 +78,6 @@ 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 @@ -103,7 +96,6 @@ 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 @@ -157,7 +149,7 @@ void givenPushApiActivityLogDisabled_whenIsPushApiActivityLogEnabled_thenReturnF
}

@Test
void givenPushApiActivityLogDisabled_whenCreatePushApiActivityLogInfo_thenReturnsError() {
void givenPushApiActivityLogDisabled_createPushApiActivityLogInfo_thenReturnsError() {
// given
givenDisabledPushActivityLogExternal();
var env = environmentService.findAll().get(0);
Expand All @@ -174,23 +166,6 @@ void givenPushApiActivityLogDisabled_whenCreatePushApiActivityLogInfo_thenReturn
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 @@ -210,7 +185,6 @@ 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 @@ -226,16 +200,4 @@ 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);
}
}
Loading