Convert mocked tests to integration tests
This commit is contained in:
parent
6834ebaac1
commit
075c691ba3
core/src/test/java/se/su/dsv/scipro/forum
@ -1,198 +0,0 @@
|
||||
package se.su.dsv.scipro.forum;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.AdditionalAnswers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumPost;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumPostReadState;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumThread;
|
||||
import se.su.dsv.scipro.system.User;
|
||||
import se.su.dsv.scipro.test.ForumBuilder;
|
||||
import se.su.dsv.scipro.test.UserBuilder;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class BasicForumServiceImplTest {
|
||||
|
||||
@Mock
|
||||
private AbstractThreadRepository threadRepository;
|
||||
|
||||
@Mock
|
||||
private ForumPostReadStateRepository readStateRepository;
|
||||
|
||||
@Mock
|
||||
private ForumPostRepository postRepository;
|
||||
|
||||
@Mock
|
||||
private EventBus eventBus;
|
||||
|
||||
@InjectMocks
|
||||
private BasicForumServiceImpl basicForumService;
|
||||
|
||||
@Test
|
||||
public void testGetPostPageByForumThread() {
|
||||
List<ForumPost> posts = Collections.singletonList(new ForumBuilder().createPost());
|
||||
when(postRepository.findByThread(isA(ForumThread.class))).thenReturn(posts);
|
||||
|
||||
List<ForumPost> servicePage = basicForumService.getPosts(mock(ForumThread.class));
|
||||
|
||||
assertEquals(posts, servicePage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkRead() {
|
||||
when(readStateRepository.find(any(User.class), any(ForumPost.class))).thenReturn(new ForumPostReadState());
|
||||
when(readStateRepository.save(isA(ForumPostReadState.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
|
||||
User user = new User();
|
||||
ForumPost post = new ForumPost();
|
||||
|
||||
boolean read = basicForumService.setRead(user, post, true);
|
||||
|
||||
assertTrue(read, "Did not return proper read state");
|
||||
|
||||
ArgumentCaptor<ForumPostReadState> captor = ArgumentCaptor.forClass(ForumPostReadState.class);
|
||||
verify(readStateRepository, times(1)).save(captor.capture());
|
||||
|
||||
assertTrue(captor.getValue().isRead(), "Did not save correct read state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkUnread() {
|
||||
when(readStateRepository.find(any(User.class), any(ForumPost.class))).thenReturn(new ForumPostReadState());
|
||||
when(readStateRepository.save(isA(ForumPostReadState.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
|
||||
// when
|
||||
User user = new User();
|
||||
ForumPost post = new ForumPost();
|
||||
|
||||
// when
|
||||
boolean read = basicForumService.setRead(user, post, false);
|
||||
|
||||
// then
|
||||
assertFalse(read, "Did not return proper read state");
|
||||
|
||||
ArgumentCaptor<ForumPostReadState> captor = ArgumentCaptor.forClass(ForumPostReadState.class);
|
||||
verify(readStateRepository, times(1)).save(captor.capture());
|
||||
|
||||
assertFalse(captor.getValue().isRead(), "Did not save correct read state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkThreadReadPostsEvent() {
|
||||
User user = new User();
|
||||
ForumPost post = new ForumPost();
|
||||
post.setContent("post 1");
|
||||
ForumPost post2 = new ForumPost();
|
||||
post2.setContent("post 2");
|
||||
ForumThread forumThread = new ForumThread();
|
||||
forumThread.addPost(post);
|
||||
forumThread.addPost(post2);
|
||||
|
||||
basicForumService.setThreadRead(user, forumThread, true);
|
||||
|
||||
verify(eventBus).post(new ForumPostReadEvent(post, user));
|
||||
verify(eventBus).post(new ForumPostReadEvent(post2, user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsThreadRead() {
|
||||
User goodUser = new UserBuilder().setFirstName("Reads").setLastName("Forum").create();
|
||||
User badUser = new UserBuilder().setFirstName("Does not read").setLastName("Forum").create();
|
||||
|
||||
ForumPost post = new ForumPost();
|
||||
ForumPostReadState readState = new ForumPostReadState(goodUser, post);
|
||||
readState.setRead(true);
|
||||
|
||||
ForumPostReadState notReadState = new ForumPostReadState(badUser, post);
|
||||
notReadState.setRead(false);
|
||||
|
||||
ForumThread forumThread = new ForumThread();
|
||||
forumThread.addPost(post);
|
||||
|
||||
when(postRepository.findByThread(forumThread)).thenReturn(List.of(post));
|
||||
|
||||
when(readStateRepository.find(eq(goodUser), isA(ForumPost.class))).thenReturn(readState);
|
||||
when(readStateRepository.find(eq(badUser), isA(ForumPost.class))).thenReturn(notReadState);
|
||||
|
||||
boolean goodUserState = basicForumService.isThreadRead(goodUser, forumThread);
|
||||
boolean badUserState = basicForumService.isThreadRead(badUser, forumThread);
|
||||
|
||||
assertTrue(goodUserState, "Good user has not read all thread posts");
|
||||
assertFalse(badUserState, "Bad user has read all thread posts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void create_forum_thread() {
|
||||
when(threadRepository.save(any(ForumThread.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
final String subject = "Subject";
|
||||
|
||||
ForumThread thread = basicForumService.createThread(subject);
|
||||
|
||||
assertThat(thread.getSubject(), is(subject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reply_to_thread() {
|
||||
when(readStateRepository.find(any(User.class), any(ForumPost.class))).thenReturn(new ForumPostReadState());
|
||||
when(readStateRepository.save(isA(ForumPostReadState.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
when(threadRepository.save(any(ForumThread.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
when(postRepository.save(any(ForumPost.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
|
||||
final ForumThread forumThread = new ForumThread();
|
||||
final User poster = User.builder().firstName("Bob").lastName("Example").emailAddress("bob@example.com").build();
|
||||
final String content = "content";
|
||||
|
||||
ForumPost forumPost = basicForumService.createReply(forumThread, poster, content, Collections.emptySet());
|
||||
|
||||
assertThat(forumPost.getContent(), is(content));
|
||||
assertThat(forumPost.getPostedBy(), is(poster));
|
||||
assertThat(forumPost.getForumThread(), is(forumThread));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mark_post_read_posts_event() {
|
||||
when(readStateRepository.find(any(User.class), any(ForumPost.class))).thenReturn(new ForumPostReadState());
|
||||
when(readStateRepository.save(isA(ForumPostReadState.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
|
||||
final ForumPost post = new ForumPost();
|
||||
post.setId(235235L);
|
||||
final User user = new User();
|
||||
user.setId(2378924L);
|
||||
|
||||
basicForumService.setRead(user, post, true);
|
||||
|
||||
ArgumentCaptor<ForumPostReadEvent> captor = ArgumentCaptor.forClass(ForumPostReadEvent.class);
|
||||
verify(eventBus).post(captor.capture());
|
||||
|
||||
ForumPostReadEvent event = captor.getValue();
|
||||
assertEquals(event.post(), post);
|
||||
assertEquals(event.user(), user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mark_post_unread_does_not_post_read_event() {
|
||||
when(readStateRepository.find(any(User.class), any(ForumPost.class))).thenReturn(new ForumPostReadState());
|
||||
when(readStateRepository.save(isA(ForumPostReadState.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
|
||||
|
||||
final ForumPost post = new ForumPost();
|
||||
post.setId(235235L);
|
||||
final User user = new User();
|
||||
user.setId(2378924L);
|
||||
|
||||
basicForumService.setRead(user, post, false);
|
||||
|
||||
verify(eventBus, never()).post(isA(ForumPostReadEvent.class));
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
package se.su.dsv.scipro.forum;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -103,4 +108,91 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
|
||||
assertFalse(basicForumService.canDelete(originalPost));
|
||||
assertThrows(IllegalArgumentException.class, () -> basicForumService.deletePost(originalPost));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPostPageByForumThread() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post1 = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
ForumPost post2 = basicForumService.createReply(thread, commenter, "Test post 2", Set.of());
|
||||
|
||||
List<ForumPost> posts = basicForumService.getPosts(thread);
|
||||
|
||||
assertThat(posts, contains(post1, post2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkRead() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
|
||||
boolean read = basicForumService.setRead(commenter, post, true);
|
||||
|
||||
assertTrue(read, "Did not return proper read state");
|
||||
|
||||
boolean isRead = basicForumService.isRead(commenter, post);
|
||||
|
||||
assertTrue(isRead, "Did not save correct read state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkUnread() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
|
||||
boolean read = basicForumService.setRead(op, post, false);
|
||||
|
||||
assertFalse(read, "Did not return proper read state");
|
||||
|
||||
boolean isRead = basicForumService.isRead(commenter, post);
|
||||
|
||||
assertFalse(isRead, "Did not save correct read state");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkThreadReadPostsEvent() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post1 = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
ForumPost post2 = basicForumService.createReply(thread, op, "Test post 2", Set.of());
|
||||
|
||||
basicForumService.setThreadRead(commenter, thread, true);
|
||||
|
||||
assertThat(getPublishedEvents(), hasItem(new ForumPostReadEvent(post1, commenter)));
|
||||
assertThat(getPublishedEvents(), hasItem(new ForumPostReadEvent(post2, commenter)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsThreadRead() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
|
||||
basicForumService.setThreadRead(commenter, thread, true);
|
||||
|
||||
basicForumService.createReply(thread, commenter, "Test post 2", Set.of());
|
||||
|
||||
boolean goodUserState = basicForumService.isThreadRead(commenter, thread);
|
||||
boolean badUserState = basicForumService.isThreadRead(op, thread);
|
||||
|
||||
assertTrue(goodUserState, "Good user has not read all thread posts");
|
||||
assertFalse(badUserState, "Bad user has read all thread posts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mark_post_read_posts_event() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
|
||||
basicForumService.setRead(commenter, post, true);
|
||||
|
||||
assertThat(getPublishedEvents(), hasItem(new ForumPostReadEvent(post, commenter)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mark_post_unread_does_not_post_read_event() {
|
||||
ForumThread thread = basicForumService.createThread("Test thread");
|
||||
ForumPost post = basicForumService.createReply(thread, op, "Test post 1", Set.of());
|
||||
|
||||
basicForumService.setRead(commenter, post, false);
|
||||
|
||||
assertThat(getPublishedEvents(), not(hasItem(new ForumPostReadEvent(post, commenter))));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user