Skip to content

Commit

Permalink
fixed oneToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
kamenitxan committed Jul 22, 2023
1 parent 3af3545 commit 59c6d8b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbtassembly.AssemblyPlugin.autoImport.assembly

val V = new {
val Scala = "3.3.1-RC4"
val jakon = "0.5.5"
val jakon = "0.5.6-SNAPSHOT"
val spark = "2.9.4-JAKON.2"
val log4j = "2.20.0"
val circeVersion = "0.14.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cz.kamenitxan.jakon.core.database

import com.zaxxer.hikari.HikariDataSource
import cz.kamenitxan.jakon.core.configuration.{DatabaseType, Settings}
import cz.kamenitxan.jakon.core.database.annotation.OneToMany
import cz.kamenitxan.jakon.core.model.*
import cz.kamenitxan.jakon.logging.Logger
import cz.kamenitxan.jakon.utils.TypeReferences.SEQ
Expand Down Expand Up @@ -169,19 +170,14 @@ object DBHelper {
if (fki._2.ids.size == 1 && r.entity.id == fki._2.ids.head) {
field.set(r.entity, r.entity)
} else {
val objectClass = field.getGenericType match {
case parameterizedType: ParameterizedType =>
Class.forName(parameterizedType.getActualTypeArguments.toList.head.getTypeName).asInstanceOf[Class[JakonObject]]
case _ =>
field.getType.asInstanceOf[Class[JakonObject]]
}
val (className, superClass) = field.getGenericType match {
case parameterizedType: ParameterizedType =>
val typeCls = parameterizedType.getActualTypeArguments.head
(typeCls.getTypeName.substring(typeCls.getTypeName.lastIndexOf(".") + 1), typeCls.getClass.getSuperclass)
case _ =>
(objectClass.getSimpleName, objectClass.getSuperclass)
val oneToManyAnn = field.getDeclaredAnnotation(classOf[OneToMany])
val objectClass = if (oneToManyAnn != null) {
oneToManyAnn.genericClass().asInstanceOf[Class[JakonObject]]
} else {
field.getType.asInstanceOf[Class[JakonObject]]
}

val (className, superClass) = (objectClass.getSimpleName, objectClass.getSuperclass)
val joinSql = if (objectClass.getSuperclass != classOf[JakonObject]) {
s"JOIN ${superClass.getSimpleName} s ON s.id = JakonObject.id"
} else {
Expand Down
26 changes: 24 additions & 2 deletions modules/backend/src/test/scala/core/ModelTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package core

import cz.kamenitxan.jakon.core.model._
import cz.kamenitxan.jakon.core.database.DBHelper
import cz.kamenitxan.jakon.core.model.*
import cz.kamenitxan.jakon.core.service.UserService
import org.scalatest.DoNotDiscover
import test.TestBase
import utils.entity.{TestEmbeddedObject, TestObject}
Expand Down Expand Up @@ -77,7 +79,7 @@ class ModelTest extends TestBase {
obj.toString
}

var eoid = 0;
var eoid = 0
test("Create TestEmbeddedObject") { _ =>
val obj = new TestObject
val embedded = new TestEmbeddedObject
Expand All @@ -99,6 +101,26 @@ class ModelTest extends TestBase {
}
*/

var otmid = 0
test("Create OneToMany") { _ =>
val users = DBHelper.withDbConnection(implicit conn => {
UserService.getAllUsers()
}).take(2)

val obj = new TestObject
obj.oneToMany = users
eoid = obj.create()
}

test("Fetch OneToMany") { _ =>
DBHelper.withDbConnection(implicit conn => {
val sql = "SELECT * From TestObject WHERE id = ?"
val stmt = conn.prepareStatement(sql)
stmt.setInt(1, eoid)
val entity = DBHelper.selectSingleDeep(stmt)(conn, classOf[TestObject])
assert(entity.oneToMany.nonEmpty)
})
}

var post: Post = _
test("Post Create") { _ =>
Expand Down
6 changes: 3 additions & 3 deletions modules/backend/src/test/scala/utils/SqlGenTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ class SqlGenTest extends TestBase {
})
}

/*test("DBHelper selectDeep") { _ =>
test("DBHelper selectDeep") { _ =>
DBHelper.withDbConnection(implicit conn => {
val stmt = conn.prepareStatement("SELECT * FROM JakonUser")
val users = DBHelper.selectDeep(stmt, classOf[JakonUser])
val users = DBHelper.selectDeep(stmt)(conn, classOf[JakonUser])
assert(users.nonEmpty)
assert(users.forall( u => u.acl != null))
})
}*/
}

test("parseFilterParams invalid number") { _ =>
val params = mutable.Map(
Expand Down
15 changes: 11 additions & 4 deletions modules/backend/src/test/scala/utils/mail/EmailTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils.mail

import cz.kamenitxan.jakon.core.configuration.{DeployMode, Settings}
import cz.kamenitxan.jakon.core.database.DBHelper
import cz.kamenitxan.jakon.core.model.JakonUser
import cz.kamenitxan.jakon.core.service.AclRuleService
import cz.kamenitxan.jakon.utils.mail.{EmailEntity, EmailSendTask, EmailTemplateEntity}
import cz.kamenitxan.jakon.webui.controller.pagelets.JakonRegistrationPagelet
import org.scalatest.DoNotDiscover
Expand All @@ -16,10 +18,15 @@ import scala.util.Random
class EmailTest extends AnyFunSuite {

test("registrationEmailTest") {
val user = new JakonUser()
user.email = "[email protected]" + Random.nextInt()
user.create()
new JakonRegistrationPagelet().sendRegistrationEmail(user)
DBHelper.withDbConnection(implicit conn => {
val user = new JakonUser()
user.firstName = "registrationEmailTest"
user.lastName = "lastName"
user.email = "[email protected]" + Random.nextInt()
user.acl = AclRuleService.getByName("Admin").get
user.create()
new JakonRegistrationPagelet().sendRegistrationEmail(user)
})
}

test("EmailEntity") {
Expand Down
22 changes: 13 additions & 9 deletions modules/backend/src/test/scala/webui/AuthTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package webui
import cz.kamenitxan.jakon.core.configuration.Settings
import cz.kamenitxan.jakon.core.database.DBHelper
import cz.kamenitxan.jakon.core.model.JakonUser
import cz.kamenitxan.jakon.core.service.AclRuleService
import cz.kamenitxan.jakon.webui.controller.impl.Authentication
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.openqa.selenium.{By, WebDriver}
import org.scalatest.{DoNotDiscover, Outcome}
import org.scalatest.funsuite.FixtureAnyFunSuite
import org.scalatest.{DoNotDiscover, Outcome}

import java.util.Locale
import scala.util.Random
Expand Down Expand Up @@ -41,14 +42,17 @@ class AuthTest extends FixtureAnyFunSuite {
}

test("create user") { _ =>
val user = new JakonUser()
user.firstName = "testName"
user.lastName = "lastName"
user.email = email
user.password = password
user.locale = new Locale("en", "US")

assert(user.create() > 0)
DBHelper.withDbConnection(implicit conn => {
val user = new JakonUser()
user.firstName = "testName"
user.lastName = "lastName"
user.email = email
user.password = password
user.locale = new Locale("en", "US")
user.acl = AclRuleService.getByName("Admin").get

assert(user.create() > 0)
})
}

test("check password") { _ =>
Expand Down

0 comments on commit 59c6d8b

Please sign in to comment.