diff --git a/openvidu-server/src/test/java/io/openvidu/server/test/AutodiscoveryKmsUrlTest.java b/openvidu-server/src/test/java/io/openvidu/server/test/AutodiscoveryKmsUrlTest.java deleted file mode 100644 index a5b880a6..00000000 --- a/openvidu-server/src/test/java/io/openvidu/server/test/AutodiscoveryKmsUrlTest.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.openvidu.server.test; - -import static org.junit.Assert.fail; - -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.TimeUnit; - -import org.junit.Test; -import org.kurento.client.internal.KmsProvider; -import org.kurento.client.internal.NotEnoughResourcesException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ConfigurableApplicationContext; - -import com.google.common.base.StandardSystemProperty; - -import io.openvidu.server.OpenViduServer; -import io.openvidu.server.core.Participant; -import io.openvidu.server.core.SessionManager; -import io.openvidu.server.core.Token; -import io.openvidu.server.kurento.core.KurentoSessionManager; - -/** - * Integration server test, checks the autodiscovery of KMS URLs. - * - * @author Micael Gallego (micael.gallego@gmail.com) - * @since 6.2.1 - */ -public class AutodiscoveryKmsUrlTest { - - private static final Logger log = LoggerFactory.getLogger(AutodiscoveryKmsUrlTest.class); - - public static BlockingQueue queue = new ArrayBlockingQueue<>(10); - - public static class TestKmsUrlProvider implements KmsProvider { - - @Override - public String reserveKms(String id, int loadPoints) throws NotEnoughResourcesException { - if (loadPoints == 50) { - log.debug("getKmsUrl called with 50"); - queue.add(true); - } else { - log.error("getKmsUrl called with {} instead of 50", loadPoints); - queue.add(false); - } - - return "ws://fakeUrl"; - } - - @Override - public String reserveKms(String id) throws NotEnoughResourcesException { - - log.error("getKmsUrl called without load points"); - queue.add(false); - - return "ws://fakeUrl"; - } - - @Override - public void releaseKms(String id) throws NotEnoughResourcesException { - // TODO Auto-generated method stub - - } - } - - @Test - public void test() throws IOException { - - /*Path backup = null; - - Path configFile = Paths.get(StandardSystemProperty.USER_HOME.value(), ".kurento", - "config.properties"); - - System.setProperty("kms.uris", "[\"autodiscovery\"]"); - - try { - - if (Files.exists(configFile)) { - - backup = configFile.getParent().resolve("config.properties.old"); - - Files.move(configFile, backup); - log.debug("Backed-up old config.properties"); - } - - Files.createDirectories(configFile.getParent()); - - try (BufferedWriter writer = Files.newBufferedWriter(configFile, StandardCharsets.UTF_8)) { - writer.write("kms.url.provider: " + TestKmsUrlProvider.class.getName() + "\r\n"); - } - - String contents = new String(Files.readAllBytes(configFile)); - log.debug("Config file contents:\n{}", contents); - - ConfigurableApplicationContext app = OpenViduServer - .start(new String[] { "--server.port=7777" }); - - final SessionManager roomManager = app.getBean(KurentoSessionManager.class); - - new Thread(new Runnable() { - @Override - public void run() { - Participant p = new Participant("privateId", "publicId", new Token("token"), "clientMetadata"); - roomManager - .joinRoom(p, "sessionId", 1); - } - }).start(); - - try { - Boolean result = queue.poll(10, TimeUnit.SECONDS); - - if (result == null) { - fail("Event in KmsUrlProvider not called"); - } else { - if (!result) { - fail("Test failed"); - } - } - - } catch (InterruptedException e) { - fail("KmsUrlProvider was not called"); - } - - } finally { - - Files.delete(configFile); - - if (backup != null) { - Files.move(backup, configFile); - } - }*/ - } - -} diff --git a/openvidu-server/src/test/java/io/openvidu/server/test/RoomProtocolTest.java b/openvidu-server/src/test/java/io/openvidu/server/test/RoomProtocolTest.java deleted file mode 100644 index 788b43bc..00000000 --- a/openvidu-server/src/test/java/io/openvidu/server/test/RoomProtocolTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.openvidu.server.test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doAnswer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorCompletionService; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.kurento.jsonrpc.Transaction; -import org.kurento.jsonrpc.client.JsonRpcClientLocal; -import org.kurento.jsonrpc.message.Request; -import org.mockito.Matchers; -import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.gson.JsonObject; - -import io.openvidu.client.OpenViduClient; -import io.openvidu.client.ServerJsonRpcHandler; -import io.openvidu.client.internal.Notification; -import io.openvidu.client.internal.ParticipantJoinedInfo; -import io.openvidu.client.internal.ProtocolElements; -import io.openvidu.client.internal.Notification.Method; -import io.openvidu.server.core.Participant; -import io.openvidu.server.core.Token; -import io.openvidu.server.kurento.core.KurentoSessionEventsHandler; -import io.openvidu.server.rpc.RpcConnection; -import io.openvidu.server.rpc.RpcHandler; -import io.openvidu.server.rpc.RpcNotificationService; - -/** - * Integration tests for the room server protocol. - * - * @author Radu Tom Vlad (rvlad@naevatec.com) - * @since 6.3.1 - */ -@RunWith(MockitoJUnitRunner.class) -public class RoomProtocolTest { - - private Integer transactionId = 0; - - private final Logger log = LoggerFactory.getLogger(RoomProtocolTest.class); - - private RpcNotificationService notificationService; - - @Mock - private RpcHandler userControl; - - private KurentoSessionEventsHandler sessionHandler; - - private JsonRpcClientLocal localClient0; - private OpenViduClient client0; - private ServerJsonRpcHandler serverHandler0; - - private JsonRpcClientLocal localClient1; - private OpenViduClient client1; - private ServerJsonRpcHandler serverHandler1; - - @Before - public void init() { - /*notificationService = new RpcNotificationService(); - sessionHandler = new KurentoSessionHandler();*/ - } - - @Test - public void joinRoom() throws IOException, InterruptedException, ExecutionException { - /*final Map> expectedEmptyPeersList = new HashMap>(); - - final Map> expectedPeersList = new HashMap>(); - List user0Streams = new ArrayList(); - user0Streams.add("user0_CAMERA"); - expectedPeersList.put("user0", user0Streams); - - final Set existingParticipants = new HashSet(); - - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Request argsRequest = invocation.getArgumentAt(1, Request.class); - Request request = new Request(argsRequest.getSessionId(), - argsRequest.getId(), argsRequest.getMethod(), (JsonObject) argsRequest.getParams()); - - String roomName = RpcHandler.getStringParam(request, - ProtocolElements.JOINROOM_ROOM_PARAM); - String userName = RpcHandler.getStringParam(request, - ProtocolElements.JOINROOM_USER_PARAM); - - log.debug("joinRoom -> {} to {}", userName, roomName); - - sessionHandler.onParticipantJoined(null, transactionId++, existingParticipants, null); - - if (userName.equalsIgnoreCase("user0")) { - existingParticipants.add(new Participant("privateId", "user0", new Token("token"), "clientMetadata")); - } - - return null; - } - - }).when(userControl).joinRoom(any(RpcConnection.class), Matchers.> any()); - - // controls the join order - final CountDownLatch joinCdl = new CountDownLatch(1); - - ExecutorService threadPool = Executors.newCachedThreadPool(); - ExecutorCompletionService exec = new ExecutorCompletionService<>(threadPool); - - exec.submit(new Callable() { - @Override - public Void call() throws Exception { - String thname = Thread.currentThread().getName(); - Thread.currentThread().setName("user0"); - - localClient0 = new JsonRpcClientLocal(userControl); - localClient0.setSessionId("session0"); - serverHandler0 = new ServerJsonRpcHandler(); - client0 = new OpenViduClient(localClient0, serverHandler0); - try { - Map> emptyPeersList = client0.joinRoom("room", "user0", null); - assertThat(emptyPeersList.entrySet(), equalTo(expectedEmptyPeersList.entrySet())); - } catch (IOException e) { - log.error("Unable to join room", e); - fail("Unable to join room: " + e.getMessage()); - } finally { - joinCdl.countDown(); - } - Thread.currentThread().setName(thname); - return null; - } - }); - - exec.submit(new Callable() { - @Override - public Void call() throws Exception { - String thname = Thread.currentThread().getName(); - Thread.currentThread().setName("user1"); - - localClient1 = new JsonRpcClientLocal(userControl); - localClient1.setSessionId("session1"); - serverHandler1 = new ServerJsonRpcHandler(); - client1 = new OpenViduClient(localClient1, serverHandler1); - joinCdl.await(); - try { - Map> peersList = client1.joinRoom("room", "user1", null); - assertThat(peersList, is(expectedPeersList)); - } catch (IOException e) { - log.error("Unable to join room", e); - fail("Unable to join room: " + e.getMessage()); - } - Thread.currentThread().setName(thname); - return null; - } - }); - - exec.take().get(); - exec.take().get(); - - threadPool.shutdown(); - - Notification notif = serverHandler0.getNotification(); - assertThat(notif.getMethod(), is(Method.PARTICIPANTJOINED_METHOD)); - ParticipantJoinedInfo joinedNotif = (ParticipantJoinedInfo) notif; - assertThat(joinedNotif.getId(), is("user1"));*/ - } -} diff --git a/openvidu-server/src/test/java/io/openvidu/server/test/core/RoomManagerTest.java b/openvidu-server/src/test/java/io/openvidu/server/test/core/RoomManagerTest.java deleted file mode 100644 index 63c2fdea..00000000 --- a/openvidu-server/src/test/java/io/openvidu/server/test/core/RoomManagerTest.java +++ /dev/null @@ -1,1336 +0,0 @@ -/* - * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.openvidu.server.test.core; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.kurento.client.Continuation; -import org.kurento.client.ErrorEvent; -import org.kurento.client.EventListener; -import org.kurento.client.FaceOverlayFilter; -import org.kurento.client.HubPort; -import org.kurento.client.KurentoClient; -import org.kurento.client.MediaPipeline; -import org.kurento.client.MediaType; -import org.kurento.client.Mixer; -import org.kurento.client.OnIceCandidateEvent; -import org.kurento.client.PassThrough; -import org.kurento.client.RtpEndpoint; -import org.kurento.client.ServerManager; -import org.kurento.client.WebRtcEndpoint; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import io.openvidu.server.core.Participant; -import io.openvidu.server.core.SessionManager; -import io.openvidu.server.kurento.core.KurentoSessionEventsHandler; -import io.openvidu.server.kurento.kms.KmsManager; - -/** - * Tests for {@link RoomManager} when using mocked {@link KurentoClient} resources. - * - * @author Radu Tom Vlad - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(fullyQualifiedNames = "org.kurento.*") -@PowerMockIgnore( {"javax.management.*"}) -public class RoomManagerTest { - - private static final String SDP_WEB_OFFER = "peer sdp web offer"; - private static final String SDP_WEB_ANSWER = "endpoint sdp web answer"; - private static final String SDP_WEB_SERVER_OFFER = "server sdp web offer"; - private static final String SDP_WEB_PEER_ANSWER = "peer sdp web answer"; - private static final String SDP_WEB_SERVER_UPDATED_OFFER = "server sdp updated web offer"; - - private static final String SDP_RTP_OFFER = "peer sdp rtp offer"; - private static final String SDP_RTP_ANSWER = "endpoint sdp rtp answer"; - // private static final String SDP_WEB_SERVER_OFFER = "server sdp offer"; - // private static final String SDP_WEB_PEER_ANSWER = "peer sdp answer"; - // private static final String SDP_WEB_SERVER_UPDATED_OFFER = - // "server sdp updated offer"; - - private static final int USERS = 10; - private static final int ROOMS = 3; - - private SessionManager manager; - - @Mock - private KmsManager kcProvider; - @Mock - private KurentoSessionEventsHandler roomHandler; - - @Mock - private KurentoClient kurentoClient; - @Mock - private ServerManager serverManager; - @Captor - private ArgumentCaptor> kurentoClientCaptor; - - @Mock - private MediaPipeline pipeline; - @Mock - private WebRtcEndpoint endpoint; - @Mock - private PassThrough passThru; - @Mock - private RtpEndpoint rtpEndpoint; - - @Mock - private WebRtcEndpoint.Builder webRtcBuilder; - @Captor - private ArgumentCaptor> webRtcCaptor; - @Captor - private ArgumentCaptor> webRtcConnectCaptor; - @Captor - private ArgumentCaptor> webRtcDisconnectCaptor; - - @Mock - private PassThrough.Builder passThruBuilder; - @Captor - private ArgumentCaptor> passThruConnectCaptor; - @Captor - private ArgumentCaptor> passThruDisconnectCaptor; - - @Mock - private RtpEndpoint.Builder rtpBuilder; - @Captor - private ArgumentCaptor> rtpCaptor; - @Captor - private ArgumentCaptor> rtpConnectCaptor; - @Captor - private ArgumentCaptor> rtpDisconnectCaptor; - - @Mock - private Mixer mixer; - @Mock - private Mixer.Builder mixerBuilder; - - @Mock - private HubPort hubPort; - @Mock - private HubPort.Builder hubPortBuilder; - @Captor - private ArgumentCaptor> hubPortConnectCaptor; - @Captor - private ArgumentCaptor hubPortConnectTypeCaptor; - - @Mock - private FaceOverlayFilter.Builder faceFilterBuilder; - @Mock - private FaceOverlayFilter faceFilter; - @Captor - private ArgumentCaptor> faceFilterConnectCaptor; - - @Captor - private ArgumentCaptor> iceEventCaptor; - @Captor - private ArgumentCaptor> mediaErrorEventCaptor; - @Captor - private ArgumentCaptor> pipelineErrorEventCaptor; - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - private String userx = "userx"; - private String pidx = "pidx"; - private String roomx = "roomx"; - - // usernames will be used as participantIds - private String[] users = new String[USERS]; - private String[] rooms = new String[ROOMS]; - - private Map usersParticipantIds = new HashMap(); - private Map usersParticipants = new HashMap(); - - @Before - public void setup() { - - /* ConfigurableApplicationContext app = OpenViduServer - .start(new String[] { "--server.port=7777" }); - - manager = app.getBean(KurentoSessionManager.class); - - when(kcProvider.getKurentoClient(any(KurentoClientSessionInfo.class))) - .thenReturn(kurentoClient); - when(kurentoClient.getServerManager()).thenReturn(serverManager); - - when(serverManager.getName()).thenReturn("mocked-kurento-client"); - - // call onSuccess when creating the pipeline to use the mocked instance - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - kurentoClientCaptor.getValue().onSuccess(pipeline); - return null; - } - }).when(kurentoClient).createMediaPipeline(kurentoClientCaptor.capture()); - - // call onSuccess when building the endpoint to use the mocked instance - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - webRtcCaptor.getValue().onSuccess(endpoint); - return null; - } - }).when(webRtcBuilder).buildAsync(webRtcCaptor.capture()); - - // call onSuccess when building the RTP endpoint to use the mocked - // instance - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - rtpCaptor.getValue().onSuccess(rtpEndpoint); - return null; - } - }).when(rtpBuilder).buildAsync(rtpCaptor.capture()); - - // still using the sync version - when(passThruBuilder.build()).thenReturn(passThru); - - try { // mock the constructor for the endpoint builder - whenNew(WebRtcEndpoint.Builder.class).withArguments(pipeline).thenReturn(webRtcBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - - try { // mock the constructor for the RTP endpoint builder - whenNew(RtpEndpoint.Builder.class).withArguments(pipeline).thenReturn(rtpBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - - try { // mock the constructor for the passThru builder - whenNew(PassThrough.Builder.class).withArguments(pipeline).thenReturn(passThruBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - - // mock the SDP answer when processing the offer on the endpoint - when(endpoint.processOffer(SDP_WEB_OFFER)).thenReturn(SDP_WEB_ANSWER); - - // mock the SDP offer when generating it from the server endpoint - when(endpoint.generateOffer()).thenReturn(SDP_WEB_SERVER_OFFER); - - // mock the SDP offer when generating it from the server endpoint - when(endpoint.processAnswer(SDP_WEB_PEER_ANSWER)).thenReturn(SDP_WEB_SERVER_UPDATED_OFFER); - - // mock the SDP answer when processing the offer on the RTP endpoint - when(rtpEndpoint.processOffer(SDP_RTP_OFFER)).thenReturn(SDP_RTP_ANSWER); - - // call onSuccess when connecting the WebRtc endpoint to any media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - webRtcConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(endpoint).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - - // call onSuccess when disconnecting the WebRtc endpoint from any media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - webRtcDisconnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(endpoint).disconnect(any(MediaElement.class), webRtcDisconnectCaptor.capture()); - - // call onSuccess when connecting the RTP endpoint to any media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - rtpConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(rtpEndpoint).connect(any(MediaElement.class), rtpConnectCaptor.capture()); - - // call onSuccess when disconnecting the RTP endpoint from any media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - rtpDisconnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(rtpEndpoint).disconnect(any(MediaElement.class), rtpDisconnectCaptor.capture()); - - // call onSuccess when connecting the PassThrough element to any media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - passThruConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(passThru).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - // call onSuccess when disconnecting the PassThrough element from any - // media - // element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - passThruDisconnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(passThru).disconnect(any(MediaElement.class), passThruDisconnectCaptor.capture()); - - try { // mock the constructor for the mixer builder - whenNew(Mixer.Builder.class).withArguments(pipeline).thenReturn(mixerBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - // using the sync version to build the mixer - when(mixerBuilder.build()).thenReturn(mixer); - - try { // mock the constructor for the hubPort builder - whenNew(HubPort.Builder.class).withArguments(mixer).thenReturn(hubPortBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - // using the sync version to build the hubPort - when(hubPortBuilder.build()).thenReturn(hubPort); - - // call onSuccess when connecting the hubPort to any media element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - hubPortConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(hubPort).connect(any(MediaElement.class), hubPortConnectCaptor.capture()); - - // call onSuccess when connecting the hubPort to any media element and - // with a given media type - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - hubPortConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(hubPort).connect(any(MediaElement.class), hubPortConnectTypeCaptor.capture(), - hubPortConnectCaptor.capture()); - - try { // mock the constructor for the face filter builder - whenNew(FaceOverlayFilter.Builder.class).withArguments(pipeline) - .thenReturn(faceFilterBuilder); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - // using the sync version to build the face filter - when(faceFilterBuilder.build()).thenReturn(faceFilter); - - // call onSuccess when connecting the face filter to any media element - doAnswer(new Answer>() { - @Override - public Continuation answer(InvocationOnMock invocation) throws Throwable { - faceFilterConnectCaptor.getValue().onSuccess(null); - return null; - } - }).when(faceFilter).connect(any(MediaElement.class), faceFilterConnectCaptor.capture()); - - when(pipeline.getId()).thenReturn("mocked-pipeline"); - when(endpoint.getId()).thenReturn("mocked-webrtc-endpoint"); - when(rtpEndpoint.getId()).thenReturn("mocked-rtp-endpoint"); - when(passThru.getId()).thenReturn("mocked-pass-through"); - when(hubPort.getId()).thenReturn("mocked-hub-port"); - when(faceFilter.getId()).thenReturn("mocked-faceoverlay-filter"); - - for (int i = 0; i < USERS; i++) { - users[i] = "user" + i; - usersParticipantIds.put(users[i], "pid" + i); - usersParticipants.put(users[i], new Participant(users[i], users[i], new Token("token"), "clientMetadata")); - } - for (int i = 0; i < ROOMS; i++) { - rooms[i] = "room" + i; - }*/ - } - - @After - public void tearDown() { - /* manager.close(); */ - } - - @Test - public void joinNewRoom() { - /*assertThat(manager.getRooms(), not(hasItem(roomx))); - - assertTrue(userJoinRoom(roomx, userx, pidx, true).isEmpty()); - - assertThat(manager.getRooms(), hasItem(roomx)); - assertThat(manager.getParticipants(roomx), hasItem(new UserParticipant(pidx, userx)));*/ - } - - /*@Test - public void rtpJoinNewRoom() { - assertThat(manager.getRooms(), not(hasItem(roomx))); - - assertTrue(userJoinRoom(roomx, userx, pidx, true).isEmpty()); - - assertThat(manager.getRooms(), hasItem(roomx)); - assertThat(manager.getParticipants(roomx), hasItem(new UserParticipant(pidx, userx))); - } - - @Test - public void joinRoomFail() { - assertThat(manager.getSessions(), not(hasItem(roomx))); - - //exception.expect(OpenViduException.class); - //exception.expectMessage(containsString("must be created before")); - userJoinRoom(roomx, userx, pidx, false); - - assertThat(manager.getSessions(), (hasItem(roomx))); - } - - @Test - public void joinManyUsersOneRoom() { - int count = 0; - for (Entry userPid : usersParticipantIds.entrySet()) { - String user = userPid.getKey(); - String pid = userPid.getValue(); - - if (count == 0) { - assertThat(manager.getRooms(), not(hasItem(roomx))); - } else { - assertThat(manager.getParticipants(roomx), not(hasItem(usersParticipants.get(user)))); - } - - Set peers = userJoinRoom(roomx, user, pid, count == 0); - - if (count == 0) { - assertTrue(peers.isEmpty()); - assertThat(manager.getRooms(), hasItem(roomx)); - } else { - assertTrue(!peers.isEmpty()); - } - - assertThat(manager.getParticipants(roomx), hasItem(usersParticipants.get(user))); - - count++; - } - } - - @Test - public void joinManyWebUsersAndOneRTP() { - joinManyUsersOneRoom(); - - assertFalse(userJoinRoom(roomx, userx, pidx, false, false).isEmpty()); - - assertThat(manager.getRooms(), hasItem(roomx)); - assertThat(manager.getParticipants(roomx), hasItem(new UserParticipant(pidx, userx))); - } - - @Test - public void joinManyUsersManyRooms() { - final Map usersRooms = new HashMap(); - final Map> roomsUsers = new HashMap>(); - for (int i = 0; i < users.length; i++) { - String room = rooms[i % rooms.length]; - usersRooms.put(users[i], room); - if (!roomsUsers.containsKey(room)) { - roomsUsers.put(room, new ArrayList()); - } - roomsUsers.get(room).add(users[i]); - } - for (final String room : roomsUsers.keySet()) { - manager.createRoom(new KurentoClientSessionInfo() { - @Override - public String getRoomName() { - return room; - } - }); - } - for (Entry userRoom : usersRooms.entrySet()) { - String user = userRoom.getKey(); - final String room = userRoom.getValue(); - Set peers = manager.joinRoom(user, room, false, true, - new KurentoClientSessionInfo() { - @Override - public String getRoomName() { - return room; - } - }, usersParticipantIds.get(user)).existingParticipants; - if (peers.isEmpty()) { - assertEquals("Expected one peer in room " + room + ": " + user, 1, - manager.getParticipants(room).size()); - } - } - // verifies create media pipeline was called once for each new room - verify(kurentoClient, times(roomsUsers.size())).createMediaPipeline( - kurentoClientCaptor.capture()); - } - - @Test - public void leaveRoom() { - joinManyUsersOneRoom(); - assertTrue(!userJoinRoom(roomx, userx, pidx, false).isEmpty()); - UserParticipant userxParticipant = new UserParticipant(pidx, userx); - assertThat(manager.getParticipants(roomx), hasItem(userxParticipant)); - Set remainingUsers = manager.leaveRoom(pidx); - assertEquals(new HashSet(usersParticipants.values()), remainingUsers); - assertEquals(manager.getParticipants(roomx), remainingUsers); - assertThat(manager.getParticipants(roomx), not(hasItem(userxParticipant))); - } - - @Test - public void rtpLeaveRoom() { - joinManyWebUsersAndOneRTP(); - UserParticipant userxParticipant = new UserParticipant(pidx, userx); - assertThat(manager.getParticipants(roomx), hasItem(userxParticipant)); - Set remainingUsers = manager.leaveRoom(pidx); - assertEquals(new HashSet(usersParticipants.values()), remainingUsers); - assertEquals(manager.getParticipants(roomx), remainingUsers); - assertThat(manager.getParticipants(roomx), not(hasItem(userxParticipant))); - } - - @Test - public void publisherLifecycle() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - manager.unpublishMedia(participantId0); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void rtpPublisherLifecycle() { - joinManyWebUsersAndOneRTP(); - - assertEquals("SDP RTP answer doesn't match", SDP_RTP_ANSWER, - manager.publishMedia(pidx, true, SDP_RTP_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - for (String pid : usersParticipantIds.values()) { - assertEquals("SDP WEB answer (for the web peer) doesn't match", SDP_WEB_ANSWER, - manager.subscribe(userx, SDP_WEB_OFFER, pid)); - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length)); - - manager.unpublishMedia(pidx); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void invertedPublisherLifecycle() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP server offer doesn't match", SDP_WEB_SERVER_OFFER, - manager.generatePublishOffer(participantId0)); - - assertThat(manager.getPublishers(roomx).size(), is(0)); - - assertEquals("SDP updated offer doesn't match", SDP_WEB_SERVER_UPDATED_OFFER, - manager.publishMedia(participantId0, false, SDP_WEB_PEER_ANSWER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - manager.unpublishMedia(participantId0); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void publishAndLeave() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected without loopback, publisher's internal connection - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // no external connection until someone subscribes - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // connected without loopback, - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // using same endpoint, subscribers connections - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - }*/ - - /** - * Tests publishing (w/o loopback) when the SDP offer is generated on the server-side. - * - * @throws AdminException - */ - /*@Test - public void invertedPublishAndLeave() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP server offer doesn't match", SDP_WEB_SERVER_OFFER, - manager.generatePublishOffer(participantId0)); - - assertThat(manager.getPublishers(roomx).size(), is(0)); - - assertEquals("SDP updated offer doesn't match", SDP_WEB_SERVER_UPDATED_OFFER, - manager.publishMedia(participantId0, false, SDP_WEB_PEER_ANSWER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected without loopback, no external connection until someone - // subscribes - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // connected without loopback, publisher's internal connection - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // using same endpoint, subscribers connections - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void publishWithLoopbackError() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - doThrow( - new OpenViduException(Code.MEDIA_WEBRTC_ENDPOINT_ERROR_CODE, "Loopback connection error test")) - .when(passThru).connect(any(WebRtcEndpoint.class), Matchers.> any()); - - exception.expect(OpenViduException.class); - exception.expectMessage(containsString("Loopback connection error test")); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, true)); - - assertThat(manager.getPublishers(roomx).size(), is(0)); - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void publishWithLoopback() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, true)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected with loopback, so the internal connection is performed - // right away - verify(endpoint).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - verify(passThru).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // using same endpoint, subscribers connections + the internal one - verify(passThru, times(users.length)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - }*/ - - /** - * Tests publishing (w/ loopback) when the SDP offer is generated on the server-side. - * - * @throws AdminException - */ - /*@Test - public void invertedPublishWithLoopback() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP server offer doesn't match", SDP_WEB_SERVER_OFFER, - manager.generatePublishOffer(participantId0)); - - assertThat(manager.getPublishers(roomx).size(), is(0)); - - assertEquals("SDP updated offer doesn't match", SDP_WEB_SERVER_UPDATED_OFFER, - manager.publishMedia(participantId0, false, SDP_WEB_PEER_ANSWER, true)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected with loopback, so the internal connection is performed - // right away - verify(endpoint).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - verify(passThru).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // using same endpoint, subscribers connections + the internal one - verify(passThru, times(users.length)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void publishWithAlternativeLoopbackSrc() { - joinManyUsersOneRoom(); - - Mixer m = new Mixer.Builder(pipeline).build(); - assertThat("Mixer returned by the builder is not the same as the mocked one", m, is(mixer)); - - HubPort hb = new HubPort.Builder(m).build(); - assertThat("HubPort returned by the builder is not the same as the mocked one", hb, is(hubPort)); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, hb, null, true)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected with loopback, so the internal connection is performed - // right away - verify(endpoint).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // the loopback is not done using the passThru elem - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - // the hubPort is connected to the webrtc endpoint - verify(hubPort).connect(any(MediaElement.class), hubPortConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // using same endpoint, subscribers connections only - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void publishWithAlternativeLoopbackSrcAudioType() { - joinManyUsersOneRoom(); - - Mixer m = new Mixer.Builder(pipeline).build(); - assertThat("Mixer returned by the builder is not the same as the mocked one", m, is(mixer)); - - HubPort hb = new HubPort.Builder(m).build(); - assertThat("HubPort returned by the builder is not the same as the mocked one", hb, is(hubPort)); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, hb, MediaType.AUDIO, true)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected with loopback, so the internal connection is performed - // right away - verify(endpoint).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // the loopback is not done using the passThru elem - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - // the hubPort is connected to the webrtc endpoint - verify(hubPort).connect(any(MediaElement.class), hubPortConnectTypeCaptor.capture(), - hubPortConnectCaptor.capture()); - assertThat("Connection type is not audio", hubPortConnectTypeCaptor.getValue(), - is(MediaType.AUDIO)); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // using same endpoint, subscribers connections only - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void muteUnmutePublished() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected without loopback, publisher's internal connection - verify(endpoint).connect(passThru, webRtcConnectCaptor.getValue()); - // no external connection until someone subscribes - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // connected without loopback, - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // using same endpoint, subscribers connections - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - manager.mutePublishedMedia(MutedMediaType.ALL, participantId0); - - // disconnects once from the PassThrough - verify(endpoint).disconnect(passThru, webRtcDisconnectCaptor.getValue()); - - manager.unmutePublishedMedia(participantId0); - - // reconnects once to the PassThrough - verify(endpoint).connect(passThru, webRtcConnectCaptor.getValue()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void muteUnmuteSubscribed() { - joinManyUsersOneRoom(); - - String participantId0 = usersParticipantIds.get(users[0]); - String participantId1 = usersParticipantIds.get(users[1]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // connected without loopback, publisher's internal connection - verify(endpoint).connect(passThru, webRtcConnectCaptor.getValue()); - // no external connection until someone subscribes - verify(passThru, never()).connect(any(MediaElement.class), passThruConnectCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // connected without loopback, - verify(endpoint, times(1)).connect(any(MediaElement.class), webRtcConnectCaptor.capture()); - // using same endpoint, subscribers connections - verify(passThru, times(users.length - 1)).connect(any(MediaElement.class), - passThruConnectCaptor.capture()); - - manager.muteSubscribedMedia(users[0], MutedMediaType.ALL, participantId1); - - // disconnects the PassThrough once from the subscriber's endpoint - verify(passThru).disconnect(endpoint, passThruDisconnectCaptor.getValue()); - - manager.unmuteSubscribedMedia(users[0], participantId1); - - // reconnects once to the subscriber's endpoint - verify(passThru).connect(endpoint, passThruConnectCaptor.getValue()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void addMediaFilterInParallel() throws InterruptedException, ExecutionException { - joinManyUsersOneRoom(); - - final FaceOverlayFilter filter = new FaceOverlayFilter.Builder(pipeline).build(); - assertNotNull("FaceOverlayFiler is null", filter); - assertThat("Filter returned by the builder is not the same as the mocked one", filter, - is(faceFilter)); - - final String participantId0 = usersParticipantIds.get(users[0]); - - ExecutorService threadPool = Executors.newFixedThreadPool(1); - ExecutorCompletionService exec = new ExecutorCompletionService<>(threadPool); - exec.submit(new Callable() { - @Override - public Void call() throws Exception { - System.out.println("Starting execution of addMediaElement"); - manager.addMediaElement(participantId0, filter); - return null; - } - }); - - Thread.sleep(10); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - boolean firstSubscriber = true; - for (String pid : usersParticipantIds.values()) { - if (pid.equals(participantId0)) { - continue; - } - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - if (firstSubscriber) { - firstSubscriber = false; - try { - exec.take().get(); - System.out - .println("Execution of addMediaElement ended (just after first peer subscribed)"); - } finally { - threadPool.shutdownNow(); - } - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - verify(faceFilter, times(1)).connect(passThru, faceFilterConnectCaptor.getValue()); - verify(endpoint, times(1)).connect(faceFilter, webRtcConnectCaptor.getValue()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void addMediaFilterBeforePublishing() throws InterruptedException, ExecutionException { - joinManyUsersOneRoom(); - - final FaceOverlayFilter filter = new FaceOverlayFilter.Builder(pipeline).build(); - assertNotNull("FaceOverlayFiler is null", filter); - assertThat("Filter returned by the builder is not the same as the mocked one", filter, - is(faceFilter)); - - final String participantId0 = usersParticipantIds.get(users[0]); - - System.out.println("Starting execution of addMediaElement"); - manager.addMediaElement(participantId0, filter); - System.out.println("Execution of addMediaElement ended"); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - verify(faceFilter, times(1)).connect(passThru, faceFilterConnectCaptor.getValue()); - verify(endpoint, times(1)).connect(faceFilter, webRtcConnectCaptor.getValue()); - - Set remainingUsers = manager.leaveRoom(participantId0); - Set roomParticipants = manager.getParticipants(roomx); - assertEquals(roomParticipants, remainingUsers); - assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0])))); - assertThat(manager.getPublishers(roomx).size(), is(0)); - - // peers are automatically unsubscribed - assertThat(manager.getSubscribers(roomx).size(), is(0)); - } - - @Test - public void iceCandidate() { - joinManyUsersOneRoom(); - - final String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // verifies listener is added to publisher - verify(endpoint, times(1)).addOnIceCandidateListener(iceEventCaptor.capture()); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // verifies listener is added to each subscriber - verify(endpoint, times(usersParticipantIds.size())).addOnIceCandidateListener( - iceEventCaptor.capture()); - - final IceCandidate ic = new IceCandidate("1 candidate test", "audio", 1); - - doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - assertThat(args.length, is(4)); - - // first arg : roomName - assertThat(args[0], instanceOf(String.class)); - assertEquals(roomx, args[0]); - - // second arg : participantId - assertThat(args[1], instanceOf(String.class)); - String participantId = (String) args[1]; - assertThat(usersParticipantIds.values(), hasItem(participantId)); - // not the publisher cus the captored event - // is for one of the subscribers - assertThat(participantId, is(not(participantId0))); - - // third arg : endpointName == publisher's userName - assertThat(args[2], instanceOf(String.class)); - String epn = (String) args[2]; - assertEquals(users[0], epn); - - // fourth arg : iceCandidate - assertThat(args[3], instanceOf(IceCandidate.class)); - IceCandidate icParam = (IceCandidate) args[3]; - assertEquals(ic, icParam); - - return null; - } - }).when(roomHandler).onIceCandidate(anyString(), anyString(), anyString(), - Matchers.any(IceCandidate.class)); - - // triggers the last captured listener - iceEventCaptor.getValue().onEvent( - new OnIceCandidateEvent(endpoint, "12345", null, "candidate", ic)); - - // verifies the handler's method was called once (we only triggered the - // event once) - verify(roomHandler, times(1)).onIceCandidate(anyString(), anyString(), anyString(), - Matchers.any(IceCandidate.class)); - } - - @Test - public void mediaError() { - joinManyUsersOneRoom(); - - final String participantId0 = usersParticipantIds.get(users[0]); - - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false)); - - assertThat(manager.getPublishers(roomx).size(), is(1)); - - // verifies error listener is added to publisher - verify(endpoint, times(1)).addErrorListener(mediaErrorEventCaptor.capture()); - - final String expectedErrorMessage = "TEST_ERR: Fake media error(errCode=101)"; - - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - assertThat(args.length, is(3)); - - // first arg : roomName - assertThat(args[0], instanceOf(String.class)); - assertEquals(roomx, args[0]); - - // second arg : participantId - assertThat(args[1], instanceOf(String.class)); - String participantId = (String) args[1]; - assertThat(usersParticipantIds.values(), hasItem(participantId)); - // error on the publisher's endpoint - assertThat(participantId, is(participantId0)); - - // third arg : error description - assertThat(args[2], instanceOf(String.class)); - assertEquals(expectedErrorMessage, args[2]); - - return null; - } - }).when(roomHandler).onMediaElementError(anyString(), anyString(), anyString()); - - // triggers the last captured listener - mediaErrorEventCaptor.getValue().onEvent( - new ErrorEvent(endpoint, "12345", null, "Fake media error", 101, "TEST_ERR")); - - for (String pid : usersParticipantIds.values()) { - if (!pid.equals(participantId0)) { - assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER, - manager.subscribe(users[0], SDP_WEB_OFFER, pid)); - } - } - assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1)); - - // verifies listener is added to each subscriber - verify(endpoint, times(usersParticipantIds.size())).addErrorListener( - mediaErrorEventCaptor.capture()); - - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - assertThat(args.length, is(3)); - - // first arg : roomName - assertThat(args[0], instanceOf(String.class)); - assertEquals(roomx, args[0]); - - // second arg : participantId - assertThat(args[1], instanceOf(String.class)); - String participantId = (String) args[1]; - assertThat(usersParticipantIds.values(), hasItem(participantId)); - // error on a subscriber's endpoint - assertThat(participantId, is(not(participantId0))); - - // third arg : error description - assertThat(args[2], instanceOf(String.class)); - assertEquals(expectedErrorMessage, args[2]); - - return null; - } - }).when(roomHandler).onMediaElementError(anyString(), anyString(), anyString()); - - // triggers the last captured listener (once again) - mediaErrorEventCaptor.getValue().onEvent( - new ErrorEvent(endpoint, "12345", null, "Fake media error", 101, "TEST_ERR")); - - // verifies the handler's method was called twice - verify(roomHandler, times(2)).onMediaElementError(anyString(), anyString(), anyString());; - - } - - @Test - public void pipelineError() { - joinManyUsersOneRoom(); - - // verifies pipeline error listener is added to room - verify(pipeline, times(1)).addErrorListener(pipelineErrorEventCaptor.capture()); - - final String expectedErrorMessage = "TEST_PP_ERR: Fake pipeline error(errCode=505)"; - - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - assertThat(args.length, is(3)); - - // first arg : roomName - assertThat(args[0], instanceOf(String.class)); - assertEquals(roomx, args[0]); - - // second arg : participantIds - assertThat(args[1], instanceOf(Set.class)); - Set pids = new HashSet(); - for (Object o : (Set) args[1]) { - assertThat(o, instanceOf(String.class)); - pids.add((String) o); - } - assertThat( - pids, - CoreMatchers.hasItems(usersParticipantIds.values().toArray( - new String[usersParticipantIds.size()]))); - - // third arg : error description - assertThat(args[2], instanceOf(String.class)); - assertEquals(expectedErrorMessage, args[2]); - - return null; - } - }).when(roomHandler).onPipelineError(anyString(), Matchers.> any(), anyString()); - - // triggers the last captured listener - pipelineErrorEventCaptor.getValue().onEvent( - new ErrorEvent(pipeline, "12345", null, "Fake pipeline error", 505, "TEST_PP_ERR")); - - // verifies the handler's method was called only once (one captor event) - verify(roomHandler, times(1)).onPipelineError(anyString(), Matchers.> any(), - anyString());; - } - - private Set userJoinRoom(final String room, String user, String pid, - boolean joinMustSucceed) { - return userJoinRoom(room, user, pid, joinMustSucceed, true); - } - - private Set userJoinRoom(final String room, String user, String pid, - boolean joinMustSucceed) { - KurentoClientSessionInfo kcsi = null; - - if (joinMustSucceed) { - kcsi = new KurentoClientSessionInfo() { - @Override - public String getRoomName() { - return room; - } - }; - } - - Participant p = new Participant(user, user, new Token(user), user); - - manager.joinRoom(p, room, 1); - - Set existingPeers = this.manager.getParticipants(room); - - // verifies create media pipeline was called once - verify(kurentoClient, times(0)).createMediaPipeline(kurentoClientCaptor.capture()); - - return existingPeers; - }*/ -}