Skip to content

Commit 96bb1a3

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents c194284 + 18c8747 commit 96bb1a3

45 files changed

Lines changed: 974 additions & 469 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryInitialMigrationTest.java renamed to flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryInitialMigrationIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@RunWith(SpringRunner.class)
1414
@SpringBootTest
15-
public class CustomerRepositoryInitialMigrationTest {
15+
public class CustomerRepositoryInitialMigrationIntegrationTest {
1616

1717
@Autowired CustomerRepository customerRepository;
1818

flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryInsertDataMigrationTest.java renamed to flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryInsertDataIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
@RunWith(SpringRunner.class)
1717
@SpringBootTest
18-
public class CustomerRepositoryInsertDataMigrationTest {
18+
public class CustomerRepositoryInsertDataIntegrationTest {
1919

2020
@Autowired CustomerRepository customerRepository;
2121

flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryNotNullConstraintMigrationTest.java renamed to flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryNotNullConstraintMigrationIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@RunWith(SpringRunner.class)
1313
@SpringBootTest
14-
public class CustomerRepositoryNotNullConstraintMigrationTest {
14+
public class CustomerRepositoryNotNullConstraintMigrationIntegrationTest {
1515

1616
@Autowired CustomerRepository customerRepository;
1717

flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryUniqueConstraintJavaMigrationTest.java renamed to flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryUniqueConstraintJavaMigrationIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@SpringBootTest(properties = {
1414
"flyway.locations[0]=db/migration", "flyway.locations[1]=com/baeldung/springflyway/migration"
1515
})
16-
public class CustomerRepositoryUniqueConstraintJavaMigrationTest {
16+
public class CustomerRepositoryUniqueConstraintJavaMigrationIntegrationTest {
1717

1818
@Autowired CustomerRepository customerRepository;
1919

flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryUniqueConstraintMigrationTest.java renamed to flyway/spring-flyway/src/test/java/com/baeldung/springflyway/CustomerRepositoryUniqueConstraintMigrationIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@RunWith(SpringRunner.class)
1313
@SpringBootTest
14-
public class CustomerRepositoryUniqueConstraintMigrationTest {
14+
public class CustomerRepositoryUniqueConstraintMigrationIntegrationTest {
1515

1616
@Autowired CustomerRepository customerRepository;
1717

‎libraries/pom.xml‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,26 @@
633633
<version>${googleclient.version}</version>
634634
</dependency>
635635
<dependency>
636-
<groupId>com.google.http-client</groupId>
637-
<artifactId>google-http-client-gson</artifactId>
638-
<version>${googleclient.version}</version>
636+
<groupId>com.google.http-client</groupId>
637+
<artifactId>google-http-client-gson</artifactId>
638+
<version>${googleclient.version}</version>
639+
</dependency>
640+
641+
<!-- google api -->
642+
<dependency>
643+
<groupId>com.google.api-client</groupId>
644+
<artifactId>google-api-client</artifactId>
645+
<version>${google-api.version}</version>
646+
</dependency>
647+
<dependency>
648+
<groupId>com.google.oauth-client</groupId>
649+
<artifactId>google-oauth-client-jetty</artifactId>
650+
<version>${google-api.version}</version>
651+
</dependency>
652+
<dependency>
653+
<groupId>com.google.apis</groupId>
654+
<artifactId>google-api-services-sheets</artifactId>
655+
<version>${google-sheets.version}</version>
639656
</dependency>
640657
</dependencies>
641658
<repositories>
@@ -710,5 +727,7 @@
710727
<cache.version>1.0.0</cache.version>
711728
<hazelcast.version>3.8.4</hazelcast.version>
712729
<caffeine.version>2.5.5</caffeine.version>
730+
<google-api.version>1.23.0</google-api.version>
731+
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
713732
</properties>
714733
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.google.sheets;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.security.GeneralSecurityException;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import com.google.api.client.auth.oauth2.Credential;
11+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
12+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
13+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
14+
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
15+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
16+
import com.google.api.client.json.jackson2.JacksonFactory;
17+
import com.google.api.client.util.store.MemoryDataStoreFactory;
18+
import com.google.api.services.sheets.v4.SheetsScopes;
19+
20+
public class GoogleAuthorizeUtil {
21+
public static Credential authorize() throws IOException, GeneralSecurityException {
22+
InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json");
23+
GoogleClientSecrets clientSecrets = GoogleClientSecrets
24+
.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
25+
26+
List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
27+
28+
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
29+
.Builder(GoogleNetHttpTransport.newTrustedTransport(),
30+
JacksonFactory.getDefaultInstance(),
31+
clientSecrets,
32+
scopes)
33+
.setDataStoreFactory(new MemoryDataStoreFactory())
34+
.setAccessType("offline")
35+
.build();
36+
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
37+
.authorize("user");
38+
39+
return credential;
40+
}
41+
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.google.sheets;
2+
3+
import java.io.IOException;
4+
import java.security.GeneralSecurityException;
5+
6+
import com.google.api.client.auth.oauth2.Credential;
7+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
8+
import com.google.api.client.json.jackson2.JacksonFactory;
9+
import com.google.api.services.sheets.v4.Sheets;
10+
11+
public class SheetsServiceUtil {
12+
13+
private static final String APPLICATION_NAME = "Google Sheets Example";
14+
15+
public static Sheets getSheetsService() throws IOException, GeneralSecurityException {
16+
Credential credential = GoogleAuthorizeUtil.authorize();
17+
return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(),
18+
JacksonFactory.getDefaultInstance(), credential)
19+
.setApplicationName(APPLICATION_NAME)
20+
.build();
21+
}
22+
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"installed":{"client_id":"394827218507-2ev02b2ha8plt7g2lh5nqse02ee737cf.apps.googleusercontent.com","project_id":"decisive-octane-187810","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"2MnN1DfenoCGWMay3v8Bf7eI","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.baeldung.google.sheets;
2+
3+
import java.io.IOException;
4+
import java.security.GeneralSecurityException;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import com.google.api.services.sheets.v4.Sheets;
13+
import com.google.api.services.sheets.v4.model.AppendValuesResponse;
14+
import com.google.api.services.sheets.v4.model.BatchGetValuesResponse;
15+
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
16+
import com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest;
17+
import com.google.api.services.sheets.v4.model.BatchUpdateValuesResponse;
18+
import com.google.api.services.sheets.v4.model.CopyPasteRequest;
19+
import com.google.api.services.sheets.v4.model.GridRange;
20+
import com.google.api.services.sheets.v4.model.Request;
21+
import com.google.api.services.sheets.v4.model.Spreadsheet;
22+
import com.google.api.services.sheets.v4.model.SpreadsheetProperties;
23+
import com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest;
24+
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
25+
import com.google.api.services.sheets.v4.model.ValueRange;
26+
27+
import static org.assertj.core.api.Assertions.*;
28+
29+
public class GoogleSheetsIntegrationTest {
30+
31+
private static Sheets sheetsService;
32+
33+
// this id can be replaced with your spreadsheet id
34+
// otherwise be advised that multiple people may run this test and update the public spreadsheet
35+
private static final String SPREADSHEET_ID = "1sILuxZUnyl_7-MlNThjt765oWshN3Xs-PPLfqYe4DhI";
36+
37+
@BeforeClass
38+
public static void setup() throws GeneralSecurityException, IOException {
39+
sheetsService = SheetsServiceUtil.getSheetsService();
40+
}
41+
42+
@Test
43+
public void whenWriteSheet_thenReadSheetOk() throws IOException {
44+
ValueRange body = new ValueRange()
45+
.setValues(Arrays.asList(
46+
Arrays.asList("Expenses January"),
47+
Arrays.asList("books", "30"),
48+
Arrays.asList("pens", "10"),
49+
Arrays.asList("Expenses February"),
50+
Arrays.asList("clothes", "20"),
51+
Arrays.asList("shoes", "5")));
52+
UpdateValuesResponse result = sheetsService.spreadsheets().values()
53+
.update(SPREADSHEET_ID, "A1", body)
54+
.setValueInputOption("RAW")
55+
.execute();
56+
57+
List<ValueRange> data = new ArrayList<>();
58+
data.add(new ValueRange()
59+
.setRange("D1")
60+
.setValues(Arrays.asList(
61+
Arrays.asList("January Total", "=B2+B3"))));
62+
data.add(new ValueRange()
63+
.setRange("D4")
64+
.setValues(Arrays.asList(
65+
Arrays.asList("February Total", "=B5+B6"))));
66+
67+
BatchUpdateValuesRequest batchBody = new BatchUpdateValuesRequest()
68+
.setValueInputOption("USER_ENTERED")
69+
.setData(data);
70+
BatchUpdateValuesResponse batchResult =
71+
sheetsService.spreadsheets().values()
72+
.batchUpdate(SPREADSHEET_ID, batchBody)
73+
.execute();
74+
75+
List<String> ranges = Arrays.asList("E1","E4");
76+
BatchGetValuesResponse readResult =
77+
sheetsService.spreadsheets().values()
78+
.batchGet(SPREADSHEET_ID)
79+
.setRanges(ranges)
80+
.execute();
81+
82+
ValueRange januaryTotal = readResult.getValueRanges().get(0);
83+
assertThat(januaryTotal.getValues().get(0).get(0)).isEqualTo("40");
84+
85+
ValueRange febTotal = readResult.getValueRanges().get(1);
86+
assertThat(febTotal.getValues().get(0).get(0)).isEqualTo("25");
87+
88+
ValueRange appendBody = new ValueRange()
89+
.setValues(Arrays.asList(
90+
Arrays.asList("Total", "=E1+E4")));
91+
AppendValuesResponse appendResult =
92+
sheetsService.spreadsheets().values()
93+
.append(SPREADSHEET_ID, "A1", appendBody)
94+
.setValueInputOption("USER_ENTERED")
95+
.setInsertDataOption("INSERT_ROWS")
96+
.setIncludeValuesInResponse(true)
97+
.execute();
98+
99+
ValueRange total = appendResult.getUpdates().getUpdatedData();
100+
assertThat(total.getValues().get(0).get(1)).isEqualTo("65");
101+
}
102+
103+
104+
@Test
105+
public void whenUpdateSpreadSheetTitle_thenOk() throws IOException {
106+
107+
UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest()
108+
.setFields("*")
109+
.setProperties(new SpreadsheetProperties().setTitle("Expenses"));
110+
111+
CopyPasteRequest copyRequest = new CopyPasteRequest()
112+
.setSource(new GridRange().setSheetId(0)
113+
.setStartColumnIndex(0).setEndColumnIndex(2)
114+
.setStartRowIndex(0).setEndRowIndex(1))
115+
.setDestination(new GridRange().setSheetId(1)
116+
.setStartColumnIndex(0).setEndColumnIndex(2)
117+
.setStartRowIndex(0).setEndRowIndex(1))
118+
.setPasteType("PASTE_VALUES");
119+
120+
List<Request> requests = new ArrayList<>();
121+
122+
requests.add(new Request().setCopyPaste(copyRequest));
123+
requests.add(new Request().setUpdateSpreadsheetProperties(updateRequest));
124+
125+
BatchUpdateSpreadsheetRequest body =
126+
new BatchUpdateSpreadsheetRequest().setRequests(requests);
127+
128+
sheetsService.spreadsheets().batchUpdate(SPREADSHEET_ID, body).execute();
129+
}
130+
131+
@Test
132+
public void whenCreateSpreadSheet_thenIdOk() throws IOException {
133+
Spreadsheet spreadSheet = new Spreadsheet()
134+
.setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet"));
135+
Spreadsheet result = sheetsService.spreadsheets().create(spreadSheet).execute();
136+
137+
assertThat(result.getSpreadsheetId()).isNotNull();
138+
}
139+
140+
}

0 commit comments

Comments
 (0)