Skip to content

Commit

Permalink
Communication - Refactor Chat Tests to use StepVerifier (#16601)
Browse files Browse the repository at this point in the history
* Communication - Refactor Chat Tests

* Fix build

* Update tests to get rid of all blocks

* Clean up and fix build

Co-authored-by: Minnie Liu <[email protected]>
  • Loading branch information
minnieliu and Minnie Liu authored Oct 21, 2020
1 parent 3bce404 commit cf024aa
Show file tree
Hide file tree
Showing 2 changed files with 398 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

import reactor.test.StepVerifier;

import com.azure.communication.administration.CommunicationIdentityClient;
import com.azure.communication.administration.CommunicationUserToken;
import com.azure.communication.common.CommunicationUser;
import com.azure.communication.chat.implementation.ChatOptionsProvider;
import com.azure.communication.chat.models.*;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.util.logging.ClientLogger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
* Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to determine if tests are playback or
Expand Down Expand Up @@ -57,125 +61,196 @@ protected void afterTest() {

@Test
public void canCreateThread() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());

ChatThreadAsyncClient chatThreadClient = client.createChatThread(threadRequest).block();
assertNotNull(chatThreadClient);
assertNotNull(chatThreadClient.getChatThreadId());
// Act & Assert
StepVerifier.create(client.createChatThread(threadRequest))
.assertNext(chatThreadClient -> {
assertNotNull(chatThreadClient);
assertNotNull(chatThreadClient.getChatThreadId());
})
.verifyComplete();
}

@Test
public void canCreateThreadWithResponse() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());

ChatThreadAsyncClient chatThreadClient = client.createChatThreadWithResponse(threadRequest).block().getValue();
assertNotNull(chatThreadClient);
assertNotNull(chatThreadClient.getChatThreadId());
// Act & Assert
StepVerifier.create(client.createChatThreadWithResponse(threadRequest))
.assertNext(chatThreadClientResponse -> {
ChatThreadAsyncClient chatThreadClient = chatThreadClientResponse.getValue();
assertNotNull(chatThreadClient);
assertNotNull(chatThreadClient.getChatThreadId());
})
.verifyComplete();
}

@Test
public void canGetChatThreadClient() {
// Arrange
String threadId = "19:[email protected]";

// Act
ChatThreadAsyncClient chatThreadClient = client.getChatThreadClient(threadId);

// Assert
assertNotNull(chatThreadClient);
assertEquals(chatThreadClient.getChatThreadId(), threadId);
}

@Test
public void canGetExistingChatThread() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
ChatThreadAsyncClient chatThreadClient = client.createChatThread(threadRequest).block();

ChatThread chatThread = client.getChatThread(chatThreadClient.getChatThreadId()).block();
assertEquals(chatThreadClient.getChatThreadId(), chatThread.getId());
// Act & Assert
AtomicReference<ChatThreadAsyncClient> chatThreadClientRef = new AtomicReference<>();
StepVerifier.create(
client.createChatThread(threadRequest)
.flatMap(chatThreadClient -> {
chatThreadClientRef.set(chatThreadClient);
return client.getChatThread(chatThreadClient.getChatThreadId());
}))
.assertNext(chatThread -> {
assertEquals(chatThreadClientRef.get().getChatThreadId(), chatThread.getId());
})
.verifyComplete();
}

@Test
public void canGetExistingChatThreadWithResponse() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
ChatThreadAsyncClient chatThreadClient = client.createChatThread(threadRequest).block();

ChatThread chatThread = client.getChatThreadWithResponse(chatThreadClient.getChatThreadId()).block().getValue();
assertEquals(chatThreadClient.getChatThreadId(), chatThread.getId());
// Act & Assert
AtomicReference<ChatThreadAsyncClient> chatThreadClientRef = new AtomicReference<>();
StepVerifier.create(
client.createChatThread(threadRequest)
.flatMap(chatThreadClient -> {
chatThreadClientRef.set(chatThreadClient);
return client.getChatThreadWithResponse(chatThreadClient.getChatThreadId());
}))
.assertNext(chatThreadResponse -> {
ChatThread chatThread = chatThreadResponse.getValue();
assertEquals(chatThreadClientRef.get().getChatThreadId(), chatThread.getId());
})
.verifyComplete();
}

@Test
public void getNotFoundOnNonExistingChatThread() {
assertRestException(
() -> client.getChatThread("19:[email protected]").block(), 404);
// Act & Assert
StepVerifier.create(client.getChatThread("19:[email protected]"))
.expectErrorMatches(exception ->
((HttpResponseException) exception).getResponse().getStatusCode() == 404
)
.verify();
}

