Commit 413f1df0 authored by Matija Obreza's avatar Matija Obreza

mockito in test scope, make tests pass

parent c4094c0a
......@@ -483,6 +483,7 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
</dependencies>
......
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;
......@@ -27,72 +29,72 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AuthUserDetailsServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class AuthUserDetailsServiceTest {
private final Log LOG = LogFactory.getLog(AuthUserDetailsServiceTest.class);
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 {
@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 UserDetailsService userDetailsService() {
return new AuthUserDetailsService();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
}
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
}
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private UserService userService;
@Autowired
private UserService userService;
@Autowired
private UserPersistence userPersistence;
@Autowired
private UserPersistence userPersistence;
@Test
public void loadUserByUsernameTest() {
LOG.info("Start test-method loadUserByUsernameTest");
@Test
public void loadUserByUsernameTest() {
LOG.info("Start test-method loadUserByUsernameTest");
User user = new User();
user.setEmail("email@mail.com");
user.setName("UserName");
user.setPassword("UserPassword");
User user = new User();
user.setEmail("email@mail.com");
user.setName("UserName");
user.setPassword("UserPassword");
try {
userService.addUser(user);
UserDetails userDetails = userDetailsService.loadUserByUsername(user.getEmail());
assertTrue(userDetails != null);
assertTrue(userDetails.getPassword().equals(user.getPassword()));
} catch (UserException e) {
e.printStackTrace();
}
try {
userService.addUser(user);
UserDetails userDetails = userDetailsService.loadUserByUsername(user.getEmail());
assertTrue(userDetails != null);
assertTrue(userDetails.getPassword().equals(user.getPassword()));
userService.removeUser(user);
} catch (UserException e) {
org.junit.Assert.fail(e.getMessage());
}
LOG.info("Test loadUserByUsernameTest passed!");
}
LOG.info("Test loadUserByUsernameTest passed!");
}
@Test(expected = UsernameNotFoundException.class)
public void loadUserByUsernameTestUserNotFound() {
LOG.info("Start test-method loadUserByUsernameTestUserNotFound");
@Test(expected = UsernameNotFoundException.class)
public void loadUserByUsernameTestUserNotFound() {
LOG.info("Start test-method loadUserByUsernameTestUserNotFound");
assertTrue(null == userDetailsService.loadUserByUsername("WrongMail"));
assertTrue(null == userDetailsService.loadUserByUsername("WrongMail"));
LOG.info("Test loadUserByUsernameTestUserNotFound passed!");
}
LOG.info("Test loadUserByUsernameTestUserNotFound passed!");
}
}
......@@ -31,6 +31,7 @@ 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;
......@@ -87,22 +88,9 @@ public class ContentSanitizerTest {
String articleBody = "<script></script><h2><small>Genesys account</small><br />Verify your email address</h2><p>You can already use your Genesys account. We need to confirm your email account before granting you access to all Genesys features.</p><p><a href=\"{0}/profile/{1}/validate\" rel=\"nofollow\">Verify {2}</a></p><h2>Validation key: {3}</h2><p>If you didn&#39;t make this request, <a href=\"{0}/profile/{1}/cancel\" rel=\"nofollow\">click here to cancel</a>.</p><p>Thanks,<br />Genesys team</p>";
String expectedBody = "<h2><small>Genesys account</small><br />Verify your email address</h2><p>You can already use your Genesys account. We need to confirm your email account before granting you access to all Genesys features.</p><p><a href=\"{0}/profile/{1}/validate\" rel=\"nofollow\">Verify {2}</a></p><h2>Validation key: {3}</h2><p>If you didn&#39;t make this request, <a href=\"{0}/profile/{1}/cancel\" rel=\"nofollow\">click here to cancel</a>.</p><p>Thanks,<br />Genesys team</p>";
Article article = new Article();
article.setBody(articleBody);
ClassPK classPK = new ClassPK();
classPK.setClassName(Article.class.getName());
classPkRepository.save(classPK);
article.setClassPk(classPK);
article.setLang("en");
article.setSlug("smtp.email-verification");
List<Article> articles = new ArrayList<>();
articles.add(article);
contentService.save(articles);
contentService.createGlobalArticle("smtp.email-verification", Locale.ENGLISH, "Test title", articleBody);
contentSanitizer.sanitizeAll();
Article articleAfterSanitize = contentService.listArticles(new PageRequest(0, 10)).getContent().get(0);
......
......@@ -210,10 +210,11 @@ public class EmailVerificationServiceTest {
@Test
public void sendPasswordResetEmailTest() {
LOG.info("Start test-method sendPasswordResetEmailTest");
User user=userService.listUsers(new PageRequest(0, 1)).getContent().get(0);
Mockito.doNothing().when(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", "salexandrbasov@gmail.com");
emailVerificationService.sendPasswordResetEmail(userService.listUsers(new PageRequest(0, 1)).getContent().get(0));
Mockito.verify(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", "salexandrbasov@gmail.com");
Mockito.doNothing().when(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", user.getEmail());
emailVerificationService.sendPasswordResetEmail(user);
Mockito.verify(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", user.getEmail());
LOG.info("Test sendPasswordResetEmailTest passed!");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment