Skip to content

Commit e10bf46

Browse files
RajeshGovosanjuktaghosh7Manvendra-P-Singh
authored
Unit Test Case for gmail_enable_auto_reply (googleworkspace#191)
* git-on-borg files of gmail-api-snippets * Update build.gradle test12 * Update build.gradle * Unit Test Case for gmail_enable_auto_reply Co-authored-by: sanjuktaghosh7 <sanjuktaghosh@google.com> Co-authored-by: Manvendra-P-Singh <singhmanvendra@google.com>
1 parent d8a7c53 commit e10bf46

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

gmail/snippets/src/main/java/EnableAutoReply.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Google LLC
1+
// Copyright 2022 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -34,13 +34,19 @@ public class EnableAutoReply {
3434
/**
3535
* Enables the auto reply
3636
*
37-
* @throws IOException
37+
* @return the reply message and response metadata.
38+
* @throws IOException - if service account credentials file not found.
3839
*/
39-
public static void autoReply() throws IOException{
40-
// Load pre-authorized user credentials from the environment.
41-
// TODO(developer) - See https://developers.google.com/identity for
42-
// guides on implementing OAuth2 for your application.
43-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC));
40+
public static VacationSettings autoReply() throws IOException{
41+
// TODO(developer) - Replace with your email address.
42+
String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev";
43+
44+
/* Load pre-authorized user credentials from the environment.
45+
TODO(developer) - See https://developers.google.com/identity for
46+
guides on implementing OAuth2 for your application. */
47+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
48+
.createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC))
49+
.createDelegated(USER_EMAIL_ADDRESS);
4450
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
4551
credentials);
4652

@@ -57,12 +63,16 @@ public static void autoReply() throws IOException{
5763
.setEnableAutoReply(true)
5864
.setResponseBodyHtml("I am on vacation and will reply when I am back in the office. Thanks!")
5965
.setRestrictToDomain(true)
60-
.setStartTime(LocalDateTime.now().toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)
61-
.setEndTime(LocalDateTime.now().plusDays(7).toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);
66+
.setStartTime(LocalDateTime.now()
67+
.toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)
68+
.setEndTime(LocalDateTime.now().plusDays(7)
69+
.toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);
6270

63-
VacationSettings response = service.users().settings().updateVacation("me", vacationSettings).execute();
71+
VacationSettings response = service.users().settings()
72+
.updateVacation("me", vacationSettings).execute();
6473
// Prints the auto-reply response body
6574
System.out.println("Enabled auto reply with message : "+response.getResponseBodyHtml());
75+
return response;
6676
} catch (GoogleJsonResponseException e) {
6777
// TODO(developer) - handle error appropriately
6878
System.err.println("Unable to enable auto reply: " + e.getDetails());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
import com.google.api.services.gmail.model.VacationSettings;
17+
import org.junit.Test;
18+
import java.io.IOException;
19+
import static org.junit.Assert.assertNotNull;
20+
21+
public class TestEnableAutoReply extends EnableAutoReply {
22+
23+
@Test
24+
public void testAutoReply() throws IOException {
25+
VacationSettings settings = EnableAutoReply.autoReply();
26+
assertNotNull(settings);
27+
}
28+
}

0 commit comments

Comments
 (0)