DeleteConnectionCommand extends AbstractMgcpCommand { @Override public MgcpCommandResult call() { DlcxContext context = new DlcxContext(); try { validateParameters(this.requestParameters, context); executeCommand(context); context.code = MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(); context.message = MgcpResponseCode.TRANSACTION_WAS_EXECUTED.message(); } catch (RuntimeException e) { log.error("Unexpected error occurred during tx=" + this.transactionId + " execution. Rolling back."); context.code = MgcpResponseCode.PROTOCOL_ERROR.code(); context.message = MgcpResponseCode.PROTOCOL_ERROR.message(); } catch (MgcpCallNotFoundException e) { log.error("Protocol error occurred during tx=" + this.transactionId + " execution: " + e.getMessage()); context.code = MgcpResponseCode.INCORRECT_CALL_ID.code(); context.message = MgcpResponseCode.INCORRECT_CALL_ID.message(); } catch (MgcpConnectionNotFoundException e) { log.error("Protocol error occurred during tx=" + this.transactionId + " execution: " + e.getMessage()); context.code = MgcpResponseCode.INCORRECT_CONNECTION_ID.code(); context.message = MgcpResponseCode.INCORRECT_CONNECTION_ID.message(); } catch (MgcpCommandException e) { log.error("Protocol error occurred during tx=" + this.transactionId + " execution: " + e.getMessage()); context.code = e.getCode(); context.message = e.getMessage(); } return respond(context); } DeleteConnectionCommand(int transactionId, Parameters<MgcpParameterType> parameters, MgcpEndpointManager endpointManager); @Override MgcpCommandResult call();  }
@Test public void testDeleteSingleConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final MgcpRemoteConnection connection = mock(MgcpRemoteConnection.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnection(1, 1)).thenReturn(connection); when(connection.getIdentifier()).thenReturn(1); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnection(1, 1); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); assertNotNull(result.getParameters().getString(MgcpParameterType.CONNECTION_PARAMETERS).orNull()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertTrue(parameters.getString(MgcpParameterType.CONNECTION_PARAMETERS).isPresent()); assertEquals(1, parameters.size()); }
@Test public void testDeleteAllCallConnections() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpRemoteConnection connection1 = mock(MgcpRemoteConnection.class); final MgcpLocalConnection connection2 = mock(MgcpLocalConnection.class); final List<MgcpConnection> connections = new ArrayList<>(); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnections(1)).thenReturn(connections); connections.add(connection1); connections.add(connection2); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnections(1); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); assertNull(result.getParameters().getString(MgcpParameterType.CONNECTION_PARAMETERS).orNull()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testDeleteAllEndpointConnections() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final List<MgcpConnection> connections = new ArrayList<>(); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnections()).thenReturn(connections); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnections(); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); assertNull(result.getParameters().getString(MgcpParameterType.CONNECTION_PARAMETERS).orNull()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testNoConnectionFoundWhenDeletingSingleConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnection(1, 1)).thenThrow(new MgcpConnectionNotFoundException("")); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnection(1, 1); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CONNECTION_ID.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testNoCallFoundWhenDeletingSingleConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnection(1, 1)).thenThrow(new MgcpCallNotFoundException("")); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnection(1, 1); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CALL_ID.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testNoCallFoundWhenDeletingMultipleConnections() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.deleteConnections(1)).thenThrow(new MgcpCallNotFoundException("")); MgcpCommandResult result = dlcx.call(); verify(bridgeEndpoint, times(1)).deleteConnections(1); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); assertNull(result.getParameters().getString(MgcpParameterType.CONNECTION_PARAMETERS).orNull()); }
@Test public void testValidateRequestWithEndpointNameContainingWildCardAllWithEndpointIdSpecified() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/*@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); builder.append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = dlcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestWithEndpointNameContainingWildCardAnyWithEndpointIdSpecified() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); builder.append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = dlcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestWithEndpointNameContainingWildCardAnyWithoutEndpointIdSpecified() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); MgcpCommandResult result = dlcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testUnknownEndpoint() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(null); MgcpCommandResult result = dlcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.ENDPOINT_UNKNOWN.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testUnexpectedExceptionWhileExecutingCommand() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("DLCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append(System.lineSeparator()); final int transactionId = 147483653; final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final DeleteConnectionCommand dlcx = new DeleteConnectionCommand(transactionId, request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenThrow(new RuntimeException("Test Purposes!")); MgcpCommandResult result = dlcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.PROTOCOL_ERROR.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }