org.bisen.blog.service.BlogServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.bisen.blog.service.BlogServiceTest.java

Source

/*
 * Copyright 2014 asikprad.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bisen.blog.service;

import java.util.Arrays;
import java.util.HashSet;
import org.bisen.blog.model.Blog;
import org.bisen.blog.model.BlogUser;
import org.bisen.blog.model.SolrBlog;
import org.bisen.blog.model.Tag;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 *
 * @author asikprad
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:business-config.xml" })
public class BlogServiceTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(BlogServiceTest.class);

    @Autowired
    protected BlogService blogService;

    /**
     * Test of saveBlog method, of class BlogServiceImpl.
     */
    @Test
    public void testSaveAndSearchBlog() {
        System.out.println("saveBlog");

        Blog blog = new Blog();

        blog.setContent("<p>This is test content</p>");
        blog.setTitle("This is test title");
        blog.setTags(new HashSet<>(Arrays.asList(new Tag("test"), new Tag("test1"))));

        SolrBlog result = blogService.save(blog);

        Assert.assertTrue(result.getId() > 0);

        Page<SolrBlog> solrResults = blogService.search("content", new PageRequest(0, 10));
        Assert.assertTrue(solrResults.getTotalElements() > 0);
    }

    @Test
    public void testSaveUser() {

        BlogUser user = new BlogUser();
        user.setEmail("asikpradhan@gmail.com");

        blogService.saveUser(user);

        Assert.assertEquals("asikpradhan@gmail.com", blogService.findUser("asikpradhan@gmail.com").getEmail());

    }

}