}
@Test(description = "POST /vdc/{id}/media")
public void testAddMedia() throws URISyntaxException {
Vdc vdc = lazyGetVdc();
Link addMedia = find(vdc.getLinks(), and(relEquals("add"), typeEquals(VCloudDirectorMediaType.MEDIA)));
// TODO: generate an iso
byte[] iso = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name("Test media " + random.nextInt())
.size(iso.length).imageType(Media.ImageType.ISO).description("Test media generated by testAddMedia()")
.build();
media = mediaApi.add(addMedia.getHref(), sourceMedia);
Checks.checkMediaFor(MEDIA, media);
assertNotNull(media.getFiles(), String.format(OBJ_FIELD_REQ, MEDIA, "files"));
assertEquals(1, media.getFiles().size(),
String.format(OBJ_FIELD_LIST_SIZE_EQ, MEDIA, "files", 1, media.getFiles().size()));
File uploadFile = getFirst(media.getFiles(), null);
assertNotNull(uploadFile, String.format(OBJ_FIELD_REQ, MEDIA, "files.first"));
assertEquals(uploadFile.getSize(), Long.valueOf(iso.length));
assertEquals(uploadFile.getSize().longValue(), sourceMedia.getSize(),
String.format(OBJ_FIELD_EQ, MEDIA, "uploadFile.size()", sourceMedia.getSize(), uploadFile.getSize()));
Set<Link> links = uploadFile.getLinks();
assertNotNull(links, String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links"));
assertTrue(links.size() >= 1, String.format(OBJ_FIELD_LIST_SIZE_GE, MEDIA, "uploadfile.links", 1, links.size()));
assertTrue(
Iterables.all(
links,
Predicates.or(LinkPredicates.relEquals(Link.Rel.UPLOAD_DEFAULT),
LinkPredicates.relEquals(Link.Rel.UPLOAD_ALTERNATE))),
String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links.first"));
Link uploadLink = Iterables.find(links, LinkPredicates.relEquals(Link.Rel.UPLOAD_DEFAULT));
context.getApi().getUploadApi().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso));
media = mediaApi.get(media.getId());
if (media.getTasks().size() == 1) {
Task uploadTask = Iterables.getOnlyElement(media.getTasks());
Checks.checkTask(uploadTask);