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

mockito in test scope, make tests pass

parent c4094c0a
...@@ -483,6 +483,7 @@ ...@@ -483,6 +483,7 @@
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId> <artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version> <version>2.0.2-beta</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
package org.genesys2.unit; package org.genesys2.unit;
import static org.junit.Assert.assertTrue;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.genesys2.server.exception.UserException; import org.genesys2.server.exception.UserException;
...@@ -27,72 +29,72 @@ import org.springframework.test.context.ActiveProfiles; ...@@ -27,72 +29,72 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AuthUserDetailsServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class) @ContextConfiguration(classes = AuthUserDetailsServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev") @ActiveProfiles("dev")
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class AuthUserDetailsServiceTest { public class AuthUserDetailsServiceTest {
private final Log LOG = LogFactory.getLog(AuthUserDetailsServiceTest.class); private final Log LOG = LogFactory.getLog(AuthUserDetailsServiceTest.class);
@Import({JpaDataConfig.class, HazelcastConfig.class}) @Import({ JpaDataConfig.class, HazelcastConfig.class })
@PropertySource({"classpath:application.properties", "classpath:spring/spring.properties"}) @PropertySource({ "classpath:application.properties", "classpath:spring/spring.properties" })
public static class Config { public static class Config {
@Bean @Bean
public UserDetailsService userDetailsService() { public UserDetailsService userDetailsService() {
return new AuthUserDetailsService(); return new AuthUserDetailsService();
} }
@Bean @Bean
public UserService userService() { public UserService userService() {
return new UserServiceImpl(); return new UserServiceImpl();
} }
@Bean @Bean
public CacheManager cacheManager() { public CacheManager cacheManager() {
return new NoOpCacheManager(); return new NoOpCacheManager();
} }
} }
@Autowired @Autowired
private UserDetailsService userDetailsService; private UserDetailsService userDetailsService;
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired @Autowired
private UserPersistence userPersistence; private UserPersistence userPersistence;
@Test @Test
public void loadUserByUsernameTest() { public void loadUserByUsernameTest() {
LOG.info("Start test-method loadUserByUsernameTest"); LOG.info("Start test-method loadUserByUsernameTest");
User user = new User(); User user = new User();
user.setEmail("email@mail.com"); user.setEmail("email@mail.com");
user.setName("UserName"); user.setName("UserName");
user.setPassword("UserPassword"); user.setPassword("UserPassword");
try { try {
userService.addUser(user); userService.addUser(user);
UserDetails userDetails = userDetailsService.loadUserByUsername(user.getEmail()); UserDetails userDetails = userDetailsService.loadUserByUsername(user.getEmail());
assertTrue(userDetails != null); assertTrue(userDetails != null);
assertTrue(userDetails.getPassword().equals(user.getPassword())); assertTrue(userDetails.getPassword().equals(user.getPassword()));
} catch (UserException e) { userService.removeUser(user);
e.printStackTrace();
} } catch (UserException e) {
org.junit.Assert.fail(e.getMessage());
}
LOG.info("Test loadUserByUsernameTest passed!"); LOG.info("Test loadUserByUsernameTest passed!");
} }
@Test(expected = UsernameNotFoundException.class) @Test(expected = UsernameNotFoundException.class)
public void loadUserByUsernameTestUserNotFound() { public void loadUserByUsernameTestUserNotFound() {
LOG.info("Start test-method 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; ...@@ -31,6 +31,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -87,22 +88,9 @@ public class ContentSanitizerTest { ...@@ -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 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>"; 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(); contentSanitizer.sanitizeAll();
Article articleAfterSanitize = contentService.listArticles(new PageRequest(0, 10)).getContent().get(0); Article articleAfterSanitize = contentService.listArticles(new PageRequest(0, 10)).getContent().get(0);
......
...@@ -210,10 +210,11 @@ public class EmailVerificationServiceTest { ...@@ -210,10 +210,11 @@ public class EmailVerificationServiceTest {
@Test @Test
public void sendPasswordResetEmailTest() { public void sendPasswordResetEmailTest() {
LOG.info("Start test-method 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"); Mockito.doNothing().when(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", user.getEmail());
emailVerificationService.sendPasswordResetEmail(userService.listUsers(new PageRequest(0, 1)).getContent().get(0)); emailVerificationService.sendPasswordResetEmail(user);
Mockito.verify(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", "salexandrbasov@gmail.com"); Mockito.verify(eMailService).sendMail("title for twoArticle", "<h1>Tho BOdy of twoArticle</h1", user.getEmail());
LOG.info("Test sendPasswordResetEmailTest passed!"); 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