@Test
public void testHandleRequest_MultipleSubResources() throws Exception {
Request request = createNiceMock(Request.class);
RequestBody body = createNiceMock(RequestBody.class);
ResourceInstance resourceInstance = createNiceMock(ResourceInstance.class);
ResourceDefinition resourceDefinition = createNiceMock(ResourceDefinition.class);
ResourceInstanceFactory resourceInstanceFactory = createNiceMock(ResourceInstanceFactory.class);
Query query = createNiceMock(Query.class);
Predicate predicate = createNiceMock(Predicate.class);
Result result = createNiceMock(Result.class);
ResourceInstance subResource1 = createNiceMock(ResourceInstance.class);
ResourceInstance subResource2 = createNiceMock(ResourceInstance.class);
ResourceDefinition subResourceDefinition1 = createNiceMock(ResourceDefinition.class);
ResourceDefinition subResourceDefinition2 = createNiceMock(ResourceDefinition.class);
ClusterController controller = createNiceMock(ClusterController.class);
Schema serviceSchema = createNiceMock(Schema.class);
Schema subResSchema1 = createNiceMock(Schema.class);
Schema subResSchema2 = createNiceMock(Schema.class);
String resourceKeyProperty = "resourceKeyProperty";
String createKeyProperty = "createKeyProperty";
Resource resource1 = createNiceMock(Resource.class);
Resource resource2 = createNiceMock(Resource.class);
PersistenceManager pm = createNiceMock(PersistenceManager.class);
ResourceInstance createResource = createNiceMock(ResourceInstance.class);
RequestStatus status = createNiceMock(RequestStatus.class);
Resource statusResource1 = createNiceMock(Resource.class);
Resource statusResource2 = createNiceMock(Resource.class);
RequestHandler readHandler = createStrictMock(RequestHandler.class);
ResultStatus queryResultStatus = createNiceMock(ResultStatus.class);
// test request body. Multiple valid sub-resource types
// {
// "foo" : [
// { "prop" : "val" }
// ],
// "bar" : [
// { "prop" : "val" }
// ]
// }
Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
Set<NamedPropertySet> setRequestProps = new HashSet<NamedPropertySet>();
Map<String, Object> mapProperties = new HashMap<String, Object>();
Set<Map<String, Object>> arraySet = new HashSet<Map<String, Object>>();
mapProperties.put("foo", arraySet);
Map<String, Object> map = new HashMap<String, Object>();
map.put("prop", "val");
arraySet.add(map);
arraySet = new HashSet<Map<String, Object>>();
mapProperties.put("bar", arraySet);
map = new HashMap<String, Object>();
map.put("prop", "val");
arraySet.add(map);
setRequestProps.add(new NamedPropertySet("", mapProperties));
Map<String, ResourceInstance> mapSubResources = new HashMap<String, ResourceInstance>();
mapSubResources.put("foo", subResource1);
mapSubResources.put("bar", subResource2);
TreeNode<Resource> resultTree = new TreeNodeImpl<Resource>(null, null, "result");
resultTree.addChild(resource1, "resource1");
resultTree.addChild(resource2, "resource2");
//expectations
expect(readHandler.handleRequest(request)).andReturn(result);
expect(result.getStatus()).andReturn(queryResultStatus).anyTimes();
expect(queryResultStatus.isErrorState()).andReturn(false);
expect(result.getResultTree()).andReturn(resultTree);
expect(request.getResource()).andReturn(resourceInstance).anyTimes();
expect(request.getBody()).andReturn(body).anyTimes();
expect(body.getNamedPropertySets()).andReturn(setRequestProps).anyTimes();
expect(resourceInstance.getResourceDefinition()).andReturn(resourceDefinition).anyTimes();
expect(resourceInstance.getIds()).andReturn(mapIds).anyTimes();
expect(resourceInstance.getSubResources()).andReturn(mapSubResources).anyTimes();
expect(resourceDefinition.getType()).andReturn(Resource.Type.Service).anyTimes();
expect(subResource1.getResourceDefinition()).andReturn(subResourceDefinition1).anyTimes();
expect(subResourceDefinition1.getType()).andReturn(Resource.Type.Component).anyTimes();
expect(subResource2.getResourceDefinition()).andReturn(subResourceDefinition2).anyTimes();
expect(subResourceDefinition2.getType()).andReturn(Resource.Type.HostComponent).anyTimes();
expect(controller.getSchema(Resource.Type.Service)).andReturn(serviceSchema).anyTimes();
expect(controller.getSchema(Resource.Type.Component)).andReturn(subResSchema1).anyTimes();
expect(controller.getSchema(Resource.Type.HostComponent)).andReturn(subResSchema2).anyTimes();