public void testPersistUserSuppliedResourceNameOnCreatedResource() throws Exception {
// First inventory the platform and the server
InventoryReport inventoryReport = new InventoryReport(agent);
Resource platform = new Resource(prefix("userPlatform"), prefix("platform"), platformType);
platform.setUuid(String.valueOf(new Random().nextInt()));
Resource server = new Resource(prefix("userServer"), prefix("server"), serverType);
server.setUuid(String.valueOf(new Random().nextInt()));
platform.addChildResource(server);
inventoryReport.addAddedRoot(platform);
MergeInventoryReportResults results = discoveryBoss.mergeInventoryReport(serialize(inventoryReport));
assertNotNull(results);
assert checkIgnoredTypes(results) : "nothing should have been ignored in this test "
+ results.getIgnoredResourceTypes();
final PlatformSyncInfo firstDiscoverySyncInfo = results.getPlatformSyncInfo();
assertNotNull(firstDiscoverySyncInfo);
// Then simulate a create resource request
final String userSuppliedResourceName = prefix("User Supplied Resource Name");
final String newResourceKey = prefix("Created Resource Key");
final int serverResourceId = firstDiscoverySyncInfo.getTopLevelServerIds().iterator().next();
executeInTransaction(false, new TransactionCallback() {
@Override
public void execute() throws Exception {
Resource serverResource = getEntityManager().find(Resource.class, serverResourceId);
CreateResourceHistory createResourceHistory = new CreateResourceHistory(serverResource, serviceType1,
subjectManager.getOverlord().getName(), new Configuration());
createResourceHistory.setCreatedResourceName(userSuppliedResourceName);
createResourceHistory.setNewResourceKey(newResourceKey);
createResourceHistory.setStatus(SUCCESS);
getEntityManager().persist(createResourceHistory);
serverResource.addCreateChildResourceHistory(createResourceHistory);
getEntityManager().flush();
}
});
// Eventually inventory the newly discovered service
inventoryReport = new InventoryReport(agent);
Resource service1 = new Resource(newResourceKey, prefix("Plugin Computed Resource Name"), serviceType1);
service1.setUuid(String.valueOf(new Random().nextInt()));
server.setId(serverResourceId);
server.addChildResource(service1);
inventoryReport.addAddedRoot(server);
results = discoveryBoss.mergeInventoryReport(serialize(inventoryReport));
assertNotNull(results);
assert checkIgnoredTypes(results) : "nothing should have been ignored in this test "
+ results.getIgnoredResourceTypes();
PlatformSyncInfo secondDiscoverySyncInfo = results.getPlatformSyncInfo();
assertNotNull(secondDiscoverySyncInfo);
// Check that the resource ends with the user supplied name in inventory
Integer toplevelServerId = secondDiscoverySyncInfo.getTopLevelServerIds().iterator().next();
Collection<ResourceSyncInfo> topLevelServerSyncInfo = discoveryBoss.getResourceSyncInfo(toplevelServerId);
assert topLevelServerSyncInfo.size() == 2;
Iterator<ResourceSyncInfo> iter = topLevelServerSyncInfo.iterator();
Integer childId = iter.next().getId();
childId = childId.equals(toplevelServerId) ? iter.next().getId() : childId;
Resource service1Resource = getEntityManager().find(Resource.class, childId);
assertEquals(userSuppliedResourceName, service1Resource.getName());
}