public Configuration loadResourceConfiguration() throws Exception { ConfigurationDefinition configDef = context.getResourceType().getResourceConfigurationDefinition(); ConfigurationLoadDelegate delegate = new ConfigurationLoadDelegate(configDef, connection, address); return delegate.loadResourceConfiguration(); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void loadResourceConfigurationWithType() throws Exception { // tell the method story as it happens: mock or create dependencies and configure // those dependencies to get the method under test to completion. ResourceContext mockResourceContext = mock(ResourceContext.class); ResourceType mockResourceType = mock(ResourceType.class); when(mockResourceContext.getResourceType()).thenReturn(mockResourceType); ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class); when(mockResourceType.getResourceConfigurationDefinition()) .thenReturn(mockConfigurationDefinition); ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class); when(mockConfigurationDefinition.getDefaultTemplate()).thenReturn(mockConfigurationTemplate); Configuration mockConfiguration = mock(Configuration.class); when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration); Property mockProperty = mock(Property.class); when(mockConfiguration.get(eq("__type"))).thenReturn(mockProperty); Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class); when(mockConfigurationDefinition.getPropertyDefinitions()).thenReturn(mockMap); ConfigurationLoadDelegate mockConfigurationLoadDelegate = mock(ConfigurationLoadDelegate.class); PowerMockito.whenNew(ConfigurationLoadDelegate.class) .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class) .withArguments( any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class)) .thenReturn(mockConfigurationLoadDelegate); when(mockConfigurationLoadDelegate.loadResourceConfiguration()).thenReturn(mockConfiguration); PropertySimple pathPropertySimple = new PropertySimple("path", "abc=def,xyz=test1"); when(mockConfiguration.get(eq("path"))).thenReturn(pathPropertySimple); when(mockResourceContext.getPluginConfiguration()).thenReturn(mockConfiguration); ASConnection mockASConnection = mock(ASConnection.class); PropertySimple typePropertySimple = new PropertySimple("__type", "xyz"); PowerMockito.whenNew(PropertySimple.class) .withParameterTypes(String.class, Object.class) .withArguments(eq("__type"), eq("xyz")) .thenReturn(typePropertySimple); // create object to test and inject required dependencies TemplatedComponent objectUnderTest = new TemplatedComponent(); objectUnderTest.context = mockResourceContext; objectUnderTest.testConnection = mockASConnection; // run code under test Configuration result = objectUnderTest.loadResourceConfiguration(); // verify the results (Assert and mock verification) Assert.assertEquals(result, mockConfiguration); verify(mockMap, times(1)).remove(eq("__type")); verify(mockConfiguration, times(1)).get(eq("__type")); verify(mockConfiguration, never()).get(eq("__name")); verify(mockConfiguration, times(1)).get(eq("path")); verify(mockConfiguration, times(1)).put(eq(typePropertySimple)); PowerMockito.verifyNew(PropertySimple.class).withArguments(eq("__type"), eq("xyz")); PowerMockito.verifyNew(ConfigurationLoadDelegate.class) .withArguments( any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class)); }
@Override public void updateResourceConfiguration( final ConfigurationUpdateReport configurationUpdateReport) { ResourceType resourceType = context.getResourceType(); ConfigurationDefinition configDef = resourceType.getResourceConfigurationDefinition(); Configuration newConfig = configurationUpdateReport.getConfiguration(); // remove special property being possibly sent from server in case EAP requires reload/restart newConfig.remove("__OOB"); // 1. First of all, read the current configuration ConfigurationLoadDelegate readDelegate = new ConfigurationLoadDelegate(configDef, getASConnection(), address, includeRuntime); Configuration currentConfig; try { currentConfig = readDelegate.loadResourceConfiguration(); } catch (Exception e) { getLog().error("Could not read current configuration before update", e); configurationUpdateReport.setStatus(FAILURE); configurationUpdateReport.setErrorMessage( "Could not read current configuration before update: " + ThrowableUtil.getRootMessage(e)); return; } // 2. We will wrap all property-simple and connection properties changes in a composite // operation CompositeOperation updateOperation = new CompositeOperation(); // 3. Capture property-simple changes Map<String, PropertySimple> newConfigSimpleProperties = newConfig.getSimpleProperties(); Map<String, PropertySimple> currentConfigSimpleProperties = currentConfig.getSimpleProperties(); Set<String> allSimplePropertyNames = new HashSet<String>( newConfigSimpleProperties.size() + currentConfigSimpleProperties.size()); allSimplePropertyNames.addAll(newConfigSimpleProperties.keySet()); allSimplePropertyNames.addAll(currentConfigSimpleProperties.keySet()); // Read-only allSimplePropertyNames.remove(ENABLED_ATTRIBUTE); for (String simplePropertyName : allSimplePropertyNames) { PropertySimple newConfigPropertySimple = newConfigSimpleProperties.get(simplePropertyName); String newConfigPropertySimpleValue = newConfigPropertySimple == null ? null : newConfigPropertySimple.getStringValue(); PropertySimple currentConfigPropertySimple = currentConfigSimpleProperties.get(simplePropertyName); String currentConfigPropertySimpleValue = currentConfigPropertySimple == null ? null : currentConfigPropertySimple.getStringValue(); boolean canUnset = !UNSET_FORBIDDEN_ATTRIBUTES.contains(simplePropertyName); if (newConfigPropertySimpleValue == null) { if (currentConfigPropertySimpleValue != null) { String val; if (canUnset) { val = null; } else { val = configDef.getPropertyDefinitionSimple(simplePropertyName).getDefaultValue(); } updateOperation.addStep(new WriteAttribute(getAddress(), simplePropertyName, val)); } } else if (!newConfigPropertySimpleValue.equals(currentConfigPropertySimpleValue)) { updateOperation.addStep( new WriteAttribute(getAddress(), simplePropertyName, newConfigPropertySimpleValue)); } } // 4. Capture connection property changes String connPropAttributeNameOnServer, connPropPluginConfigPropertyName, keyName; if (isXADatasourceResource(resourceType)) { connPropAttributeNameOnServer = "xa-datasource-properties"; connPropPluginConfigPropertyName = XA_DATASOURCE_PROPERTIES_ATTRIBUTE; keyName = "key"; } else { connPropAttributeNameOnServer = "connection-properties"; connPropPluginConfigPropertyName = CONNECTION_PROPERTIES_ATTRIBUTE; keyName = "pname"; } Map<String, String> newConfigConnectionProperties = getConnectionPropertiesAsMap(newConfig.getList(connPropPluginConfigPropertyName), keyName); Map<String, String> currentConfigConnectionProperties = getConnectionPropertiesAsMap( currentConfig.getList(connPropPluginConfigPropertyName), keyName); Set<String> allConnectionPropertyNames = new HashSet<String>( newConfigConnectionProperties.size() + currentConfigConnectionProperties.size()); allConnectionPropertyNames.addAll(newConfigConnectionProperties.keySet()); allConnectionPropertyNames.addAll(currentConfigConnectionProperties.keySet()); for (String connectionPropertyName : allConnectionPropertyNames) { Address propertyAddress = new Address(getAddress()); propertyAddress.add(connPropAttributeNameOnServer, connectionPropertyName); String newConfigConnectionPropertyValue = newConfigConnectionProperties.get(connectionPropertyName); String currentConfigConnectionPropertyValue = currentConfigConnectionProperties.get(connectionPropertyName); if (newConfigConnectionPropertyValue == null) { updateOperation.addStep(new Operation("remove", propertyAddress)); } else if (currentConfigConnectionPropertyValue == null) { Operation addOperation = new Operation("add", propertyAddress); addOperation.addAdditionalProperty("value", newConfigConnectionPropertyValue); updateOperation.addStep(addOperation); } else if (!newConfigConnectionPropertyValue.equals(currentConfigConnectionPropertyValue)) { updateOperation.addStep(new Operation("remove", propertyAddress)); Operation addOperation = new Operation("add", propertyAddress); addOperation.addAdditionalProperty("value", newConfigConnectionPropertyValue); updateOperation.addStep(addOperation); } } // 5. Update config if needed if (updateOperation.numberOfSteps() > 0) { Result res = getASConnection().execute(updateOperation); if (res.isSuccess()) { configurationUpdateReport.setStatus(SUCCESS); } else { configurationUpdateReport.setStatus(FAILURE); configurationUpdateReport.setErrorMessage(res.getFailureDescription()); } } else { configurationUpdateReport.setStatus(NOCHANGE); } }