Is it possible to mock afMongo's MongoClient or ConnectionManager? - mongodb

I am using afMongo for accessing a Mongo DB from a Fantom web app and I'm wondering if it is possible to mock the MongoClient or the ConnectionManager so that the Test classes don't need a Mongo DB running.

The short answer is no.
The long answer is yes, but you need to write the mock yourself and it's quite low level.
Connection is the thing to mock because it's a mixin. Internally, afMongo uses MockMongoConnection so you could try using / extending that.
Typical usage would be:
mmc = MockMongoConnection()
conMgr = ConnectionManagerLocal(mmc, ``)
mmc.reset.reply(...your map obj...)
You're probably better off wrapping your Mongo calls in your own DAO service and mocking that.

Related

how to implement multi-tenant using spring-data-mongodb

I am new to multi-tenancy with mongodb using spring-data-mongodb, we need to use spring-data-mongodb for rest APIs and scheduling tasks( we have more than one schedulers in our application) in the same code with thread-safe. Is autowiring mongoTemplate will make application thread safe as the same mongoTemplate will be accessed from Schedulers and APIs both. Please get me the good practice in such a situation.
Regards
Kris
MongoTemplate itself is thread-safe, i.e. you can call it from multiple threads at the same time, and it will work correctly, i.e. send the different requests correctly to MongoDB.
But that doesn't guarantee consistency: if the scheduler is running and executes multiple updates in the same task, an API call can possibly get some updated records and some other records that aren't updated yet.
By the way: multi-tenancy is having data from multiple organisational entities in the same database. I'm not sure how that links to your question, did you mean multi-threading?
If you use different databases, then you can't use an autowired MongoTemplate.
For autowiring, there must be a single instance, but since the database connection string is a dependency of a MongoTemplate, there must be a single database as well.
You could go for an approach where you do not auto-wire the MongoTemplate directly, but use some sort of factory pattern to create the correct MongoTemplate for the current tenant. See Making spring-data-mongodb multi-tenant for some examples. (It's an old question, but its answers get updated every now and then).
Or you could go with an infrastructural solution, and deploy separate instances of your application, one for each tenant, e.g. on Kubernetes.

Where to store database connection using Slick standalone

I found many examples online that put a call to Database.forConfig inside a trait, and each repository extends this trait. Some examples:
https://github.com/BBartosz/akkaRestApi/blob/master/src/main/scala/utils/DatabaseConfig.scala
https://github.com/Platoonhead/SlickWithScala/blob/master/src/main/scala/com/edu/knoldus/connection/ConnectedDbMysql.scala
https://github.com/cdiniz/slick-akka-http/blob/master/src/main/scala/utils/PersistenceModule.scala
Will it lead to creating too many instances of the DB client object, memory overhead, any other performance problems, when having many repositories?
Isn't it better to have one object that will call Database.forConfig and will have a link to the database?
What is the best practice here?
Here is an example of how I did it:
https://github.com/joesan/plant-simulator/blob/master/app/com/inland24/plantsim/config/AppConfig.scala
So what I basically do is that, I create a single copy of the variable that will call the Slick API and then I specify the number of threads (effectively the number of connections) that I want in the connection pool.
http://slick.lightbend.com/doc/3.0.0/database.html

Best way to use Scala MongoClient in Play 2.0

I have just started using Play 2.0 with Scala and Casbah for connecting to MongoDB. I have been able to connect to my MongoDB instance but what I am looking for is a way to be able to access the MongoClient from all my model classes.
Is there any DependencyInjection way to inject mongoClient in all Scala models ? or
Should I have one Scala object which initialises the MongoClient and use that object to refer to MongoClient in all my models ? or
Is there a more better way to do this ?
As MongoClient uses a connection pool internally, its optimal to only have a single instance for your application and that single object can then be used by all your models.
Also, you could look at Salat which might do what you require or give you an idea on how best to implement your own models.

.NET Session State Caching with Redis, MongoDB, ServiceStack

I have been doing some research on whether it is ok or not to cache .NET Session State in external Dbs such as Redis, MongoDb, or other highly scalable tools.
The output of my research was that even though MongoDB has more integration to do this kind of things, it seems that Redis is far more performant and has way more options (key expiration, sets, etc) to use.
There is this other framework called ServiceStack which has an implementation of a RedisClient but IMHO the way it is implemented is far more coupled than I would like to.
public override object OnGet(CachedOrders request)
{
var cacheKey = "some_unique_key_for_order";
return base.RequestContext.ToOptimizedResultUsingCache(this.CacheClient, cacheKey, () =>
{
//This delegate will be executed if the cache doesn't have an item
//with the provided key
//Return here your response DTO
//It will be cached automatically
});
}
So after this research, I would like to know your opinion and whether you have implemented this kind of caching in any of your apps. Can you please share you experiences?
Thanks!
ServiceStack's caching isn't coupled, the ToOptimizedResultUsingCache() method is just a convenience Extension method allowing you implement a common caching pattern in the minimal boilerplate necessary. The ToOptimizedResult method returns the most optimized result based on the MimeType and CompressionType from the IRequestContext. e.g. in a JSON service it would normally be the deflate'd output of the JSON Response DTO.
You don't have to use the extension method and can access the ICacheClient API directly as it's an auto-wired property in the ServiceBase class. If you require more functionality than the ICacheClient API can provide, I recommend using Redis and ServiceStack's C# RedisClient which gives you fast, atomic access to distributed comp-sci collections.
The benefit of using an ICacheClient API is that it's a testable implementation agnostic caching interface that currently has InMemory, Redis and Memcached providers.
I wrote a NuGet package for this 2 years ago, and we've been using it in production since then. It overrides the .NET SessionStateProvider with a custom class that allows persistence to Redis, with various features like:
Only write to Redis on values changing
Concurrent session access from multiple requests with the same session ID
Ability to access the session from Web API (although doing so violates REST principles, if you care)
Easy customization of serialization format (roll your own or change Json structure)
You can get it here: https://www.nuget.org/packages/RedisSessionProvider
Docs: https://github.com/welegan/RedisSessionProvider

Test MongoDB Interactions in a PHP Application With Mocking

What is the best practice to work efficiently with MongoDB and PHPUnit? What should (or could) I use to mock objects that access MongoDB? PHPUnit Mocker, Mockery, Phrophecy, Phactory?
If you look at mocking data for SQL databases, there are lots of opinions here.
Some people suggest using an in-memory SQL database.
Some people just mock the ORM calls and assume that the ORM to DB portion is tested.
Some people just use a "local" DB for unit testing and just ignore the whole "mocking" concept.
Given the lack of consensus on SQL, it's even less likely that you will find consensus on the new DBs like MongoDB.
I think there are some important details to consider here.
Are you using some form of ORM / ODM? Just the driver directly?
Are you trying to mock all communications with the DB? Are you trying to mock the ODM?
If you are just trying to mock communications to DB, then the ideal solution is a "fake" implementation of the MongoDB driver. This is probably a lot of work as the driver was never written with "mockability" in mind.
If you have an ODM, then you can simply mock the ODM calls and assume the ODM is doing its job. Ideally the ODM should provide some mockable interface, but this is not always the case.
Again, this answer comes back down to what you're really planning to test and what you consider as a good unit test. Unfortunately, most of these products are still very new so there is very little guidance in this space.
Phactory provides direct support for mocking MongoDB.
Edit: Phactory is no longer maintained. However, I've found a new project called php-mongomock that seems to solve this problem:
<?php
use Helmich\MongoMock\MockCollection;
$collection = new MockCollection();
$collection->createIndex(['foo' => 1]);
$documentId = $collection->insertOne(['foo' => 'bar'])->insertedId();
$collection->updateOne(['_id' => $documentId], ['$set' => ['foo' => 'baz']]);