Skip to content

Commit

Permalink
fix: add a try-catch to handle DataIntegrityViolationException for lo…
Browse files Browse the repository at this point in the history
…ng topic description

Signed-off-by: Enrique-Giottonini <[email protected]>
  • Loading branch information
Enrique-Giottonini committed Aug 9, 2024
1 parent edc255d commit 29f818f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/io/aiven/klaw/error/KlawErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ public class KlawErrorMessages {
public static final String TOPICS_VLD_ERR_124 =
"Failure. This topic does not exist in the cluster.";

public static final String TOPICS_VLD_ERR_125 = "Failure. Topic values exceed allowed lengths.";

public static final String TOPICS_VLD_ERR_126 = "Failure. Data integrity violation: ";

// Topic overview service
public static final String TOPIC_OVW_ERR_101 = "Topic does not exist in any environment.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_ERR_115;
import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_ERR_116;
import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_VLD_ERR_121;
import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_VLD_ERR_125;
import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_VLD_ERR_126;
import static io.aiven.klaw.helpers.KwConstants.KLAW_OPTIONAL_PERMISSION_NEW_TOPIC_CREATION_KEY;
import static io.aiven.klaw.helpers.KwConstants.ORDER_OF_TOPIC_ENVS;
import static io.aiven.klaw.helpers.UtilMethods.updateEnvStatus;
Expand Down Expand Up @@ -77,6 +79,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -153,8 +156,17 @@ private ApiResponse createTopicRequest(TopicRequestModel topicRequestReq) throws
mapAdvancedTopicConfiguration(topicRequestReq, topicRequestDao);
topicRequestDao.setTenantId(commonUtilsService.getTenantId(userName));

String result = dbHandle.requestForTopic(topicRequestDao).get("result");
// default to Topic_CREATE which is the old hard coded
String result;
try {
result = dbHandle.requestForTopic(topicRequestDao).get("result");
// default to Topic_CREATE which is the old hard coded
} catch (DataIntegrityViolationException e) {
log.error("Error in creating a topic ", e);
if (e.getCause().getMessage().contains("Value too long for column")) {
return ApiResponse.notOk(TOPICS_VLD_ERR_125);
}
throw new KlawException(TOPICS_VLD_ERR_126 + e);
}

mailService.sendMail(
topicRequestReq.getTopicname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.aiven.klaw.model.response.TopicDetailsPerEnv;
import io.aiven.klaw.model.response.TopicRequestsResponseModel;
import io.aiven.klaw.model.response.TopicTeamResponse;
import java.sql.SQLDataException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -57,6 +58,7 @@
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -221,6 +223,31 @@ public void createTopicsFailureInvalidClusterTenantIds()
assertThat(apiResponse.getMessage()).isEqualTo(ApiResultStatus.FAILURE.value);
}

@Test
@Order(5)
public void createTopicsFailureInvalidTopicDescriptionLength()
throws KlawException, KlawNotAuthorizedException {

when(manageDatabase.getTenantConfig()).thenReturn(tenantConfig);
when(tenantConfig.get(anyInt())).thenReturn(tenantConfigModel);
when(tenantConfigModel.getBaseSyncEnvironment()).thenReturn("1");
stubUserInfo();
when(commonUtilsService.isNotAuthorizedUser(any(), any(PermissionType.class)))
.thenReturn(false);
when(manageDatabase.getKafkaEnvList(anyInt())).thenReturn(utilMethods.getEnvListsIncorrect1());
when(manageDatabase.getTeamsAndAllowedEnvs(anyInt(), anyInt()))
.thenReturn(Collections.singletonList("1"));
when(commonUtilsService.getEnvProperty(anyInt(), anyString())).thenReturn("1");

SQLDataException causeException = new SQLDataException("Seed size for the object exceeds the\n" +
"column size in the database. Value too long for column...");
DataIntegrityViolationException exception = new DataIntegrityViolationException("Error message", causeException);
when(handleDbRequests.requestForTopic(any())).thenThrow(exception);

ApiResponse apiResponse = topicControllerService.createTopicsCreateRequest(getFailureTopicWithLongDescription());
assertThat(apiResponse.getMessage()).isEqualTo("Failure. Topic values exceed allowed lengths.");
}

@Test
@Order(6)
public void createTopicDeleteRequestFailureTopicAlreadyExists() {
Expand Down Expand Up @@ -1763,6 +1790,17 @@ private TopicRequestModel getFailureTopic1() {
return topicRequest;
}

private TopicRequestModel getFailureTopicWithLongDescription() {
TopicRequestModel topicRequest = new TopicRequestModel();
topicRequest.setTopicname("newtopicname");
int DESCRIPTION_COLUMN_LENGTH = 100; // VARCHAR(100) but can be other length.
topicRequest.setDescription("x".repeat(DESCRIPTION_COLUMN_LENGTH + 1));
topicRequest.setEnvironment(env.getId());
topicRequest.setTopicpartitions(2);
topicRequest.setRequestOperationType(RequestOperationType.CREATE);
return topicRequest;
}

private TopicRequest getTopicRequest(String name) {
TopicRequest topicRequest = new TopicRequest();
topicRequest.setTopicname(name);
Expand Down

0 comments on commit 29f818f

Please sign in to comment.