Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added forum service unit tests. #916

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.app.gamereview.model;

import com.app.gamereview.model.common.BaseModel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
Expand All @@ -13,6 +15,8 @@
@TypeAlias("Profile")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Profile extends BaseModel {
private String userId; // user the profile is linked to

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.app.gamereview.service;

import com.app.gamereview.enums.ForumType;
import com.app.gamereview.exception.ResourceNotFoundException;
import com.app.gamereview.model.Forum;
import com.app.gamereview.repository.ForumRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.modelmapper.ModelMapper;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class ForumServiceTest {

@Mock
private ForumRepository forumRepository;

@Mock
private MongoTemplate mongoTemplate;

@Mock
private ModelMapper modelMapper;

@InjectMocks
private ForumService forumService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void banUser_SuccessfullyBanned() {
// Arrange
String forumId = "forum123";
String userId = "user456";
Forum forum = new Forum();
forum.setId(forumId);
forum.setName("ForumName");
forum.setType(ForumType.GROUP);
forum.setParent("ParentGroup");
forum.setSubscribers(new ArrayList<>());
forum.setBannedUsers(new ArrayList<>());
when(forumRepository.findById(forumId)).thenReturn(Optional.of(forum));

// Act
boolean result = forumService.banUser(forumId, userId);

// Assert
assertTrue(result);
assertTrue(forum.getBannedUsers().contains(userId));
verify(forumRepository, times(1)).save(forum);
}

@Test
void banUser_ForumNotFound() {
// Arrange
String forumId = "nonExistentForum";
String userId = "user123";
when(forumRepository.findById(forumId)).thenReturn(Optional.empty());

// Act & Assert
assertThrows(ResourceNotFoundException.class, () -> forumService.banUser(forumId, userId));
verify(forumRepository, never()).save(any());
}

@Test
void unbanUser_SuccessfullyUnbanned() {
// Arrange
String forumId = "forum789";
String userId = "user987";
Forum forum = new Forum();
forum.setId(forumId);
forum.setName("AnotherForum");
forum.setType(ForumType.GAME);
forum.setParent("ParentGame");
forum.setSubscribers(new ArrayList<>());
forum.setBannedUsers(new ArrayList<>(List.of(userId)));
when(forumRepository.findById(forumId)).thenReturn(Optional.of(forum));

// Act
boolean result = forumService.unbanUser(forumId, userId);

// Assert
assertTrue(result);
assertFalse(forum.getBannedUsers().contains(userId));
verify(forumRepository, times(1)).save(forum);
}

@Test
void unbanUser_ForumNotFound() {
// Arrange
String forumId = "nonExistentForum";
String userId = "user789";
when(forumRepository.findById(forumId)).thenReturn(Optional.empty());

// Act & Assert
assertThrows(ResourceNotFoundException.class, () -> forumService.unbanUser(forumId, userId));
verify(forumRepository, never()).save(any());
}
}