Search in sources :

Example 1 with Command

use of cern.c2mon.server.common.device.Command in project c2mon by c2mon.

the class DeviceClassMapperTest method testInsertDeviceClass.

@Test
public void testInsertDeviceClass() throws ClassNotFoundException {
    DeviceClassCacheObject deviceClass = new DeviceClassCacheObject(402L, "TEST_DEVICE_CLASS_3", "Description of TEST_DEVICE_CLASS_3");
    List<Property> properties = new ArrayList<>();
    properties.add(new Property(10L, "TEST_PROPERTY_1", "Test property 1"));
    properties.add(new Property(11L, "TEST_PROPERTY_2", "Test property 2"));
    List<Property> fields = new ArrayList<>();
    fields.add(new Property(12L, "TEST_FIELD_1", null));
    fields.add(new Property(13L, "TEST_FIELD_2", null));
    properties.add(new Property(14L, "TEST_PROPERTY_WITH_FIELDS", "Test property with fields", fields));
    deviceClass.setProperties(properties);
    deviceClass.setCommands(Arrays.asList(new Command(10L, "TEST_COMMAND_1", "Test command 1"), new Command(11L, "TEST_COMMAND_2", "Test command 2")));
    deviceClassMapper.insertDeviceClass(deviceClass);
    for (Property property : ((DeviceClassCacheObject) deviceClass).getProperties()) {
        deviceClassMapper.insertDeviceClassProperty(deviceClass.getId(), property);
        if (property.getFields() != null) {
            for (Property field : property.getFields()) {
                deviceClassMapper.insertDeviceClassField(property.getId(), field);
            }
        }
    }
    for (Command command : ((DeviceClassCacheObject) deviceClass).getCommands()) {
        deviceClassMapper.insertDeviceClassCommand(deviceClass.getId(), command);
    }
    Assert.assertTrue(deviceClassMapper.isInDb(402L));
    DeviceClassCacheObject fromDb = (DeviceClassCacheObject) deviceClassMapper.getItem(402L);
    Assert.assertNotNull(fromDb);
    List<Property> retrievedProperties = fromDb.getProperties();
    Assert.assertNotNull(retrievedProperties);
    Assert.assertTrue(retrievedProperties.size() == 3);
    assertPropertyListContains(retrievedProperties, new Property(10L, "TEST_PROPERTY_1", "Test property 1"));
    assertPropertyListContains(retrievedProperties, new Property(11L, "TEST_PROPERTY_2", "Test property 2"));
    assertPropertyListContains(retrievedProperties, new Property(14L, "TEST_PROPERTY_WITH_FIELDS", "Test property with fields", fields));
    List<Property> retrievedFields = retrievedProperties.get(0).getFields();
    assertPropertyListContains(retrievedFields, new Property(12L, "TEST_FIELD_1", null));
    assertPropertyListContains(retrievedFields, new Property(13L, "TEST_FIELD_2", null));
    List<Command> commands = fromDb.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(10L, "TEST_COMMAND_1", "Test command 1"));
    assertCommandListContains(commands, new Command(11L, "TEST_COMMAND_2", "Test command 2"));
}
Also used : Command(cern.c2mon.server.common.device.Command) ArrayList(java.util.ArrayList) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) Property(cern.c2mon.server.common.device.Property) Test(org.junit.Test)

Example 2 with Command

use of cern.c2mon.server.common.device.Command in project c2mon by c2mon.

the class DeviceClassFacadeImpl method parseXmlCommands.

/**
 * Parse the XML representation of the commands of a device class (which comes
 * from configuration) and return it as a list of commands.
 *
 * @param xmlString the XML representation string of the device class commands
 *
 * @return the list of commands
 * @throws Exception if the XML could not be parsed
 */
