Skip to content

Commit

Permalink
Server Unit Tests
Browse files Browse the repository at this point in the history
Applied code style to test classes
  • Loading branch information
Zargess committed Dec 1, 2024
1 parent 14473db commit 15a05b2
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void whenNewParticipantIsAlreadyInConversation_ShouldResultInUserExistsResponse(

@Test
void whenNewParticipantUsernameIsNotAnExistingUser_ShouldResultInNoSuchUserResponse() {
mockConversationAndRegisterInData(SENDER_USERNAME
);
mockConversationAndRegisterInData(SENDER_USERNAME);

inputHandler.handleInput(addParticipantRequest);

Expand All @@ -100,8 +99,7 @@ void whenNewParticipantUsernameIsNotAnExistingUser_ShouldResultInNoSuchUserRespo

@Test
void whenAddingUserToConversation_ShouldResultInNewParticipantBeingInConversationParticipantList() {
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME
);
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -117,8 +115,7 @@ void whenAddingUserToConversation_ShouldResultInNewParticipantBeingInConversatio

@Test
void whenAddingUserToConversation_ShouldResultInConversationAddedToTheUsersConversationList() {
mockConversationAndRegisterInData(SENDER_USERNAME
);
mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -134,8 +131,7 @@ void whenAddingUserToConversation_ShouldResultInConversationAddedToTheUsersConve

@Test
void whenAddingOnlineUserToConversation_ShouldResultInSendingConversationToThatUser() {
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME
);
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME);
OutputAgent outputAgent = mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -152,8 +148,7 @@ void whenAddingOnlineUserToConversation_ShouldResultInSendingConversationToThatU

@Test
void whenAddingUserToConversation_ShouldResultInConversationBeingStored() {
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME
);
Conversation conversation = mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -170,8 +165,7 @@ void whenAddingUserToConversation_ShouldResultInConversationBeingStored() {

@Test
void whenAddingUserToConversation_ShouldResultInNewParticipantBeingStored() {
mockConversationAndRegisterInData(SENDER_USERNAME
);
mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -188,8 +182,7 @@ void whenAddingUserToConversation_ShouldResultInNewParticipantBeingStored() {

@Test
void whenAddingOfflineUserToConversation_ShouldResultInAllIsWellResponse() {
mockConversationAndRegisterInData(SENDER_USERNAME
);
mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand All @@ -206,8 +199,7 @@ void whenAddingOfflineUserToConversation_ShouldResultInAllIsWellResponse() {

@Test
void whenAddingOnlineUserToConversation_ShouldResultInAllIsWellResponse() {
mockConversationAndRegisterInData(SENDER_USERNAME
);
mockConversationAndRegisterInData(SENDER_USERNAME);
mockOnlineStatusOfUser(
data,
newParticipant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ void creatingConversationWithNonExistingUser_ShouldResultIn_NoSuchUserResponse()
)
.addResponse(argumentCaptor.capture());

Assertions.assertTrue(argumentCaptor.getValue() instanceof NoSuchUser);
Assertions.assertInstanceOf(
NoSuchUser.class,
argumentCaptor.getValue()
);
}

@Test
Expand Down Expand Up @@ -93,7 +96,7 @@ void creatingConversation_ShouldResultIn_AddingConversationIdToAllParticipants()
}

@Test
void creatingConversation_ShouldResultIn_StoringParticipants() throws Exception {
void creatingConversation_ShouldResultIn_StoringParticipants() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -143,7 +146,7 @@ void creatingConversation_ShouldResultIn_StoringParticipants() throws Exception
() -> Assertions.assertEquals(
self.getUserName(),
storedUsersList
.get(0)
.getFirst()
.getUserName()
),
() -> Assertions.assertEquals(
Expand All @@ -156,7 +159,7 @@ void creatingConversation_ShouldResultIn_StoringParticipants() throws Exception
}

@Test
void creatingConversation_ShouldNotResultIn_DataRequestedForCurrentUserOuptAgent() throws Exception {
void creatingConversation_ShouldNotResultIn_DataRequestedForCurrentUserOuptAgent() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -200,7 +203,7 @@ void creatingConversation_ShouldNotResultIn_DataRequestedForCurrentUserOuptAgent
}

@Test
void creatingConversation_ShouldResultIn_NotifyingOnlineParticipants() throws Exception {
void creatingConversation_ShouldResultIn_NotifyingOnlineParticipants() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -283,7 +286,7 @@ void creatingConversation_ShouldResultIn_NotifyingOnlineParticipants() throws Ex
),
() -> Assertions.assertEquals(
conversation,
conversations.get(0)
conversations.getFirst()
),
() -> Assertions.assertEquals(
conversation,
Expand All @@ -293,7 +296,7 @@ void creatingConversation_ShouldResultIn_NotifyingOnlineParticipants() throws Ex
}

@Test
void creatingConversation_ShouldResultIn_ConversationBeingStored() throws Exception {
void creatingConversation_ShouldResultIn_ConversationBeingStored() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -340,7 +343,7 @@ void creatingConversation_ShouldResultIn_ConversationBeingStored() throws Except
}

@Test
void creatingConversation_ShouldResultIn_AllIsWellResponse() throws Exception {
void creatingConversation_ShouldResultIn_AllIsWellResponse() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -384,7 +387,7 @@ void creatingConversation_ShouldResultIn_AllIsWellResponse() throws Exception {
}

@Test
void creatingConversation_ShouldResultIn_ConversationResponse() throws Exception {
void creatingConversation_ShouldResultIn_ConversationResponse() {
User self = new User(
"Me",
"Some password"
Expand Down Expand Up @@ -424,6 +427,9 @@ void creatingConversation_ShouldResultIn_ConversationResponse() throws Exception
)
.addResponse(argumentCaptor.capture());

Assertions.assertEquals(conversation, argumentCaptor.getValue());
Assertions.assertEquals(
conversation,
argumentCaptor.getValue()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ void createUserSucceeds_ShouldDeleteInviteCode() {

var argumentCaptor = ArgumentCaptor.forClass(InviteCodeContainer.class);
Mockito
.verify(data,
times(1))
.verify(
data,
times(1)
)
.storeInviteCodes(argumentCaptor.capture());

Assertions.assertAll(
() -> Assertions.assertFalse(inviteCodeContainer.contains(inviteCode)),
() -> Assertions.assertEquals(inviteCodeContainer, argumentCaptor.getValue())
() -> Assertions.assertEquals(
inviteCodeContainer,
argumentCaptor.getValue()
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

@ExtendWith(MockitoExtension.class)
public class DeleteFriendRequestTests extends BaseInputHandlerTest {
@Mock
private User user;
@Mock private User user;

void beforeEach() {
}
Expand All @@ -46,8 +45,20 @@ void whenRemovingFriendRequest_ShouldRemoveFromCorrectUser() {
String otherUsername = "otherUsername";
inputHandler.handleInput(new DeleteFriendRequest(otherUsername));

verify(otherUser, never()).removeFriendRequest(eq(data), anyString());
verify(user, times(1)).removeFriendRequest(data, otherUsername);
verify(
otherUser,
never()
).removeFriendRequest(
eq(data),
anyString()
);
verify(
user,
times(1)
).removeFriendRequest(
data,
otherUsername
);
}

@Test
Expand All @@ -59,13 +70,19 @@ void whenSuccessfulRemovingFriendRequest_ShouldGetAllIsWellResponse() {
.getUser(username);
doReturn(new AllIsWell())
.when(user)
.removeFriendRequest(data, otherUsername);
.removeFriendRequest(
data,
otherUsername
);
doReturn(username)
.when(inputAgent)
.getUsername();

inputHandler.handleInput(new DeleteFriendRequest(otherUsername));

OutputAgentTestUtil.assertOutputAgentReceivedResponseClass(outputAgent, AllIsWell.class);
OutputAgentTestUtil.assertOutputAgentReceivedResponseClass(
outputAgent,
AllIsWell.class
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ void whenRemovingFriend_ShouldRemoveFromCorrectUser() {
String otherUsername = "otherUsername";
inputHandler.handleInput(new DeleteFriend(otherUsername));

verify(otherUser, never()).removeFriend(eq(data), anyString());
verify(user, times(1)).removeFriend(data, otherUsername);
verify(
otherUser,
never()
).removeFriend(
eq(data),
anyString()
);
verify(
user,
times(1)
).removeFriend(
data,
otherUsername
);
}

@Test
Expand All @@ -58,13 +70,19 @@ void whenSuccessfulRemovingFriend_ShouldGetAllIsWellResponse() {
.getUser(username);
doReturn(new AllIsWell())
.when(user)
.removeFriend(data, otherUsername);
.removeFriend(
data,
otherUsername
);
doReturn(username)
.when(inputAgent)
.getUsername();

inputHandler.handleInput(new DeleteFriend(otherUsername));

OutputAgentTestUtil.assertOutputAgentReceivedResponseClass(outputAgent, AllIsWell.class);
OutputAgentTestUtil.assertOutputAgentReceivedResponseClass(
outputAgent,
AllIsWell.class
);
}
}
Loading

0 comments on commit 15a05b2

Please sign in to comment.