-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #677 from bounswe/fix/backend/tag-self-following
[Backend] Fix Self Following Field in Tag Details
- Loading branch information
Showing
2 changed files
with
80 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,37 @@ | ||
package com.group1.programminglanguagesforum.Services; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
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 com.group1.programminglanguagesforum.DTOs.Requests.CreateTagRequestDto; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.GetQuestionWithTagDto; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.GetTagDetailsResponseDto; | ||
import com.group1.programminglanguagesforum.Entities.DifficultyLevel; | ||
import com.group1.programminglanguagesforum.Entities.Question; | ||
import com.group1.programminglanguagesforum.Entities.Tag; | ||
import com.group1.programminglanguagesforum.Entities.User; | ||
import com.group1.programminglanguagesforum.Repositories.QuestionRepository; | ||
import com.group1.programminglanguagesforum.Repositories.TagRepository; | ||
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 java.util.Arrays; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
class TagServiceTest { | ||
|
||
|
@@ -89,11 +99,45 @@ void testCreateTag() { | |
@Test | ||
void testGetTagDetails_Success() { | ||
Long tagId = 1L; | ||
Date mockDate = new Date(); | ||
|
||
// Create a mock user with all required fields | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.username("testUser") | ||
.email("[email protected]") // Add required email | ||
.firstName("Test") | ||
.lastName("User") | ||
.reputationPoints(0L) | ||
.build(); | ||
|
||
Tag mockTag = new Tag(1L, null, "Tag1", "Description1", null); | ||
List<Question> mockQuestions = Arrays.asList( | ||
new Question(1L, "Question1", "Body1", DifficultyLevel.EASY, 0L, 0L, null, null, null, null, null,null), | ||
new Question(2L, "Question2", "Body2", DifficultyLevel.MEDIUM, 0L, 0L, null, null, null, null, null,null)); | ||
|
||
Question q1 = Question.builder() | ||
.id(1L) | ||
.title("Question1") | ||
.questionBody("Body1") | ||
.difficulty(DifficultyLevel.EASY) | ||
.likeCount(0L) | ||
.commentCount(0L) | ||
.createdAt(mockDate) | ||
.updatedAt(mockDate) | ||
.askedBy(mockUser) | ||
.build(); | ||
|
||
Question q2 = Question.builder() | ||
.id(2L) | ||
.title("Question2") | ||
.questionBody("Body2") | ||
.difficulty(DifficultyLevel.MEDIUM) | ||
.likeCount(0L) | ||
.commentCount(0L) | ||
.createdAt(mockDate) | ||
.updatedAt(mockDate) | ||
.askedBy(mockUser) | ||
.build(); | ||
|
||
List<Question> mockQuestions = Arrays.asList(q1, q2); | ||
|
||
when(tagRepository.findById(tagId)).thenReturn(Optional.of(mockTag)); | ||
when(questionRepository.findQuestionsByTagId(tagId)).thenReturn(mockQuestions); | ||
|