Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Use dummy organization service in register portlet unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jun 21, 2013
1 parent 10595fe commit 93e44ee
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,36 @@ public class Controller {
@Path("index.gtmpl")
Template index;


@Inject
Flash flash;

@Inject
UserManager manager;

@View
public Response index() {
return index.with().ok();
}

@Action
public Response register(User user, String confirmPassword) {
if (!user.password.equals(confirmPassword)) {
public Response register(UserBean userBean, String confirmPassword) {
if (!userBean.password.equals(confirmPassword)) {
flash.setError("Password and Confirm Password must be the same.");
return Controller_.index();
}
User isExisted = User.getUser(user.userName);
if (isExisted != null) {
flash.setError("This user is already existed. Please enter different userName.");
return Controller_.index();
} else {
try {
UserBean isExisted = manager.getUser(userBean.userName);
if (isExisted != null) {
flash.setError("This user is already existed. Please enter different userName.");
} else {
manager.saveUser(userBean);
flash.setSuccess("You have successfully registered a new account!");
flash.setUserName(userBean.userName);
}
} catch (Exception e) {
flash.setError("Could not register user due to internal error.");
e.printStackTrace();
}
}
User.saveUser(user);
flash.setSuccess("You have successfully registered a new account!");
flash.setUserName(user.userName);
return Controller_.index();
}
}
49 changes: 0 additions & 49 deletions portal/ui/src/main/java/org/gatein/portal/ui/register/User.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.gatein.portal.ui.register;

import juzu.Mapped;
import org.exoplatform.services.organization.User;

@Mapped
public class UserBean {
public String userName;

public String password;

public String firstName;

public String lastName;

public String displayName;

public String emailAddress;

public UserBean(User user) {
this.userName = user.getUserName();
this.password = "";
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.displayName = user.getDisplayName();
this.emailAddress = user.getEmail();
}

public UserBean() {
}

public UserBean(String userName, String password, String firstName, String lastName, String displayName,
String emailAddress) {
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.displayName = displayName;
this.emailAddress = emailAddress;
}

public void update(User user) {
user.setDisplayName(displayName);
user.setEmail(emailAddress);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setPassword(password);
}

public String toString() {
return "User (userName = " + this.userName + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2012 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.gatein.portal.ui.register;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.organization.UserHandler;

/**
* Encapsulate operations with org service.
*
* @author Julien Viet
*/
@Singleton
public class UserManager {

@Inject
OrganizationService orgService;

public UserBean getUser(String userName) throws Exception {
UserHandler handler = orgService.getUserHandler();
User user = handler.findUserByName(userName);
if (user != null) {
return new UserBean(user);
} else {
return null;
}
}

public void saveUser(UserBean userBean) throws Exception {
UserHandler handler = orgService.getUserHandler();
User user = handler.createUserInstance(userBean.userName);
userBean.update(user);
handler.saveUser(user, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*/
@Application
@Bindings({
@Binding(Flash.class)
@Binding(Flash.class),
@Binding(UserManager.class),
@Binding(OrganizationService.class)
})
@Portlet
@Assets(stylesheets = @Stylesheet(src = "registerportlet.css")) package org.gatein.portal.ui.register;
Expand All @@ -14,4 +16,5 @@
import juzu.plugin.binding.Binding;
import juzu.plugin.binding.Bindings;
import juzu.plugin.portlet.Portlet;
import org.exoplatform.services.organization.OrganizationService;

12 changes: 12 additions & 0 deletions portal/ui/src/main/resources/conf/configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_2.xsd http://www.exoplaform.org/xml/ns/kernel_1_2.xsd"
xmlns="http://www.exoplaform.org/xml/ns/kernel_1_2.xsd">

<component>
<key>org.exoplatform.services.organization.OrganizationService</key>
<type>org.exoplatform.services.organization.impl.mock.DummyOrganizationService</type>
</component>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

import junit.framework.AssertionFailedError;
import juzu.arquillian.Helper;
import org.exoplatform.container.RootContainer;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.organization.UserHandler;
import org.gatein.portal.common.kernel.KernelLifeCycle;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
Expand Down Expand Up @@ -95,20 +99,34 @@ private String getBaseURL() {
public void testRegisterSuccess() {
driver.get(getBaseURL());
// fill the form
driver.findElements(By.name("userName")).get(0).sendKeys("test");
driver.findElements(By.name("password")).get(0).sendKeys("test");
driver.findElements(By.name("confirmPassword")).get(0).sendKeys("test");
driver.findElements(By.name("firstName")).get(0).sendKeys("test");
driver.findElements(By.name("lastName")).get(0).sendKeys("test");
driver.findElements(By.name("displayName")).get(0).sendKeys("test");
driver.findElements(By.name("emailAddress")).get(0).sendKeys("test");
driver.findElements(By.name("userName")).get(0).sendKeys("test_user_name");
driver.findElements(By.name("password")).get(0).sendKeys("test_password");
driver.findElements(By.name("confirmPassword")).get(0).sendKeys("test_password");
driver.findElements(By.name("firstName")).get(0).sendKeys("test_first_name");
driver.findElements(By.name("lastName")).get(0).sendKeys("test_last_name");
driver.findElements(By.name("displayName")).get(0).sendKeys("test_display_name");
driver.findElements(By.name("emailAddress")).get(0).sendKeys("test_email_address");

driver.findElements(By.name("submit")).get(0).click();
assertTrue(driver.findElement(By.id("registerMessage")).getText().contains("You have successfully registered a new account"));
}

@Test
@InSequence(2)
public void testUserExist() throws Exception {
OrganizationService orgService = (OrganizationService)RootContainer.getComponent(OrganizationService.class);
UserHandler handler = orgService.getUserHandler();
User user = handler.findUserByName("test_user_name");
assertNotNull(user);
assertEquals("test_password", user.getPassword());
assertEquals("_test_user_name", user.getFirstName()); // This is due to dumb dummy org service impl
assertEquals("test_last_name", user.getLastName());
assertEquals("test_display_name", user.getDisplayName());
assertEquals("test_user_name@mail.com", user.getEmail()); // This is due to dumb dummy org service impl
}

@Test
@InSequence(3)
@RunAsClient
public void testPasswordFail() {
driver.get(getBaseURL());
Expand All @@ -126,12 +144,12 @@ public void testPasswordFail() {
}

@Test
@InSequence(3)
@InSequence(4)
@RunAsClient
public void testAccountExisted() {
driver.get(getBaseURL());
// fill the form
driver.findElements(By.name("userName")).get(0).sendKeys("test");
driver.findElements(By.name("userName")).get(0).sendKeys("test_user_name");
driver.findElements(By.name("password")).get(0).sendKeys("test");
driver.findElements(By.name("confirmPassword")).get(0).sendKeys("test");
driver.findElements(By.name("firstName")).get(0).sendKeys("test");
Expand Down

0 comments on commit 93e44ee

Please sign in to comment.