Package org.olat.course.nodes.projectbroker

Source Code of org.olat.course.nodes.projectbroker.ProjectBrokerManagerTest

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.course.nodes.projectbroker;


import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;
import org.olat.admin.securitygroup.gui.IdentitiesRemoveEvent;
import org.olat.basesecurity.ManagerFactory;
import org.olat.basesecurity.SecurityGroup;
import org.olat.basesecurity.SecurityGroupImpl;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.id.Identity;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.Encoder;
import org.olat.course.nodes.projectbroker.datamodel.Project;
import org.olat.course.nodes.projectbroker.datamodel.ProjectBroker;
import org.olat.course.nodes.projectbroker.service.ProjectBrokerManagerFactory;
import org.olat.course.nodes.projectbroker.service.ProjectBrokerModuleConfiguration;
import org.olat.group.BusinessGroup;
import org.olat.group.BusinessGroupAddResponse;
import org.olat.group.BusinessGroupImpl;
import org.olat.group.BusinessGroupManagerImpl;
import org.olat.group.ui.BGConfigFlags;
import org.olat.login.OLATAuthenticationController;
import org.olat.modules.ModuleConfiguration;
import org.olat.repository.RepositoryEntry;
import org.olat.test.JunitTestHelper;

/**
*  
* @author Christian Guretzki
*/

public class ProjectBrokerManagerTest extends OlatTestCase {
  //
  private static Logger log = Logger.getLogger(ProjectBrokerManagerTest.class.getName());
  /*
   * ::Test Setup::
   */
  private static Identity id1 = null;
  private static Identity id2 = null;
  //
  private static boolean isInitialized;
  private static Long resourceableId;

  /**
   * @param name
   */
  public ProjectBrokerManagerTest(String name) {
    super(name);
  }

  /**
   * @see junit.framework.TestCase#setUp()
   */
  protected void setUp() throws Exception {
    super.setUp();
    System.out.println("ProjectBrokerManagerTest.setUp start...");
    if (ProjectBrokerManagerTest.isInitialized == false) {
      try {
        System.out.println("ProjectBrokerManagerTest.setUp: isInitialized=false");
        DBFactory.getJunitInstance().clearDatabase();
        System.out.println("ProjectBrokerManagerTest.setUp clearDatabase");
        id1 = ManagerFactory.getManager().createAndPersistIdentity("id1", null, OLATAuthenticationController.PROVIDER_OLAT, "id1",
            Encoder.encrypt("id1"));
        id2 = ManagerFactory.getManager().createAndPersistIdentity("id2", null, OLATAuthenticationController.PROVIDER_OLAT, "id2",
            Encoder.encrypt("id2"));

        RepositoryEntry repositoryEntry = JunitTestHelper.deployDemoCourse();
        resourceableId = repositoryEntry.getOlatResource().getResourceableId();
        System.out.println("Demo course imported - resourceableId: " + resourceableId);
       
        DBFactory.getInstance().closeSession();
       
        ProjectBrokerManagerTest.isInitialized = true;
        System.out.println("ProjectBrokerManagerTest.setUp finished");
      } catch (Exception e) {
        System.out.println("ProjectBrokerManagerTest.setUp Exception=" + e.getMessage());
        e.printStackTrace();
        log.error("Error while " + "generating database " + "tables or opening " + "hibernate session: " + e);
        this.fail(e.getMessage());
      }
    }
  }


