Java tutorial
/* * 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.chatamari.service; import org.bisen.chatamari.service.BlogService; import java.util.Arrays; import java.util.HashSet; import org.bisen.chatamari.model.Blog; import org.bisen.chatamari.model.BlogUser; import org.bisen.chatamari.model.ElasticBlog; import org.bisen.chatamari.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")))); ElasticBlog result = blogService.save(blog); Assert.assertTrue(result.getId() > 0); Page<ElasticBlog> elasticBlog = blogService.findAll(new PageRequest(0, 10)); Assert.assertTrue(elasticBlog.getTotalElements() > 0); Page<ElasticBlog> blogs = blogService.search("content", new PageRequest(0, 10)); Assert.assertTrue(blogs.getTotalElements() > 0); } //@Test public void testMoreLikeThis() { Blog blog = new Blog(); blog.setContent("<p>This is test content</p>"); blog.setTitle("This is test title"); ElasticBlog result = blogService.save(blog); Blog blog1 = new Blog(); blog1.setContent("<p>This is test content</p>"); blog1.setTitle("This is test title is what"); ElasticBlog result1 = blogService.save(blog1); Page<ElasticBlog> eb = blogService.moreLikeThis(String.valueOf(result.getId())); System.out.println(eb); } @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()); } }