Search in sources :

Example 6 with ChatParticipant

use of com.azure.android.communication.chat.models.ChatParticipant in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canAddSingleParticipantWithResponse.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canAddSingleParticipantWithResponse(HttpClient httpClient) throws InterruptedException, ExecutionException {
    setupTest(httpClient);
    Response<Void> addResponse = this.chatThreadClient.addParticipantWithResponse(new ChatParticipant().setCommunicationIdentifier(this.firstThreadParticipant), null).get();
    assertNotNull(addResponse);
    assertEquals(201, addResponse.getStatusCode());
    PagedAsyncStream<ChatParticipant> participantPagedAsyncStream = this.chatThreadClient.listParticipants(new ListParticipantsOptions(), null);
    CountDownLatch latch = new CountDownLatch(1);
    List<ChatParticipant> returnedParticipants = new ArrayList<ChatParticipant>();
    participantPagedAsyncStream.forEach(new AsyncStreamHandler<ChatParticipant>() {

        @Override
        public void onNext(ChatParticipant participant) {
            returnedParticipants.add(participant);
        }

        @Override
        public void onError(Throwable throwable) {
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canAddSingleParticipantWithResponse");
    assertTrue(returnedParticipants.size() > 0);
    for (ChatParticipant participant : returnedParticipants) {
        assertNotNull(participant);
        assertNotNull(participant.getDisplayName());
        assertNotNull(participant.getShareHistoryTime());
        assertNotNull(participant.getCommunicationIdentifier());
        assertTrue(participant.getCommunicationIdentifier() instanceof CommunicationUserIdentifier);
        assertNotNull(((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId());
    }
    if (TEST_MODE != TestMode.PLAYBACK) {
        assertTrue(super.checkParticipantsListContainsParticipantId(returnedParticipants, this.firstThreadParticipant.getId()));
    }
}
Also used : ArrayList(java.util.ArrayList) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) ListParticipantsOptions(com.azure.android.communication.chat.models.ListParticipantsOptions) CountDownLatch(java.util.concurrent.CountDownLatch) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with ChatParticipant

use of com.azure.android.communication.chat.models.ChatParticipant in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canAddListAndRemoveParticipants.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canAddListAndRemoveParticipants(HttpClient httpClient) throws InterruptedException, ExecutionException {
    setupTest(httpClient);
    Iterable<ChatParticipant> participants = addParticipants(this.firstThreadParticipant.getId(), this.secondThreadParticipant.getId());
    this.chatThreadClient.addParticipants(participants).get();
    PagedAsyncStream<ChatParticipant> participantPagedAsyncStream = this.chatThreadClient.listParticipants(new ListParticipantsOptions(), null);
    CountDownLatch latch = new CountDownLatch(1);
    List<ChatParticipant> returnedParticipants = new ArrayList<ChatParticipant>();
    participantPagedAsyncStream.forEach(new AsyncStreamHandler<ChatParticipant>() {

        @Override
        public void onNext(ChatParticipant participant) {
            returnedParticipants.add(participant);
        }

        @Override
        public void onError(Throwable throwable) {
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canAddListAndRemoveParticipants");
    assertTrue(returnedParticipants.size() > 0);
    for (ChatParticipant participant : returnedParticipants) {
        assertNotNull(participant);
        assertNotNull(participant.getDisplayName());
        assertNotNull(participant.getShareHistoryTime());
        assertNotNull(participant.getCommunicationIdentifier());
        assertTrue(participant.getCommunicationIdentifier() instanceof CommunicationUserIdentifier);
        assertNotNull(((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId());
    }
    if (TEST_MODE != TestMode.PLAYBACK) {
        for (ChatParticipant participant : participants) {
            assertTrue(super.checkParticipantsListContainsParticipantId(returnedParticipants, ((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId()));
        }
        for (ChatParticipant participant : participants) {
            final String id = ((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId();
            assertNotNull(id);
            if (!id.equals(this.firstThreadParticipant.getId())) {
                this.chatThreadClient.removeParticipant(participant.getCommunicationIdentifier()).get();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) ListParticipantsOptions(com.azure.android.communication.chat.models.ListParticipantsOptions) CountDownLatch(java.util.concurrent.CountDownLatch) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with ChatParticipant

use of com.azure.android.communication.chat.models.ChatParticipant in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method cannotAddParticipantsWithInvalidUser.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void cannotAddParticipantsWithInvalidUser(HttpClient httpClient) throws InterruptedException, ExecutionException {
    setupTest(httpClient);
    Iterable<ChatParticipant> participants = addParticipants("8:acs:invalidUserId", this.secondThreadParticipant.getId());
    ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
        this.chatThreadClient.addParticipants(participants).get();
    });
    Throwable cause = executionException.getCause();
    assertNotNull(cause);
    assertTrue(cause instanceof ChatErrorResponseException);
    ChatErrorResponseException exception = (ChatErrorResponseException) cause;
    assertNotNull(exception.getResponse());
    assertEquals(400, exception.getResponse().getStatusCode());
}
Also used : ChatErrorResponseException(com.azure.android.communication.chat.models.ChatErrorResponseException) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) ExecutionException(java.util.concurrent.ExecutionException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with ChatParticipant

use of com.azure.android.communication.chat.models.ChatParticipant in project azure-sdk-for-android by Azure.

the class MainActivity method listParticipants.

public void listParticipants(View view) {
    if (chatThreadAsyncClient == null) {
        createChatThreadAsyncClient();
    }
    if (chatThreadAsyncClient != null) {
        // The maximum number of participants to be returned per page, optional.
        int maxPageSize = 10;
        // Skips participants up to a specified position in response.
        int skip = 0;
        // Options to pass to the list method.
        ListParticipantsOptions listParticipantsOptions = new ListParticipantsOptions().setMaxPageSize(maxPageSize).setSkip(skip);
        try {
            PagedAsyncStream<ChatParticipant> participantPagedAsyncStream = chatThreadAsyncClient.listParticipants(new ListParticipantsOptions(), null);
            StringJoiner participantsStringJoiner = new StringJoiner("\nParticipant: ", "", "");
            CountDownLatch latch = new CountDownLatch(1);
            participantPagedAsyncStream.forEach(new AsyncStreamHandler<ChatParticipant>() {

                @Override
                public void onNext(ChatParticipant participant) {
                    participantsStringJoiner.add(participant.getDisplayName());
                }

                @Override
                public void onError(Throwable throwable) {
                    latch.countDown();
                }

                @Override
                public void onComplete() {
                    latch.countDown();
                }
            });
            awaitOnLatch(latch);
            logAndToast(participantsStringJoiner.toString());
        } catch (Exception e) {
            logAndToast("Listing participants failed: " + e.getMessage());
        }
    } else {
        logAndToast("ChatThreadAsyncClient creation failed");
    }
}
Also used : ListParticipantsOptions(com.azure.android.communication.chat.models.ListParticipantsOptions) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) CountDownLatch(java.util.concurrent.CountDownLatch) StringJoiner(java.util.StringJoiner) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with ChatParticipant

use of com.azure.android.communication.chat.models.ChatParticipant in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method canAddListAndRemoveParticipantsWithResponse.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canAddListAndRemoveParticipantsWithResponse(HttpClient httpClient) throws InterruptedException, ExecutionException {
    setupTest(httpClient);
    Iterable<ChatParticipant> participants = addParticipants(this.firstThreadParticipant.getId(), this.secondThreadParticipant.getId());
    Response<AddChatParticipantsResult> addResponse = this.chatThreadClient.addParticipantsWithResponse(participants, null).get();
    assertNotNull(addResponse);
    assertEquals(201, addResponse.getStatusCode());
    PagedAsyncStream<ChatParticipant> participantPagedAsyncStream = this.chatThreadClient.listParticipants(new ListParticipantsOptions(), null);
    CountDownLatch latch = new CountDownLatch(1);
    List<ChatParticipant> returnedParticipants = new ArrayList<ChatParticipant>();
    participantPagedAsyncStream.forEach(new AsyncStreamHandler<ChatParticipant>() {

        @Override
        public void onNext(ChatParticipant participant) {
            returnedParticipants.add(participant);
        }

        @Override
        public void onError(Throwable throwable) {
            latch.countDown();
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canAddListAndRemoveParticipantsWithResponse");
    assertTrue(returnedParticipants.size() > 0);
    for (ChatParticipant participant : returnedParticipants) {
        assertNotNull(participant);
        assertNotNull(participant.getDisplayName());
        assertNotNull(participant.getShareHistoryTime());
        assertNotNull(participant.getCommunicationIdentifier());
        assertTrue(participant.getCommunicationIdentifier() instanceof CommunicationUserIdentifier);
        assertNotNull(((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId());
    }
    if (TEST_MODE != TestMode.PLAYBACK) {
        for (ChatParticipant participant : participants) {
            assertTrue(super.checkParticipantsListContainsParticipantId(returnedParticipants, ((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId()));
        }
        for (ChatParticipant participant : participants) {
            final String id = ((CommunicationUserIdentifier) participant.getCommunicationIdentifier()).getId();
            assertNotNull(id);
            if (!id.equals(this.firstThreadParticipant.getId())) {
                this.chatThreadClient.removeParticipant(participant.getCommunicationIdentifier()).get();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) CountDownLatch(java.util.concurrent.CountDownLatch) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier) AddChatParticipantsResult(com.azure.android.communication.chat.models.AddChatParticipantsResult) ListParticipantsOptions(com.azure.android.communication.chat.models.ListParticipantsOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ChatParticipant (com.azure.android.communication.chat.models.ChatParticipant)11 ArrayList (java.util.ArrayList)7 CommunicationUserIdentifier (com.azure.android.communication.common.CommunicationUserIdentifier)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 ListParticipantsOptions (com.azure.android.communication.chat.models.ListParticipantsOptions)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutionException (java.util.concurrent.ExecutionException)4 ChatErrorResponseException (com.azure.android.communication.chat.models.ChatErrorResponseException)2 CreateChatThreadOptions (com.azure.android.communication.chat.models.CreateChatThreadOptions)2 AddChatParticipantsResult (com.azure.android.communication.chat.models.AddChatParticipantsResult)1 ChatThreadProperties (com.azure.android.communication.chat.models.ChatThreadProperties)1 CreateChatThreadResult (com.azure.android.communication.chat.models.CreateChatThreadResult)1 StringJoiner (java.util.StringJoiner)1