-
Notifications
You must be signed in to change notification settings - Fork 2k
/
JobIntegrationTest.java
347 lines (281 loc) · 13.5 KB
/
JobIntegrationTest.java
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* Copyright 2012 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.Asset;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.ErrorDetail;
import com.microsoft.windowsazure.services.media.models.Job;
import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.JobState;
import com.microsoft.windowsazure.services.media.models.LinkInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.Task;
import com.microsoft.windowsazure.services.media.models.Task.CreateBatchOperation;
import com.microsoft.windowsazure.services.media.models.TaskInfo;
public class JobIntegrationTest extends IntegrationTestBase {
private static AssetInfo assetInfo;
private void verifyJobInfoEqual(String message, JobInfo expected, JobInfo actual) throws ServiceException {
verifyJobProperties(message, expected.getName(), expected.getPriority(), expected.getRunningDuration(),
expected.getState(), expected.getTemplateId(), expected.getCreated(), expected.getLastModified(),
expected.getStartTime(), expected.getEndTime(), null, actual);
}
@SuppressWarnings("deprecation")
private void verifyJobProperties(String message, String testName, Integer priority, double runningDuration,
JobState state, String templateId, Date created, Date lastModified, Date startTime, Date endTime,
Integer expectedTaskCount, JobInfo actualJob) throws ServiceException {
assertNotNull(message, actualJob);
assertNotNull(message + "Id", actualJob.getId());
assertEquals(message + " Name", testName, actualJob.getName());
// comment out due to issue 464
// assertEquals(message + " Priority", priority, actualJob.getPriority());
assertEquals(message + " RunningDuration", runningDuration, actualJob.getRunningDuration(), 0.001);
assertEquals(message + " State", state, actualJob.getState());
assertEqualsNullEmpty(message + " TemplateId", templateId, actualJob.getTemplateId());
assertDateApproxEquals(message + " Created", created, actualJob.getCreated());
assertDateApproxEquals(message + " LastModified", lastModified, actualJob.getLastModified());
assertDateApproxEquals(message + " StartTime", startTime, actualJob.getStartTime());
assertDateApproxEquals(message + " EndTime", endTime, actualJob.getEndTime());
if (expectedTaskCount != null) {
LinkInfo tasksLink = actualJob.getTasksLink();
ListResult<TaskInfo> actualTasks = service.list(Task.list(tasksLink));
assertEquals(message + " tasks size", expectedTaskCount.intValue(), actualTasks.size());
}
}
private JobInfo createJob(String name) throws ServiceException {
return service.create(Job.create().setName(name).setPriority(3).addInputMediaAsset(assetInfo.getId())
.addTaskCreator(getTaskCreator(0)));
}
private CreateBatchOperation getTaskCreator(int outputAssetPosition) {
return Task
.create("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5",
"<taskBody>" + "<inputAsset>JobInputAsset(0)</inputAsset>" + "<outputAsset>JobOutputAsset("
+ outputAssetPosition + ")</outputAsset>" + "</taskBody>")
.setConfiguration("H.264 256k DSL CBR").setName("My encoding Task");
}
@BeforeClass
public static void setup() throws Exception {
IntegrationTestBase.setup();
assetInfo = setupAssetWithFile();
}
@Test
public void createJobSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "createJobSuccess";
int priority = 3;
double duration = 0.0;
JobState state = JobState.Queued;
String templateId = null;
Date created = new Date();
Date lastModified = new Date();
Date stateTime = null;
Date endTime = null;
// Act
JobInfo actualJob = service.create(Job.create().setName(name).setPriority(priority)
.addInputMediaAsset(assetInfo.getId()).addTaskCreator(getTaskCreator(0)));
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
endTime, 1, actualJob);
}
@Test
public void createJobTwoTasksSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "createJobSuccess";
int priority = 3;
double duration = 0.0;
JobState state = JobState.Queued;
String templateId = null;
Date created = new Date();
Date lastModified = new Date();
Date stateTime = null;
Date endTime = null;
List<CreateBatchOperation> tasks = new ArrayList<CreateBatchOperation>();
tasks.add(getTaskCreator(0));
tasks.add(getTaskCreator(1));
// Act
JobInfo actualJob = service.create(Job.create().setName(name).setPriority(priority)
.addInputMediaAsset(assetInfo.getId()).addTaskCreator(tasks.get(0)).addTaskCreator(tasks.get(1)));
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
endTime, 2, actualJob);
}
@Test
public void getJobSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "getJobSuccess";
int priority = 3;
double duration = 0.0;
JobState state = JobState.Queued;
String templateId = null;
String jobId = createJob(name).getId();
Date created = new Date();
Date lastModified = new Date();
Date stateTime = null;
Date endTime = null;
// Act
JobInfo actualJob = service.get(Job.get(jobId));
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
endTime, 1, actualJob);
}
@Test
public void getJobInvalidIdFailed() throws ServiceException {
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
service.get(Job.get(invalidId));
}
@Test
public void listJobSuccess() throws ServiceException {
// Arrange
JobInfo jobInfo = createJob(testJobPrefix + "listJobSuccess");
List<JobInfo> jobInfos = new ArrayList<JobInfo>();
jobInfos.add(jobInfo);
ListResult<JobInfo> expectedListJobsResult = new ListResult<JobInfo>(jobInfos);
// Act
ListResult<JobInfo> actualListJobResult = service.list(Job.list());
// Assert
verifyListResultContains("listJobs", expectedListJobsResult, actualListJobResult, new ComponentDelegate() {
@Override
public void verifyEquals(String message, Object expected, Object actual) {
try {
verifyJobInfoEqual(message, (JobInfo) expected, (JobInfo) actual);
}
catch (ServiceException e) {
fail(e.getMessage());
}
}
});
}
@Test
public void canListJobsWithOptions() throws ServiceException {
String[] assetNameSuffixes = new String[] { "A", "B", "C", "D" };
List<JobInfo> expectedJobs = new ArrayList<JobInfo>();
for (String suffix : assetNameSuffixes) {
JobInfo jobInfo = createJob(testJobPrefix + "assetListOptions" + suffix);
expectedJobs.add(jobInfo);
}
ListResult<JobInfo> listJobsResult = service.list(Job.list().setTop(2));
// Assert
assertEquals(2, listJobsResult.size());
}
@Test
public void cancelJobSuccess() throws ServiceException {
// Arrange
JobInfo jobInfo = createJob(testJobPrefix + "cancelJobSuccess");
// Act
service.action(Job.cancel(jobInfo.getId()));
// Assert
JobInfo canceledJob = service.get(Job.get(jobInfo.getId()));
assertEquals(JobState.Canceling, canceledJob.getState());
}
@Test
public void cancelJobFailedWithInvalidId() throws ServiceException {
// Arrange
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
// Act
service.action(Job.cancel(invalidId));
// Assert
}
@Test
public void deleteJobSuccess() throws ServiceException, InterruptedException {
// Arrange
JobInfo jobInfo = createJob(testJobPrefix + "deleteJobSuccess");
service.action(Job.cancel(jobInfo.getId()));
JobInfo cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
int retryCounter = 0;
while (cancellingJobInfo.getState() == JobState.Canceling && retryCounter < 10) {
Thread.sleep(2000);
cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
retryCounter++;
}
// Act
service.delete(Job.delete(jobInfo.getId()));
// Assert
expectedException.expect(ServiceException.class);
service.get(Job.get(jobInfo.getId()));
}
@Test
public void deleteJobInvalidIdFail() throws ServiceException {
// Arrange
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
// Act
service.delete(Job.delete(invalidId));
// Assert
}
@Test
public void canGetInputOutputAssetsFromJob() throws Exception {
String name = testJobPrefix + "canGetInputOutputAssetsFromJob";
int priority = 3;
JobInfo actualJob = service.create(Job.create().setName(name).setPriority(priority)
.addInputMediaAsset(assetInfo.getId()).addTaskCreator(getTaskCreator(0)));
ListResult<AssetInfo> inputs = service.list(Asset.list(actualJob.getInputAssetsLink()));
ListResult<AssetInfo> outputs = service.list(Asset.list(actualJob.getOutputAssetsLink()));
assertEquals(1, inputs.size());
assertEquals(assetInfo.getId(), inputs.get(0).getId());
assertEquals(1, outputs.size());
assertTrue(outputs.get(0).getName().contains(name));
}
@Test
public void canGetTasksFromJob() throws Exception {
String name = testJobPrefix + "canGetTaskAssetsFromJob";
int priority = 3;
JobInfo actualJob = service.create(Job.create().setName(name).setPriority(priority)
.addInputMediaAsset(assetInfo.getId()).addTaskCreator(getTaskCreator(0)));
ListResult<TaskInfo> tasks = service.list(Task.list(actualJob.getTasksLink()));
assertEquals(1, tasks.size());
}
@Test
public void canGetErrorDetailsFromTask() throws Exception {
String name = testJobPrefix + "canGetErrorDetailsFromTask";
JobInfo actualJob = service.create(Job.create().setName(name).addInputMediaAsset(assetInfo.getId())
.addTaskCreator(getTaskCreator(0)));
JobInfo currentJobInfo = actualJob;
int retryCounter = 0;
while (currentJobInfo.getState().getCode() < 3 && retryCounter < 30) {
Thread.sleep(10000);
currentJobInfo = service.get(Job.get(actualJob.getId()));
retryCounter++;
}
ListResult<TaskInfo> tasks = service.list(Task.list(actualJob.getTasksLink()));
TaskInfo taskInfo = tasks.get(0);
List<ErrorDetail> errorDetails = taskInfo.getErrorDetails();
assertEquals(1, errorDetails.size());
ErrorDetail errorDetail = errorDetails.get(0);
assertNotNull(errorDetail.getCode());
assertNotNull(errorDetail.getMessage());
}
@Test
public void canGetInputOutputAssetsFromTask() throws Exception {
String name = testJobPrefix + "canGetInputOutputAssetsFromTask";
int priority = 3;
JobInfo actualJob = service.create(Job.create().setName(name).setPriority(priority)
.addInputMediaAsset(assetInfo.getId()).addTaskCreator(getTaskCreator(0)));
ListResult<TaskInfo> tasks = service.list(Task.list(actualJob.getTasksLink()));
ListResult<AssetInfo> inputs = service.list(Asset.list(tasks.get(0).getInputAssetsLink()));
ListResult<AssetInfo> outputs = service.list(Asset.list(tasks.get(0).getOutputAssetsLink()));
assertEquals(1, inputs.size());
assertEquals(assetInfo.getId(), inputs.get(0).getId());
assertEquals(1, outputs.size());
assertTrue(outputs.get(0).getName().contains(name));
}
}