Search in sources :

Example 91 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class SnmpTrapNorthbounderConfigDaoTest method testBeanWrapper.

/**
 * Test bean wrapper.
 *
 * @throws Exception the exception
 */
@Test
public void testBeanWrapper() throws Exception {
    SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest2");
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(sink);
    Map<String, String> params = new HashMap<String, String>();
    params.put("ipAddress", "192.168.0.1");
    Assert.assertEquals("127.0.0.2", sink.getIpAddress());
    boolean modified = false;
    for (final String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            final String stringValue = params.get(key);
            final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    Assert.assertTrue(modified);
    Assert.assertEquals("192.168.0.1", sink.getIpAddress());
    configDao.save();
    configDao.reload();
    Assert.assertEquals("192.168.0.1", configDao.getConfig().getSnmpTrapSink("localTest2").getIpAddress());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 92 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class DefaultPluginRegistry method getPluginInstance.

/**
 * {@inheritDoc}
 */
@Override
public <T> T getPluginInstance(Class<T> pluginClass, PluginConfig pluginConfig) {
    T pluginInstance = beanWithNameOfType(pluginConfig.getPluginClass(), pluginClass);
    if (pluginInstance == null) {
        return null;
    }
    Map<String, String> parameters = new HashMap<String, String>(pluginConfig.getParameterMap());
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(pluginInstance);
    try {
        wrapper.setPropertyValues(parameters);
    } catch (BeansException e) {
        error(e, "Could not set properties on report definition: {}", e.getMessage());
    }
    return pluginInstance;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) HashMap(java.util.HashMap) BeansException(org.springframework.beans.BeansException)

Example 93 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class MockForeignSourceRepositoryTest method testBeanWrapperAccess.

/**
 * This test ensures that the Spring Bean accessor classes work properly
 * since our REST implementation uses bean access to update the values.
 */
@Test
public void testBeanWrapperAccess() throws Exception {
    createRequisition();
    Requisition r = m_foreignSourceRepository.getRequisition(m_defaultForeignSourceName);
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(r);
    assertEquals("AC", wrapper.getPropertyValue("node[0].category[0].name"));
    assertEquals("UK", wrapper.getPropertyValue("node[0].category[1].name"));
    assertEquals("low", wrapper.getPropertyValue("node[0].category[2].name"));
    try {
        wrapper.getPropertyValue("node[1].category[0].name");
        fail("Did not catch expected InvalidPropertyException exception");
    } catch (InvalidPropertyException e) {
    // Expected failure
    }
    assertEquals(0, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
    wrapper.setPropertyValue("node[1].categories[0]", new RequisitionCategory("Hello world"));
    wrapper.setPropertyValue("node[1].categories[1]", new RequisitionCategory("Hello again"));
    assertEquals(2, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) RequisitionCategory(org.opennms.netmgt.provision.persist.requisition.RequisitionCategory) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) Test(org.junit.Test)

Example 94 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class OnmsAssetRecord method mergeRecord.

/**
 * Used to merge the contents of one asset record to another.  If equals implementation
 * returns false, the merge is aborted.
 *
 * @param newRecord a {@link org.opennms.netmgt.model.OnmsAssetRecord} object.
 */
public void mergeRecord(OnmsAssetRecord newRecord) {
    if (!this.equals(newRecord)) {
        return;
    }
    OnmsGeolocation toGeolocation = this.getGeolocation();
    if (toGeolocation == null) {
        toGeolocation = new OnmsGeolocation();
        this.setGeolocation(toGeolocation);
    }
    final OnmsGeolocation fromGeolocation = newRecord.getGeolocation();
    // this works because all asset properties are strings
    // if the model dependencies ever change to not include spring, this will break
    final BeanWrapper currentBean = PropertyAccessorFactory.forBeanPropertyAccess(this);
    final BeanWrapper newBean = PropertyAccessorFactory.forBeanPropertyAccess(newRecord);
    final PropertyDescriptor[] pds = newBean.getPropertyDescriptors();
    // Don't update these properties
    final List<String> blackListedProperties = new ArrayList<>();
    blackListedProperties.add("class");
    blackListedProperties.add("city");
    blackListedProperties.add("zip");
    blackListedProperties.add("state");
    blackListedProperties.add("country");
    blackListedProperties.add("longitude");
    blackListedProperties.add("latitude");
    blackListedProperties.add("address1");
    blackListedProperties.add("address2");
    for (final PropertyDescriptor pd : pds) {
        final String propertyName = pd.getName();
        if (blackListedProperties.contains(propertyName)) {
            continue;
        }
        // This should never fail since both of these objects are of the same type
        if (newBean.getPropertyValue(propertyName) != null) {
            currentBean.setPropertyValue(propertyName, newBean.getPropertyValue(propertyName));
        }
    }
    toGeolocation.mergeGeolocation(fromGeolocation);
    setGeolocation(toGeolocation);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList)

Example 95 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class SyslogNorthbounderConfigurationResource method updateSyslogDestination.

/**
 * Update a specific Syslog Destination.
 *
 * @param destinationName the destination name
 * @param params the parameters map
 * @return the response
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("destinations/{destinationName}")
public Response updateSyslogDestination(@PathParam("destinationName") final String destinationName, final MultivaluedMapImpl params) {
    writeLock();
    try {
        boolean modified = false;
        SyslogDestination destination = getSyslogDestination(destinationName);
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(destination);
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                final String stringValue = params.getFirst(key);
                final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            saveConfiguration();
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) SyslogDestination(org.opennms.netmgt.alarmd.northbounder.syslog.SyslogDestination) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5