ModifyConnectionCommand extends AbstractMgcpCommand { @Override public MgcpCommandResult call() { MdcxContext context = new MdcxContext(); 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 (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); } ModifyConnectionCommand(int transactionId, Parameters<MgcpParameterType> parameters, MgcpEndpointManager endpointManager); @Override MgcpCommandResult call();  }
@Test public void testModifyConnectionMode() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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()); builder.append("M:sendonly").append(System.lineSeparator()); 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 ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getConnection(1, 1)).thenReturn(connection); MgcpCommandResult result = mdcx.call(); verify(connection, times(1)).setMode(ConnectionMode.SEND_ONLY); verify(connection, never()).open(any(String.class)); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testModifyRemoteDescription() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append(System.lineSeparator()); final StringBuilder builderSdp = new StringBuilder(); builderSdp.append("v=0").append(System.lineSeparator()); builderSdp.append("o=hrosa 3616 1899 IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("s=Talk").append(System.lineSeparator()); builderSdp.append("c=IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("t=0 0").append(System.lineSeparator()); builderSdp.append("m=audio 7070 RTP/AVP 8 0 101").append(System.lineSeparator()); builderSdp.append("a=rtpmap:101 telephone-event/8000"); builder.append(builderSdp.toString()); 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 ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getConnection(1, 1)).thenReturn(connection); when(connection.open(any(String.class))).thenReturn("answer"); MgcpCommandResult result = mdcx.call(); verify(connection, times(1)).setMode(ConnectionMode.SEND_RECV); verify(connection, times(1)).open(builderSdp.toString()); assertNotNull(result); assertEquals(MgcpResponseCode.TRANSACTION_WAS_EXECUTED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(1, parameters.size()); assertEquals("answer", parameters.getString(MgcpParameterType.SDP).get()); }
@Test public void testValidateRequestWithEndpointNameContainingWildCardAll() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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("M:sendrecv").append(System.lineSeparator()); builder.append(System.lineSeparator()); final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestWithEndpointNameContainingWildCardAny() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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("M:sendrecv").append(System.lineSeparator()); builder.append(System.lineSeparator()); final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.WILDCARD_TOO_COMPLICATED.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestMissingCallId() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("I:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append(System.lineSeparator()); final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CALL_ID.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestMissingConnectionId() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 147483653 mobicents/bridge/1@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendrecv").append(System.lineSeparator()); builder.append(System.lineSeparator()); final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CONNECTION_ID.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testValidateRequestWithInvalidMode() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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()); builder.append("M:xywz").append(System.lineSeparator()); builder.append(System.lineSeparator()); final MgcpMessageParser parser = new MgcpMessageParser(); final MgcpRequest request = parser.parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INVALID_OR_UNSUPPORTED_MODE.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("MDCX 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()); builder.append("M:sendonly").append(System.lineSeparator()); 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 ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(null); when(bridgeEndpoint.getConnection(1, 1)).thenReturn(connection); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.ENDPOINT_UNKNOWN.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test public void testUnknownConnection() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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()); builder.append("M:sendonly").append(System.lineSeparator()); 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 ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getConnection(1, 1)).thenReturn(null); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.INCORRECT_CONNECTION_ID.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }
@Test @SuppressWarnings("unchecked") public void testInvalidRemoteDescription() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("MDCX 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()); builder.append("M:sendonly").append(System.lineSeparator()); builder.append(System.lineSeparator()); final StringBuilder builderSdp = new StringBuilder(); builderSdp.append("v=0").append(System.lineSeparator()); builderSdp.append("o=hrosa 3616 1899 IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("s=Talk").append(System.lineSeparator()); builderSdp.append("c=IN IP4 127.0.0.1").append(System.lineSeparator()); builderSdp.append("t=0 0").append(System.lineSeparator()); builderSdp.append("m=audio 7070 RTP/AVP 8 0 101").append(System.lineSeparator()); builderSdp.append("a=rtpmap:101 telephone-event/8000"); builder.append(builderSdp.toString()); 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 ModifyConnectionCommand mdcx = new ModifyConnectionCommand(request.getTransactionId(), request.getParameters(), endpointManager); when(endpointManager.getEndpoint("mobicents/bridge/1@127.0.0.1:2427")).thenReturn(bridgeEndpoint); when(bridgeEndpoint.getConnection(1, 1)).thenReturn(connection); when(connection.open(builderSdp.toString())).thenThrow(MgcpConnectionException.class); MgcpCommandResult result = mdcx.call(); assertNotNull(result); assertEquals(MgcpResponseCode.UNSUPPORTED_SDP.code(), result.getCode()); Parameters<MgcpParameterType> parameters = result.getParameters(); assertEquals(0, parameters.size()); }