Package org.olat.test

Source Code of org.olat.test.JunitTestHelper

/**
* 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>
* Description:<br>
* Helper methods to create identities that can be used in junit tests. Start
* the test case with -Djunit.maildomain=mydomain.com to create identities with
* mail accounts that go to your domain, otherwhise mytrashmail.com will be used
* <P>
* Initial Date: 21.11.2006 <br>
*
* @author Florian Gnaegi, frentix GmbH<br>
*         http://www.frentix.com
*/
package org.olat.test;

import java.io.File;

import org.olat.basesecurity.Constants;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.basesecurity.SecurityGroup;
import org.olat.core.id.Identity;
import org.olat.core.id.User;
import org.olat.core.util.Encoder;
import org.olat.core.util.WebappHelper;
import org.olat.course.CourseFactory;
import org.olat.login.OLATAuthenticationController;
import org.olat.properties.Property;
import org.olat.properties.PropertyManager;
import org.olat.repository.RepositoryEntry;
import org.olat.user.UserManager;

public class JunitTestHelper {

  static String maildomain = System.getProperty("junit.maildomain");
  static {
    if (maildomain == null) {
      maildomain = "mytrashmail.com";
    }
  }

  /**
   * Create an identity with user permissions
   * @param login
   * @return
   */
  public static final Identity createAndPersistIdentityAsUser(String login) {
    Manager securityManager = ManagerFactory.getManager();
    SecurityGroup group = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    if (group == null) group = securityManager.createAndPersistNamedSecurityGroup(Constants.GROUP_OLATUSERS);
    User user = UserManager.getInstance().createUser("first" + login, "last" + login, login + "@" + maildomain);
    Identity identity = securityManager.createAndPersistIdentityAndUser(login, user, OLATAuthenticationController.PROVIDER_OLAT, login,
        Encoder.encrypt("A6B7C8"));
    securityManager.addIdentityToSecurityGroup(identity, group);
    return identity;
  }

  /**
   * Create an identity with author permissions
   * @param login
   * @return
   */
  public static final Identity createAndPersistIdentityAsAuthor(String login) {
    Manager securityManager = ManagerFactory.getManager();
    SecurityGroup group = securityManager.findSecurityGroupByName(Constants.GROUP_AUTHORS);
    if (group == null) group = securityManager.createAndPersistNamedSecurityGroup(Constants.GROUP_AUTHORS);
    User user = UserManager.getInstance().createUser("first" + login, "last" + login, login + "@" + maildomain);
    Identity identity = securityManager.createAndPersistIdentityAndUser(login, user, OLATAuthenticationController.PROVIDER_OLAT, login,
        Encoder.encrypt("A6B7C8"));
    securityManager.addIdentityToSecurityGroup(identity, group);
    return identity;
  }
 
  /**
   * Create an identity with admin permissions
   * @param login
   * @return
   */
  public static final Identity createAndPersistIdentityAsAdmin(String login) {
    Manager securityManager = ManagerFactory.getManager();
    SecurityGroup group = securityManager.findSecurityGroupByName(Constants.GROUP_ADMIN);
    if (group == null) group = securityManager.createAndPersistNamedSecurityGroup(Constants.GROUP_ADMIN);
    User user = UserManager.getInstance().createUser("first" + login, "last" + login, login + "@" + maildomain);
    Identity identity = securityManager.createAndPersistIdentityAndUser(login, user, OLATAuthenticationController.PROVIDER_OLAT, login,
        Encoder.encrypt("A6B7C8"));
    securityManager.addIdentityToSecurityGroup(identity, group);
    return identity;
  }
 
  /**
   * Remove identity from <code>Constants.GROUP_OLATUSERS</code> group.
   * @param identity
   */
  /*public static void deleteIdentityFromUsersGroup(Identity identity) {
    Manager securityManager = ManagerFactory.getManager();
    SecurityGroup group = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    if (group != null) {
      securityManager.removeIdentityFromSecurityGroup(identity, group);
    }
  }*/
 
  /**
   * Deploys/imports the "Demo Course".
   * @return the created RepositoryEntry
   */
  public static RepositoryEntry deployDemoCourse() {
    String absOrRelPath = "examples/Demo Kurs.zip";
    int access = 4;
    String resolvedPath = absOrRelPath;
    if (resolvedPath == null) return null;

    Identity admin = createAndPersistIdentityAsAdmin("administrator");
   
    if (resolvedPath.length() > 0 && resolvedPath.charAt(0) != '/') resolvedPath = WebappHelper.getContextRoot() + "/" + resolvedPath;
    File exportedCourseZIPFile = new File(resolvedPath);
    if (!exportedCourseZIPFile.exists()) {
      //do not throw exception as users may upload bad file
      System.out.println("Cannot deploy course from file: " + exportedCourseZIPFile.getAbsolutePath());
      return null;
    }
    RepositoryEntry re = CourseFactory.deployCourseFromZIP(exportedCourseZIPFile, access)
    if (re != null) {
      PropertyManager pm = PropertyManager.getInstance();
      Property prop = pm.createPropertyInstance(null, null, null, "_o3_", "deployedCourses", null, re.getKey(), absOrRelPath, null);
      pm.saveProperty(prop);
    }
    return re;
  }

}
TOP

Related Classes of org.olat.test.JunitTestHelper

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.