@Test
public void testToResource() throws Exception {

  ViewInstanceResourceProvider provider = new ViewInstanceResourceProvider();
  Set<String> propertyIds = new HashSet<String>();
  propertyIds.add(ViewInstanceResourceProvider.PROPERTIES_PROPERTY_ID);
  propertyIds.add(ViewInstanceResourceProvider.CLUSTER_HANDLE_PROPERTY_ID);
  ViewInstanceEntity viewInstanceEntity = createNiceMock(ViewInstanceEntity.class);
  ViewEntity viewEntity = createNiceMock(ViewEntity.class);
  expect(viewInstanceEntity.getViewEntity()).andReturn(viewEntity).anyTimes();
  Map<String, String> propertyMap = new HashMap<String, String>();
  propertyMap.put("par1", "val1");
  propertyMap.put("par2", "val2");
  expect(viewInstanceEntity.getPropertyMap()).andReturn(propertyMap);
  expect(viewInstanceEntity.getData()).andReturn(Collections.<ViewInstanceDataEntity>emptyList()).anyTimes();
  expect(viewregistry.checkAdmin()).andReturn(true);
  expect(viewregistry.checkAdmin()).andReturn(false);
  expect(viewInstanceEntity.getClusterHandle()).andReturn(1L);
  replay(viewregistry, viewEntity, viewInstanceEntity);
  // as admin
  Resource resource = provider.toResource(viewInstanceEntity, propertyIds);
  Map<String, Map<String, Object>> properties = resource.getPropertiesMap();
  assertEquals(2, properties.size());
  Map<String, Object> props = properties.get("ViewInstanceInfo");
  assertNotNull(props);
  assertEquals(1, props.size());
  assertEquals(1L, props.get("cluster_handle"));
  props = properties.get("ViewInstanceInfo/properties");
  assertNotNull(props);
  assertEquals(2, props.size());
  assertEquals("val1", props.get("par1"));
  assertEquals("val2", props.get("par2"));
  // as non-admin
  resource = provider.toResource(viewInstanceEntity, propertyIds);
  properties = resource.getPropertiesMap();
  props = properties.get("ViewInstanceInfo/properties");
  assertNull(props);
  verify(viewregistry, viewEntity, viewInstanceEntity);
}