private List<Command> parseXmlCommands(String xmlString) throws Exception {
    List<Command> commands = new ArrayList<>();
    Serializer serializer = new Persister();
    CommandList commandList = serializer.read(CommandList.class, xmlString);
    for (Command command : commandList.getCommands()) {
        commands.add(command);
    }
    return commands;
}
Also used : CommandList(cern.c2mon.server.common.device.CommandList) Command(cern.c2mon.server.common.device.Command) ArrayList(java.util.ArrayList) Persister(org.simpleframework.xml.core.Persister) Serializer(org.simpleframework.xml.Serializer)

Example 3 with Command

use of cern.c2mon.server.common.device.Command in project c2mon by c2mon.

the class DeviceClassMapperTest method testGetItem.

@Test
public void testGetItem() throws ClassNotFoundException {
    DeviceClassCacheObject deviceClass1 = (DeviceClassCacheObject) deviceClassMapper.getItem(400L);
    Assert.assertNotNull(deviceClass1);
    List<Property> properties = deviceClass1.getProperties();
    Assert.assertNotNull(properties);
    Assert.assertTrue(properties.size() == 5);
    assertPropertyListContains(properties, new Property(1L, "cpuLoadInPercent", "The current CPU load in percent"));
    assertPropertyListContains(properties, new Property(2L, "responsiblePerson", "The person responsible for this device"));
    assertPropertyListContains(properties, new Property(3L, "someCalculations", "Some super awesome calculations"));
    assertPropertyListContains(properties, new Property(4L, "numCores", "The number of CPU cores on this device"));
    List<Property> fields = new ArrayList<>();
    fields.add(new Property(1L, "FIELD_CPULOAD", null));
    fields.add(new Property(2L, "FIELD_RESPONSIBLE_PERSON", null));
    fields.add(new Property(3L, "FIELD_SOME_CALCULATIONS", null));
    fields.add(new Property(4L, "FIELD_NUM_CORES", null));
    assertPropertyListContains(properties, new Property(9L, "TEST_PROPERTY_WITH_FIELDS", "Description of TEST_PROPERTY_WITH_FIELDS", fields));
    List<Command> commands = deviceClass1.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(1L, "TEST_COMMAND_1", "Description of TEST_COMMAND_1"));
    assertCommandListContains(commands, new Command(2L, "TEST_COMMAND_2", "Description of TEST_COMMAND_2"));
    List<Long> deviceIds = deviceClass1.getDeviceIds();
    Assert.assertNotNull(deviceIds);
    Assert.assertFalse(deviceIds.isEmpty());
    Assert.assertTrue(deviceIds.contains(300L));
    Assert.assertTrue(deviceIds.contains(301L));
    DeviceClassCacheObject deviceClass2 = (DeviceClassCacheObject) deviceClassMapper.getItem(401L);
    Assert.assertNotNull(deviceClass2);
    properties = deviceClass2.getProperties();
    Assert.assertNotNull(properties);
    Assert.assertTrue(properties.size() == 4);
    assertPropertyListContains(properties, new Property(5L, "TEST_PROPERTY_1", "Description of TEST_PROPERTY_1"));
    assertPropertyListContains(properties, new Property(6L, "TEST_PROPERTY_2", "Description of TEST_PROPERTY_2"));
    assertPropertyListContains(properties, new Property(7L, "TEST_PROPERTY_3", "Description of TEST_PROPERTY_3"));
    assertPropertyListContains(properties, new Property(8L, "TEST_PROPERTY_4", "Description of TEST_PROPERTY_4"));
    commands = deviceClass2.getCommands();
    Assert.assertNotNull(commands);
    Assert.assertTrue(commands.size() == 2);
    assertCommandListContains(commands, new Command(3L, "TEST_COMMAND_3", "Description of TEST_COMMAND_3"));
    assertCommandListContains(commands, new Command(4L, "TEST_COMMAND_4", "Description of TEST_COMMAND_4"));
    deviceIds = deviceClass2.getDeviceIds();
    Assert.assertNotNull(deviceIds);
    Assert.assertFalse(deviceIds.isEmpty());
    Assert.assertTrue(deviceIds.contains(302L));
    Assert.assertTrue(deviceIds.contains(303L));
}
Also used : Command(cern.c2mon.server.common.device.Command) ArrayList(java.util.ArrayList) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) Property(cern.c2mon.server.common.device.Property) Test(org.junit.Test)

