Commit 75e84ebb authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Create abstract test class

parent 05f2bf34
package org.genesys2.unit;
import org.apache.velocity.app.VelocityEngine;
import org.genesys2.server.aspect.AsAdminAspect;
import org.genesys2.server.persistence.domain.*;
import org.genesys2.server.service.*;
import org.genesys2.server.service.impl.*;
import org.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AbstractServicesTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
public abstract class AbstractServicesTest {
@Configuration
@Import({ JpaDataConfig.class, HazelcastConfig.class })
@PropertySource({ "classpath:application.properties", "classpath:spring/spring.properties" })
public static class Config {
@Bean
public UserDetailsService userDetailsService() {
return new AuthUserDetailsService();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public CacheManager cacheManager(){
return new NoOpCacheManager();
}
@Bean
public ContentService contentService(){
return new ContentServiceImpl();
}
@Bean
public HtmlSanitizer htmlSanitizer(){
return new OWASPSanitizer();
}
@Bean
public ContentSanitizer contentSanitizer(){
return new ContentSanitizer();
}
@Bean
public VelocityEngine velocityEngine(){
return new VelocityEngine();
}
@Bean
public DescriptorService descriptorService(){
return new DescriptorServiceImpl();
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
@Bean
public EMailService eMailService() {
return new EMailServiceImpl();
}
@Bean
public JavaMailSender mailSender() {
return Mockito.mock(JavaMailSenderImpl.class);
}
@Bean
public EMailVerificationService emailVerificationService() {
return new EMailVerificationServiceImpl();
}
@Bean
public TokenVerificationService tokenVerificationService() {
return new TokenVerificationServiceImpl();
}
@Bean
public TaxonomyService taxonomyService(){
return new TaxonomyServiceImpl();
}
@Bean
public CropService cropService(){
return new CropServiceImpl();
}
@Bean
public TeamService teamService() {
return new TeamServiceImpl();
}
@Bean
public GenesysLowlevelRepository genesysLowlevelRepository(){
return new GenesysLowlevelRepositoryCustomImpl();
}
@Bean
public AclService aclService(){
return new AclServiceImpl();
}
@Bean
public AsAdminAspect asAdminAspect(){
return new AsAdminAspect();
}
@Bean
public InstituteService instituteService(){
return new InstituteServiceImpl();
}
}
@Autowired
public UserDetailsService userDetailsService;
@Autowired
public UserPersistence userPersistence;
@Autowired
public ContentSanitizer contentSanitizer;
@Autowired
public ActivityPostRepository postRepository;
@Autowired
public DescriptorService descriptorService;
@Autowired
public DescriptorRepository descriptorRepository;
@Autowired
public JavaMailSender mailSender;
@Autowired
public EMailService eMailService;
@Autowired
public VerificationTokenRepository verificationTokenRepository;
@Autowired
public EMailVerificationService emailVerificationService;
@Autowired
public ContentService contentService;
@Autowired
public ArticleRepository articleRepository;
@Autowired
public HtmlSanitizer htmlSanitizer;
@Autowired
public TaxonomyService taxonomyService;
@Autowired
public TokenVerificationService tokenVerificationService;
@Autowired
public VerificationTokenRepository tokenRepository;
@Autowired
public InstituteService instituteService;
@Autowired
public TeamRepository teamRepository;
@Autowired
public TeamService teamService;
@Autowired
public UserService userService;
}
package org.genesys2.unit;
import static org.junit.Assert.assertTrue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.persistence.domain.UserPersistence;
import org.genesys2.server.service.UserService;
import org.genesys2.server.service.impl.AuthUserDetailsService;
import org.genesys2.server.service.impl.UserServiceImpl;
import org.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AuthUserDetailsServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class AuthUserDetailsServiceTest {
private final Log LOG = LogFactory.getLog(AuthUserDetailsServiceTest.class);
@Import({ JpaDataConfig.class, HazelcastConfig.class })
@PropertySource({ "classpath:application.properties", "classpath:spring/spring.properties" })
public static class Config {
@Bean
public UserDetailsService userDetailsService() {
return new AuthUserDetailsService();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
}
@Autowired
private UserDetailsService userDetailsService;
import static org.junit.Assert.assertTrue;
@Autowired
private UserService userService;
public class AuthUserDetailsServiceTest extends AbstractServicesTest {
@Autowired
private UserPersistence userPersistence;
private final Log LOG = LogFactory.getLog(AuthUserDetailsServiceTest.class);
@Test
public void loadUserByUsernameTest() {
......
......@@ -2,86 +2,21 @@ package org.genesys2.unit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.ClassPK;
import org.genesys2.server.persistence.domain.ClassPKRepository;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.impl.ContentSanitizer;
import org.genesys2.server.service.impl.ContentServiceImpl;
import org.genesys2.server.service.impl.OWASPSanitizer;
import org.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ContentSanitizerTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)
public class ContentSanitizerTest {
public class ContentSanitizerTest extends AbstractServicesTest {
private static final Log LOG = LogFactory.getLog(DescriptorServiceTest.class);
@Import({JpaDataConfig.class, HazelcastConfig.class})
@PropertySource({"classpath:application.properties", "classpath:spring/spring.properties"})
public static class Config{
@Bean
public CacheManager cacheManager(){
return new NoOpCacheManager();
}
@Bean
public ContentService contentService(){
return new ContentServiceImpl();
}
@Bean
public HtmlSanitizer htmlSanitizer(){
return new OWASPSanitizer();
}
@Bean
public ContentSanitizer contentSanitizer(){
return new ContentSanitizer();
}
@Bean
public VelocityEngine velocityEngine(){
return new VelocityEngine();
}
}
@Autowired
private ContentSanitizer contentSanitizer;
@Autowired
private ContentService contentService;
@Autowired
private ClassPKRepository classPkRepository;
@Test
public void sanitizeAllTest(){
LOG.info("Start test-method sanitizeAllTest");
......
......@@ -2,84 +2,21 @@ package org.genesys2.unit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.persistence.domain.ActivityPostRepository;
import org.genesys2.server.persistence.domain.ArticleRepository;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.impl.ContentServiceImpl;
import org.genesys2.server.service.impl.OWASPSanitizer;
import org.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ContentServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class ContentServiceTest {
public class ContentServiceTest extends AbstractServicesTest {
private final Log LOG = LogFactory.getLog(ContentServiceTest.class);
@Import({JpaDataConfig.class, HazelcastConfig.class})
@PropertySource({"classpath:application.properties", "classpath:spring/spring.properties"})
public static class Config {
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
@Bean
public ContentService contentService() {
return new ContentServiceImpl();
}
@Bean
public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer();
}
@Bean
public VelocityEngine velocityEngine() {
return new VelocityEngine();
}
}
@Autowired
private ContentService contentService;
@Autowired
private ActivityPostRepository postRepository;
@Autowired
private ArticleRepository articleRepository;
@Before
public void setUp() {
//create activity post for test
......@@ -90,6 +27,8 @@ public class ContentServiceTest {
Article article = new Article();
article.setLang("en");
article.setSlug("smtp.email-verification");
article.setTitle("title of article");
article.setBody("<h3>body of article</h3");
article.setClassPk(contentService.ensureClassPK(Article.class));
articleList.add(article);
contentService.save(articleList);
......@@ -99,7 +38,7 @@ public class ContentServiceTest {
@After
public void teardown() {
//delete activity post
contentService.deleteActivityPost(contentService.lastNews().get(0).getId());
postRepository.deleteAll();
//delete articles
articleRepository.deleteAll();
......@@ -139,7 +78,7 @@ public class ContentServiceTest {
}
@Test
public void listArticlesByLangTest(){
public void listArticlesByLangTest() {
LOG.info("Start test-method listArticlesByLangTest");
List<Article> articleList = contentService.listArticlesByLang("ru", new PageRequest(0, 6)).getContent();
......@@ -152,7 +91,7 @@ public class ContentServiceTest {
}
@Test
public void saveTest(){
public void saveTest() {
LOG.info("Start test-method saveTest");
List<Article> articleList = contentService.listArticles(new PageRequest(0, 6)).getContent();
......@@ -172,7 +111,7 @@ public class ContentServiceTest {
}
@Test
public void getGlobalArticleTets(){
public void getGlobalArticleTets() {
LOG.info("Start test-method getGlobalArticleTets");
assertNotNull(contentService.getGlobalArticle("smtp.email-verification", Locale.ENGLISH));
......@@ -180,4 +119,149 @@ public class ContentServiceTest {
LOG.info("Test getGlobalArticleTets is passed!");
}
@Test
public void getArticleTest() {
LOG.info("Start test-method getArticleTest");
String slug = contentService.listArticles(new PageRequest(0, 6)).getContent().get(0).getSlug();
Article article = contentService.getArticle(Article.class, null, slug, Locale.getDefault(), true);
assertNotNull(article);
LOG.info("Test getArticleTest is passed!");
}
@Test
public void getArticleBySlugAndLangTest(){
LOG.info("Start test-method getArticleBySlugAndLangTest");
Article article = contentService.getArticleBySlugAndLang("smtp.email-verification", "en");
assertNotNull(article);
LOG.info("Test getArticleBySlugAndLangTest is passed!");
}
@Test
public void updateArticleTest(){
LOG.info("Start test-method updateArticleTest");
Article beforeUpdateArticle = contentService.listArticles(new PageRequest(0, 6)).getContent().get(0);
contentService.updateArticle(beforeUpdateArticle.getId(), "new slug for update test", "title for update test", "<h2>Body for update test</h2");
Article afterUpdateArticle = contentService.listArticles(new PageRequest(0, 6)).getContent().get(0);
assertFalse(beforeUpdateArticle.getBody().equals(afterUpdateArticle.getBody()));
assertFalse(beforeUpdateArticle.getTitle().equals(afterUpdateArticle.getTitle()));
assertFalse(beforeUpdateArticle.getSlug().equals(afterUpdateArticle.getSlug()));
LOG.info("Test updateArticleTest is passed!");
}
@Test
public void createGlobalArticleTest(){
LOG.info("Start test-method createGlobalArticleTest");
String slug = "create_global_article";
Locale locale = Locale.ENGLISH;
String title = "Create Global Article";
String body = "<h1>Create Global Article</h1>";
Article globalArticle = contentService.createGlobalArticle(slug, locale, title, body);
assertNotNull(globalArticle);
assertTrue(contentService.listArticles(new PageRequest(0, 6)).getContent().size() == 2);
LOG.info("Test createGlobalArticleTest is passed!");
}
@Test
public void updateGlobalArticleTest(){
LOG.info("Start test-method updateGlobalArticleTest");
Locale locale = Locale.ENGLISH;
String title = "Update Global Article Title";
String body = "<h1>Update Global Article Body</h1>";
Article globalArticle = contentService.updateGlobalArticle("smtp.email-verification", locale, title, body);
assertTrue(contentService.listArticles(new PageRequest(0, 6)).getContent().size() == 1);
assertTrue(globalArticle.getBody().equals("Update Global Article Body"));
assertTrue(globalArticle.getTitle().equals("Update Global Article Title"));
LOG.info("Test updateGlobalArticleTest is passed!");
}
@Test
public void ensureClassPKInternalTest(){
LOG.info("Start test-method ensureClassPKInternalTest");
assertNotNull(contentService.ensureClassPK(Article.class));
LOG.info("Test ensureClassPKInternalTest is passed!");
}
@Test
public void getActivityPostTest(){
LOG.info("Start test-method getActivityPostTest");
ActivityPost activityPost = postRepository.findAll().get(0);
assertNotNull(contentService.getActivityPost(activityPost.getId()));
LOG.info("Test getActivityPostTest is passed!");
}
@Test
public void createActivityPostTest(){
LOG.info("Start test-method createActivityPostTest");
assertNotNull(contentService.createActivityPost("title for new activity post", "body of new activity post"));
assertTrue(postRepository.findAll().size() == 2);
LOG.info("Test createActivityPostTest is passed!");
}