Search in sources :

Example 1 with CreateChatThreadOptions

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

the class MainActivity method createChatThreadAsyncClient.

public void createChatThreadAsyncClient() {
    if (chatAsyncClient == null) {
        createChatAsyncClient();
    }
    // A list of participants to start the thread with.
    List<ChatParticipant> participants = new ArrayList<>();
    // The display name for the thread participant.
    String displayName = "First participant";
    participants.add(new ChatParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(firstUserId)).setDisplayName(displayName));
    // The topic for the thread.
    final String topic = "General";
    // Optional, set a repeat request ID.
    final String repeatabilityRequestID = "";
    // Options to pass to the create method.
    CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions().setTopic(topic).setParticipants(participants).setIdempotencyToken(repeatabilityRequestID);
    try {
        CreateChatThreadResult createChatThreadResult = chatAsyncClient.createChatThread(createChatThreadOptions).get();
        ChatThreadProperties chatThreadProperties = createChatThreadResult.getChatThreadProperties();
        threadId = chatThreadProperties.getId();
        logAndToast("Created thread with ID: " + threadId);
        chatThreadAsyncClient = chatAsyncClient.getChatThreadClient(threadId);
        Log.d(TAG, "Created ChatThreadAsyncClient");
    } catch (InterruptedException | ExecutionException e) {
        Log.e("ChatThreadAsyncClient creation failed", Objects.requireNonNull(e.getMessage()));
    }
}
Also used : CreateChatThreadResult(com.azure.android.communication.chat.models.CreateChatThreadResult) ArrayList(java.util.ArrayList) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) ChatThreadProperties(com.azure.android.communication.chat.models.ChatThreadProperties) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) ExecutionException(java.util.concurrent.ExecutionException) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier)

Example 2 with CreateChatThreadOptions

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

the class ChatAsyncClientTest method canCreateThread.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canCreateThread(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    final CreateChatThreadOptions threadRequest = createThreadOptions(firstThreadParticipant.getId(), secondThreadParticipant.getId());
    CompletableFuture<CreateChatThreadResult> completableFuture = this.client.createChatThread(threadRequest);
    assertNotNull(completableFuture);
    CreateChatThreadResult result = completableFuture.get();
    assertNotNull(result);
    assertNotNull(result.getChatThreadProperties());
    assertNotNull(result.getChatThreadProperties().getId());
}
Also used : CreateChatThreadResult(com.azure.android.communication.chat.models.CreateChatThreadResult) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with CreateChatThreadOptions

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

the class ChatAsyncClientTest method cannotCreateThreadWithResponseWithInvalidUser.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void cannotCreateThreadWithResponseWithInvalidUser(HttpClient httpClient) {
    setupTest(httpClient);
    final CreateChatThreadOptions threadRequest = createThreadOptions("8:acs:invalidUserId", secondThreadParticipant.getId());
    ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
        client.createChatThreadWithResponse(threadRequest, null).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) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) ExecutionException(java.util.concurrent.ExecutionException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with CreateChatThreadOptions

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

the class ChatAsyncClientTest method canDeleteChatThread.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canDeleteChatThread(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    final CreateChatThreadOptions threadRequest = createThreadOptions(firstThreadParticipant.getId(), secondThreadParticipant.getId());
    CompletableFuture<CreateChatThreadResult> completableFuture1 = this.client.createChatThread(threadRequest);
    assertNotNull(completableFuture1);
    CreateChatThreadResult result1 = completableFuture1.get();
    assertNotNull(result1);
    assertNotNull(result1.getChatThreadProperties());
    assertNotNull(result1.getChatThreadProperties().getId());
    CompletableFuture<Void> completableFuture2 = this.client.deleteChatThread(result1.getChatThreadProperties().getId());
    assertNotNull(completableFuture2);
    Void result2 = completableFuture2.get();
    assertNull(result2);
}
Also used : CreateChatThreadResult(com.azure.android.communication.chat.models.CreateChatThreadResult) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with CreateChatThreadOptions

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

the class ChatAsyncClientTest method canListChatThreadsWithMaxPage.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void canListChatThreadsWithMaxPage(HttpClient httpClient) throws InterruptedException, ExecutionException {
    setupTest(httpClient);
    final CreateChatThreadOptions threadRequest = createThreadOptions(firstThreadParticipant.getId(), secondThreadParticipant.getId());
    CompletableFuture<CreateChatThreadResult> completableFuture1 = this.client.createChatThread(threadRequest);
    assertNotNull(completableFuture1);
    CreateChatThreadResult result1 = completableFuture1.get();
    assertNotNull(result1);
    assertNotNull(result1.getChatThreadProperties());
    assertNotNull(result1.getChatThreadProperties().getId());
    CompletableFuture<CreateChatThreadResult> completableFuture2 = this.client.createChatThread(threadRequest);
    assertNotNull(completableFuture2);
    CreateChatThreadResult result2 = completableFuture1.get();
    assertNotNull(result2);
    assertNotNull(result2.getChatThreadProperties());
    assertNotNull(result2.getChatThreadProperties().getId());
    PagedAsyncStream<ChatThreadItem> chatAsyncStream = this.client.listChatThreads(new ListChatThreadsOptions().setMaxPageSize(2), null);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean gotNullResponse = new AtomicBoolean();
    AtomicBoolean gotNullList = new AtomicBoolean();
    List<ChatThreadItem> returnedThreads = new ArrayList<ChatThreadItem>();
    chatAsyncStream.byPage().forEach(new AsyncStreamHandler<PagedResponse<ChatThreadItem>>() {

        @Override
        public void onNext(PagedResponse<ChatThreadItem> response) {
            if (!gotNullResponse.get()) {
                gotNullResponse.set(response == null);
            }
            if (response != null) {
                List<ChatThreadItem> threadsInPage = response.getValue();
                if (!gotNullList.get()) {
                    gotNullResponse.set(threadsInPage == null);
                }
                if (threadsInPage != null) {
                    for (ChatThreadItem thread : threadsInPage) {
                        returnedThreads.add(thread);
                    }
                }
            }
        }

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

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canListChatThreads");
    Assertions.assertFalse(gotNullResponse.get());
    Assertions.assertFalse(gotNullList.get());
// REVISIT: Unreliable assert
// assertTrue(returnedThreads.size() > 0);
}
Also used : CreateChatThreadResult(com.azure.android.communication.chat.models.CreateChatThreadResult) ArrayList(java.util.ArrayList) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) CountDownLatch(java.util.concurrent.CountDownLatch) ListChatThreadsOptions(com.azure.android.communication.chat.models.ListChatThreadsOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChatThreadItem(com.azure.android.communication.chat.models.ChatThreadItem) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CreateChatThreadOptions (com.azure.android.communication.chat.models.CreateChatThreadOptions)15 CreateChatThreadResult (com.azure.android.communication.chat.models.CreateChatThreadResult)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)12 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)6 Response (com.azure.android.core.rest.Response)5 ArrayList (java.util.ArrayList)4 ChatThreadProperties (com.azure.android.communication.chat.models.ChatThreadProperties)3 ExecutionException (java.util.concurrent.ExecutionException)3 ChatErrorResponseException (com.azure.android.communication.chat.models.ChatErrorResponseException)2 ChatParticipant (com.azure.android.communication.chat.models.ChatParticipant)2 ChatThreadItem (com.azure.android.communication.chat.models.ChatThreadItem)2 ListChatThreadsOptions (com.azure.android.communication.chat.models.ListChatThreadsOptions)2 CommunicationUserIdentifier (com.azure.android.communication.common.CommunicationUserIdentifier)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 UUID (java.util.UUID)1