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

[Backport release/8.5] Google Sheets Connector: Adding a worksheet will return the properties of the worksheet #2812

Merged
merged 1 commit into from
Jul 8, 2024
Merged
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
Expand Up @@ -8,6 +8,7 @@

import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.ValueRange;
import io.camunda.connector.gsheets.supplier.GoogleSheetsServiceSupplier;
import io.camunda.google.model.Authentication;
Expand All @@ -17,20 +18,21 @@
public abstract class GoogleSheetOperation {

protected static final String SPREADSHEET_ID_FIELD = "spreadsheetId";
protected static final String REPLIES_FIELD = "replies";
protected static final String SPREADSHEET_URL_FIELD = "spreadsheetUrl";
protected static final String VALUE_INPUT_OPTION = "USER_ENTERED";

public abstract Object execute(final Authentication auth);

protected final void batchUpdate(
protected final BatchUpdateSpreadsheetResponse batchUpdate(
final Authentication auth, final String spreadsheetId, BatchUpdateSpreadsheetRequest request)
throws IOException {
Sheets service = GoogleSheetsServiceSupplier.getGoogleSheetsService(auth);

service
return service
.spreadsheets()
.batchUpdate(spreadsheetId, request)
.setFields(buildFields(SPREADSHEET_ID_FIELD))
.setFields(buildFields(SPREADSHEET_ID_FIELD, REPLIES_FIELD))
.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import com.google.api.services.sheets.v4.model.AddSheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.SheetProperties;
import io.camunda.connector.gsheets.model.request.input.CreateWorksheet;
import io.camunda.connector.gsheets.model.response.GoogleSheetsResult;
import io.camunda.connector.gsheets.operation.GoogleSheetOperation;
import io.camunda.google.model.Authentication;
import java.io.IOException;
Expand Down Expand Up @@ -39,9 +39,13 @@ public Object execute(Authentication auth) {
new BatchUpdateSpreadsheetRequest().setRequests(List.of(request));

try {
this.batchUpdate(auth, model.spreadsheetId(), updateRequest);
BatchUpdateSpreadsheetResponse response =
this.batchUpdate(auth, model.spreadsheetId(), updateRequest);

return new GoogleSheetsResult("Create worksheet", "OK");
return response.getReplies().stream()
.map(r -> r.getAddSheet().getProperties())
.findFirst()
.orElse(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
*/
package io.camunda.connector.gsheets.operation.impl;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.AddSheetResponse;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.Response;
import com.google.api.services.sheets.v4.model.SheetProperties;
import io.camunda.connector.gsheets.BaseTest;
import io.camunda.connector.gsheets.model.request.input.CreateWorksheet;
import io.camunda.connector.gsheets.supplier.GoogleSheetsServiceSupplier;
import io.camunda.google.model.Authentication;
import io.camunda.google.model.AuthenticationType;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -53,6 +53,11 @@ class CreateWorksheetOperationTest extends BaseTest {
public void before() {
response = new BatchUpdateSpreadsheetResponse();
response.setSpreadsheetId(SPREADSHEET_ID);
response.setReplies(
Collections.singletonList(
new Response()
.setAddSheet(
new AddSheetResponse().setProperties(new SheetProperties().setSheetId(123)))));
}

@DisplayName("Should create worksheet in the end of worksheets")
Expand All @@ -74,8 +79,12 @@ void createWorksheet_shouldFormRequestWithoutIndex() throws IOException {
.thenReturn(response);

// When
new CreateWorksheetOperation(model)
.execute(new Authentication(AuthenticationType.BEARER, "abc", null, null, null));
Object response =
new CreateWorksheetOperation(model)
.execute(new Authentication(AuthenticationType.BEARER, "abc", null, null, null));
assertThat(response.getClass(), typeCompatibleWith(SheetProperties.class));
SheetProperties responseTyped = (SheetProperties) response;
assertThat(responseTyped.getSheetId(), is(123));

// Then
mockedServiceSupplier.verify(
Expand Down