forked from googleworkspace/java-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.java
More file actions
208 lines (184 loc) · 8.44 KB
/
BaseTest.java
File metadata and controls
208 lines (184 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.slides.v1.Slides;
import com.google.api.services.slides.v1.model.*;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
public class BaseTest {
static {
enableLogging();
}
protected GoogleCredential credential;
protected Slides service;
protected Drive driveService;
protected Sheets sheetsService;
protected Set<String> filesToDelete = new HashSet<>();
public static void enableLogging() {
Logger logger = Logger.getLogger(HttpTransport.class.getName());
logger.setLevel(Level.INFO);
logger.addHandler(new Handler() {
@Override
public void close() throws SecurityException {
}
@Override
public void flush() {
}
@Override
public void publish(LogRecord record) {
// default ConsoleHandler will print >= INFO to System.err
if (record.getLevel().intValue() < Level.INFO.intValue()) {
System.out.println(record.getMessage());
}
}
});
}
public GoogleCredential getCredential() throws IOException {
return GoogleCredential.getApplicationDefault()
.createScoped(Arrays.asList(DriveScopes.DRIVE));
}
public Slides buildService(GoogleCredential credential)
throws IOException, GeneralSecurityException {
return new Slides.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName("Slides API Snippets")
.build();
}
public Drive buildDriveService(GoogleCredential credential)
throws IOException, GeneralSecurityException {
return new Drive.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName("Slides API Snippets")
.build();
}
public Sheets buildSheetsService(GoogleCredential credential)
throws IOException, GeneralSecurityException {
return new Sheets.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName("Slides API Snippets")
.build();
}
@Before
public void setup() throws IOException, GeneralSecurityException {
this.credential = getCredential();
this.service = buildService(credential);
this.driveService = buildDriveService(credential);
this.sheetsService = buildSheetsService(credential);
this.filesToDelete.clear();
}
@After
public void cleanupFiles() {
for(String id : filesToDelete) {
try {
this.driveService.files().delete(id).execute();
} catch (IOException e) {
System.err.println("Unable to cleanup file " + id);
}
}
}
protected void deleteFileOnCleanup(String id) {
filesToDelete.add(id);
}
protected String createTestPresentation() throws IOException {
Presentation presentation = new Presentation()
.setTitle("Test Presentation");
presentation = service.presentations().create(presentation)
.setFields("presentationId")
.execute();
String presentationId = presentation.getPresentationId();
this.deleteFileOnCleanup(presentationId);
return presentationId;
}
protected String createTestSlide(String presentationId) throws IOException {
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setCreateSlide(new CreateSlideRequest()
.setObjectId("TestSlide")
.setInsertionIndex(0)
.setSlideLayoutReference(new LayoutReference()
.setPredefinedLayout("BLANK"))));
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
service.presentations().batchUpdate(presentationId, body).execute();
return response.getReplies().get(0).getCreateSlide().getObjectId();
}
protected String createTestTextBox(String presentationId, String pageId) throws IOException {
String textBoxId = "MyTextBox_01";
Dimension pt350 = new Dimension().setMagnitude(350.0).setUnit("PT");
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setCreateShape(new CreateShapeRequest()
.setObjectId(textBoxId)
.setShapeType("TEXT_BOX")
.setElementProperties(new PageElementProperties()
.setPageObjectId(pageId)
.setSize(new Size()
.setHeight(pt350)
.setWidth(pt350))
.setTransform(new AffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(350.0)
.setTranslateY(100.0)
.setUnit("PT")))));
requests.add(new Request()
.setInsertText(new InsertTextRequest()
.setObjectId(textBoxId)
.setInsertionIndex(0)
.setText("New Box Text Inserted")));
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
service.presentations().batchUpdate(presentationId, body).execute();
return response.getReplies().get(0).getCreateShape().getObjectId();
}
protected String createTestSheetsChart(String presentationId,
String pageId,
String spreadsheetId,
Integer sheetChartId) throws IOException {
String presentationChartId = "MyChartId_01";
Dimension emu4M = new Dimension().setMagnitude(4000000.0).setUnit("EMU");
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setCreateSheetsChart(new CreateSheetsChartRequest()
.setObjectId(presentationChartId)
.setSpreadsheetId(spreadsheetId)
.setChartId(sheetChartId)
.setLinkingMode("LINKED")
.setElementProperties(new PageElementProperties()
.setPageObjectId(pageId)
.setSize(new Size()
.setHeight(emu4M)
.setWidth(emu4M))
.setTransform(new AffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(100000.0)
.setTranslateY(100000.0)
.setUnit("EMU")))));
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
service.presentations().batchUpdate(presentationId, body).execute();
return response.getReplies().get(0).getCreateSheetsChart().getObjectId();
}
}