Advertisement
Guest User

testtJava

a guest
Nov 16th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. //COMENT SERVICE
  2. @Service
  3. @AllArgsConstructor
  4. public class CommentService {
  5.  
  6.     private final CommentRepository commentRepository;
  7.     private final TaskRepository taskRepository;
  8.     private final AuthService authService;
  9.     private final CommentMapper commentMapper;
  10.     private final UserRepository userRepository;
  11.  
  12.     @Transactional
  13.     @Procedure
  14.     public Comment createComment(CommentDto commentDto)
  15.     {
  16.         User current = authService.getCurrentUser();
  17.         Task task = taskRepository.findById(commentDto.getTaskId()).orElseThrow(()-> new IllegalStateException("Task doesnt exist"));
  18.  
  19.         Comment save = commentRepository.save(commentMapper.map(commentDto,current,task));
  20.  
  21.         return save;
  22.     }
  23.  
  24.     @Transactional(readOnly = true)
  25.     public List<CommentDto> getCommentsByTask(Long taskId) {
  26.         Task task = taskRepository.findById(taskId).orElseThrow(()-> new SpringTodoException("No such task found"));
  27.         List<Comment> comments = commentRepository.findByTask(task);
  28.  
  29.         return comments
  30.                 .stream()
  31.                 .map(commentMapper::mapCommentToDto)
  32.                 .collect(Collectors.toList());
  33.     }
  34.     @Transactional(readOnly = true)
  35.     public List<CommentDto> getCommentsByUsername(Long userId) {
  36.         User user = userRepository.findById(userId).orElseThrow(()-> new SpringTodoException("No such user found"));
  37.         List<Comment> comments = commentRepository.findByUserName(user.getUsername());
  38.  
  39.         return comments
  40.                 .stream()
  41.                 .map(commentMapper::mapCommentToDto)
  42.                 .collect(Collectors.toList());
  43.     }
  44.  
  45.     @Transactional
  46.     public void editComment(CommentDto commentDto)
  47.     {
  48.         Comment editedComment = commentRepository.findById(commentDto.getCommentId()).orElseThrow(() -> new SpringTodoException("No such comment found"));
  49.  
  50.         editedComment.setText(commentDto.getText());
  51.     }
  52.  
  53.     @Transactional
  54.     public void deleteComment(Long commentId)
  55.     {
  56.         Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new SpringTodoException("No such comment found"));
  57.         commentRepository.delete(comment);
  58.     }
  59.  
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////
  62. //COMMENT SERVICE TEST
  63. @RunWith(SpringRunner.class)
  64. @SpringBootTest
  65. @AutoConfigureMockMvc
  66. @ExtendWith(MockitoExtension.class)
  67. class CommentServiceTest {
  68.  
  69.     @Mock
  70.     private CommentRepository commentRepository;
  71.     @Mock
  72.     private TaskRepository taskRepository;
  73.     @Mock
  74.     private AuthService authService;
  75.     @Mock
  76.     private CommentMapper commentMapper;
  77.     @Mock
  78.     private UserRepository userRepository;
  79.  
  80.     @InjectMocks
  81.     private CommentService commentService;
  82.     @Test
  83.     void shouldCreateComment() {
  84.         //given
  85.         CommentDto commentDto = new CommentDto();
  86.         commentDto.setCommentId(1L);
  87.         commentDto.setText("Test");
  88.         commentDto.setUsername("testowy");
  89.         commentDto.setCreatedDate(Instant.now());
  90.         commentDto.setTaskId(1L);
  91.  
  92.         Set<Team> teams = new HashSet<>();
  93.         UserProfile userProfile = new UserProfile();
  94.         Task task = new Task();
  95.         task.setTaskId(1L);
  96.  
  97.         Comment save = new Comment();
  98.  
  99.         //when
  100.         Mockito.when(authService.getCurrentUser()).thenReturn(new User(1L,"testowy","passwd","testowy@test.com", Instant.now(),false,teams, userProfile));
  101.         Mockito.when(taskRepository.findById(1L)).thenReturn(Optional.of(task));
  102.         Mockito.when(commentRepository.save(Mockito.any())).thenReturn(save);
  103.  
  104.         Comment created = commentService.createComment(commentDto);
  105.         //then
  106.         assertThat(created).isEqualTo(1);
  107.     }
  108.  
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement