-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ContentKeyIntegrationTest.java
179 lines (146 loc) · 8.27 KB
/
ContentKeyIntegrationTest.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
/**
* 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.List;
import java.util.UUID;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.ContentKey;
import com.microsoft.windowsazure.services.media.models.ContentKeyInfo;
import com.microsoft.windowsazure.services.media.models.ContentKeyType;
import com.microsoft.windowsazure.services.media.models.ProtectionKeyType;
public class ContentKeyIntegrationTest extends IntegrationTestBase {
private final String validButNonexistContentKeyId = "nb:kid:UUID:80dfe751-e5a1-4b29-a992-4a75276473af";
private final ContentKeyType testContentKeyType = ContentKeyType.CommonEncryption;
private final String testEncryptedContentKey = "ThisIsEncryptedContentKey";
private String createRandomContentKeyId() {
UUID uuid = UUID.randomUUID();
String randomContentKey = String.format("nb:kid:UUID:%s", uuid);
return randomContentKey;
}
private void verifyInfosEqual(String message, ContentKeyInfo expected, ContentKeyInfo actual) {
verifyContentKeyProperties(message, expected.getId(), expected.getContentKeyType(),
expected.getEncryptedContentKey(), expected.getName(), expected.getProtectionKeyId(),
expected.getProtectionKeyType(), expected.getChecksum(), actual);
}
private void verifyContentKeyProperties(String message, String id, ContentKeyType contentKeyType,
String encryptedContentKey, String name, String protectionKeyId, ProtectionKeyType protectionKeyType,
String checksum, ContentKeyInfo actual) {
assertNotNull(message, actual);
assertEquals(message + " Id", id, actual.getId());
assertEquals(message + " ContentKeyType", contentKeyType, actual.getContentKeyType());
assertEquals(message + " EncryptedContentKey", encryptedContentKey, actual.getEncryptedContentKey());
assertEquals(message + " Name", name, actual.getName());
assertEquals(message + " ProtectionKeyId", protectionKeyId, actual.getProtectionKeyId());
assertEquals(message + " ProtectionKeyType", protectionKeyType, actual.getProtectionKeyType());
assertEquals(message + " Checksum", checksum, actual.getChecksum());
}
@Test
public void canCreateContentKey() throws Exception {
// Arrange
String testCanCreateContentKeyId = createRandomContentKeyId();
String testCanCreateContentKeyName = "testCanCreateContentKey";
// Act
ContentKeyInfo contentKeyInfo = service.create(ContentKey.create(testCanCreateContentKeyId, testContentKeyType,
testEncryptedContentKey).setName(testCanCreateContentKeyName));
// Assert
verifyContentKeyProperties("ContentKey", testCanCreateContentKeyId, testContentKeyType,
testEncryptedContentKey, testCanCreateContentKeyName, "", ProtectionKeyType.fromCode(0), "",
contentKeyInfo);
}
@Test
public void canGetSingleContentKeyById() throws Exception {
// Arrange
String expectedName = testContentKeyPrefix + "GetOne";
String testGetSingleContentKeyByIdId = createRandomContentKeyId();
ContentKeyInfo ContentKeyToGet = service.create(ContentKey.create(testGetSingleContentKeyByIdId,
testContentKeyType, testEncryptedContentKey).setName(expectedName));
// Act
ContentKeyInfo retrievedContentKeyInfo = service.get(ContentKey.get(ContentKeyToGet.getId()));
// Assert
assertEquals(ContentKeyToGet.getId(), retrievedContentKeyInfo.getId());
verifyContentKeyProperties("ContentKey", testGetSingleContentKeyByIdId, testContentKeyType,
testEncryptedContentKey, expectedName, "", ProtectionKeyType.fromCode(0), "", retrievedContentKeyInfo);
}
@Test
public void cannotGetSingleContentKeyByInvalidId() throws Exception {
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
service.get(ContentKey.get(invalidId));
}
@Test
public void canRetrieveListOfContentKeys() throws Exception {
String[] ContentKeyNames = new String[] { testContentKeyPrefix + "ListOne", testContentKeyPrefix + "ListTwo" };
List<ContentKeyInfo> expectedContentKeys = new ArrayList<ContentKeyInfo>();
for (int i = 0; i < ContentKeyNames.length; i++) {
String testCanRetrieveListOfContentKeysId = createRandomContentKeyId();
ContentKeyInfo contentKey = service.create(ContentKey.create(testCanRetrieveListOfContentKeysId,
testContentKeyType, testEncryptedContentKey));
expectedContentKeys.add(contentKey);
}
List<ContentKeyInfo> actualContentKeys = service.list(ContentKey.list());
verifyListResultContains("listContentKeyss", expectedContentKeys, actualContentKeys, new ComponentDelegate() {
@Override
public void verifyEquals(String message, Object expected, Object actual) {
verifyInfosEqual(message, (ContentKeyInfo) expected, (ContentKeyInfo) actual);
}
});
}
@Test
public void canUseQueryParametersWhenListingContentKeys() throws Exception {
String[] ContentKeyNames = new String[] { testContentKeyPrefix + "ListThree",
testContentKeyPrefix + "ListFour", testContentKeyPrefix + "ListFive", testContentKeyPrefix + "ListSix",
testContentKeyPrefix + "ListSeven" };
List<ContentKeyInfo> expectedContentKeys = new ArrayList<ContentKeyInfo>();
for (int i = 0; i < ContentKeyNames.length; i++) {
ContentKeyInfo contentKeyInfo = service.create(ContentKey.create(createRandomContentKeyId(),
testContentKeyType, testEncryptedContentKey));
expectedContentKeys.add(contentKeyInfo);
}
List<ContentKeyInfo> actualContentKeys = service.list(ContentKey.list().setTop(2));
assertEquals(2, actualContentKeys.size());
}
@Test
public void canDeleteContentKeyById() throws Exception {
String contentKeyName = testContentKeyPrefix + "ToDelete";
ContentKeyInfo contentKeyToDelete = service.create(ContentKey.create(createRandomContentKeyId(),
testContentKeyType, testEncryptedContentKey).setName(contentKeyName));
List<ContentKeyInfo> listContentKeysResult = service.list(ContentKey.list());
int ContentKeyCountBaseline = listContentKeysResult.size();
service.delete(ContentKey.delete(contentKeyToDelete.getId()));
listContentKeysResult = service.list(ContentKey.list());
assertEquals("listPoliciesResult.size", ContentKeyCountBaseline - 1, listContentKeysResult.size());
for (ContentKeyInfo contentKey : service.list(ContentKey.list())) {
assertFalse(contentKeyToDelete.getId().equals(contentKey.getId()));
}
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(404));
service.get(ContentKey.get(contentKeyToDelete.getId()));
}
@Test
public void cannotDeleteContentKeyByInvalidId() throws Exception {
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
service.delete(ContentKey.delete(invalidId));
}
@Test
public void cannotDeleteContentKeyByNonexistId() throws Exception {
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(404));
service.delete(ContentKey.delete(validButNonexistContentKeyId));
}
}