package com.lehrkraftnews.model import com.lehrkraftnews.util.MailProperties import net.liftweb.util.Log import org.scalatest.Assertions import net.liftweb.mapper._ /** * Zweck: Stellt eine mit definierten Beispieldaten gefüllte Datenbank zur Verfügung. * @author Christoph Knabe * @since 2010-09-01 */ object DatabaseUtil extends Assertions { val boot = new bootstrap.liftweb.Boot /**Creates a connection to the database and the schema for all model classes.*/ lazy val initORMapper: Unit = { DB.defineConnectionManager(DefaultConnectionIdentifier, boot.DBVendor) Schemifier.schemify(true, Log.infoF _, modelMetaObjects : _ *) } /**Zählt für alle Model-Klassen deren Meta-Objekte auf.*/ val modelMetaObjects = List(User, Subscription, News) /**Deletes all rows in the tables for all model classes.*/ private lazy val deleteAllRows = { User.bulkDelete_!!() Subscription.bulkDelete_!!() News.bulkDelete_!!() } /**Populates the database with 1 admin, 5 teachers, and 1 student for test purposes.*/ lazy val populateDatabase = { initORMapper deleteAllRows createUsers } /**Erzeugt Beispielbenutzer für die Testsuite. Diese sind ein Administrator, 5 Lehrkräfte und ein Student.*/ private def createUsers { //BEGIN(createUsers) boot.makeSureAdminExists //val mailPropertyEntries = MailProperties.getAll _createUser(User.Teacher, "Knabe", "Christoph", MailProperties("mail.teacher.1")) _createUser(User.Teacher, "Triel", "Bernd Holger", MailProperties("mail.teacher.2")) _createUser(User.Student, "Heinrich", "Theodor Friedrich", MailProperties("mail.student")) _createUser(User.Teacher, "Merkel", "Angela", "Angela.Merkel@bundesdeutschland.de") _createUser(User.Teacher, "Brandt", "Willi", "Willi.Brandt@beuth-hochschule.de") _createUser(User.Teacher, "Obama", "Barack", "Barack.Obama@beuth-hochschule.de") //END(createUsers) } /** Assures existence of a validated user with the given attributes and the password "test". */ private def _createUser(role: String, lastName: String, firstName: String, eMail: String): Unit = { val newUser = User.create.firstName(firstName).lastName(lastName).userRole(role).email(eMail).password("passwort").validated(true) newUser.save val found = User.findAll( By(User.lastName, lastName) ) val foundUser = found(0) assert(newUser === foundUser) assert(foundUser.lastName === lastName) assert(foundUser.firstName === firstName) assert(foundUser.email === eMail) assert(foundUser.validated === true) assert(foundUser.validate === noErrors) } private val noErrors = List() }//object