package com.dodo.blog.server.impl.it;
import com.dodo.blog.exception.UserAlreadyRated;
import com.dodo.blog.model.Account;
import com.dodo.blog.model.Article;
import com.dodo.blog.model.Category;
import com.dodo.blog.model.Tag;
import com.dodo.blog.request.ArticlesRequest;
import com.dodo.blog.server.AccountService;
import com.dodo.blog.server.ArticleService;
import com.dodo.blog.server.CategoryService;
import com.dodo.blog.server.TagService;
import com.google.guiceberry.junit4.GuiceBerryRule;
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import javax.inject.Inject;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
*/
public class ArticleServiceBeanTest
extends ServicesEnvironment
{
@Rule
public final GuiceBerryRule guiceBerry = new GuiceBerryRule( ServicesEnvironment.class );
@Inject
private ArticleService articleService;
@Inject
private AccountService accountService;
@Inject
private TagService tagService;
@Inject
private CategoryService categoryService;
@Test
public void testSaveArticle() throws Exception
{
Assert.assertEquals( "True about GAE", saveArticle().getTitle() );
}
@Test
public void testGetArticleById() throws Exception
{
Article article = saveArticle();
article = articleService.getArticleById( article.getId() );
Assert.assertEquals( "True about GAE", article.getTitle() );
}
@Test
public void testGetArticlesByCategory() throws Exception
{
Category category = saveCategory();
Article article = new Article();
article.setCategory( category.getId() );
articleService.saveArticle( article );
ArticlesRequest request = new ArticlesRequest( -1, -1 );
request.setCategory( "backend" );
List<Article> articles = articleService.getArticles( request );
Assert.assertEquals( Long.valueOf( 1 ), articles.get( 0 ).getCategory() );
}
@Test
public void testGetArticlesByTag() throws Exception
{
Tag tag = saveTag();
for ( int i = 0; i < 30; i++ )
{
Article article = new Article();
article.setTag_1( tag.getId() );
articleService.saveArticle( article );
article = new Article();
article.setTag_2( tag.getId() );
articleService.saveArticle( article );
}
ArticlesRequest request = new ArticlesRequest( -1, 20 );
request.setTag( "google-app-engine" );
List<Article> articles = articleService.getArticles( request );
Assert.assertEquals( Long.valueOf( 1 ), articles.get( 0 ).getTag_2() );
Assert.assertEquals( Long.valueOf( 1 ), articles.get( 1 ).getTag_1() );
}
@Test
public void testGetArticlesByDate() throws Exception
{
Article article = new Article();
articleService.saveArticle( article );
Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date() );
Format format = new DecimalFormat( "00" );
String year = Integer.valueOf( calendar.get( Calendar.YEAR ) ).toString();
String month = format.format( calendar.get( Calendar.MONTH ) + 1 );
String day = format.format( calendar.get( Calendar.DAY_OF_MONTH ) );
ArticlesRequest request = new ArticlesRequest( -1, -1 );
request.setYear( year );
request.setMonth( month );
request.setDay( day );
List<Article> articles = articleService.getArticles( request );
Assert.assertEquals( year, articles.get( 0 ).getYear() );
Assert.assertEquals( month, articles.get( 0 ).getMonth() );
Assert.assertEquals( day, articles.get( 0 ).getDay() );
}
@Test
public void testGetMostViewedArticles() throws Exception
{
Article article = new Article();
article.setTitle( "0 views" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.incrementViewed();
article.setTitle( "1 views" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.incrementViewed();
article.incrementViewed();
article.setTitle( "2 views" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
List<Article> articles = articleService.getMostViewedArticles();
Assert.assertEquals( "2 views", articles.get( 0 ).getTitle() );
Assert.assertEquals( "1 views", articles.get( 1 ).getTitle() );
Assert.assertEquals( "0 views", articles.get( 2 ).getTitle() );
}
@Test
public void testGetBestRatedArticles() throws Exception
{
Article article = new Article();
article.setTitle( "0 rates" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.incrementRate( 3L );
article.setTitle( "1 rates" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.incrementRate( 5l );
article.setTitle( "2 rates" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
List<Article> articles = articleService.getBestRatedArticles();
Assert.assertEquals( "2 rates", articles.get( 0 ).getTitle() );
Assert.assertEquals( "1 rates", articles.get( 1 ).getTitle() );
Assert.assertEquals( "0 rates", articles.get( 2 ).getTitle() );
}
@Test
public void testGetRecentArticles() throws Exception
{
Article article = new Article();
article.setTitle( "New" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.setTitle( "Newer" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
article = new Article();
article.setTitle( "Newest" );
articleService.saveArticle( article );
articleService.publishArticle( article.getId() );
List<Article> articles = articleService.getRecentArticles();
Assert.assertEquals( "Newest", articles.get( 0 ).getTitle() );
Assert.assertEquals( "Newer", articles.get( 1 ).getTitle() );
Assert.assertEquals( "New", articles.get( 2 ).getTitle() );
}
@Test
public void testDeleteArticle() throws Exception
{
Article article = saveArticle();
Long id = article.getId();
articleService.deleteArticle( id );
Assert.assertNull( articleService.getArticleById( id ) );
}
@Test
public void testRateLogic()
{
Article article = new Article();
org.junit.Assert.assertEquals( Double.valueOf( 0 ), article.calculateRate() );
article.incrementRate( 3L );
org.junit.Assert.assertEquals( Double.valueOf( 3 ), article.calculateRate() );
article.incrementRate( 5L );
org.junit.Assert.assertEquals( Double.valueOf( 4 ), article.calculateRate() );
article.incrementRate( 1L );
org.junit.Assert.assertEquals( Double.valueOf( 3 ), article.calculateRate() );
}
@Test
public void testRate() throws Exception
{
Account account = new Account();
accountService.saveAccount( account );
// rate article
Article article = saveArticle();
article = articleService.rate( article.getId(), account.getId(), 5L );
Assert.assertEquals( ( double ) 5, article.calculateRate() );
// rate again to test exception
try
{
articleService.rate( article.getId(), account.getId(), 3L );
Assert.fail( UserAlreadyRated.class.getName() + " should be thrown." );
}
catch ( UserAlreadyRated e )
{
Assert.assertEquals( UserAlreadyRated.class, e.getClass() );
}
}
@Test
public void testIsArticleRated() throws Exception
{
Account account = new Account();
accountService.saveAccount( account );
Article article = saveArticle();
// test before rate
Assert.assertFalse( articleService.isArticleRated( article.getId(), account.getId() ) );
// test after rate
articleService.rate( article.getId(), account.getId(), 5L );
Assert.assertTrue( articleService.isArticleRated( article.getId(), account.getId() ) );
}
@Test
public void testPublish() throws Exception
{
Article article = saveArticle();
// test after publish
articleService.publishArticle( article.getId() );
article = articleService.getArticleById( article.getId() );
Assert.assertTrue( article.isPublished() );
Assert.assertTrue( article.getPublishedDate().after( article.getCreatedDate() ) );
}
@Test
public void testHide() throws Exception
{
Article article = saveArticle();
// test after hide
articleService.hideArticle( article.getId() );
article = articleService.getArticleById( article.getId() );
Assert.assertFalse( article.isPublished() );
Assert.assertNull( article.getPublishedDate() );
}
private Article saveArticle()
{
Article article = new Article();
article.setTitle( "True about GAE" );
articleService.saveArticle( article );
return article;
}
private Tag saveTag()
{
Tag tag = new Tag();
tag.setName( "Google App Engine" );
tagService.saveTag( tag );
return tag;
}
private Category saveCategory()
{
Category category = new Category();
category.setName( "Backend" );
categoryService.saveCategory( category );
return category;
}
}