BaseProvider { protected static boolean setResourceProperty(Resource resource, String propertyId, Object value, Set<String> requestedIds) { boolean contains = requestedIds.contains(propertyId) || isPropertyCategoryRequested(propertyId, requestedIds); if (contains) { if (LOG.isDebugEnabled()) { LOG.debug("Setting property for resource" + ", resourceType=" + resource.getType() + ", propertyId=" + propertyId + ", value=" + value); } if (!setResourceMapProperty(resource, propertyId, value)){ resource.setProperty(propertyId, value); } } else { if (value instanceof Map<?, ?>) { Map<?, ?> mapValue = (Map) value; for (Map.Entry entry : mapValue.entrySet()) { String entryPropertyId = PropertyHelper.getPropertyId(propertyId, entry.getKey().toString()); Object entryValue = entry.getValue(); contains = setResourceProperty(resource, entryPropertyId, entryValue, requestedIds) || contains; } } if (!contains && LOG.isDebugEnabled()) { LOG.debug("Skipping property for resource as not in requestedIds" + ", resourceType=" + resource.getType() + ", propertyId=" + propertyId + ", value=" + value); } } return contains; } BaseProvider(Set<String> propertyIds); Set<String> checkPropertyIds(Set<String> propertyIds); Set<String> getPropertyIds(); Set<String> getCategoryIds();  }
@Test public void testSetResourceProperty() { Set<String> propertyIds = new HashSet<String>(); propertyIds.add("p1"); propertyIds.add("foo"); propertyIds.add("cat1/foo"); propertyIds.add("cat2/bar"); propertyIds.add("cat2/baz"); propertyIds.add("cat3/sub1/bam"); propertyIds.add("cat4/sub2/sub3/bat"); propertyIds.add("cat5/sub5"); Resource resource = new ResourceImpl(Resource.Type.Service); assertNull(resource.getPropertyValue("foo")); BaseProvider.setResourceProperty(resource, "foo", "value1", propertyIds); assertEquals("value1", resource.getPropertyValue("foo")); BaseProvider.setResourceProperty(resource, "cat2/bar", "value2", propertyIds); assertEquals("value2", resource.getPropertyValue("cat2/bar")); assertNull(resource.getPropertyValue("unsupported")); BaseProvider.setResourceProperty(resource, "unsupported", "valueX", propertyIds); assertNull(resource.getPropertyValue("unsupported")); BaseProvider.setResourceProperty(resource, "cat5/sub5/prop5", "value5", propertyIds); assertEquals("value5", resource.getPropertyValue("cat5/sub5/prop5")); BaseProvider.setResourceProperty(resource, "cat5/sub5/sub5a/prop5a", "value5", propertyIds); assertEquals("value5", resource.getPropertyValue("cat5/sub5/sub5a/prop5a")); BaseProvider.setResourceProperty(resource, "cat5/sub7/unsupported", "valueX", propertyIds); assertNull(resource.getPropertyValue("cat5/sub7/unsupported")); }
@Test public void testSetResourcePropertyWithMaps() { Set<String> propertyIds = new HashSet<String>(); propertyIds.add("cat1/emptyMapProperty"); propertyIds.add("cat1/mapProperty"); propertyIds.add("cat2/mapMapProperty"); propertyIds.add("cat3/mapProperty3/key2"); propertyIds.add("cat4/mapMapProperty4/subMap1/key3"); propertyIds.add("cat4/mapMapProperty4/subMap2"); Resource resource = new ResourceImpl(Resource.Type.Service); Map<String, String> emptyMapProperty = new HashMap<String, String>(); BaseProvider.setResourceProperty(resource, "cat1/emptyMapProperty", emptyMapProperty, propertyIds); assertTrue(resource.getPropertiesMap().containsKey("cat1/emptyMapProperty")); Map<String, String> mapProperty = new HashMap<String, String>(); mapProperty.put("key1", "value1"); mapProperty.put("key2", "value2"); mapProperty.put("key3", "value3"); BaseProvider.setResourceProperty(resource, "cat1/mapProperty", mapProperty, propertyIds); assertNull(resource.getPropertyValue("cat1/mapProperty")); assertEquals("value1", resource.getPropertyValue("cat1/mapProperty/key1")); assertEquals("value2", resource.getPropertyValue("cat1/mapProperty/key2")); assertEquals("value3", resource.getPropertyValue("cat1/mapProperty/key3")); Map<String, Map<String, String>> mapMapProperty = new HashMap<String, Map<String, String>>(); Map<String, String> mapSubProperty1 = new HashMap<String, String>(); mapSubProperty1.put("key1", "value11"); mapSubProperty1.put("key2", "value12"); mapSubProperty1.put("key3", "value13"); mapMapProperty.put("subMap1", mapSubProperty1); Map<String, String> mapSubProperty2 = new HashMap<String, String>(); mapSubProperty2.put("key1", "value21"); mapSubProperty2.put("key2", "value22"); mapSubProperty2.put("key3", "value23"); mapMapProperty.put("subMap2", mapSubProperty2); Map<String, String> mapSubProperty3 = new HashMap<String, String>(); mapMapProperty.put("subMap3", mapSubProperty3); BaseProvider.setResourceProperty(resource, "cat2/mapMapProperty", mapMapProperty, propertyIds); assertNull(resource.getPropertyValue("cat2/mapMapProperty")); assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap1")); assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap2")); assertTrue(resource.getPropertiesMap().containsKey( "cat2/mapMapProperty/subMap3")); assertEquals("value11", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key1")); assertEquals("value12", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key2")); assertEquals("value13", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key3")); assertEquals("value21", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key1")); assertEquals("value22", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key2")); assertEquals("value23", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key3")); Map<String, String> mapProperty3 = new HashMap<String, String>(); mapProperty3.put("key1", "value1"); mapProperty3.put("key2", "value2"); mapProperty3.put("key3", "value3"); BaseProvider.setResourceProperty(resource, "cat3/mapProperty3", mapProperty3, propertyIds); assertNull(resource.getPropertyValue("cat3/mapProperty3")); assertNull(resource.getPropertyValue("cat3/mapProperty3/key1")); assertEquals("value2", resource.getPropertyValue("cat3/mapProperty3/key2")); assertNull(resource.getPropertyValue("cat3/mapProperty3/key3")); Map<String, Map<String, String>> mapMapProperty4 = new HashMap<String, Map<String, String>>(); mapMapProperty4.put("subMap1", mapSubProperty1); mapMapProperty4.put("subMap2", mapSubProperty2); BaseProvider.setResourceProperty(resource, "cat4/mapMapProperty4", mapMapProperty4, propertyIds); assertNull(resource.getPropertyValue("cat4/mapMapProperty4")); assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1")); assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap2")); assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key1")); assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key2")); assertEquals("value13", resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key3")); assertEquals("value21", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key1")); assertEquals("value22", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key2")); assertEquals("value23", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key3")); }