### Question:
CapabilityDrivenAttributeMapTransformer extends AbstractAttributeMapTransformer { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public AttributeMap transformToAttributeMap(Map<String, Object> attributes) { if(attributes == null) { return null; } AttributeMap attributeMap = AttributeMap.newMap(); attributes.entrySet().forEach((entry) -> { NamespacedKey name = NamespacedKey.parse(entry.getKey()); String attrName = name.getNamedRepresentation(); Object attrValue = entry.getValue(); CapabilityDefinition capability = getCapabilityDefinition(name); if(capability != null) { AttributeDefinition attrDef = capability.getAttributes().get(name.getNamedRepresentation()); if(attrDef != null) { AttributeKey key = attrDef.getKey(); try { if(name.isInstanced()) { attributeMap.set(key.instance(name.getInstance()), attrDef.getAttributeType().coerce(attrValue)); } else { attributeMap.set(key, attrDef.getAttributeType().coerce(attrValue)); } } catch(Exception e) { throw new IllegalArgumentException("Invalid value: " +attrValue + " for attribute: " + key, e ); } } else { LOGGER.warn("Dropping attribute {} = {} tranforming to an AttributeMap because no attribute is defined for it.", attrName, attrValue); } } else { LOGGER.warn("Dropping attribute {} = {} transforming to an AttributeMap because no capability is defined for it.", attrName, attrValue); } }); return attributeMap; } @Inject  CapabilityDrivenAttributeMapTransformer(CapabilityRegistry registry); @SuppressWarnings({ "unchecked", "rawtypes" }) @Override AttributeMap transformToAttributeMap(Map<String, Object> attributes);  }### Answer:
@Test public void testTransformFromMap() { assertNull(transformer.transformFromAttributeMap(null)); AttributeMap map = transformer.transformToAttributeMap(Collections.<String,Object>emptyMap()); assertTrue(map.isEmpty()); Map<String,Object> attributes = new HashMap<>(); attributes.put("cont:contact", "OPENED"); attributes.put("temp:temperature", 32.2d); map = transformer.transformToAttributeMap(attributes); assertEquals(ContactCapability.CONTACT_OPENED, map.get(ContactCapability.KEY_CONTACT)); assertEquals(Double.valueOf(32.2), map.get(TemperatureCapability.KEY_TEMPERATURE)); }
@Test public void testTransformFromMapWithInstances() { Map<String,Object> attributes = new HashMap<>(); attributes.put("cont:contact:id1", "OPENED"); attributes.put("cont:contact:id2", "CLOSED"); attributes.put("temp:temperature:id3", 32.2); AttributeMap map = transformer.transformToAttributeMap(attributes); assertEquals(ContactCapability.CONTACT_OPENED, map.get(ContactCapability.KEY_CONTACT.instance("id1"))); assertEquals(ContactCapability.CONTACT_CLOSED, map.get(ContactCapability.KEY_CONTACT.instance("id2"))); assertEquals(Double.valueOf(32.2), map.get(TemperatureCapability.KEY_TEMPERATURE.instance("id3"))); }
@Test(expected=IllegalArgumentException.class) public void testTransformFromMapInvalidEnumValue() { Map<String,Object> attributes = new HashMap<>(); attributes.put("cont:contact", "ON"); attributes.put("temp:temperature", 32.2d); transformer.transformToAttributeMap(attributes); }
@Test public void testTransformFromMapNoCapability() { Map<String,Object> attributes = new HashMap<>(); attributes.put("foo:contact", "ON"); AttributeMap map = transformer.transformToAttributeMap(attributes); assertTrue(map.isEmpty()); }
@Test public void testTransformFromMapNoAttributeDefined() { Map<String,Object> attributes = new HashMap<>(); attributes.put("cont:foo", "ON"); AttributeMap map = transformer.transformToAttributeMap(attributes); assertTrue(map.isEmpty()); }