@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().getVAppClient().undeployVApp(URI.create(node.getId()));
// wait up to ten minutes per above
assert taskTester.apply(task.getHref()) : node;
VApp vApp = getVCloudApi().getVAppClient().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().getVAppTemplateClient().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().getCatalogClient().addVAppTemplateOrMediaImageToCatalogAndNameItem(
vappTemplate.getHref(),
getVCloudApi().getCatalogClient().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().getCatalogClient().deleteCatalogItem(item.getHref());
if (vappTemplate != null)
getVCloudApi().getVAppTemplateClient().deleteVAppTemplate(vappTemplate.getHref());
if (node != null)
client.destroyNode(node.getId());
}