AMXNICommandBuilder implements CommandBuilder { @Override public Command build(Element element) { String commandAsString = null; String deviceIndexAsString = null; String channelAsString = null; String levelAsString = null; String valueAsString = null; String pulseTimeAsString = null; String statusFilterAsString = null; String statusFilterGroupAsString = null; Integer deviceIndex = null; Integer channel = null; Integer level = null; Integer pulseTime = null; Integer statusFilterGroup = null; @SuppressWarnings("unchecked") List<Element> propertyElements = element.getChildren(XML_ELEMENT_PROPERTY, element.getNamespace()); for (Element el : propertyElements) { String propertyName = el.getAttributeValue(XML_ATTRIBUTENAME_NAME); String propertyValue = el.getAttributeValue(XML_ATTRIBUTENAME_VALUE); if (AMX_NI_XMLPROPERTY_COMMAND.equalsIgnoreCase(propertyName)) { commandAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_DEVICE_INDEX.equalsIgnoreCase(propertyName)) { deviceIndexAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_CHANNEL.equalsIgnoreCase(propertyName)) { channelAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_LEVEL.equalsIgnoreCase(propertyName)) { levelAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_VALUE.equalsIgnoreCase(propertyName)) { valueAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_PULSE_TIME.equalsIgnoreCase(propertyName)) { pulseTimeAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_STATUS_FILTER.equalsIgnoreCase(propertyName)) { statusFilterAsString = propertyValue; } else if (AMX_NI_XMLPROPERTY_STATUS_FILTER_GROUP.equalsIgnoreCase(propertyName)) { statusFilterGroupAsString = propertyValue; } else if (!AMX_NI_XMLPROPERTY_NAME.equalsIgnoreCase(propertyName)) { log.warn("Unknown AMX NI property '<" + XML_ELEMENT_PROPERTY + " " + XML_ATTRIBUTENAME_NAME + " = \"" + propertyName + "\" " + XML_ATTRIBUTENAME_VALUE + " = \"" + propertyValue + "\"/>'."); } } if (commandAsString == null || "".equals(commandAsString)) { throw new NoSuchCommandException("AMX NI command must have a '" + AMX_NI_XMLPROPERTY_COMMAND + "' property."); } if (deviceIndexAsString == null || "".equals(deviceIndexAsString)) { throw new NoSuchCommandException("AMX NI command must have a '" + AMX_NI_XMLPROPERTY_DEVICE_INDEX + "' property."); } try { deviceIndex = Integer.parseInt(deviceIndexAsString); } catch (NumberFormatException e) { log.error("Invalid deviceIndex", e); throw new NoSuchCommandException(e.getMessage(), e); } if (channelAsString != null && !"".equals(channelAsString)) { try { channel = Integer.parseInt(channelAsString); } catch (NumberFormatException e) { log.error("Invalid channel number", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (levelAsString != null && !"".equals(levelAsString)) { try { level = Integer.parseInt(levelAsString); } catch (NumberFormatException e) { log.error("Invalid level number", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (pulseTimeAsString != null && !"".equals(pulseTimeAsString)) { try { pulseTime = Integer.parseInt(pulseTimeAsString); } catch (NumberFormatException e) { log.error("Invalid pulse time", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (statusFilterGroupAsString != null && !"".equals(statusFilterGroupAsString)) { try { statusFilterGroup = Integer.parseInt(statusFilterGroupAsString); } catch (NumberFormatException e) { log.error("Invalid status filter group", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (valueAsString == null) { valueAsString = element.getAttributeValue(Command.DYNAMIC_VALUE_ATTR_NAME); } Command cmd = AMXNICommand.createCommand(commandAsString, gateway, deviceIndex, channel, level, valueAsString, pulseTime, statusFilterAsString, statusFilterGroup); log.info("Created AMX NI Command " + cmd); return cmd; } AMXNICommandBuilder(); @Override Command build(Element element); AMXNIGateway getGateway(); void setGateway(AMXNIGateway gateway); final static String AMX_NI_LOG_CATEGORY; final static String AMX_NI_XMLPROPERTY_NAME; final static String AMX_NI_XMLPROPERTY_COMMAND; final static String AMX_NI_XMLPROPERTY_DEVICE_INDEX; final static String AMX_NI_XMLPROPERTY_CHANNEL; final static String AMX_NI_XMLPROPERTY_LEVEL; final static String AMX_NI_XMLPROPERTY_VALUE; final static String AMX_NI_XMLPROPERTY_PULSE_TIME; final static String AMX_NI_XMLPROPERTY_STATUS_FILTER; final static String AMX_NI_XMLPROPERTY_STATUS_FILTER_GROUP; }
@Test public void testChannelCommands() { CommandBuilder cb = new AMXNICommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("ON", "1", "1", null, null, null, null, null)) instanceof ChannelCommand); Assert.assertTrue(cb.build(getCommandElement("OFF", "1", "1", null, null, null, null, null)) instanceof ChannelCommand); Assert.assertTrue(cb.build(getCommandElement("PULSE", "1", "1", null, null, null, null, null)) instanceof ChannelCommand); Assert.assertTrue(cb.build(getCommandElement("PULSE", "1", "1", null, null, "5", null, null)) instanceof ChannelCommand); Assert.assertTrue(cb.build(getCommandElement("CHANNEL_STATUS", "1", "1", null, null, null, null, null)) instanceof ChannelCommand); }
@Test(expected = NoSuchCommandException.class) public void testChannelCommandInvalidDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("ON", "a", "1", null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testChannelCommandMissingDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("ON", null, "1", null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testChannelCommandInvalidChannel() { new AMXNICommandBuilder().build(getCommandElement("ON", "1", "a", null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testChannelCommandMissingChannel() { new AMXNICommandBuilder().build(getCommandElement("ON", "1", null, null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testChannelCommandInvalidPulseTime() { new AMXNICommandBuilder().build(getCommandElement("ON", "1", "1", null, null, "a", null, null)); }
@Test public void testLevelCommands() { CommandBuilder cb = new AMXNICommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("SEND_LEVEL", "1", null, "1", "50", null, null, null)) instanceof LevelCommand); Assert.assertTrue(cb.build(getCommandElement("LEVEL_STATUS", "1", null, "1", null, null, null, null)) instanceof LevelCommand); }
@Test public void testLevelCommandDynamicElement() { CommandBuilder cb = new AMXNICommandBuilder(); Element ele = getCommandElement("SEND_LEVEL", "1", null, "1", null, null, null, null); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "50"); Assert.assertTrue(cb.build(ele) instanceof LevelCommand); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandInvalidDynamicElement() { Element ele = getCommandElement("SEND_LEVEL", "1", null, "1", null, null, null, null); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "a"); new AMXNICommandBuilder().build(ele); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandInvalidDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", "a", null, "1", "50", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandMissingDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", null, null, "1", "50", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandInvalidLevel() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", "1", null, "a", "50", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandMissingLevel() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", "1", null, null, "50", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandInvalidValue() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", "1", null, "1", "a", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testLevelCommandMissingValue() { new AMXNICommandBuilder().build(getCommandElement("SEND_LEVEL", "1", null, "1", null, null, null, null)); }
@Test public void testCommandCommands() { CommandBuilder cb = new AMXNICommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("SEND_COMMAND", "1", null, null, "a command", null, null, null)) instanceof CommandCommand); Assert.assertTrue(cb.build(getCommandElement("COMMAND_READ", "1", null, null, null, null, null, null)) instanceof CommandCommand); Assert.assertTrue(cb.build(getCommandElement("COMMAND_READ", "1", null, null, null, null, "command", null)) instanceof CommandCommand); Assert.assertTrue(cb.build(getCommandElement("COMMAND_READ", "1", null, null, null, null, "command", "1")) instanceof CommandCommand); }
@Test(expected = NoSuchCommandException.class) public void testCommandCommandInvalidDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_COMMAND", "a", null, null, "a command", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testCommandCommandMissingDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_COMMAND", null, null, null, "a command", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testCommandCommandMissingCommand() { new AMXNICommandBuilder().build(getCommandElement("SEND_COMMAND", "1", null, null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testCommandCommandInvalidStatusFilterGroup() { new AMXNICommandBuilder().build(getCommandElement("SEND_COMMAND", "1", null, null, "a command", null, null, "a")); }
@Test public void testStringCommands() { CommandBuilder cb = new AMXNICommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("SEND_STRING", "1", null, null, "a string", null, null, null)) instanceof StringCommand); Assert.assertTrue(cb.build(getCommandElement("STRING_READ", "1", null, null, null, null, null, null)) instanceof StringCommand); Assert.assertTrue(cb.build(getCommandElement("STRING_READ", "1", null, null, null, null, "string", null)) instanceof StringCommand); Assert.assertTrue(cb.build(getCommandElement("STRING_READ", "1", null, null, null, null, "string", "1")) instanceof StringCommand); }
@Test(expected = NoSuchCommandException.class) public void testStringCommandInvalidDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_STRING", "a", null, null, "a string", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testStringCommandMissingDeviceIndex() { new AMXNICommandBuilder().build(getCommandElement("SEND_STRING", null, null, null, "a string", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testCommandStringMissingCommand() { new AMXNICommandBuilder().build(getCommandElement("SEND_STRING", "1", null, null, null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testStringCommandInvalidStatusFilterGroup() { new AMXNICommandBuilder().build(getCommandElement("SEND_STRING", "1", null, null, "a string", null, null, "a")); }
@Test(expected = NoSuchCommandException.class) public void testInvalidCommand() { new AMXNICommandBuilder().build(getCommandElement("INVALID", null, null, null, null, null, null, null)); }