Skip to content
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
Expand Up @@ -48,12 +48,12 @@ class AlbumControllerTest {
@Autowired
private ObjectMapper objectMapper;

static User usuario;
static Photo photo1, photo2;
static List<Photo> photos;
static Album album;
static AlbumRequest albumRequest;
static UserPrincipal user;
private static User usuario;
private static Photo photo1, photo2;
private static List<Photo> photos;
private static Album album;
private static AlbumRequest albumRequest;
private static UserPrincipal user;

@BeforeEach
void initTest(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.sopromadze.blogapi.service.impl;
import com.sopromadze.blogapi.exception.BlogapiException;
import com.sopromadze.blogapi.exception.ResourceNotFoundException;
import com.sopromadze.blogapi.model.Comment;
import com.sopromadze.blogapi.model.Post;
Expand Down Expand Up @@ -95,6 +96,19 @@ void addComment() {

assertEquals(COMMENT_ID, comment.getId());
}
@Test
void addtCommentPostException(){
when(postRepository.findById(POST_ID)).thenReturn(Optional.empty());

assertThrows(ResourceNotFoundException.class, ()-> commentService.addComment(getCommentRequest(), POST_ID, getUserPrincipal()));
}

@Test
void addCommentCommentException(){
when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.empty());

assertThrows(ResourceNotFoundException.class, () -> commentService.addComment(getCommentRequest(), POST_ID, getUserPrincipal()));
}

@Test
void getComment() {
Expand All @@ -121,6 +135,16 @@ void getCommentCommentException(){
assertThrows(ResourceNotFoundException.class, () -> commentService.getComment(POST_ID,COMMENT_ID));
}

/*@Test
void getCommentExceptionId(){
when(postRepository.findById(POST_ID)).thenReturn(Optional.of(getPost()));

when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.of(getCommentEntity()));

getCommentEntity().getPost().setId(777L);

assertThrows(BlogapiException.class, ()-> commentService.getComment(POST_ID,COMMENT_ID));
}*/

@Test
void updateComment() {
Expand All @@ -135,7 +159,7 @@ void updateComment() {
assertEquals(COMMENT_ID, comment.getId());
}

/* @Test
@Test
void updateCommentExceptionRole(){

when(postRepository.findById(POST_ID)).thenReturn(Optional.of(getPost()));
Expand All @@ -144,10 +168,19 @@ void updateCommentExceptionRole(){

getCommentEntity().getUser().setId(98L);

assertThrows(BlogapiException.class, () -> commentService.updateComment(POST_ID, COMMENT_ID, getCommentRequest(), getUserRoleUser()));
assertThrows(BlogapiException.class, () -> commentService.updateComment(POST_ID, COMMENT_ID, getCommentRequest(), getUserRoleUser()));
}

}*/
@Test
void updateCommentExceptionPostId(){
when(postRepository.findById(POST_ID)).thenReturn(Optional.of(getPost()));

when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.of(getCommentEntity()));

getPost().setId(4586L);

assertThrows(BlogapiException.class, ()-> commentService.updateComment(POST_ID, COMMENT_ID, getCommentRequest(), getUserRoleUser()));
}

@Test()
void updateCommentExceptionPost(){
Expand All @@ -171,7 +204,7 @@ private UserPrincipal getUserPrincipal() {
}

private UserPrincipal getUserRoleUser(){
UserPrincipal userPrincipal = new UserPrincipal(USER_ID, "Pepe","Palote", "pepalote","pepalote@gmail.com","1234", Collections.singleton(new SimpleGrantedAuthority(RoleName.ROLE_USER.toString())));;
UserPrincipal userPrincipal = new UserPrincipal(1655L, "Pepe","Palote", "pepalote","pepalote@gmail.com","1234", Collections.singleton(new SimpleGrantedAuthority(RoleName.ROLE_USER.toString())));;
return userPrincipal;
}

Expand Down Expand Up @@ -213,4 +246,45 @@ void deleteComment() {

assertTrue(apiResponse.getSuccess());
}

@Test()
void deleteCommentExceptionPost(){

when(postRepository.findById(POST_ID)).thenReturn(Optional.empty());

assertThrows(ResourceNotFoundException.class, () -> commentService.deleteComment(POST_ID, COMMENT_ID, getUserPrincipal()));
}

@Test
void deleteCommentExceptionComment(){

when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.empty());

assertThrows(ResourceNotFoundException.class, ()-> commentService.deleteComment(POST_ID, COMMENT_ID, getUserPrincipal()));
}

@Test
void deleteCommentExceptionRole(){

when(postRepository.findById(POST_ID)).thenReturn(Optional.of(getPost()));

when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.of(getCommentEntity()));

getCommentEntity().getUser().setId(98L);

assertThrows(BlogapiException.class, () -> commentService.deleteComment(POST_ID, COMMENT_ID, getUserRoleUser()));
}

@Test
void deleteCommentFalse(){
when(postRepository.findById(POST_ID)).thenReturn(Optional.of(getPost()));

getPost().setId(65983L);

when(commentRepository.findById(COMMENT_ID)).thenReturn(Optional.of(getCommentEntity()));

ApiResponse apiResponse = commentService.deleteComment(POST_ID, COMMENT_ID, getUserPrincipal());

assertFalse(!apiResponse.getSuccess());
}
}