  /**
   *
   */
  public void testCreateListDeleteProjects() throws Exception {
    System.out.println("testCreateListDeleteProjects: start...");
    // create ProjectBroker A + B
    ProjectBroker projectBrokerA = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
    Long idProjectBrokerA = projectBrokerA.getKey();
    ProjectBroker projectBrokerB = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
    Long idProjectBrokerB = projectBrokerB.getKey();
    // add project to ProjectBroker A
    createProject("thema A1", id1, idProjectBrokerA, resourceableId );
    createProject("thema A2", id1, idProjectBrokerA, resourceableId );
    // add project to ProjectBroker B
    createProject("thema B1", id1, idProjectBrokerB, resourceableId );
    createProject("thema B2", id1, idProjectBrokerB, resourceableId );
   
    DBFactory.getInstance().closeSession();
    // get project list and check content
    List<Project> projectListA = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerA);
    List<Project> projectListB = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerB);
    assertEquals("Wrong projectList.size for project-broker A",2, projectListA.size());
    assertEquals("Wrong projectList.size for project-broker B",2, projectListB.size());
    assertTrue("Wrong thema in project list A, title must start with 'thema A'", projectListA.get(0).getTitle().startsWith("thema A"));
    assertTrue("Wrong thema in project list A, title must start with 'thema A'", projectListA.get(1).getTitle().startsWith("thema A"));
    assertTrue("Wrong thema in project list B, title must start with 'thema B'", projectListB.get(0).getTitle().startsWith("thema B"));
    assertTrue("Wrong thema in project list B, title must start with 'thema B'", projectListB.get(1).getTitle().startsWith("thema B"));
    if (projectListA.get(0).getTitle().equals("thema A1")) {
      assertTrue("Wrong thema in project list A, title must be 'thema A2'", projectListA.get(1).getTitle().equals("thema A2"));
    } else if (projectListA.get(0).getTitle().equals("thema A2")) {
      assertTrue("Wrong thema in project list A, title must be 'thema A1'", projectListA.get(1).getTitle().equals("thema A1"));
    }
    if (projectListB.get(0).getTitle().equals("thema B1")) {
      assertTrue("Wrong thema in project list B, title must be 'thema B2'", projectListB.get(1).getTitle().equals("thema B2"));
    } else if (projectListB.get(0).getTitle().equals("thema B2")) {
      assertTrue("Wrong thema in project list B, title must be 'thema B1'", projectListB.get(1).getTitle().equals("thema B1"));
    }
   
    // delete project
    long candiadteGroupKey = projectListA.get(0).getCandidateGroup().getKey();
    long projectGroupKey = projectListA.get(0).getProjectGroup().getKey();
    assertNotNull("CandidateGroup does not exist before delete project", DBFactory.getInstance().findObject(SecurityGroupImpl.class, candiadteGroupKey));
    assertNotNull("ProjectGroup does not exist before delete project", DBFactory.getInstance().findObject(BusinessGroupImpl.class, projectGroupKey));
    ProjectBrokerManagerFactory.getProjectBrokerManager().deleteProject(projectListA.get(0), true, null, null);
    assertNull("CandidateGroup still exists after delete project", DBFactory.getInstance().findObject(SecurityGroupImpl.class, candiadteGroupKey));
    assertNull("ProjectGroup still exists after delete project", DBFactory.getInstance().findObject(BusinessGroupImpl.class, projectGroupKey));

    // get project list and check content
    projectListA = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerA);
    projectListB = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerB);
    assertEquals("Wrong projectList.size for project-broker A after delete 'thema A1'",1, projectListA.size());
    assertEquals("Wrong projectList.size for project-broker B after delete 'thema A1'",2, projectListB.size());
    // delete project
    ProjectBrokerManagerFactory.getProjectBrokerManager().deleteProject(projectListB.get(1), true, null, null);
    // get project list and check content
    projectListA = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerA);
    projectListB = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerB);
    assertEquals("Wrong projectList.size for project-broker A after delete 'thema B2'",1, projectListA.size());
    assertEquals("Wrong projectList.size for project-broker B after delete 'thema B2'",1, projectListB.size());
   
    // delete project
    ProjectBrokerManagerFactory.getProjectBrokerManager().deleteProject(projectListA.get(0), true, null, null);
    projectListA = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerA);
    projectListB = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerB);
    System.out.println("testCreateListDeleteProjects: projectListA=" + projectListA);
    assertEquals("Wrong projectList.size for project-broker A after delete all thema",0, projectListA.size());
    assertEquals("Wrong projectList.size for project-broker B after delete all thema",1, projectListB.size());
    // cleanup
    System.out.println("testCreateListDeleteProjects: done");
  }

  public void testPerformanceGetProjectList() throws Exception {
    System.out.println("testPerformanceGetProjectList: start...");
    int FIRST_ITERATION = 10;
    int SECOND_ITERATION = 90;
    int THIRD_ITERATION = 400;
    // create ProjectBroker C
    ProjectBroker projectBrokerC = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
    Long idProjectBrokerC = projectBrokerC.getKey();
    DBFactory.getInstance().closeSession();
    for (int i = 0; i < FIRST_ITERATION; i++) {
      createProject("thema C1_" + i, id1, idProjectBrokerC, resourceableId );   
    }
    DBFactory.getInstance().closeSession();
    long startTime = System.currentTimeMillis();   
    List<Project> projectListC = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerC);
    long endTime = System.currentTimeMillis();
    assertEquals("Wrong projectList.size for project-broker C after first iteration",FIRST_ITERATION, projectListC.size());
    long duration = endTime - startTime;
    System.out.println("getProjectListBy takes " + duration + "ms with " + FIRST_ITERATION + " projects");

    for (int i = 0; i < SECOND_ITERATION; i++) {
      createProject("thema C1_" + i, id1, idProjectBrokerC, resourceableId );     
    }
    DBFactory.getInstance().closeSession();
    startTime = System.currentTimeMillis();
    projectListC = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerC);
    endTime = System.currentTimeMillis();
    int numberOfProjects = FIRST_ITERATION + SECOND_ITERATION;
    assertEquals("Wrong projectList.size for project-broker C", numberOfProjects, projectListC.size());
    duration = endTime - startTime;
    System.out.println("getProjectListBy takes " + duration + "ms with " + numberOfProjects + " projects");

    for (int i = 0; i < THIRD_ITERATION; i++) {
      createProject("thema C1_" + i, id1, idProjectBrokerC, resourceableId );     
    }
    DBFactory.getInstance().closeSession();
    startTime = System.currentTimeMillis();
    projectListC = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerC);
    endTime = System.currentTimeMillis();
    numberOfProjects = FIRST_ITERATION + SECOND_ITERATION + THIRD_ITERATION;
    assertEquals("Wrong projectList.size for project-broker C", numberOfProjects, projectListC.size());
    duration = endTime - startTime;
    System.out.println("getProjectListBy takes " + duration + "ms with " + numberOfProjects + " projects");
    // cleanup
    System.out.println("testPerformance: done");
  }

  public void testPerformanceTableModel() throws Exception {
    int ITERATION = 300;
    int START_PAGE_INDEX = 100;
    int PAGE_SIZE = 20;
    PackageTranslator translator = new PackageTranslator(this.getClass().getPackage().getName(), Locale.GERMAN);

    ProjectBroker projectBrokerD = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
    Long idProjectBrokerD = projectBrokerD.getKey();
    ProjectBrokerModuleConfiguration moduleConfig = new ProjectBrokerModuleConfiguration( new ModuleConfiguration() );

    for (int i = 0; i < ITERATION; i++) {
      createProject("thema D1_" + i, id1, idProjectBrokerD, resourceableId );     
    }
    List projectListD = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(idProjectBrokerD);
    ProjectListTableModel tableModel = new ProjectListTableModel(projectListD, id1, translator, moduleConfig, 0, 0, 0, false);
   
    // loop over table like rendering loop
    long startTime = System.currentTimeMillis();
    for (int row = START_PAGE_INDEX; row < START_PAGE_INDEX+PAGE_SIZE; row++) {
      for (int col = 0; col < tableModel.getColumnCount(); col++) {
        Object element = tableModel.getValueAt(row, col);
      }
    }
    long endTime = System.currentTimeMillis();
    long duration = endTime - startTime;
    System.out.println("tableModel.getValueAt(row, col) for " + PAGE_SIZE + "elements (of " + ITERATION + ") takes " + duration + "ms with " + ITERATION + " projects");
    // cleanup
  }

  public void testIsProjectManager() throws Exception {
    ProjectBroker projectBrokerD = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
    Long idProjectBrokerD = projectBrokerD.getKey();
    ProjectBrokerModuleConfiguration moduleConfig = new ProjectBrokerModuleConfiguration( new ModuleConfiguration() );
   
    Project testProjectA = createProject("thema A", id1, idProjectBrokerD, resourceableId );
    List<Identity> projectManagerList = new ArrayList<Identity>();
    projectManagerList.add(id1);
    BGConfigFlags flags = BGConfigFlags.createRightGroupDefaultFlags();
    Project testProjectB = createProject("thema B", id2, idProjectBrokerD, resourceableId );
    // check project leader in ProjectA
    assertTrue("Must be project-leader of project A", ProjectBrokerManagerFactory.getProjectBrokerManager().isProjectManager(id1, testProjectA));
    assertFalse("Can not be project leader of project B",ProjectBrokerManagerFactory.getProjectBrokerManager().isProjectManager(id1, testProjectB));
    assertTrue("Must be project-leader of project A", ProjectBrokerManagerFactory.getProjectBrokerManager().isProjectManager(id2, testProjectB));

    BusinessGroupManagerImpl.getInstance().removeOwnersAndFireEvent(id1, projectManagerList, testProjectA.getProjectGroup(),flags);
    // check no project leader anymore
    assertFalse("Can not be project leader of project A",ProjectBrokerManagerFactory.getProjectBrokerManager().isProjectManager(id1, testProjectA));
    assertFalse("Can not be project leader of project B",ProjectBrokerManagerFactory.getProjectBrokerManager().isProjectManager(id1, testProjectB));
    // cleanup
  }
 
  public void testAcceptManuall() throws Exception {
   
  }
 
  public void testAcceptAutomaticly() throws Exception {
   
  }


private Project createProject(String name, Identity creator, Long projectBrokerId, Long courseId) {
  BusinessGroup projectGroup = ProjectBrokerManagerFactory.getProjectGroupManager().createProjectGroupFor(projectBrokerId, creator, name + "_Group", name + "GroupDescription", courseId);
  Project project = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectFor(name + "title", name + "description1", projectBrokerId, projectGroup);
  return project;
}

  /**
   * @see junit.framework.TestCase#tearDown()
   */
  protected void tearDown() throws Exception {
    super.tearDown();
    try {
      DBFactory.getInstance().closeSession();
    } catch (Exception e) {
      log.error("tearDown failed: ", e);
    }
  }

  /**
   * Export all test cases as suite. Make sure you add your testcase to
   * org.olat.test.AllTest.java
   *
   * @return test suite
   * @throws Exception
   */
  public static Test suite() throws Exception {
    return new TestSuite(ProjectBrokerManagerTest.class);
  }
 

}
TOP

Related Classes of org.olat.course.nodes.projectbroker.ProjectBrokerManagerTest

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.