@Test
public void getNotFoundOnNonExistingChatThreadWithResponse() {
assertRestException(
() -> client.getChatThreadWithResponse("19:[email protected]").block(), 404);
// Act & Assert
StepVerifier.create(client.getChatThreadWithResponse("19:[email protected]"))
.expectErrorMatches(exception ->
((HttpResponseException) exception).getResponse().getStatusCode() == 404
)
.verify();
}

@Test
public void canDeleteChatThread() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
ChatThreadAsyncClient chatThreadClient = client.createChatThread(threadRequest).block();

client.deleteChatThread(chatThreadClient.getChatThreadId()).block();
// Act & Assert
AtomicReference<ChatThreadAsyncClient> chatThreadClientRef = new AtomicReference<>();
StepVerifier.create(
client.createChatThread(threadRequest)
.flatMap(chatThreadClient -> {
chatThreadClientRef.set(chatThreadClient);
return client.deleteChatThread(chatThreadClient.getChatThreadId());
})
)
.verifyComplete();
}

@Test
public void canDeleteChatThreadWithResponse() {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
ChatThreadAsyncClient chatThreadClient = client.createChatThread(threadRequest).block();

client.deleteChatThreadWithResponse(chatThreadClient.getChatThreadId()).block();

// Act & Assert
AtomicReference<ChatThreadAsyncClient> chatThreadClientRef = new AtomicReference<>();
StepVerifier.create(
client.createChatThread(threadRequest)
.flatMap(chatThreadClient -> {
chatThreadClientRef.set(chatThreadClient);
return client.deleteChatThreadWithResponse(chatThreadClient.getChatThreadId());
})
)
.assertNext(deleteResponse -> {
assertEquals(deleteResponse.getStatusCode(), 204);
})
.verifyComplete();
}

@Test
public void canListChatThreads() throws InterruptedException {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
client.createChatThread(threadRequest).block();
client.createChatThread(threadRequest).block();

Thread.sleep(500);

PagedIterable<ChatThreadInfo> threadsResponse = new PagedIterable<>(client.listChatThreads());

// process the iterableByPage
List<ChatThreadInfo> returnedThreads = new ArrayList<ChatThreadInfo>();
threadsResponse.iterableByPage().forEach(resp -> {
assertEquals(resp.getStatusCode(), 200);
resp.getItems().forEach(item -> returnedThreads.add(item));
});

assertTrue(returnedThreads.size() == 2);

StepVerifier.create(
client.createChatThread(threadRequest)
.concatWith(client.createChatThread(threadRequest)))
.assertNext(chatThreadClient -> {
// Act & Assert
PagedIterable<ChatThreadInfo> threadsResponse = new PagedIterable<>(client.listChatThreads());

// process the iterableByPage
List<ChatThreadInfo> returnedThreads = new ArrayList<ChatThreadInfo>();
threadsResponse.iterableByPage().forEach(resp -> {
assertEquals(resp.getStatusCode(), 200);
resp.getItems().forEach(item -> returnedThreads.add(item));
});

assertTrue(returnedThreads.size() == 2);
});
}

@Test
public void canListChatThreadsWithMaxPageSize() throws InterruptedException {
// Arrange
CreateChatThreadOptions threadRequest = ChatOptionsProvider.createThreadOptions(
firstThreadMember.getId(), secondThreadMember.getId());
client.createChatThread(threadRequest).block();
client.createChatThread(threadRequest).block();

Thread.sleep(500);


ListChatThreadsOptions options = new ListChatThreadsOptions();
options.setMaxPageSize(10);

PagedIterable<ChatThreadInfo> threadsResponse = new PagedIterable<>(client.listChatThreads(options));

// process the iterableByPage
List<ChatThreadInfo> returnedThreads = new ArrayList<ChatThreadInfo>();
threadsResponse.iterableByPage().forEach(resp -> {
assertEquals(resp.getStatusCode(), 200);
resp.getItems().forEach(item -> returnedThreads.add(item));
});

assertTrue(returnedThreads.size() == 2);
StepVerifier.create(
client.createChatThread(threadRequest)
.concatWith(client.createChatThread(threadRequest)))
.assertNext(chatThreadClient -> {
// Act & Assert
PagedIterable<ChatThreadInfo> threadsResponse = new PagedIterable<>(client.listChatThreads(options));

// process the iterableByPage
List<ChatThreadInfo> returnedThreads = new ArrayList<ChatThreadInfo>();
threadsResponse.iterableByPage().forEach(resp -> {
assertEquals(resp.getStatusCode(), 200);
resp.getItems().forEach(item -> returnedThreads.add(item));
});

assertTrue(returnedThreads.size() == 2);
});
}
}
Loading

0 comments on commit cf024aa

Please sign in to comment.