-
Notifications
You must be signed in to change notification settings - Fork 2k
/
TaskIntegrationTest.java
293 lines (243 loc) · 13.8 KB
/
TaskIntegrationTest.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
/**
* 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.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.ws.rs.core.MultivaluedMap;
import org.junit.Before;
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.Job;
import com.microsoft.windowsazure.services.media.models.Job.Creator;
import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.JobState;
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;
import com.microsoft.windowsazure.services.media.models.TaskOption;
import com.microsoft.windowsazure.services.media.models.TaskState;
import com.sun.jersey.core.util.Base64;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class TaskIntegrationTest extends IntegrationTestBase {
private static AssetInfo assetInfo;
private Creator jobCreator;
private static final String commonMediaProcessorId = "nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5";
private static final String commonConfiguration = "H.264 256k DSL CBR";
@BeforeClass
public static void setup() throws Exception {
IntegrationTestBase.setup();
assetInfo = setupAssetWithFile();
}
@Before
public void instanceSetup() {
this.jobCreator = Job.create().setName(testJobPrefix + UUID.randomUUID().toString()).setPriority(3)
.addInputMediaAsset(assetInfo.getId());
}
@Test
public void createTaskSuccess() throws ServiceException, UnsupportedEncodingException {
// Arrange
// Required
String mediaProcessorId = commonMediaProcessorId;
String taskBody = constructTaskBody(0);
// Optional parameters
String configuration = new String(Base64.encode(commonConfiguration), "UTF8");
String name = "My encoding Task " + UUID.randomUUID().toString();
int priority = 0;
TaskOption options = TaskOption.ProtectedConfiguration;
// Use a fake id, to simulate real use.
String encryptionKeyId = "nb:kid:UUID:" + UUID.randomUUID().toString();
String encryptionScheme = "ConfigurationEncryption";
String encryptionVersion = "1.0";
// Use a trivial vector, 16 bytes of zeros, base-64 encoded.
String initializationVector = new String(Base64.encode(new byte[16]), "UTF8");
CreateBatchOperation taskCreator = Task.create(mediaProcessorId, taskBody).setConfiguration(configuration)
.setName(name).setPriority(priority).setOptions(options).setEncryptionKeyId(encryptionKeyId)
.setEncryptionScheme(encryptionScheme).setEncryptionVersion(encryptionVersion)
.setInitializationVector(initializationVector);
jobCreator.addTaskCreator(taskCreator);
// Act
JobInfo job = service.create(jobCreator);
List<TaskInfo> taskInfos = service.list(Task.list(job.getTasksLink()));
// Assert
assertEquals("taskInfos count", 1, taskInfos.size());
verifyTaskPropertiesJustStarted("taskInfo", mediaProcessorId, options, taskBody, configuration, name, priority,
encryptionKeyId, encryptionScheme, encryptionVersion, initializationVector, taskInfos.get(0));
}
@Test
public void createTwoTasksSuccess() throws ServiceException {
// Arrange
// Required
String mediaProcessorId = commonMediaProcessorId;
String[] taskBodies = new String[] { constructTaskBody(0), constructTaskBody(1) };
// Optional parameters
String configuration = commonConfiguration;
String baseName = "My encoding Task " + UUID.randomUUID().toString();
String[] suffixes = new String[] { " 1", " 2" };
int priority = 0;
TaskOption options = TaskOption.None;
List<CreateBatchOperation> taskCreators = new ArrayList<CreateBatchOperation>();
for (int i = 0; i < taskBodies.length; i++) {
CreateBatchOperation taskCreator = Task.create(mediaProcessorId, taskBodies[i])
.setConfiguration(configuration).setName(baseName + suffixes[i]);
taskCreators.add(taskCreator);
jobCreator.addTaskCreator(taskCreator);
}
// Act
JobInfo job = service.create(jobCreator);
List<TaskInfo> taskInfos = service.list(Task.list(job.getTasksLink()));
// Assert
assertEquals("taskInfos count", taskCreators.size(), taskInfos.size());
for (int i = 0; i < taskCreators.size(); i++) {
verifyTaskPropertiesJustStartedNoEncryption("taskInfo", mediaProcessorId, options, taskBodies[i],
configuration, baseName + suffixes[i], priority, taskInfos.get(i));
}
}
@Test
public void canListTasksWithOptions() throws ServiceException {
// Arrange
String mediaProcessorId = commonMediaProcessorId;
String configuration = commonConfiguration;
String[] taskNameSuffixes = new String[] { "A", "B", "C", "D" };
String baseName = "My encoding Task " + UUID.randomUUID().toString();
int taskCounter = 0;
for (String suffix : taskNameSuffixes) {
CreateBatchOperation taskCreator = Task.create(mediaProcessorId, constructTaskBody(taskCounter++))
.setConfiguration(configuration).setName(baseName + suffix);
jobCreator.addTaskCreator(taskCreator);
}
service.create(jobCreator);
// Act
MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl();
queryParameters.add("$filter", "startswith(Name, '" + baseName + "') eq true");
ListResult<TaskInfo> listTaskResult1 = service.list(Task.list(queryParameters));
queryParameters.add("$top", "2");
ListResult<TaskInfo> listTaskResult2 = service.list(Task.list(queryParameters));
// Assert
assertEquals("listTaskResult1.size", 4, listTaskResult1.size());
assertEquals("listTaskResult2.size", 2, listTaskResult2.size());
}
@Test
public void cancelTaskSuccess() throws ServiceException, InterruptedException {
// Arrange
String mediaProcessorId = commonMediaProcessorId;
String taskBody = constructTaskBody(0);
String configuration = commonConfiguration;
String name = "My encoding Task " + UUID.randomUUID().toString();
CreateBatchOperation taskCreator = Task.create(mediaProcessorId, taskBody).setConfiguration(configuration)
.setName(name);
jobCreator.addTaskCreator(taskCreator);
JobInfo jobInfo = service.create(jobCreator);
// Act
service.action(Job.cancel(jobInfo.getId()));
JobInfo cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
while (cancellingJobInfo.getState() == JobState.Canceling) {
Thread.sleep(2000);
cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
}
//Assert
List<TaskInfo> taskInfos = service.list(Task.list(cancellingJobInfo.getTasksLink()));
for (TaskInfo taskInfo : taskInfos) {
verifyTaskPropertiesNoEncryption("canceled task", mediaProcessorId, TaskOption.None, taskBody,
configuration, name, 0, new Date(), null, 0.0, 0.0, null, TaskState.Canceled, taskInfo);
}
}
private void verifyTaskProperties(String message, String mediaProcessorId, TaskOption options, String taskBody,
String configuration, String name, int priority, String encryptionKeyId, String encryptionScheme,
String encryptionVersion, String initializationVector, Date endTime, String errorDetails, double progress,
double runningDuration, Date startTime, TaskState state, TaskInfo actual) throws ServiceException {
assertNotNull(message, actual);
assertNotNull(message + " id", actual.getId());
// Required fields
assertEquals(message + " getMediaProcessorId", mediaProcessorId, actual.getMediaProcessorId());
assertEquals(message + " getOptions", options, actual.getOptions());
assertEquals(message + " getTaskBody", taskBody, actual.getTaskBody());
// Optional fields
assertEquals(message + " getConfiguration", configuration, actual.getConfiguration());
assertEquals(message + " getName", name, actual.getName());
assertEquals(message + " getPriority", priority, actual.getPriority());
// Optional encryption fields
assertEqualsNullEmpty(message + " getEncryptionKeyId", encryptionKeyId, actual.getEncryptionKeyId());
assertEqualsNullEmpty(message + " getEncryptionScheme", encryptionScheme, actual.getEncryptionScheme());
assertEqualsNullEmpty(message + " getEncryptionVersion", encryptionVersion, actual.getEncryptionVersion());
assertEqualsNullEmpty(message + " getInitializationVector", initializationVector,
actual.getInitializationVector());
// Read-only fields
assertDateApproxEquals(message + " getEndTime", endTime, actual.getEndTime());
assertNotNull(message + " getErrorDetails", actual.getErrorDetails());
assertEquals(message + " getErrorDetails.size", 0, actual.getErrorDetails().size());
ListResult<AssetInfo> inputAssets = service.list(Asset.list(actual.getInputAssetsLink()));
ListResult<AssetInfo> outputAssets = service.list(Asset.list(actual.getOutputAssetsLink()));
assertEquals(message + " inputAssets.size", 1, inputAssets.size());
assertEquals(message + " inputAssets.get(0).getId", assetInfo.getId(), inputAssets.get(0).getId());
assertEquals(message + " outputAssets.size", 1, outputAssets.size());
// Because this is a new asset, there is not much else to test
assertTrue(message + " outputAssets.get(0).getId != assetInfo.getId",
!assetInfo.getId().equals(outputAssets.get(0).getId()));
assertEquals(message + " getProgress", progress, actual.getProgress(), 0.01);
assertEquals(message + " getRunningDuration", runningDuration, actual.getRunningDuration(), 0.01);
assertDateApproxEquals(message + " getStartTime", startTime, actual.getStartTime());
assertEquals(message + " getState", state, actual.getState());
// Note: The PerfMessage is not validated because it is server generated.
}
private void verifyTaskPropertiesJustStarted(String message, String mediaProcessorId, TaskOption options,
String taskBody, String configuration, String name, int priority, String encryptionKeyId,
String encryptionScheme, String encryptionVersion, String initializationVector, TaskInfo actual)
throws ServiceException {
// Read-only
Date endTime = null;
String errorDetails = null;
double progress = 0.0;
int runningDuration = 0;
Date startTime = null;
TaskState state = TaskState.None;
verifyTaskProperties(message, mediaProcessorId, options, taskBody, configuration, name, priority,
encryptionKeyId, encryptionScheme, encryptionVersion, initializationVector, endTime, errorDetails,
progress, runningDuration, startTime, state, actual);
}
private void verifyTaskPropertiesJustStartedNoEncryption(String message, String mediaProcessorId,
TaskOption options, String taskBody, String configuration, String name, int priority, TaskInfo actual)
throws ServiceException {
String encryptionKeyId = null;
String encryptionScheme = null;
String encryptionVersion = null;
String initializationVector = null;
verifyTaskPropertiesJustStarted(message, mediaProcessorId, options, taskBody, configuration, name, priority,
encryptionKeyId, encryptionScheme, encryptionVersion, initializationVector, actual);
}
private void verifyTaskPropertiesNoEncryption(String message, String mediaProcessorId, TaskOption options,
String taskBody, String configuration, String name, int priority, Date endTime, String errorDetails,
double progress, double runningDuration, Date startTime, TaskState state, TaskInfo actual)
throws ServiceException {
String encryptionKeyId = null;
String encryptionScheme = null;
String encryptionVersion = null;
String initializationVector = null;
verifyTaskProperties(message, mediaProcessorId, options, taskBody, configuration, name, priority,
encryptionKeyId, encryptionScheme, encryptionVersion, initializationVector, endTime, errorDetails,
progress, runningDuration, startTime, state, actual);
}
private String constructTaskBody(int outputIndex) {
return "<taskBody><inputAsset>JobInputAsset(0)</inputAsset>" + "<outputAsset>JobOutputAsset(" + outputIndex
+ ")</outputAsset></taskBody>";
}
}