Configuration resourceConfiguration = new Configuration();
resourceConfiguration.put(new PropertySimple("property1", "value1"));
// Test
CreateResourceHistory history1 = resourceFactoryManager.createResource(overlord, parentResource.getId(),
configBackedChildResourceType2.getId(), "newResource", null, resourceConfiguration);
// Verify
try {
getTransactionManager().begin();
Resource parent = em.find(Resource.class, parentResource.getId());
List<CreateResourceHistory> historyList = parent.getCreateChildResourceRequests();
assert historyList.size() == 1 : "Incorrect number of children found. Expected: 1, Found: "
+ historyList.size();
CreateResourceHistory historyItem = historyList.get(0);
assert historyItem.getStatus() == CreateResourceStatus.SUCCESS : "Incorrect status for history item. Expected: Success, Found: "
+ historyItem.getStatus();
assert historyItem.getNewResourceKey().equals("mockResourceKey") : "Incorrect resource key for history item. Expected: mockResourceKey, Found: "
+ historyItem.getNewResourceKey();
assert historyItem.getErrorMessage() == null : "Error message found for successful call";
assert historyItem.getConfiguration() != null : "Null configuration found for history item";
} finally {
getTransactionManager().rollback();
}
// Invoke the same callbacks the plugin container would to actually commit the new Resource to inventory.
resourceFactoryManager.completeCreateResource(new CreateResourceResponse(history1.getId(), "newResource",
"key", CreateResourceStatus.SUCCESS, null, new Configuration()));
Resource resource = new Resource(1000000);
resource.setUuid(UUID.randomUUID().toString());
resource.setResourceType(configBackedChildResourceType2);
long randomLong = UUID.randomUUID().getLeastSignificantBits();
resource.setResourceKey(String.valueOf("key-" + randomLong));
resource.setName("name-" + randomLong);
resource.setParentResource(parentResource);
discoveryBoss.addResource(resource, overlord.getId());
// Now try to create another Resource of the same singleton type - this should fail.
try {
CreateResourceHistory history2 = resourceFactoryManager.createResource(overlord, parentResource.getId(),
configBackedChildResourceType2.getId(), "newResource2", null, resourceConfiguration);
fail("Creating a singleton that already existed succeeded: " + history2);
} catch (EJBException e) {
assertEquals(String.valueOf(e.getCause()), RuntimeException.class, e.getCause().getClass());
assertTrue(String.valueOf(e.getCause()), e.getCause().getMessage().contains("singleton"));