【问题标题】:Unit Test Grails Domain Class with Multitenant具有多租户的单元测试 Grails 域类
【发布时间】:2020-04-29 11:40:37
【问题描述】:

我无法对 Multitenant 的 grails 域类进行单元测试。我得到了

rg.spockframework.runtime.ConditionFailedWithExceptionError at AccountSpec.groovy:26 原因:AccountSpec.groovy:26 处的 org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException

 package crm

    import grails.gorm.MultiTenant
    import usermanagement.User

    class Account  implements MultiTenant<Account> {
        String avatar
        String tenantId
        String name
        String description
        Date establishedDate
        String email
        String mobile
        String website
        String fax
        Date dateCreated
        Date lastUpdated
        User user

        static constraints = {
            avatar nullable:true, blank:true
            name unique: 'tenantId'
            description nullable: true, blank: true
            establishedDate nullable: true, blank: true
            fax nullable: true, blank: true
            email unique:'tenantId',email: true
            website unique:'tenantId',nullable: true, blank: true

        }
    }

帐户单元测试

package crm

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification


class AccountSpec extends Specification implements DomainUnitTest<Account> {



    def setup() {

    }

    def cleanup() {

    }

    void 'test name cannot be null'() {
        when:
        domain.mobile = null
        then:
        !domain.validate(['mobile'])
        domain.errors['mobile'].code == 'nullable'
    }


}

自定义租户解析器

package usermanagement

import grails.plugin.springsecurity.SpringSecurityService
import org.grails.datastore.mapping.multitenancy.AllTenantsResolver
import org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.security.core.userdetails.UserDetails

class CustomTenentResolver implements AllTenantsResolver{
    @Lazy
    @Autowired
    SpringSecurityService springSecurityService

    @Override
    Iterable<Serializable> resolveTenantIds() {
        return DetachedCriteria(Organisation).distinct("namespace").list()
    }

    @Override
    Serializable resolveTenantIdentifier() throws TenantNotFoundException {
        final String tenantId = organisation()
        if(tenantId){
            return tenantId
        }
        throw new TenantNotFoundException("unable to retrive tenent")
    }

    String organisation(){

        if (springSecurityService.principal == null){
            return null
        }

        if (springSecurityService.principal instanceof  String){
            return springSecurityService.principal
        }

        if (springSecurityService.principal instanceof UserDetails){

           return User.findByUsername(((UserDetails)springSecurityService.principal).username).organisation.namespace
        }

        null
    }
}

【问题讨论】:

    标签: java grails groovy multi-tenant grails-domain-class


    【解决方案1】:

    正确的做法取决于您实际想要完成的任务。由于这是一个单元测试,我假设您正在尝试测试 Account 验证并完全排除多租户。有多种方法可以实现这一点,一种简单的方法是在测试中禁用多租户:

    import grails.testing.gorm.DomainUnitTest
    import spock.lang.Specification
    
    class AccountSpec extends Specification implements DomainUnitTest<Account> {
    
        @Override
        Closure doWithConfig() {
            { config ->
                config.grails.gorm.multiTenancy.mode = null
            }
        }
    
        void 'test name cannot be null'() {
            when:
            domain.mobile = null
            then:
            !domain.validate(['mobile'])
            domain.errors['mobile'].code == 'nullable'
        }
    }
    

    【讨论】:

    • 谢谢你,Jeff.Really 你和 Grails 框架的忠实粉丝
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 2015-02-22
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2010-11-15
    相关资源
    最近更新 更多