/** * Returns the {@link SingleProperty} with the given logical name in the configuration or * <code>null</code> if such does not exist. * * @param <T> * Type of property value. * * @param propertyLogicalName * Property logical name to search for. * @return {@link SingleProperty} or <code>null</code> */ @SuppressWarnings("unchecked") public <T> SingleProperty<T> forLogicalName(String propertyLogicalName) { for (AbstractProperty property : getAllProperties()) { SingleProperty<?> returnProperty = property.forLogicalname(propertyLogicalName); if (null != returnProperty) { return (SingleProperty<T>) returnProperty; } } return null; }
@BeforeMethod public void init() { MockitoAnnotations.initMocks(this); configuration = new Configuration(); PropertySection section = new PropertySection(); section.addProperty(property1); section.addProperty(property2); configuration.addSection(section); }
/** * Returns the currently existing {@link PropertySection} in the CMR configuration. * * @return Returns the currently existing {@link PropertySection} in the CMR configuration. */ public Collection<PropertySection> getConfigurationPropertySections() { return configuration.getSections(); }
SingleProperty<Object> property = configuration.forLogicalName(propertyUpdate.getPropertyLogicalName()); Map<AbstractProperty, PropertyValidation> validationMap = configuration.validate(); for (AbstractProperty property : configuration.getAllProperties()) { if (!validationMap.containsKey(property)) { property.register(properties);
/** * Test that if validation fails for the property it won't be included in the * {@link Properties} returned by {@link PropertyManager}. */ @SuppressWarnings("unchecked") @Test public void propertyNotValidInDefaultConfiguration() throws JAXBException, IOException, SAXException { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); SingleProperty<?> property = mock(SingleProperty.class); PropertyValidation propertyValidation = mock(PropertyValidation.class, Mockito.RETURNS_SMART_NULLS); when(configuration.getAllProperties()).thenReturn(Collections.<AbstractProperty> singleton(property)); when(configuration.validate()).thenReturn(MapUtils.putAll(new HashMap<AbstractProperty, PropertyValidation>(), new Object[][] { { property, propertyValidation } })); Properties properties = propertyManager.getProperties(); verify(property, times(0)).register(Matchers.<Properties> anyObject()); assertThat(properties.size(), is(0)); }
@Test public void increaseBufferSizeWithOccupancy() throws Exception { Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxOldSpaceOcc = configuration.forLogicalName("buffer.maxOldSpaceOccupancy"); SingleProperty<Float> minOldSpaceOcc = configuration.forLogicalName("buffer.minOldSpaceOccupancy"); long oldBufferSize = bufferProperties.getInitialBufferSize(); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(maxOldSpaceOcc.getValue().floatValue() + 0.05f))); configurationUpdate.addPropertyUpdate(minOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(minOldSpaceOcc.getValue().floatValue() + 0.05f))); propertyManager.updateConfiguration(configurationUpdate, false); long newBufferSize = bufferProperties.getInitialBufferSize(); assertThat(newBufferSize, is(greaterThan(oldBufferSize))); assertThat(newBufferSize, is(buffer.getMaxSize())); }
@Test public void validate() { when(propertyValidation1.hasErrors()).thenReturn(true); when(property1.validate()).thenReturn(propertyValidation1); when(propertyValidation2.hasErrors()).thenReturn(false); when(property2.validate()).thenReturn(propertyValidation2); Map<AbstractProperty, PropertyValidation> validateMap = configuration.validate(); assertThat(validateMap, hasEntry(property1, propertyValidation1)); assertThat(validateMap, not(hasKey(property2))); } }
/** * Test that the {@link Properties} returned by the {@link PropertyManager} are correctly * take from {@link Configuration}. */ @Test public void propertyInDefaultConfiguration() throws JAXBException, IOException, SAXException { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); when(configuration.validate()).thenReturn(Collections.<AbstractProperty, PropertyValidation> emptyMap()); SingleProperty<?> property = mock(SingleProperty.class); Answer<Object> answer = new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Properties properties = (Properties) invocation.getArguments()[0]; properties.put("property1", "value1"); return null; } }; doAnswer(answer).when(property).register(Matchers.<Properties> anyObject()); when(configuration.getAllProperties()).thenReturn(Collections.<AbstractProperty> singleton(property)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo("value1"))); assertThat(properties.size(), is(1)); }
/** * Check that Exception will be thrown if the {@link ConfigurationUpdate} has update for non * existing property. */ @SuppressWarnings("unchecked") @Test(expectedExceptions = { Exception.class }) public void noUpdateIfPropertyNotFound() throws Exception { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); when(configuration.forLogicalName(Matchers.<String> anyObject())).thenReturn(null); ConfigurationUpdate configurationUpdate = mock(ConfigurationUpdate.class); AbstractPropertyUpdate<Long> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); propertyManager.init(); propertyManager.loadConfigurationAndUpdates(); propertyManager.updateConfiguration(configurationUpdate, false); }
/** * Test that not matching property update will not be taken into account. */ @Test @SuppressWarnings("unchecked") public void savedPropertyUpdateNotValid() throws JAXBException, IOException, SAXException { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<Long> property = new LongProperty("", "", "property1", 10L, false, false); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(configurationUpdate).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); AbstractPropertyUpdate<String> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.getUpdateValue()).thenReturn("updatedValue"); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); Properties properties = propertyManager.getProperties(); assertThat(Long.valueOf(properties.getProperty("property1")), is(10L)); assertThat(properties.size(), is(1)); }
/** * Check that Exception will be thrown if the {@link ConfigurationUpdate} has update that * can not be applied. */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expectedExceptions = { Exception.class }) public void noUpdateIfCanNotUpdateProperty() throws Exception { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); ConfigurationUpdate configurationUpdate = mock(ConfigurationUpdate.class); AbstractPropertyUpdate<Long> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); SingleProperty singleProperty = mock(SingleProperty.class); when(singleProperty.canUpdate(propertyUpdate)).thenReturn(false); when(configuration.forLogicalName(Matchers.<String> anyObject())).thenReturn(singleProperty); propertyManager.init(); propertyManager.loadConfigurationAndUpdates(); propertyManager.updateConfiguration(configurationUpdate, false); }
/** * Validates all properties in this configuration returning the map of containing the properties * that have validation errors. * * @return Map of properties with {@link PropertyValidation} containing errors. */ public Map<AbstractProperty, PropertyValidation> validate() { Map<AbstractProperty, PropertyValidation> validationMap = new HashMap<>(); for (AbstractProperty property : getAllProperties()) { PropertyValidation propertyValidation = property.validate(); if (propertyValidation.hasErrors()) { validationMap.put(property, propertyValidation); } } return validationMap; }
/** * Test that {@link Properties} will hold an updated value of the property if it's included * in the {@link ConfigurationUpdate}. */ @Test @SuppressWarnings("unchecked") public void savedPropertyUpdateFromTheDefaultConfiguration() throws JAXBException, IOException, SAXException { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<String> property = new StringProperty("", "", "property1", "value1", false, false); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(configurationUpdate).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); AbstractPropertyUpdate<String> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.getUpdateValue()).thenReturn("updatedValue"); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo("updatedValue"))); assertThat(properties.size(), is(1)); }
@Test public void increaseExpansionRate() throws Exception { long bufferSize = bufferProperties.getInitialBufferSize(); Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxExpansionRate = configuration.forLogicalName("buffer.maxObjectExpansionRate"); float oldExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxExpansionRate.createAndValidatePropertyUpdate(Float.valueOf(maxExpansionRate.getValue().floatValue() + 0.1f))); propertyManager.updateConfiguration(configurationUpdate, false); float newExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); assertThat(newExpansionrate, is(greaterThan(oldExpansionrate))); }
/** * Test that {@link Properties} will reseted to default if it's included in the * {@link ConfigurationUpdate}. */ @Test @SuppressWarnings("unchecked") public void savedPropertyRestoreToDefaultUpdate() throws JAXBException, IOException, SAXException { String defaultValue = "value1"; Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<String> property = new StringProperty("", "", "property1", defaultValue, false, false); property.setValue("someotherthandefault"); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(configurationUpdate).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); AbstractPropertyUpdate<String> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.isRestoreDefault()).thenReturn(true); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo(defaultValue))); assertThat(properties.size(), is(1)); }
SingleProperty<Object> property = configuration.forLogicalName(propertyUpdate.getPropertyLogicalName()); if (null == property) { throw new Exception("Property " + propertyUpdate.getPropertyLogicalName() + " can not be updated because the property does not exist in the current configuration."); SingleProperty<Object> property = configuration.forLogicalName(propertyUpdate.getPropertyLogicalName()); if (propertyUpdate.isRestoreDefault()) { property.setToDefaultValue();
@Test public void runtimePropertyUpdate() throws Exception { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<Long> property = new LongProperty("", "", "property1", 10L, false, false); section.addProperty(property); configuration.addSection(section);
public void runtimePropertyResetToDefault() throws Exception { long defaultValue = 10L; Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<Long> property = new LongProperty("", "", "property1", defaultValue, false, false); property.setValue(45523L); section.addProperty(property); configuration.addSection(section);
@Test public void runtimePropertyUpdateOfAlreadyUpdatedProperty() throws Exception { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<Long> property = new LongProperty("", "", "property1", 10L, false, false); section.addProperty(property); configuration.addSection(section);
/** * Check that property will be correctly updated during runtime and server will be * restarted. */ @SuppressWarnings({ "unchecked" }) @Test public void runtimePropertyUpdateServerRestart() throws Exception { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<Long> property = new LongProperty("", "", "property1", 10L, false, true); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); doNothing().when(transformator).marshall(Matchers.<Path> anyObject(), any(), anyString()); ConfigurationUpdate configurationUpdate = mock(ConfigurationUpdate.class); AbstractPropertyUpdate<Long> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.getUpdateValue()).thenReturn(20L); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); propertyManager.init(); propertyManager.loadConfigurationAndUpdates(); propertyManager.updateConfiguration(configurationUpdate, true); assertThat(property.getValue(), is(20L)); // confirm configuration update write verify(transformator, times(1)).marshall(Matchers.<Path> anyObject(), eq(configurationUpdate), anyString()); // confirm server service was called verify(shutdownService).restart(); verifyZeroInteractions(propertyUpdateExecutor); }