Search in sources :

Example 1 with PostNotFoundException

use of com.nixmash.blog.jpa.exceptions.PostNotFoundException in project nixmash-blog by mintster.

the class AdminPostsController method isDuplicatePost.

private Boolean isDuplicatePost(PostDTO postDTO, Post sessionPost) {
    Boolean isDuplicate = false;
    if (StringUtils.isNotEmpty(postDTO.getPostTitle())) {
        String slug = PostUtils.createSlug(postDTO.getPostTitle());
        Post found = null;
        try {
            found = postService.getPost(slug);
        } catch (PostNotFoundException e) {
        // can be null for this check of a pre-existing post
        }
        if (sessionPost != null) {
            if (found != null && !(found.getPostId().equals(sessionPost.getPostId()))) {
                isDuplicate = true;
            }
        } else {
            if (found != null)
                isDuplicate = true;
        }
    }
    return isDuplicate;
}
Also used : Post(com.nixmash.blog.jpa.model.Post) PostNotFoundException(com.nixmash.blog.jpa.exceptions.PostNotFoundException)

Example 2 with PostNotFoundException

use of com.nixmash.blog.jpa.exceptions.PostNotFoundException in project nixmash-blog by mintster.

the class PermaPostsControllerTests method notFoundPostName_ThrowsPostNotFoundException.

@Test(expected = PostNotFoundException.class)
public void notFoundPostName_ThrowsPostNotFoundException() throws Exception {
    String badName = "bad-name";
    when(postService.getPost(badName)).thenThrow(new PostNotFoundException());
    mockMvc.perform(get("/post/" + badName)).andExpect(status().isOk()).andExpect(view().name("errors/custom"));
}
Also used : PostNotFoundException(com.nixmash.blog.jpa.exceptions.PostNotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with PostNotFoundException

use of com.nixmash.blog.jpa.exceptions.PostNotFoundException in project nixmash-blog by mintster.

the class PostDocServiceImpl method getMoreLikeThisPostsFromPostDocs.

@Transactional(readOnly = true)
@Override
public List<Post> getMoreLikeThisPostsFromPostDocs(List<PostDoc> postDocs) {
    List<Post> posts = new ArrayList<>();
    String result = StringUtils.EMPTY;
    for (int i = 0; i < applicationSettings.getMoreLikeThisNum(); i++) {
        try {
            PostDoc postDoc = postDocs.get(i);
            posts.add(postService.getPostById(Long.parseLong(postDoc.getPostId())));
        } catch (PostNotFoundException | IndexOutOfBoundsException e) {
            if (e.getClass().equals(PostNotFoundException.class))
                logger.info("MoreLikeThis PostDoc {} to Post with title \"{}\" NOT FOUND", postDocs.get(i).getPostId(), postDocs.get(i).getPostTitle());
            else {
                logger.info("EXCEPTION: AppSetting.MoreLikeThisNum > post count");
                return null;
            }
        }
    }
    return posts;
}
Also used : Post(com.nixmash.blog.jpa.model.Post) PostNotFoundException(com.nixmash.blog.jpa.exceptions.PostNotFoundException) ArrayList(java.util.ArrayList) PostDoc(com.nixmash.blog.solr.model.PostDoc) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with PostNotFoundException

use of com.nixmash.blog.jpa.exceptions.PostNotFoundException in project nixmash-blog by mintster.

the class AdminPostsControllerTests method badPostIdOnPostUpdate_ThrowsPostNotFoundException.

@Test(expected = PostNotFoundException.class)
public void badPostIdOnPostUpdate_ThrowsPostNotFoundException() throws Exception {
    when(postService.getPostById(-2L)).thenThrow(new PostNotFoundException());
    mvc.perform(get("/admin/posts/update/-2")).andExpect(status().isOk()).andExpect(view().name("errors/custom"));
}
Also used : PostNotFoundException(com.nixmash.blog.jpa.exceptions.PostNotFoundException) Test(org.junit.Test)

Aggregations

PostNotFoundException (com.nixmash.blog.jpa.exceptions.PostNotFoundException)4 Post (com.nixmash.blog.jpa.model.Post)2 Test (org.junit.Test)2 PostDoc (com.nixmash.blog.solr.model.PostDoc)1 ArrayList (java.util.ArrayList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Transactional (org.springframework.transaction.annotation.Transactional)1