package at.fhj.itm.beans;
import java.util.HashMap;
import junit.framework.Assert;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import at.fhj.itm.business.ServiceAssembler;
import at.fhj.itm.business.ServiceException;
import at.fhj.itm.business.ServiceTrip;
import at.fhj.itm.business.ServiceUser;
import at.fhj.itm.util.EmailUtil;
import at.fhj.itm.util.GoogleUtil;
import at.fhj.itm.util.JsfUtil;
import at.fhj.itm.util.RandomUtil;
/**
*
* @author Astrid Wiesenhfoer
* @purpose JUnit Test for the following methods of the class Forgot Passoword:
* recoverPasswordButtonClicked, isResettingAllowed, setNewPasswordButtonClicked
*/
public class ForgotPasswordTest
{
private ServiceUser mockServiceUser;
private JsfUtil mockJsfUtil;
/**
* Initiates the needed mocks for the tests
* and creates a Service Assembler
*/
@Before
public void setUp()
{
mockServiceUser = EasyMock.createMock(ServiceUser.class);
mockJsfUtil = EasyMock.createMock(JsfUtil.class);
ServiceAssembler serviceAssembler = new ServiceAssembler() {
@Override
public ServiceUser createServiceUser() {
return mockServiceUser;
}
@Override
public ServiceTrip createServiceTrip() {
throw new UnsupportedOperationException("createServiceTrip()");
}
@Override
public JsfUtil createJsfUtil() {
return mockJsfUtil;
}
@Override
public GoogleUtil createGoogleUtil() {
throw new UnsupportedOperationException();
}
@Override
public RandomUtil createRandomUtil()
{
throw new UnsupportedOperationException("createRandomUtil()");
}
@Override
public EmailUtil createEmailUtil()
{
throw new UnsupportedOperationException("createEmailUtil()");
}
};
ServiceAssembler.setServiceAssembler(serviceAssembler);
}
/**
* tests the recoverPasswordButtonClicked method
* in this case everthing is correct and should work fine
*/
@Test
public void testRecoverPasswordButtonClicked()
{
//SetUp input data
String email = "email";
String phonenumber = "phonenumber";
//SetUp mocks
EasyMock.expect(mockServiceUser.recoverUserPassword(email, phonenumber)).andReturn("sessionID");
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setEmail(email);
forgotPassword.setPhonenumber(phonenumber);
String returnValue = forgotPassword.recoverPasswordButtonClicked();
//Verify
Assert.assertEquals("userFound", returnValue);
EasyMock.verify(mockServiceUser);
}
/**
* tests the recoverPasswordButtonClicked method
* in this case no user with right email or phonenumber
* was found
*/
@Test
public void testRecoverPasswordButtonClickedNoSessionIDFound()
{
//SetUp input data
String email = "email";
String phonenumber = "phonenumber";
//SetUp mocks
EasyMock.expect(mockServiceUser.recoverUserPassword(email, phonenumber)).andReturn(null);
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setEmail(email);
forgotPassword.setPhonenumber(phonenumber);
String returnValue = forgotPassword.recoverPasswordButtonClicked();
//Verify
Assert.assertEquals("noUserFound", returnValue);
EasyMock.verify(mockServiceUser);
}
/**
* tests the recoverPasswordButtonClicked method
* in this case the try and catch block throws the Service Exception
*/
@Test
public void testRecoverPasswordButtonClickedServiceException()
{
//SetUp input data
String email = "email";
String phonenumber = "phonenumber";
//SetUp mocks
EasyMock.expect(mockServiceUser.recoverUserPassword(email, phonenumber)).andThrow(new ServiceException("ServiceException"));
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setEmail(email);
forgotPassword.setPhonenumber(phonenumber);
String returnValue = forgotPassword.recoverPasswordButtonClicked();
//Verify
Assert.assertEquals("errorDatabase", returnValue);
EasyMock.verify(mockServiceUser);
}
/**
* tests the isResettingAllowd method
* in this case everthing is correct and should work fine
*/
@Test
public void testIsResettingAllowed()
{
//SetUp input data
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.replay(mockJsfUtil);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
boolean returnValue = forgotPassword.isResettingAllowed();
//Verify
Assert.assertTrue(returnValue);
EasyMock.verify(mockJsfUtil);
}
/**
* tests the isResettingAllowd method
* in this case getRequestParameterMap() returns null
* so the return value is FALSE
*/
@Test
public void testIsResettingAllowedSessionIdIsNull()
{
//SetUp input data
HashMap<String, String> map = new HashMap<String, String>();
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.replay(mockJsfUtil);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
boolean returnValue = forgotPassword.isResettingAllowed();
//Verify
Assert.assertFalse(returnValue);
EasyMock.verify(mockJsfUtil);
}
/**
* tests the isResettingAllowd method
* in this case the sessIdFromLink is TRUE and sessId is set
* so the return value is TRUE
*/
@Test
public void testIsResettingAllowedIfIsFalse()
{
//SetUp input data
String sessId = "sessId";
boolean sessIdFromLink = true;
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setSessId(sessId, sessIdFromLink);
boolean returnValue = forgotPassword.isResettingAllowed();
//Verify
Assert.assertTrue(returnValue);
}
/**
* tests the isResettingAllowd method
* in this case the sessId is set but the sessIdFromLink is FALSE
* so the return value is TRUE
*/
@Test
public void testIsResettingAllowedSessIdNotNull()
{
//SetUp input data
String sessId = "sessId";
boolean sessIdFromLink = false;
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.replay(mockJsfUtil);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setSessId(sessId, sessIdFromLink);
boolean returnValue = forgotPassword.isResettingAllowed();
//Verify
Assert.assertTrue(returnValue);
EasyMock.verify(mockJsfUtil);
}
/**
* tests the isResettingAllowd method
* in this case sessId is NULL and sessIdfromLink is TRUE
* so the return value is TRUE
*/
@Test
public void testIsResettingAllowedSessIdFromLinkIsTrue()
{
//SetUp input data
String sessId = null;
boolean sessIdFromLink = true;
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.replay(mockJsfUtil);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setSessId(sessId, sessIdFromLink);
boolean returnValue = forgotPassword.isResettingAllowed();
//Verify
Assert.assertTrue(returnValue);
EasyMock.verify(mockJsfUtil);
}
/**
* tests the SetNewPasswordButtonClicked method
* in this case everthing is correct and should work fine
*/
@Test
public void testSetNewPasswordButtonClicked()
{
//SetUp input data
String newPassword = "newPassword";
String newPassword2 = "newPassword";
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.expect(mockServiceUser.setPassword(newPassword, "sessId")).andReturn(true);
EasyMock.replay(mockJsfUtil);
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setNewPassword(newPassword);
forgotPassword.setNewPassword2(newPassword2);
String returnValue = forgotPassword.setNewPasswordButtonClicked();
//Verify
Assert.assertEquals("passwordSet", returnValue);
EasyMock.verify(mockJsfUtil);
EasyMock.verify(mockServiceUser);
}
/**
* tests the isResettingAllowd method
* in this case the ServiceUser will throw a Service Exception
*/
@Test
public void testSetNewPasswordButtonClickedServiceExeption()
{
//SetUp input data
String newPassword = "newPassword";
String newPassword2 = "newPassword";
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.expect(mockServiceUser.setPassword(newPassword, "sessId")).andThrow(new ServiceException("ServiceExeption"));
EasyMock.replay(mockJsfUtil);
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setNewPassword(newPassword);
forgotPassword.setNewPassword2(newPassword2);
String returnValue = forgotPassword.setNewPasswordButtonClicked();
//Verify
Assert.assertEquals("errorDatabase", returnValue);
EasyMock.verify(mockJsfUtil);
EasyMock.verify(mockServiceUser);
}
/**
* tests the isResettingAllowd method
* in this case the new passwords given do NOT match
*/
@Test
public void testSetNewPasswordButtonClickedPasswordsNotEqual()
{
//SetUp input data
String newPassword = "newPassword";
String newPassword2 = "notnewPassword";
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setNewPassword(newPassword);
forgotPassword.setNewPassword2(newPassword2);
String returnValue = forgotPassword.setNewPasswordButtonClicked();
//Verify
Assert.assertEquals("passwordNotSet", returnValue);
}
/**
* tests the isResettingAllowd method
* in this case success is FALSE
*/
@Test
public void testSetNewPasswordButtonClickedSuccessIsFalse()
{
//SetUp input data
String newPassword = "newPassword";
String newPassword2 = "newPassword";
HashMap<String, String> map = new HashMap<String, String>();
map.put("sessId", "sessId");
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.expect(mockServiceUser.setPassword(newPassword, "sessId")).andReturn(false);
EasyMock.replay(mockJsfUtil);
EasyMock.replay(mockServiceUser);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setNewPassword(newPassword);
forgotPassword.setNewPassword2(newPassword2);
String returnValue = forgotPassword.setNewPasswordButtonClicked();
//Verify
Assert.assertEquals("passwordNotSet", returnValue);
EasyMock.verify(mockJsfUtil);
EasyMock.verify(mockServiceUser);
}
/**
* tests the isResettingAllowd method
* in this case case getRequestParameterMap returns NULL
*/
@Test
public void testSetNewPasswordButtonClickedResettingNotAllowed()
{
//SetUp input data
String newPassword = "newPassword";
String newPassword2 = "newPassword";
HashMap<String, String> map = new HashMap<String, String>();
//SetUp mocks
EasyMock.expect(mockJsfUtil.getRequestParameterMap()).andReturn(map);
EasyMock.replay(mockJsfUtil);
//Do Test
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setNewPassword(newPassword);
forgotPassword.setNewPassword2(newPassword2);
String returnValue = forgotPassword.setNewPasswordButtonClicked();
//Verify
Assert.assertEquals("passwordNotSet", returnValue);
EasyMock.verify(mockJsfUtil);
}
}