Package com.agiletec.plugins.jpaddressbook.apsadmin.addressbook

Source Code of com.agiletec.plugins.jpaddressbook.apsadmin.addressbook.TestContactFinderAction

/*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
* This file is part of Entando software.
* Entando is a free software;
* You can redistribute it and/or modify it
* under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; version 2.
*
* See the file License for the specific language governing permissions  
* and limitations under the License
*
*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
*/
package com.agiletec.plugins.jpaddressbook.apsadmin.addressbook;

import java.util.Date;
import java.util.List;

import com.agiletec.plugins.jpaddressbook.apsadmin.ApsAdminPluginBaseTestCase;
import com.agiletec.plugins.jpaddressbook.util.JpaddressbookTestHelper;

import com.agiletec.apsadmin.system.entity.IApsEntityFinderAction;
import com.agiletec.plugins.jpaddressbook.aps.system.JpaddressbookSystemConstants;
import com.agiletec.plugins.jpaddressbook.aps.system.services.addressbook.IAddressBookManager;
import com.opensymphony.xwork2.Action;

public class TestContactFinderAction extends ApsAdminPluginBaseTestCase {
 
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    this.init();
  }
 
  private void init() {
    this._addressBookManager = (IAddressBookManager) this.getService(JpaddressbookSystemConstants.ADDRESSBOOK_MANAGER);
    this._helper = new JpaddressbookTestHelper(this.getApplicationContext());
  }
 
  public void testList() throws Throwable {
    try {
      String result = this.executeList("editorCoach");
      assertEquals(Action.SUCCESS, result);
      List<String> contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { }, contacts);
     
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5", birthdate5, "it");
     
      result = this.executeList("editorCustomers");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "3", "4", "5" }, contacts);
     
      result = this.executeList("editorCoach");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "1", "2", "4" }, contacts);
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  public void testSearch() throws Throwable {
    try {
      String result = this.executeSearch("editorCoach", "");
      assertEquals(Action.SUCCESS, result);
      List<String> contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { }, contacts);
     
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1@company.it", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2@company.it", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3@company.it", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis@company.it", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5@company.it", birthdate5, "it");
     
      result = this.executeSearch("editorCustomers", "");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "3", "4", "5" }, contacts);
     
      result = this.executeSearch("editorCoach", "");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "1", "2", "4" }, contacts);
     
      result = this.executeSearch("editorCoach", "email");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "1", "2", "4" }, contacts);
     
      result = this.executeSearch("editorCoach", "email2");
      assertEquals(Action.SUCCESS, result);
      contacts = ((IApsEntityFinderAction) this.getAction()).getSearchResult();
      this.compareIds(new String[] { "2", "4" }, contacts);
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  public void testTrashWithErrors() throws Throwable {
    try {
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5", birthdate5, "it");
     
      String result = this.executeTrash("editorCoach", "notExistantContact");
      assertEquals(Action.INPUT, result);
      List<String> fieldErrors = this.getAction().getFieldErrors().get("entityId");
      assertEquals(1, fieldErrors.size());
     
      result = this.executeTrash("editorCoach", "3");
      assertEquals(Action.INPUT, result);
      fieldErrors = this.getAction().getFieldErrors().get("contactKey");
      assertEquals(1, fieldErrors.size());
      assertEquals("*CONTATTO NON PROPRIETARIO*", fieldErrors.get(0));
      assertNotNull(this._addressBookManager.getContact("3"));
     
      result = this.executeTrash("editorCustomers", "4");
      assertEquals(Action.INPUT, result);
      fieldErrors = this.getAction().getFieldErrors().get("contactKey");
      assertEquals(1, fieldErrors.size());
      assertEquals("*CONTATTO NON PROPRIETARIO*", fieldErrors.get(0));
      assertNotNull(this._addressBookManager.getContact("4"));
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  public void testTrashSuccessful() throws Throwable {
    try {
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5", birthdate5, "it");

      String result = this.executeTrash("editorCoach", "1");
      assertEquals(Action.SUCCESS, result);
     
      result = this.executeTrash("editorCoach", "2");
      assertEquals(Action.SUCCESS, result);
     
      result = this.executeTrash("editorCustomers", "3");
      assertEquals(Action.SUCCESS, result);
     
      result = this.executeTrash("editorCoach", "4");
      assertEquals(Action.SUCCESS, result);
     
      result = this.executeTrash("editorCustomers", "5");
      assertEquals(Action.SUCCESS, result);
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  public void testDeleteWithErrors() throws Throwable {
    try {
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5", birthdate5, "it");
     
      String result = this.executeDelete("editorCoach", "notExistantContact");
      assertEquals(Action.INPUT, result);
      List<String> fieldErrors = this.getAction().getFieldErrors().get("entityId");
      assertEquals(1, fieldErrors.size());
     
      result = this.executeDelete("editorCoach", "3");
      assertEquals(Action.INPUT, result);
      fieldErrors = this.getAction().getFieldErrors().get("contactKey");
      assertEquals(1, fieldErrors.size());
      assertNotNull(this._addressBookManager.getContact("3"));
     
      result = this.executeDelete("editorCustomers", "4");
      assertEquals(Action.INPUT, result);
      fieldErrors = this.getAction().getFieldErrors().get("contactKey");
      assertEquals(1, fieldErrors.size());
      assertNotNull(this._addressBookManager.getContact("4"));
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  public void testDeleteSuccessful() throws Throwable {
    try {
      Date birthdate1 = this._helper.setBirthdate(1981, 10, 25);
      Date birthdate2 = this._helper.setBirthdate(1982, 10, 25);
      Date birthdate3 = this._helper.setBirthdate(1983, 10, 25);
      Date birthdate4 = this._helper.setBirthdate(1984, 10, 25);
      Date birthdate5 = this._helper.setBirthdate(1985, 10, 25);
      this._helper.addContact("1", "editorCoach", false, "name1 surname1", "email1", birthdate1, "it");
      this._helper.addContact("2", "editorCoach", false, "name2 surname2", "email2", birthdate2, "en");
      this._helper.addContact("3", "editorCustomers", false, "name3 surname3", "email3", birthdate3, "fr");
      this._helper.addContact("4", "editorCoach", true, "name4 surname4", "email2bis", birthdate4, "de");
      this._helper.addContact("5", "editorCustomers", false, "name5 surname5", "email5", birthdate5, "it");

      String result = this.executeDelete("editorCoach", "1");
      assertEquals(Action.SUCCESS, result);
      assertNull(this._addressBookManager.getContact("1"));
     
      result = this.executeDelete("editorCoach", "2");
      assertEquals(Action.SUCCESS, result);
      assertNull(this._addressBookManager.getContact("2"));
     
      result = this.executeDelete("editorCustomers", "3");
      assertEquals(Action.SUCCESS, result);
      assertNull(this._addressBookManager.getContact("3"));
     
      result = this.executeDelete("editorCoach", "4");
      assertEquals(Action.SUCCESS, result);
      assertNull(this._addressBookManager.getContact("4"));
     
      result = this.executeDelete("editorCustomers", "5");
      assertEquals(Action.SUCCESS, result);
      assertNull(this._addressBookManager.getContact("5"));
    } catch (Throwable t) {
      throw t;
    } finally {
      this._helper.cleanAddressBook();
    }
  }
 
  private void compareIds(String[] expected, List<String> received) {
    assertEquals(expected.length, received.size());
    for (String id : expected) {
      if (!received.contains(id)) {
        throw new Error("Expected id \"" + id + "\" not found");
      }
    }
  }
 
  private String executeList(String username) throws Throwable {
    this.setUserOnSession(username);
    this.initAction("/do/jpaddressbook/AddressBook", "list");
    String result = this.executeAction();
    return result;
  }
 
  private String executeSearch(String username, String email) throws Throwable {
    this.setUserOnSession(username);
    this.initAction("/do/jpaddressbook/AddressBook", "search");
    this.addParameter("email_textFieldName", email);
    String result = this.executeAction();
    return result;
  }
 
  private String executeTrash(String username, String entityId) throws Throwable {
    this.setUserOnSession(username);
    this.initAction("/do/jpaddressbook/AddressBook", "trash");
    this.addParameter("entityId", entityId);
    String result = this.executeAction();
    return result;
  }
 
  private String executeDelete(String username, String entityId) throws Throwable {
    this.setUserOnSession(username);
    this.initAction("/do/jpaddressbook/AddressBook", "delete");
    this.addParameter("entityId", entityId);
    String result = this.executeAction();
    return result;
  }
 
  private IAddressBookManager _addressBookManager;
  private JpaddressbookTestHelper _helper;
 
}
TOP

Related Classes of com.agiletec.plugins.jpaddressbook.apsadmin.addressbook.TestContactFinderAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.