public void setConfigAttributes(Object attributes, SecureKeyInfoProvider secureKeyInfoProvider) { this.clear(); Map attributesMap = (Map) attributes; for (Object o : attributesMap.values()) { Map configurationAttributeMap = (Map) o; ConfigurationProperty configurationProperty = new ConfigurationProperty(); configurationProperty.setConfigAttributes(configurationAttributeMap, secureKeyInfoProvider); this.add(configurationProperty); } }
@Test public void shouldSetConfigAttributesWhenMetadataIsNotPassedInMap() { ConfigurationProperty configurationProperty = new ConfigurationProperty(); HashMap attributes = new HashMap(); HashMap keyMap = new HashMap(); keyMap.put("name", "fooKey"); attributes.put(ConfigurationProperty.CONFIGURATION_KEY, keyMap); HashMap valueMap = new HashMap(); valueMap.put("value", "fooValue"); attributes.put(ConfigurationProperty.CONFIGURATION_VALUE, valueMap); configurationProperty.setConfigAttributes(attributes,null); assertThat(configurationProperty.getConfigurationKey().getName(), is("fooKey")); assertThat(configurationProperty.getConfigurationValue().getValue(), is("fooValue")); assertThat(configurationProperty.getEncryptedConfigurationValue(), is(nullValue())); }
@Test public void shouldSetConfigAttributesForSecurePropertyWhenUserDoesNotChangeIt() { ConfigurationProperty configurationProperty = new ConfigurationProperty(); HashMap attributes = new HashMap(); HashMap keyMap = new HashMap(); final String secureKey = "fooKey"; keyMap.put("name", secureKey); attributes.put(ConfigurationProperty.CONFIGURATION_KEY, keyMap); HashMap valueMap = new HashMap(); valueMap.put("value", "fooValue"); attributes.put(ConfigurationProperty.CONFIGURATION_VALUE, valueMap); HashMap encryptedValueMap = new HashMap(); encryptedValueMap.put("value", "encryptedValue"); attributes.put(ConfigurationProperty.ENCRYPTED_VALUE, encryptedValueMap); configurationProperty.setConfigAttributes(attributes,new SecureKeyInfoProvider() { @Override public boolean isSecure(String key) { return secureKey.equals(key); } }); assertThat(configurationProperty.getConfigurationKey().getName(), is(secureKey)); assertThat(configurationProperty.getConfigurationValue(), is(nullValue())); assertThat(configurationProperty.getEncryptedValue(), is("encryptedValue")); }
@Test public void shouldSetConfigAttributesForNonSecureProperty() { ConfigurationProperty configurationProperty = new ConfigurationProperty(); HashMap attributes = new HashMap(); HashMap keyMap = new HashMap(); keyMap.put("name", "fooKey"); attributes.put(ConfigurationProperty.CONFIGURATION_KEY, keyMap); HashMap valueMap = new HashMap(); valueMap.put("value", "fooValue"); attributes.put(ConfigurationProperty.CONFIGURATION_VALUE, valueMap); PackageConfigurations metadata = new PackageConfigurations(); metadata.addConfiguration(new PackageConfiguration("fooKey", null)); attributes.put(Configuration.METADATA, metadata); configurationProperty.setConfigAttributes(attributes,null); assertThat(configurationProperty.getConfigurationKey().getName(), is("fooKey")); assertThat(configurationProperty.getConfigurationValue().getValue(), is("fooValue")); }
@Test public void shouldSetConfigAttributesForSecurePropertyWhenUserChangesIt() throws Exception { ConfigurationProperty configurationProperty = new ConfigurationProperty(); HashMap attributes = new HashMap(); HashMap keyMap = new HashMap(); final String secureKey = "fooKey"; keyMap.put("name", secureKey); attributes.put(ConfigurationProperty.CONFIGURATION_KEY, keyMap); HashMap valueMap = new HashMap(); valueMap.put("value", "fooValue"); attributes.put(ConfigurationProperty.CONFIGURATION_VALUE, valueMap); attributes.put(ConfigurationProperty.IS_CHANGED, "0"); configurationProperty.setConfigAttributes(attributes,new SecureKeyInfoProvider() { @Override public boolean isSecure(String key) { return secureKey.equals(key); } }); String encryptedValue = new GoCipher().encrypt("fooValue"); assertThat(configurationProperty.getConfigurationKey().getName(), is(secureKey)); assertThat(configurationProperty.getConfigurationValue(), is(nullValue())); assertThat(configurationProperty.getEncryptedValue(), is(encryptedValue)); }