CreateConnectionCommand extends AbstractMgcpCommand { private void rollback(CrcxContext context) { final int callId = context.getCallId(); final String endpointId = context.getEndpointId(); final String secondEndpointId = context.getSecondEndpointId(); final int connectionId = context.getConnectionId(); final int secondConnectionId = context.getSecondConnectionId(); MgcpEndpoint endpoint1 = endpointId.isEmpty() ? null : this.endpointManager.getEndpoint(endpointId); MgcpEndpoint endpoint2 = secondEndpointId.isEmpty() ? null : this.endpointManager.getEndpoint(secondEndpointId); if (endpoint1 != null && connectionId > 0) { try { endpoint1.deleteConnection(callId, connectionId); } catch (MgcpCallNotFoundException | MgcpConnectionNotFoundException e) { log.error("Could not delete primary connection. " + e.getMessage()); } } if (endpoint2 != null && secondConnectionId > 0) { try { endpoint2.deleteConnection(callId, secondConnectionId); } catch (MgcpCallNotFoundException | MgcpConnectionNotFoundException e) { log.error("Could not delete secondary connection. " + e.getMessage()); } } } CreateConnectionCommand(int transactionId, Parameters<MgcpParameterType> parameters, MgcpEndpointManager endpointManager); @Override MgcpCommandResult call();  }
@Test public void testRollback() throws MgcpException { final StringBuilder builder = new StringBuilder(); builder.append("CRCX 147483655 mobicents/bridge/$@127.0.0.1:2427 MGCP 1.0").append(System.lineSeparator()); builder.append("C:1").append(System.lineSeparator()); builder.append("M:sendonly").append(System.lineSeparator()); builder.append("N:restcomm@127.0.0.1:2727").append(System.lineSeparator()); builder.append("Z2:mobicents/ivr/$@127.0.0.1:2427"); final int callId = 1; final int transactionId = 147483655; final MgcpRequest request = new MgcpMessageParser().parseRequest(builder.toString()); final MgcpEndpointManager endpointManager = mock(MgcpEndpointManager.class); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MgcpEndpoint bridgeEndpoint = mock(MgcpEndpoint.class); final MgcpEndpoint ivrEndpoint = mock(MgcpEndpoint.class); final EndpointIdentifier bridgeEndpointId = new EndpointIdentifier("mobicents/bridge/1", "127.0.0.1:2427"); final EndpointIdentifier ivrEndpointId = new EndpointIdentifier("mobicents/ivr/2", "127.0.0.1:2427"); final MgcpLocalConnection connection1 = mock(MgcpLocalConnection.class); final MgcpLocalConnection connection2 = mock(MgcpLocalConnection.class); final CreateConnectionCommand crcx = new CreateConnectionCommand(transactionId, request.getParameters(), endpointManager); when(connectionProvider.provideLocal(callId)).thenReturn(connection1, connection2); when(connection1.getIdentifier()).thenReturn(1); when(connection1.getHexIdentifier()).thenReturn(Integer.toHexString(1)); when(connection2.getIdentifier()).thenReturn(2); when(connection2.getHexIdentifier()).thenReturn(Integer.toHexString(1)); when(bridgeEndpoint.getEndpointId()).thenReturn(bridgeEndpointId); when(ivrEndpoint.getEndpointId()).thenReturn(ivrEndpointId); when(endpointManager.registerEndpoint("mobicents/bridge/")).thenReturn(bridgeEndpoint); when(endpointManager.registerEndpoint("mobicents/ivr/")).thenReturn(ivrEndpoint); when(endpointManager.getEndpoint(bridgeEndpoint.getEndpointId().toString())).thenReturn(bridgeEndpoint); when(endpointManager.getEndpoint(ivrEndpoint.getEndpointId().toString())).thenReturn(ivrEndpoint); when(bridgeEndpoint.createConnection(callId, true)).thenReturn(connection1); when(ivrEndpoint.createConnection(callId, true)).thenReturn(connection2); doThrow(MgcpConnectionException.class).when(connection1).join(connection2); MgcpCommandResult result = crcx.call(); verify(bridgeEndpoint, times(1)).deleteConnection(callId, connection1.getIdentifier()); verify(ivrEndpoint, times(1)).deleteConnection(callId, connection2.getIdentifier()); assertNotNull(result); assertEquals(MgcpResponseCode.PROTOCOL_ERROR.code(), result.getCode()); assertEquals(0, result.getParameters().size()); }