@Test
public void testCaptureVApp() throws Exception {
String group = prefix + "cap";
NodeMetadata node = null;
VAppTemplate vappTemplate = null;
CatalogItem item = null;
try {
node = getOnlyElement(client.createNodesInGroup(group, 1));
Predicate<URI> taskTester = retry(new TaskSuccess(getVCloudApi()), 600, 5, SECONDS);
// I have to undeploy first
Task task = getVCloudApi().getVAppApi().undeployVApp(URI.create(node.getId()));
// wait up to ten minutes per above
assert taskTester.apply(task.getHref()) : node;
VApp vApp = getVCloudApi().getVAppApi().getVApp(URI.create(node.getId()));
// wait up to ten minutes per above
assertEquals(vApp.getStatus(), Status.OFF);
// vdc is equiv to the node's location
// vapp uri is the same as the node's id
vappTemplate = getVCloudApi().getVAppTemplateApi().captureVAppAsTemplateInVDC(URI.create(node.getId()),
group, URI.create(node.getLocation().getId()));
assertEquals(vappTemplate.getName(), group);
task = vappTemplate.getTasks().get(0);
// wait up to ten minutes per above
assert taskTester.apply(task.getHref()) : vappTemplate;
item = getVCloudApi().getCatalogApi().addVAppTemplateOrMediaImageToCatalogAndNameItem(
vappTemplate.getHref(),
getVCloudApi().getCatalogApi().findCatalogInOrgNamed(null, null).getHref(), "fooname",
CatalogItemOptions.Builder.description("description").properties(ImmutableMap.of("foo", "bar")));
assertEquals(item.getName(), "fooname");
assertEquals(item.getDescription(), "description");
assertEquals(item.getProperties(), ImmutableMap.of("foo", "bar"));
assertEquals(item.getEntity().getName(), "fooname");
assertEquals(item.getEntity().getHref(), vappTemplate.getHref());
assertEquals(item.getEntity().getType(), vappTemplate.getType());
} finally {
if (item != null)
getVCloudApi().getCatalogApi().deleteCatalogItem(item.getHref());
if (vappTemplate != null)
getVCloudApi().getVAppTemplateApi().deleteVAppTemplate(vappTemplate.getHref());
if (node != null)
client.destroyNode(node.getId());
}