GenericMgcpEndpoint implements MgcpEndpoint { @Override public List<MgcpConnection> deleteConnections(int callId) throws MgcpCallNotFoundException { Collection<MgcpConnection> current = this.connections.values(); List<MgcpConnection> deleted = new ArrayList<>(current.size()); for (MgcpConnection connection : current) { if(connection.getCallIdentifier() == callId) { MgcpConnection removed = this.connections.remove(connection.getIdentifier()); if(removed != null) { deleted.add(removed); } } } if(deleted.size() == 0) { throw new MgcpCallNotFoundException(this.endpointId + " could not find call " + Integer.toHexString(callId).toUpperCase()); } if(log.isDebugEnabled()) { String hexIdentifiers = Arrays.toString(getConnectionHexId(deleted)); log.debug("Endpoint " + this.endpointId.toString() + " deleted " + deleted.size() + " connections from call " + callId + ": "+ hexIdentifiers +". Connection count: " + this.connections.size()); } if (!hasConnections() && isActive()) { deactivate(); } return deleted; } GenericMgcpEndpoint(EndpointIdentifier endpointId, MgcpConnectionProvider connectionProvider, MediaGroup mediaGroup); @Override EndpointIdentifier getEndpointId(); @Override MediaGroup getMediaGroup(); boolean hasConnections(); @Override MgcpConnection getConnection(int callId, int connectionId); @Override MgcpConnection createConnection(int callId, boolean local); @Override MgcpConnection deleteConnection(int callId, int connectionId); @Override List<MgcpConnection> deleteConnections(int callId); @Override List<MgcpConnection> deleteConnections(); boolean isActive(); @Override synchronized void requestNotification(NotificationRequest request); @Override void cancelSignal(String signal); @Override void observe(MgcpMessageObserver observer); @Override void forget(MgcpMessageObserver observer); @Override void notify(Object originator, InetSocketAddress from, InetSocketAddress to, MgcpMessage message, MessageDirection direction); @Override void onEvent(Object originator, MgcpEvent event); @Override void observe(MgcpEndpointObserver observer); @Override void forget(MgcpEndpointObserver observer); @Override void notify(MgcpEndpoint endpoint, MgcpEndpointState state);  }
@Test public void testDeleteConnections() throws Exception { final int callId1 = 1; final int callId2 = 2; final int connectionId1 = 3; final int connectionId2 = 4; final int connectionId3 = 5; final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MgcpLocalConnection connection1 = mock(MgcpLocalConnection.class); final MgcpRemoteConnection connection2 = mock(MgcpRemoteConnection.class); final MgcpRemoteConnection connection3 = mock(MgcpRemoteConnection.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final GenericMgcpEndpoint endpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); when(connectionProvider.provideLocal(callId1)).thenReturn(connection1); when(connectionProvider.provideRemote(callId2)).thenReturn(connection2, connection3); when(connection1.getIdentifier()).thenReturn(connectionId1); when(connection1.getCallIdentifier()).thenReturn(callId1); when(connection2.getIdentifier()).thenReturn(connectionId2); when(connection2.getCallIdentifier()).thenReturn(callId2); when(connection3.getIdentifier()).thenReturn(connectionId3); when(connection3.getCallIdentifier()).thenReturn(callId2); endpoint.createConnection(callId1, true); endpoint.createConnection(callId2, false); endpoint.createConnection(callId2, false); List<MgcpConnection> deleted = endpoint.deleteConnections(); assertFalse(endpoint.hasConnections()); assertEquals(3, deleted.size()); assertTrue(deleted.contains(connection1)); assertTrue(deleted.contains(connection2)); assertTrue(deleted.contains(connection3)); }
@Test public void testDeleteConnectionsFromInactiveEndpoint() throws Exception { final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final GenericMgcpEndpoint endpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); List<MgcpConnection> deleted = endpoint.deleteConnections(); assertNotNull(deleted); assertTrue(deleted.isEmpty()); }