Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //COMENT SERVICE
- @Service
- @AllArgsConstructor
- public class CommentService {
- private final CommentRepository commentRepository;
- private final TaskRepository taskRepository;
- private final AuthService authService;
- private final CommentMapper commentMapper;
- private final UserRepository userRepository;
- @Transactional
- @Procedure
- public Comment createComment(CommentDto commentDto)
- {
- User current = authService.getCurrentUser();
- Task task = taskRepository.findById(commentDto.getTaskId()).orElseThrow(()-> new IllegalStateException("Task doesnt exist"));
- Comment save = commentRepository.save(commentMapper.map(commentDto,current,task));
- return save;
- }
- @Transactional(readOnly = true)
- public List<CommentDto> getCommentsByTask(Long taskId) {
- Task task = taskRepository.findById(taskId).orElseThrow(()-> new SpringTodoException("No such task found"));
- List<Comment> comments = commentRepository.findByTask(task);
- return comments
- .stream()
- .map(commentMapper::mapCommentToDto)
- .collect(Collectors.toList());
- }
- @Transactional(readOnly = true)
- public List<CommentDto> getCommentsByUsername(Long userId) {
- User user = userRepository.findById(userId).orElseThrow(()-> new SpringTodoException("No such user found"));
- List<Comment> comments = commentRepository.findByUserName(user.getUsername());
- return comments
- .stream()
- .map(commentMapper::mapCommentToDto)
- .collect(Collectors.toList());
- }
- @Transactional
- public void editComment(CommentDto commentDto)
- {
- Comment editedComment = commentRepository.findById(commentDto.getCommentId()).orElseThrow(() -> new SpringTodoException("No such comment found"));
- editedComment.setText(commentDto.getText());
- }
- @Transactional
- public void deleteComment(Long commentId)
- {
- Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new SpringTodoException("No such comment found"));
- commentRepository.delete(comment);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- //COMMENT SERVICE TEST
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @AutoConfigureMockMvc
- @ExtendWith(MockitoExtension.class)
- class CommentServiceTest {
- @Mock
- private CommentRepository commentRepository;
- @Mock
- private TaskRepository taskRepository;
- @Mock
- private AuthService authService;
- @Mock
- private CommentMapper commentMapper;
- @Mock
- private UserRepository userRepository;
- @InjectMocks
- private CommentService commentService;
- @Test
- void shouldCreateComment() {
- //given
- CommentDto commentDto = new CommentDto();
- commentDto.setCommentId(1L);
- commentDto.setText("Test");
- commentDto.setUsername("testowy");
- commentDto.setCreatedDate(Instant.now());
- commentDto.setTaskId(1L);
- Set<Team> teams = new HashSet<>();
- UserProfile userProfile = new UserProfile();
- Task task = new Task();
- task.setTaskId(1L);
- Comment save = new Comment();
- //when
- Mockito.when(authService.getCurrentUser()).thenReturn(new User(1L,"testowy","passwd","testowy@test.com", Instant.now(),false,teams, userProfile));
- Mockito.when(taskRepository.findById(1L)).thenReturn(Optional.of(task));
- Mockito.when(commentRepository.save(Mockito.any())).thenReturn(save);
- Comment created = commentService.createComment(commentDto);
- //then
- assertThat(created).isEqualTo(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement