Skip to content

Commit

Permalink
added RetryUtil and use of it in ITs that failed in previous pipeline…
Browse files Browse the repository at this point in the history
…; also added resilience4j to test dependencies
  • Loading branch information
ronreynolds committed Aug 27, 2024
1 parent f21483e commit 4f0952d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 43 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ext {
mockitoCoreVersion = '5.12.0'
mockitoJUnitJupiterVersion = '5.12.0'
okHttpVersion = '4.0.0'
resilience4jVersion = "1.7.1" // last version compatible with pre-17 JDKs
slf4jSimpleVersion = '1.7.25'
slf4jVersion = '1.7.25'
}
Expand Down Expand Up @@ -83,6 +84,7 @@ dependencies {
testImplementation "org.mockito:mockito-core:${mockitoCoreVersion}"
testImplementation "org.mockito:mockito-junit-jupiter:${mockitoJUnitJupiterVersion}"
testImplementation "org.slf4j:slf4j-simple:${slf4jSimpleVersion}"
testImplementation "io.github.resilience4j:resilience4j-retry:${resilience4jVersion}"
}

// Configuration for our Javadocs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.smartsheet.api.models.PaginationParameters;
import com.smartsheet.api.models.Row;
import com.smartsheet.api.models.Sheet;
import com.smartsheet.api.resilience4j.RetryUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -46,10 +47,12 @@ public void setUp() throws Exception {
void testGetCellHistory() throws SmartsheetException, IOException {
//create sheet
Sheet sheet = smartsheet.sheetResources().createSheet(createSheetObject());

PaginationParameters parameters = new PaginationParameters.PaginationParametersBuilder().setIncludeAll(true).build();

//get columns
PagedResult<Column> columns = smartsheet.sheetResources().columnResources().listColumns(sheet.getId(), null, parameters);
PagedResult<Column> columns = RetryUtil.callWithRetry(
() -> smartsheet.sheetResources().columnResources().listColumns(sheet.getId(), null, parameters));
//add rows
Row row = addRows(sheet.getId());

Expand All @@ -70,7 +73,8 @@ void testAddCellImage() throws SmartsheetException, IOException {
PaginationParameters parameters = new PaginationParameters.PaginationParametersBuilder().setIncludeAll(true).build();

//get columns
PagedResult<Column> columns = smartsheet.sheetResources().columnResources().listColumns(sheet.getId(), null, parameters);
PagedResult<Column> columns = RetryUtil.callWithRetry(
() -> smartsheet.sheetResources().columnResources().listColumns(sheet.getId(), null, parameters));
//add rows
Row row = addRows(sheet.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.smartsheet.api.models.Comment;
import com.smartsheet.api.models.Discussion;
import com.smartsheet.api.models.Sheet;
import com.smartsheet.api.resilience4j.RetryUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -77,12 +78,12 @@ public void testAddCommentWithAttachment() throws SmartsheetException, IOExcepti
.commentResources()
.addCommentWithAttachment(newSheet.getId(), newDiscussion.getId(), comment, file, "application/pdf");
assertThat(comment1).isNotNull();
file = null;
}

public void testGetComment() throws SmartsheetException, IOException {
Comment comment = smartsheet.sheetResources().commentResources().getComment(newSheet.getId(), newComment.getId());

// sometimes fails to find the just-added comment
Comment comment = RetryUtil.callWithRetry(() ->
smartsheet.sheetResources().commentResources().getComment(newSheet.getId(), newComment.getId()));
assertThat(comment).isNotNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.smartsheet.api.models.enums.FolderCopyInclusion;
import com.smartsheet.api.models.enums.FolderRemapExclusion;
import com.smartsheet.api.models.enums.SourceInclusion;
import com.smartsheet.api.resilience4j.RetryUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -100,7 +101,8 @@ public void testUpdateFolder() throws SmartsheetException {

public void testListFoldersInFolder() throws SmartsheetException {
PaginationParameters parameters = new PaginationParameters(true, 1, 1);
PagedResult<Folder> foldersWrapper = smartsheet.folderResources().listFolders(newFolder.getId(), parameters);
PagedResult<Folder> foldersWrapper = RetryUtil.callWithRetry(
() -> smartsheet.folderResources().listFolders(newFolder.getId(), parameters));

assertThat(foldersWrapper.getTotalCount()).isEqualTo(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.smartsheet.api.models.StringObjectValue;
import com.smartsheet.api.models.enums.ColumnType;
import com.smartsheet.api.models.enums.SheetInclusion;
import com.smartsheet.api.resilience4j.RetryUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -78,8 +79,9 @@ public void testAddMultiPicklistColumn() throws SmartsheetException {
}

public void testListMultiPicklistColumn() throws SmartsheetException {
PagedResult<Column> cols = smartsheet.sheetResources().columnResources().listColumns(sheet.getId(),
null, null, null);
// sometimes fails to find the just-added resource
PagedResult<Column> cols = RetryUtil.callWithRetry(
() -> smartsheet.sheetResources().columnResources().listColumns(sheet.getId(), null, null, null));
// should be TEXT_NUMBER since level not specified
assertThat(cols.getData().get(0).getType()).isEqualTo(ColumnType.TEXT_NUMBER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetException;
import com.smartsheet.api.resilience4j.RetryUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -30,6 +31,7 @@
import static org.assertj.core.api.Assertions.assertThat;

public class PassthroughResourcesIT extends ITResourcesImpl {
private static final ObjectMapper MAPPER = new ObjectMapper();
Smartsheet smartsheet;

@BeforeEach
Expand All @@ -39,7 +41,6 @@ public void setUp() throws Exception {

@Test
void testPassthroughMethods() throws SmartsheetException, IOException {
Long id = 0L;
String payload =
"{\"name\": \"my new sheet\"," +
"\"columns\": [" +
Expand All @@ -48,47 +49,28 @@ void testPassthroughMethods() throws SmartsheetException, IOException {
"]" +
"}";
String jsonResponse = smartsheet.passthroughResources().postRequest("sheets", payload, null);
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
JsonNode result = root.get("result");
assertThat(result.get("id").asLong()).isNotNull();
id = result.get("id").asLong();
} catch (IOException e) {
e.printStackTrace();
}
JsonNode root = MAPPER.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
JsonNode result = root.get("result");
assertThat(result.get("id").asLong()).isNotNull();
long id = result.get("id").asLong();

Map<String, Object> parameters = new HashMap<>();
parameters.put("include", "objectValue");
jsonResponse = smartsheet.passthroughResources().getRequest("sheets/" + id, parameters);
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonResponse);
assertThat(root.get("name").asText()).isEqualTo("my new sheet");
} catch (IOException e) {
e.printStackTrace();
}
jsonResponse = RetryUtil.callWithRetry(
() -> smartsheet.passthroughResources().getRequest("sheets/" + id, parameters));
root = MAPPER.readTree(jsonResponse);
assertThat(root.get("name").asText()).isEqualTo("my new sheet");

payload = "{\"name\": \"my new new sheet\"}";
jsonResponse = smartsheet.passthroughResources().putRequest("sheets/" + id, payload, null);
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
JsonNode result = root.get("result");
assertThat(result.get("name").asText()).isEqualTo("my new new sheet");
} catch (IOException e) {
e.printStackTrace();
}
root = MAPPER.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
result = root.get("result");
assertThat(result.get("name").asText()).isEqualTo("my new new sheet");

jsonResponse = smartsheet.passthroughResources().deleteRequest("sheets/" + id);
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
} catch (IOException e) {
e.printStackTrace();
}
root = MAPPER.readTree(jsonResponse);
assertThat(root.get("message").asText()).isEqualTo("SUCCESS");
}
}
67 changes: 67 additions & 0 deletions src/test/java/com/smartsheet/api/resilience4j/RetryUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2024 Smartsheet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.smartsheet.api.resilience4j;

import com.smartsheet.api.ResourceNotFoundException;
import com.smartsheet.api.SmartsheetException;
import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry;
import io.vavr.CheckedFunction0;

import java.time.Duration;

/**
* thin wrapper around Resilience4J
*/
public class RetryUtil {
// 1s between attempts, only retry on ResourceNotFoundException, fail after 3 attempts
private static final RetryRegistry RETRY_REGISTRY = RetryRegistry.of(RetryConfig.custom()
.maxAttempts(3)
.waitDuration(Duration.ofMillis(1000))
.retryExceptions(ResourceNotFoundException.class)
.failAfterMaxAttempts(true)
.build());

/**
* a version of Res4J's CheckedFunction0 that throws SmartsheetException (i.e., basically all the API methods)
*/
public interface SmartsheetCheckedFunction<R> extends CheckedFunction0<R> {
@Override
R apply() throws SmartsheetException;
}

/**
* retry the provided method until is succeeds or 3 attempts are exceeded.
*
* @param callback is the API code to be invoked
* @param <R> the type of return value
* @return the value of the first successful API call
* @throws SmartsheetException if we fail to successfully call the {@code callback}
* @throws RuntimeException if any non-SmartsheetException is thrown (i.e., something really really bad)
*/
public static <R> R callWithRetry(SmartsheetCheckedFunction<R> callback) throws SmartsheetException {
try {
return RETRY_REGISTRY.retry("").executeCheckedSupplier(callback);
} catch (Throwable t) {
if (t instanceof SmartsheetException) {
throw (SmartsheetException) t;
}
// no point in wrapping a RuntimeEx in another RuntimeEx, is there?
throw (t instanceof RuntimeException) ? (RuntimeException) t : new RuntimeException(t);
}
}
}

0 comments on commit 4f0952d

Please sign in to comment.