How to mock TenantUtils.doWithTenant

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to mock TenantUtils.doWithTenant

drosowsk
Hi list,
I'm using the multi-tenant plugin on some project.
Now I have a service where I need to do something for a specific tenant, therefore I use TenantUtils.doWithTenant. But when trying to unit test the service, I get a NPE in TenantUtils.doWithTenant "Cannot invoke method get() on null object". Obviously only the currentTenant is mocked.

So I tried mocking it like this:
<code>
TenantUtils.metaClass.static.doWithTenant = { tenant, closure -> closure.call() }
</code>

But the service is never using my mocked version, only the original TenantUtils.
Any idea on how to get that mocked? I really don't want to switch it to an integration test for this.

Cheers,
Daniel
Agrenon GmbH
www.agrenon.com
Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

Kimble
You have to mock the CurrentTenant interface (https://github.com/multi-tenant/grails-multi-tenant-core/blob/master/src/java/grails/plugin/multitenant/core/CurrentTenant.java) and set it on TenantUtils (static property, https://github.com/multi-tenant/grails-multi-tenant-core/blob/master/src/groovy/grails/plugin/multitenant/core/util/TenantUtils.groovy).

You might want to reconsider some design decisions if you use TenantUtils too many places.
 - Kim A. Betti
Have a nice day!
Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

Eric Martineau
You also have to be careful with TenantUtils and your open hibernate session.  It's easy to make the mistake of saving a bunch of data within a doWithTenant closure only to have the session flushed later in your call, after the current tenant has been reset.

I agree that the doWithTenant method is problematic (and I wrote it).  Just be careful about your assumptions when using it.

Eric Martineau
Infusion Software
www.infusionsoft.com

On Mar 8, 2011, at 1:47 PM, Kimble <[hidden email]> wrote:

> You have to mock the CurrentTenant interface
> (https://github.com/multi-tenant/grails-multi-tenant-core/blob/master/src/java/grails/plugin/multitenant/core/CurrentTenant.java)
> and set it on TenantUtils (static property,
> https://github.com/multi-tenant/grails-multi-tenant-core/blob/master/src/groovy/grails/plugin/multitenant/core/util/TenantUtils.groovy).
>
> You might want to reconsider some design decisions if you use TenantUtils
> too many places.
>
> -----
> - Kim
> Have a nice day!
> --
> View this message in context: http://grails.1312388.n4.nabble.com/How-to-mock-TenantUtils-doWithTenant-tp3341772p3342198.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

drosowsk
Thanks for your help guys, I eventually got it working. Just for reference if someone is facing the same problem, here's my mocking "solution".

<code>
    def currentTenantFactory = mockFor(CurrentTenant)
    currentTenantFactory.demand.get(1..10) {  -> return 0 }
    currentTenantFactory.demand.set(1..10) { Integer tenantId -> }
    TenantUtils tenantUtils = new TenantUtils()
    tenantUtils.setCurrentTenant(currentTenantFactory.createMock())
</code>

@Eric: wouldn't it be possible to refactor TenantUtils to be a service, so it can be easily mocked out?

Cheers,
Daniel
Agrenon GmbH
www.agrenon.com
Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

Kimble
I've already done this in the -single-db branch of the plugin. It's also added as a static method on the Tenant interface and application provided implementation (if any). This way it's possible to use the convenient meta method in tests and the service in production code.

Be aware that this branch is a work in progress. The goal is to either merge changes back into -core or create separate plugins (core, multi-db and single-db).

https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/grails-app/services/grails/plugin/multitenant/core/MultiTenantService.groovy
https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/src/groovy/grails/plugin/multitenant/singledb/MtSingleDbPluginSupport.groovy#L102
 - Kim A. Betti
Have a nice day!
Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

lucastex
We're already testing this other branch, and even this "GOD MODE".
It is working great here.

[]s,


Lucas Frare Teixeira .·.
- [hidden email]
- lucastex.com.br
- blog.lucastex.com
- twitter.com/lucastex


On Thu, Mar 10, 2011 at 7:54 AM, Kimble <[hidden email]> wrote:
I've already done this in the -single-db branch of the plugin. It's also
added as a static method on the Tenant interface and application provided
implementation (if any). This way it's possible to use the convenient meta
method in tests and the service in production code.

Be aware that this branch is a work in progress. The goal is to either merge
changes back into -core or create separate plugins (core, multi-db and
single-db).

https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/grails-app/services/grails/plugin/multitenant/core/MultiTenantService.groovy
https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/src/groovy/grails/plugin/multitenant/singledb/MtSingleDbPluginSupport.groovy#L102

-----
 - Kim
Have a nice day!
--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-mock-TenantUtils-doWithTenant-tp3341772p3345593.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

scryan
Why not merge it into the existing code or release a new version of the plugin.  The other mode (multi db) does not need any of the support that was hindering the single-db version.  It only requires 3-4 classes to intercept the datasource but it does not need the falcone utils and it already supported the web flow.   It would be great to refactor and move out the new release.  We have been using a refactored version as well so it would be good to merge all this work and get it out to the masses.

Scott Ryan
CTO
Denver, Colorado  USA
(303) 263-3044

On Mar 10, 2011, at 6:33 AM, Lucas F. A. Teixeira wrote:

We're already testing this other branch, and even this "GOD MODE".
It is working great here.

[]s,


Lucas Frare Teixeira .·.
- [hidden email]
- lucastex.com.br
- blog.lucastex.com
- twitter.com/lucastex


On Thu, Mar 10, 2011 at 7:54 AM, Kimble <[hidden email]> wrote:
I've already done this in the -single-db branch of the plugin. It's also
added as a static method on the Tenant interface and application provided
implementation (if any). This way it's possible to use the convenient meta
method in tests and the service in production code.

Be aware that this branch is a work in progress. The goal is to either merge
changes back into -core or create separate plugins (core, multi-db and
single-db).

https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/grails-app/services/grails/plugin/multitenant/core/MultiTenantService.groovy
https://github.com/multi-tenant/grails-multi-tenant-single-db/blob/master/src/groovy/grails/plugin/multitenant/singledb/MtSingleDbPluginSupport.groovy#L102

-----
 - Kim
Have a nice day!
--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-mock-TenantUtils-doWithTenant-tp3341772p3345593.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

Kimble
Absolutely! I'm most inclined to move all Hibernate dependent code out of multi-tenant-core and into respectively multi-tenant-single-db and multi-tenant-multi-db (or something to that effect).

 + Separation of concerns, not one large plugin trying to do everything
 + No Hibernate code in core would make it easier to add support for various no-sql databases / key value stores
 + Cleaner plugin dependency graph, plugin dependencies can be defined where they're needed
 + The various plugins can be evolved and released individually
 + Fewer configuration options

What "concerns" me regarding merging the changes back is that I've made a lot of changes in -single-db that will break peoples code. If people are fine with this I'm very happy to go forward with the plan mentioned above.
 - Kim A. Betti
Have a nice day!
Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

lucastex
Seems the most conscient thing to do.


Lucas Frare Teixeira .·.
- [hidden email]
- lucastex.com.br
- blog.lucastex.com
- twitter.com/lucastex


On Thu, Mar 10, 2011 at 11:17 AM, Kimble <[hidden email]> wrote:
Absolutely! I'm most inclined to move all Hibernate dependent code out of
multi-tenant-core and into respectively multi-tenant-single-db and
multi-tenant-multi-db (or something to that effect).

 + Separation of concerns, not one large plugin trying to do everything
 + No Hibernate code in core would make it easier to add support for various
no-sql databases / key value stores
 + Cleaner plugin dependency graph, plugin dependencies can be defined where
they're needed
 + The various plugins can be evolved and released individually
 + Fewer configuration options

What "concerns" me regarding merging the changes back is that I've made a
lot of changes in -single-db that will break peoples code. If people are
fine with this I'm very happy to go forward with the plan mentioned above.

-----
 - Kim
Have a nice day!
--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-mock-TenantUtils-doWithTenant-tp3341772p3345938.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: How to mock TenantUtils.doWithTenant

kikoalemao
I'd really appreciate to have those improvements back into core, or better:
have the multi-tenant-multi-db finally made true.

I have to put it to work with a relativelly big project, that has been
living on a multiple instance life since always, but has outgrown and now
needs to be multi-tenant (single instance, multi-db). The first try was made
in ~March, with a modified version of the multi-tenant-core plugin, taken
off some parts that were specific to single-db, among other changes.

It was not me who made those first changes/try, so I'm struggling to
understand what's what here. I feel it's not what it was meant to be... but
i just don't have the understanding to go and change it so it looks better
to my eyes.

If anybody have at least made it work in multi-db environment, let me know
what were the main problems.

I already know I'll struggle in database-migrations, integration tests and
development environment. Have already struggled in
permissions/urlMappings(solved).

-----
Frederico Galvão - Goiânia GO, Brazil
Fibonacci Soluções Ágeis,
PontoGet Inovação Web
--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-mock-TenantUtils-doWithTenant-tp3341772p3808109.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Frederico Galvão - Goiânia GO, Brazil
Fibonacci Soluções Ágeis,
PontoGet Inovação Web