rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mpie...@apache.org
Subject svn commit: r1152748 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/model/ main/java/org/apache/rave/portal/web/controller/ main/java/org/apache/rave/portal/web/util/ main/java/org/apache/rave/portal/web/validator/ main/re...
Date Mon, 01 Aug 2011 12:02:06 GMT
Author: mpierce
Date: Mon Aug  1 12:02:04 2011
New Revision: 1152748

URL: http://svn.apache.org/viewvc?rev=1152748&view=rev
Log:
(RAVE-124) Applying patch to provide improved new user validation.  Patch also includes improved
unit tests for the new account controller.

Added:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/NewUser.java
    incubator/rave/trunk/rave-portal/src/main/resources/messages.properties
    incubator/rave/trunk/rave-portal/src/main/webapp/META-INF/MANIFEST.MF
Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/NewAccountController.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ModelKeys.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ViewNames.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
    incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext.xml
    incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/newaccount.jsp
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/NewAccountControllerTest.java

Added: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/NewUser.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/NewUser.java?rev=1152748&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/NewUser.java
(added)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/NewUser.java
Mon Aug  1 12:02:04 2011
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rave.portal.model;
+
+/**
+ * {@inheritDoc}
+ *
+ * A new user registering into the system
+ */
+
+public class NewUser {
+	private String username;
+	
+	private String password;
+	
+	private String confirmPassword;
+
+	public NewUser(){
+	}
+	
+	public String getUsername() {
+		return username;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+
+	public String getConfirmPassword() {
+		return confirmPassword;
+	}
+
+	public void setConfirmPassword(String confirmPassword) {
+		this.confirmPassword = confirmPassword;
+	}
+	
+}
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/NewAccountController.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/NewAccountController.java?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/NewAccountController.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/NewAccountController.java
Mon Aug  1 12:02:04 2011
@@ -19,8 +19,12 @@
 
 package org.apache.rave.portal.web.controller;
 
-import org.apache.rave.portal.model.User;
+import java.util.List;
+
+import org.apache.rave.portal.model.NewUser;
 import org.apache.rave.portal.service.NewAccountService;
+import org.apache.rave.portal.web.util.ModelKeys;
+import org.apache.rave.portal.web.util.ViewNames;
 import org.apache.rave.portal.web.validator.NewAccountValidator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -29,6 +33,7 @@ import org.springframework.stereotype.Co
 import org.springframework.ui.Model;
 import org.springframework.ui.ModelMap;
 import org.springframework.validation.BindingResult;
+import org.springframework.validation.ObjectError;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -53,40 +58,45 @@ public class NewAccountController {
     @RequestMapping(value ="/newaccount.jsp")
 	 public void setUpForm(ModelMap model) {
 		  logger.debug("Initializing form");
-		  //TODO this should use view keys like other pages.
-		  model.addAttribute("newUser",new User());
+		  //TODO this should use view keys like other pages. (done)
+		  model.addAttribute(ModelKeys.NEW_USER,new NewUser());
 	 }
 
     @RequestMapping(value = { "/newaccount","/newacount/*"}, method = RequestMethod.POST)
-		  public String create(@ModelAttribute User user, BindingResult results, Model model,@RequestParam
String username, @RequestParam String password){
+		  public String create(@ModelAttribute NewUser newUser, BindingResult results, Model model,@RequestParam
String username, @RequestParam String password){
 		  logger.debug("Creating a new user account");
-		  model.addAttribute("newUser",user);
+		  model.addAttribute(ModelKeys.NEW_USER,newUser);
 		  
-		  newAccountValidator.validate(user,results);
+		  newAccountValidator.validate(newUser,results);
 		  if(results.hasErrors()){
-				return "newaccount";
+			  logger.error("newaccount.jsp: shows validation errors");
+			  //TODO: change this to a viewname (done)
+			  return ViewNames.NEW_ACCOUNT;
 		  }
 
 		  //Now attempt to create the account.
 		  try {
-				newAccountService.createNewAccount(username,password);
-				return "redirect:/";
+			    logger.debug("newaccount.jsp: passed form validation");
+			    
+			    newAccountService.createNewAccount(username,password);
+			    //TODO: change this to a viewname (done)
+				return ViewNames.REDIRECT;
 		  }
 		  
 		  catch (org.springframework.dao.IncorrectResultSizeDataAccessException ex) {
 				//This exception is thrown if the account already exists.
 				logger.error("Account creation failed: "+ex.getMessage());
 				results.reject("Account already exists","Unable to create account");
-				//TODO: change this to a viewname
-				return "newaccount";
+				//TODO: change this to a viewname (done)
+				return ViewNames.NEW_ACCOUNT;
 				
 		  }
 		  //TODO need to handle more specific exceptions
 		  catch (Exception ex) {
 				logger.error("Account creation failed: "+ex.getMessage());
 				results.reject("Unable to create account:"+ex.getMessage(),"Unable to create account");
-				//TODO: change this to a viewname
-				return "newaccount";
+				//TODO: change this to a viewname (done)
+				return ViewNames.NEW_ACCOUNT;
 		  }
 		  
 	 }

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ModelKeys.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ModelKeys.java?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ModelKeys.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ModelKeys.java
Mon Aug  1 12:02:04 2011
@@ -30,4 +30,5 @@ public class ModelKeys {
     public static final String WIDGET = "widget";
     public static final String REFERRING_PAGE_ID = "referringPageId";
     public static final String OPENSOCIAL_ENVIRONMENT = "openSocialEnv";
+    public static final String NEW_USER = "newUser"; //a new user instance created
 }
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ViewNames.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ViewNames.java?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ViewNames.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/util/ViewNames.java
Mon Aug  1 12:02:04 2011
@@ -27,4 +27,6 @@ public class ViewNames {
     public static final String HOME = "home";
     public static final String STORE = "store";
     public static final String WIDGET = "widget";
+    public static final String NEW_ACCOUNT = "newaccount";
+    public static final String REDIRECT = "redirect:/";
 }

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
Mon Aug  1 12:02:04 2011
@@ -19,21 +19,90 @@
 //TODO is this the right package name convention?
 package org.apache.rave.portal.web.validator;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.rave.portal.model.NewUser;
+import org.apache.rave.portal.service.UserService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.Errors;
+import org.springframework.validation.ObjectError;
 import org.springframework.validation.Validator;
 
-
 public class NewAccountValidator implements Validator {
 
 	 protected final Logger logger=LoggerFactory.getLogger(getClass());
+	 
+	 private UserService userService;
 
+	 @Autowired
+	 public NewAccountValidator(UserService userService) {
+		 this.userService = userService;
+	 }
+	 
 	 public boolean supports(Class aClass){
 		  return NewAccountValidator.class.equals(aClass);
 	 }
 	 
 	 public void validate(Object obj, Errors errors){
 		  logger.debug("Validator called");
+		  NewUser newUser = (NewUser) obj;
+		  List<ObjectError> errorList = new ArrayList<ObjectError>();
+		  
+		  //check if the username is null
+		  if(newUser.getUsername() == "") {
+			  errors.rejectValue("username", "username.required");
+			  logger.error("Username required");
+		  }
+		  
+		  //check if username length is less than 2
+		  else if(newUser.getUsername().length() < 2) {
+			  errors.rejectValue("username", "username.invalid.length");
+			  logger.error("Username must be atleast 2 characters long");
+		  }
+		  
+		  //check if username is already in use
+		  
+		  else if(userService.getUserByUsername(newUser.getUsername()) != null) {
+			  errors.rejectValue("username", "username.exits");
+			  logger.error("Username already exists");
+		  }
+		  
+		  
+		  //check if the password is null
+		  if(newUser.getPassword() == "") {
+			  errors.rejectValue("password", "password.required");
+			  logger.error("Password required");
+		  }
+		  
+		  //check if the password length is less than 4
+		  else if(newUser.getPassword().length() < 4) {
+			  errors.rejectValue("password", "password.invalid.lenght");
+			  logger.error("Password must be atleast 4 characters long");
+		  }
+		  
+		  //check if the confirm password is null
+		  if(newUser.getConfirmPassword() == "") {
+			  errors.rejectValue("confirmPassword", "confirmPassword.required");
+			  logger.error("Confirm Password required");
+		  }
+		  
+		  //check if the confirm password matches the previous entered password
+		  if(newUser.getConfirmPassword().length() != newUser.getPassword().length() || newUser.getConfirmPassword().compareTo(newUser.getPassword())
!= 0) {
+			  errors.rejectValue("confirmPassword", "confirmPassword.mismatch");
+			  logger.error("Password mismatch");
+		  }
+
+		  if(errors.hasErrors()){
+			  errorList = errors.getAllErrors();
+			  for (ObjectError error : errorList) {
+				  logger.error("Validation error: " + error.toString());
+			  }
+		  }
+		  else {
+			  logger.debug("Validation successful");
+		  }
 	 }
 }
\ No newline at end of file

Added: incubator/rave/trunk/rave-portal/src/main/resources/messages.properties
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/resources/messages.properties?rev=1152748&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/resources/messages.properties (added)
+++ incubator/rave/trunk/rave-portal/src/main/resources/messages.properties Mon Aug  1 12:02:04
2011
@@ -0,0 +1,9 @@
+username.required=Username required
+username.invalid.length=Username must be atleast 2 characters long
+username.exits=Username already exists
+
+password.required=Password required
+password.invalid.lenght=Password must be atleast 4 characters long
+
+confirmPassword.required=Confirm Password required
+confirmPassword.mismatch=Password mismatch
\ No newline at end of file

Added: incubator/rave/trunk/rave-portal/src/main/webapp/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/META-INF/MANIFEST.MF?rev=1152748&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/META-INF/MANIFEST.MF (added)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/META-INF/MANIFEST.MF Mon Aug  1 12:02:04
2011
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path: 
+

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext.xml?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext.xml (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext.xml Mon Aug
 1 12:02:04 2011
@@ -96,5 +96,9 @@
         <property name="engineRoot" value="${portal.opensocial_engine.root}"/>
         <property name="engineGadgetPath" value="${portal.opensocial_engine.gadget_path}"/>
     </bean>
-	 <bean id="newAccountValidator" class="org.apache.rave.portal.web.validator.NewAccountValidator"/>
+	<bean id="newAccountValidator" class="org.apache.rave.portal.web.validator.NewAccountValidator"/>
+	
+	<!-- Configuring messages.properties file --> 
+	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
+
 </beans>
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/newaccount.jsp
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/newaccount.jsp?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/newaccount.jsp (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/views/newaccount.jsp Mon Aug
 1 12:02:04 2011
@@ -27,38 +27,33 @@
     <form:form id="newAccountForm" commandName="newUser" action="newaccount" method="POST">
         <fieldset>
             <p>All fields are required</p>
-				<p><form:errors cssClass="error2"/>
+				<p><form:errors cssClass="error"/>
             <p>
                 <label for="userNameField">Username:</label>
                 <form:input id="userNameField" type="text" path="username" required="required"
autofocus="autofocus"/>
+                <form:errors path="username" cssClass="error" />
             </p>
             <p>
                 <label for="passwordField">Password:</label>
                 <form:input id="passwordField" type="password" path="password" required="required"/>
+            	<form:errors path="password" cssClass="error" />
             </p>
             <p>
                 <label for="passwordConfirmField">Confirm Password:</label>
-                <input id="passwordConfirmField" type="password" name="passwordConfirm"
required="required"/>
+                <form:input id="passwordConfirmField" type="password" path="confirmPassword"
required="required"/>
+            	<form:errors path="confirmPassword" cssClass="error" />
             </p>
         </fieldset>
         <fieldset>
              <input type="submit" value="Create Account" />
         </fieldset>
     </form:form>
-    <form id="loginForm" action="/portal/login.jsp" method="get">
-	<fieldset>
-		<p>
-			<label for="navigateToLoginPageButton">Already Registered? </label>
-			<input id="navigateToLoginPageButton" type="submit" value="User Login" />
-		</p>
-	</fieldset>
-    </form>
 </div>
 <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
 <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js"></script>
 <script src="<spring:url value="/script/rave.js"/>"></script>
 <script src="<spring:url value="/script/rave_forms.js"/>"></script>
 
-<script>$(document).ready(rave.forms.validateNewAccountForm());</script>
+<!--  <script>$(document).ready(rave.forms.validateNewAccountForm());</script>
-->
 </rave:rave_generic_page>
-  
+  
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/NewAccountControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/NewAccountControllerTest.java?rev=1152748&r1=1152747&r2=1152748&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/NewAccountControllerTest.java
(original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/NewAccountControllerTest.java
Mon Aug  1 12:02:04 2011
@@ -19,10 +19,358 @@
 
 package org.apache.rave.portal.web.controller;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.rave.portal.model.NewUser;
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.service.NewAccountService;
+import org.apache.rave.portal.service.UserService;
+import org.apache.rave.portal.web.util.ModelKeys;
+import org.apache.rave.portal.web.util.ViewNames;
+import org.apache.rave.portal.web.validator.NewAccountValidator;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.ui.Model;
+import org.springframework.ui.ModelMap;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.Errors;
+import org.springframework.validation.ObjectError;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertThat;
+
+
 /**
  * This is a test class for NewAccountController, which is used to make new user accounts
through
  * a web form.
  */
 public class NewAccountControllerTest {
 	 //Tests need to be implemented.
+	private NewAccountController newAccountController;
+	private NewAccountService newAccountService;
+	private NewAccountValidator newAccountValidator;
+	private UserService userService;
+	
+	@Before
+	public void setup() {
+		newAccountService = createNiceMock(NewAccountService.class);
+		userService = createNiceMock(UserService.class);
+		newAccountValidator = new NewAccountValidator(userService);
+		newAccountController = new NewAccountController(newAccountService, newAccountValidator);
+	}
+	
+	@Test
+	public void setUpForm_ShouldAddAttributeForNewUser() {
+		final ModelMap model = new ModelMap();
+		String newUser = new String(ModelKeys.NEW_USER);
+		newAccountController.setUpForm(model);
+		
+		//assert that the model is not null
+		assertThat(model, CoreMatchers.notNullValue());
+		
+		//assert that the model size is one
+		assertThat(model.size(), CoreMatchers.equalTo(1));
+		
+		//assert that the model does contain an attribute associated with newUser after setUpForm()
is called
+		assertThat(model.containsAttribute(newUser), CoreMatchers.equalTo(true)); 
+		
+		//assert that the model does not contain new user as null
+		assertThat(model.get(newUser), CoreMatchers.notNullValue());
+	}
+	
+	@Test
+	public void create_UsernameNotSpecified() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = ""; //no username specified
+		final String password = "password";
+		final String confirmPassword = password;
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+		final ObjectError error = new ObjectError("username.required", "Username required");
+		
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(error);
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username required"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_UsernameAlreadyExists() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "canonical"; //specified username already exists in database
+		final String password = "password";
+		final String confirmPassword = password;
+		final User existingUser = new User();
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+		final ObjectError error = new ObjectError("username.exists", "Username already exists");
+		
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		existingUser.setUsername(username);
+		existingUser.setPassword(password);
+		
+		errorList.add(error);
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		expect(userService.getUserByUsername(username)).andReturn(existingUser).anyTimes();
+		replay(userService);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username already
exists"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+		
+	@Test
+	public void create_InvalidUsernameLength() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "u"; //username length less than 2 characters
+		final String password = "password";
+		final String confirmPassword = password;
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+		final ObjectError error = new ObjectError("username.invalid.length", "Username must be
atleast 2 characters long");
+		
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(error);
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username must be
atleast 2 characters long"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_PasswordNotSpecified() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "username"; 
+		final String password = ""; //password not specified
+		final String confirmPassword = password;
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("password.required", "Password required"));
+		errorList.add(new ObjectError("confirmPassword.required", "Confirm password required"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(2));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password required"));
+		assertThat(errorList.get(1).getDefaultMessage(), CoreMatchers.equalTo("Confirm password
required"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_ConfirmPasswordNotSpecified() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "usename"; 
+		final String password = "pasword";
+		final String confirmPassword = ""; //confirm password not specified
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("confirmPassword.required", "Confirm password required"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Confirm password
required"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_InvalidPasswordLength() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "usename"; 
+		final String password = "pas"; //invalid length password
+		final String confirmPassword = password;
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("password.invalid.length", "Password must be atleast 4 characters
long"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password must be
atleast 4 characters long"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_PasswordMismatchCaseOne() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "username"; 
+		final String password = "password";
+		final String confirmPassword = "passwor"; //confirm password not of same length as password
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("confirmPassword.mismatch", "Password mismatch"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password mismatch"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_PasswordMismatchCaseTwo() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "username"; 
+		final String password = "password";
+		final String confirmPassword = "passwodr"; //confirm password mistyped
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("confirmPassword.mismatch", "Password mismatch"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(1));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password mismatch"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_BlankFormSubmitted() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = ""; //Username not specified
+		final String password = ""; //Password not specified
+		final String confirmPassword = ""; //Confirm password not specified
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		errorList.add(new ObjectError("username.required", "Username required"));
+		errorList.add(new ObjectError("password.required", "Password required"));
+		errorList.add(new ObjectError("confirmPassword.required", "Confirm password required"));
+		
+		expect(errors.hasErrors()).andReturn(true).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(3));
+		assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username required"));
+		assertThat(errorList.get(1).getDefaultMessage(), CoreMatchers.equalTo("Password required"));
+		assertThat(errorList.get(2).getDefaultMessage(), CoreMatchers.equalTo("Confirm password
required"));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
+	}
+	
+	@Test
+	public void create_ValidFormSubmitted() {
+		final Model model = createNiceMock(Model.class);
+		final NewUser newUser = new NewUser();
+		final BindingResult errors = createNiceMock(BindingResult.class);
+		final String username = "username"; //Username not specified
+		final String password = "password"; //Password not specified
+		final String confirmPassword = password; //Confirm password not specified
+		List<ObjectError> errorList = new ArrayList<ObjectError>();
+				
+		newUser.setUsername(username);
+		newUser.setPassword(password);
+		newUser.setConfirmPassword(confirmPassword);
+		
+		expect(errors.hasErrors()).andReturn(false).anyTimes();		
+		expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
+		replay(errors);
+		
+		String result = new String(newAccountController.create(newUser, errors, model, username,
password));
+		errorList = errors.getAllErrors();
+
+		assertThat(errorList.size(), CoreMatchers.equalTo(0));
+		assertThat(result, CoreMatchers.equalTo(ViewNames.REDIRECT));
+	}
 }
\ No newline at end of file



Mime
View raw message