ConfigureDelegator extends BaseDelegator implements IConfigureDelegator { @Override public String setOrUnsetServerConfigurationValue(@Nonnull final String name, @Nullable final String value) throws P4JavaException { Validate.notBlank(name, "Config name shouldn't null or empty"); String[] args = new String[]{UNSET, name}; if (isNotBlank(value)) { args = new String[]{SET, name + "=" + value}; } List<Map<String, Object>> resultMaps = execMapCmdList(CONFIGURE, args, null); StringBuilder messageBuilder = new StringBuilder(STRING_BUILDER_SIZE); if (nonNull(resultMaps)) { for (Map<String, Object> map : resultMaps) { if (nonNull(map)) { String str = ResultMapParser.getErrorOrInfoStr(map); if (isBlank(str)) { if (map.containsKey(NAME_KEY)) { if (map.containsKey(ACTION_KEY) && nonNull(map.get(ACTION_KEY))) { String message = str; String serverName = parseString(map, SERVER_NAME_KEY); String configureName = parseString(map, NAME_KEY); String configureValue = parseString(map, VALUE_KEY); String action = parseString(map, ACTION_KEY); if (SET.equalsIgnoreCase(action)) { message = format( "For server '%s', configuration variable '%s' " + "set to '%s'", serverName, configureName, configureValue); } else if (UNSET.equalsIgnoreCase(action)) { message = format( "For server '%s', configuration variable '%s' removed.", serverName, configureName); } if (isNotBlank(message)) { messageBuilder.append(message).append("\n"); } } } } else { return str; } } } } return messageBuilder.toString(); } ConfigureDelegator(final IOptionsServer server); @Override String setOrUnsetServerConfigurationValue(@Nonnull final String name,
                                                     @Nullable final String value); @Override List<ServerConfigurationValue> showServerConfiguration(final String serverName,
                                                                  final String variableName);  }
@Test public void testSetWithServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_NAME_MATCHER), eq((Map<String,Object>)null))) .thenReturn(getMockServerResponse(RpcFunctionMapKey.SET, SERVER_NAME, MONITOR, MONITOR_VALUE)); String result = configureDelegator .setOrUnsetServerConfigurationValue(SERVER_NAME + "#" + MONITOR, MONITOR_VALUE); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_NAME_MATCHER), eq((Map<String,Object>)null)); assertTrue(result.startsWith( String.format("For server '%s', configuration variable '%s' set to '%s'", SERVER_NAME, MONITOR, MONITOR_VALUE))); }
@Test public void testSetWithoutServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_MATCHER), eq((Map<String,Object>)null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.SET, null, MONITOR, MONITOR_VALUE)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, MONITOR_VALUE); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_MATCHER), eq((Map<String,Object>)null)); assertTrue(result.startsWith( String.format("For server '%s', configuration variable '%s' set to '%s'", null, MONITOR, MONITOR_VALUE))); }
@Test public void testUnSetWithServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_NAME_MATCHER), eq((Map<String,Object>)null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.UNSET, SERVER_NAME, MONITOR, null)); String result = configureDelegator .setOrUnsetServerConfigurationValue(SERVER_NAME + "#" + MONITOR, null); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_NAME_MATCHER), eq((Map<String,Object>)null)); assertTrue(result.startsWith(String.format( "For server '%s', configuration variable '%s' removed", SERVER_NAME, MONITOR))); }
@Test public void testUnSetWithoutServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_MATCHER), eq((Map<String,Object>)null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.UNSET, null, MONITOR, null)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, null); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_MATCHER), eq((Map<String,Object>)null)); assertTrue(result.startsWith(String .format("For server '%s', configuration variable '%s' removed", null, MONITOR))); }
@Test public void testUnSetUnknown() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), any(String[].class), eq((Map<String,Object>)null))) .thenReturn(getMockServerErrorResponse(MONITOR)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, null); assertTrue(result.startsWith( String.format("Configuration variable '%s' did not have a value.", MONITOR))); }
@Test public void testSetWithServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_NAME_MATCHER), eq(null))) .thenReturn(getMockServerResponse(RpcFunctionMapKey.SET, SERVER_NAME, MONITOR, MONITOR_VALUE)); String result = configureDelegator .setOrUnsetServerConfigurationValue(SERVER_NAME + "#" + MONITOR, MONITOR_VALUE); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_NAME_MATCHER), eq(null)); assertTrue(result.startsWith( String.format("For server '%s', configuration variable '%s' set to '%s'", SERVER_NAME, MONITOR, MONITOR_VALUE))); }
@Test public void testSetWithoutServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_MATCHER), eq(null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.SET, null, MONITOR, MONITOR_VALUE)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, MONITOR_VALUE); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(SET_MATCHER), eq(null)); assertTrue(result.startsWith( String.format("For server '%s', configuration variable '%s' set to '%s'", null, MONITOR, MONITOR_VALUE))); }
@Test public void testUnSetWithServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_NAME_MATCHER), eq(null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.UNSET, SERVER_NAME, MONITOR, null)); String result = configureDelegator .setOrUnsetServerConfigurationValue(SERVER_NAME + "#" + MONITOR, null); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_NAME_MATCHER), eq(null)); assertTrue(result.startsWith(String.format( "For server '%s', configuration variable '%s' removed", SERVER_NAME, MONITOR))); }
@Test public void testUnSetWithoutServerName() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_MATCHER), eq(null))).thenReturn( getMockServerResponse(RpcFunctionMapKey.UNSET, null, MONITOR, null)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, null); verify(server).execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), argThat(UNSET_MATCHER), eq(null)); assertTrue(result.startsWith(String .format("For server '%s', configuration variable '%s' removed", null, MONITOR))); }
@Test public void testUnSetUnknown() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.CONFIGURE.toString()), any(String[].class), eq(null))) .thenReturn(getMockServerErrorResponse(MONITOR)); String result = configureDelegator.setOrUnsetServerConfigurationValue(MONITOR, null); assertTrue(result.startsWith( String.format("Configuration variable '%s' did not have a value.", MONITOR))); }
@Test public void testSetBlankName() throws P4JavaException { try { configureDelegator.setOrUnsetServerConfigurationValue("", "value"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } catch (Exception e) { fail("Expected IllegalArgumentException was " + e); } }
@Test public void testSetNullName() throws P4JavaException { try { configureDelegator.setOrUnsetServerConfigurationValue(null, "value"); fail("Expected NullPointerException"); } catch (NullPointerException e) { } catch (Exception e) { fail("Expected NullPointerException was " + e); } }