Example 4 with Command

use of cern.c2mon.server.common.device.Command in project c2mon by c2mon.

the class DeviceClassFacadeImpl method configureCacheObject.

@Override
protected Change configureCacheObject(DeviceClass cacheObject, Properties properties) throws IllegalAccessException {
    DeviceClassCacheObject deviceClassCacheObject = (DeviceClassCacheObject) cacheObject;
    if (properties.getProperty("name") != null) {
        deviceClassCacheObject.setName(properties.getProperty("name"));
    }
    if (properties.getProperty("description") != null) {
        deviceClassCacheObject.setDescription(properties.getProperty("description"));
    }
    // Parse properties and commands from XML representation
    if (properties.getProperty("properties") != null) {
        try {
            List<Property> propertyNames = parseXmlProperties(properties.getProperty("properties"));
            deviceClassCacheObject.setProperties(propertyNames);
        } catch (Exception e) {
            throw new ConfigurationException(ConfigurationException.INVALID_PARAMETER_VALUE, "Exception: Unable to create property list from parameter \"properties\": " + e + ":\n" + properties.getProperty("properties"));
        }
    }
    if (properties.getProperty("commands") != null) {
        try {
            List<Command> commandNames = parseXmlCommands(properties.getProperty("commands"));
            deviceClassCacheObject.setCommands(commandNames);
        } catch (Exception e) {
            throw new ConfigurationException(ConfigurationException.INVALID_PARAMETER_VALUE, "Exception: Unable to create command list from parameter \"commands\": " + e + ":\n" + properties.getProperty("commands"));
        }
    }
    return null;
}
Also used : ConfigurationException(cern.c2mon.shared.common.ConfigurationException) Command(cern.c2mon.server.common.device.Command) DeviceClassCacheObject(cern.c2mon.server.common.device.DeviceClassCacheObject) Property(cern.c2mon.server.common.device.Property) ConfigurationException(cern.c2mon.shared.common.ConfigurationException)

Example 5 with Command

use of cern.c2mon.server.common.device.Command in project c2mon by c2mon.

the class ObjectEqualityComparison method assertDeviceClassEquals.

public static void assertDeviceClassEquals(DeviceClassCacheObject expectedObject, DeviceClassCacheObject cacheObject) throws ClassNotFoundException {
    assertEquals(expectedObject.getId(), cacheObject.getId());
    assertEquals(expectedObject.getName(), cacheObject.getName());
    assertEquals(expectedObject.getDescription(), cacheObject.getDescription());
    assertEquals(expectedObject.getProperties().size(), cacheObject.getProperties().size());
    for (Property property : expectedObject.getProperties()) {
        ObjectComparison.assertPropertyListContains(cacheObject.getProperties(), property);
    }
    assertEquals(expectedObject.getCommands().size(), cacheObject.getCommands().size());
    for (Command command : expectedObject.getCommands()) {
        ObjectComparison.assertCommandListContains(cacheObject.getCommands(), command);
    }
}
Also used : Command(cern.c2mon.server.common.device.Command) DeviceCommand(cern.c2mon.shared.client.device.DeviceCommand) DeviceProperty(cern.c2mon.shared.client.device.DeviceProperty) Property(cern.c2mon.server.common.device.Property)

Aggregations

Command (cern.c2mon.server.common.device.Command)5 Property (cern.c2mon.server.common.device.Property)4 DeviceClassCacheObject (cern.c2mon.server.common.device.DeviceClassCacheObject)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 CommandList (cern.c2mon.server.common.device.CommandList)1 DeviceCommand (cern.c2mon.shared.client.device.DeviceCommand)1 DeviceProperty (cern.c2mon.shared.client.device.DeviceProperty)1 ConfigurationException (cern.c2mon.shared.common.ConfigurationException)1 Serializer (org.simpleframework.xml.Serializer)1 Persister (org.simpleframework.xml.core.Persister)1