#SpringBootTest how to pre-populate embedded MongoDB? - mongodb

Doing implementation of a microservice with a few endpoints based on Spring Boot and MongoDB and trying to write integration tests using #SpringBootTest annotation capabilities.
At the moment, I am facing an issue that I need to pre-populate an embedded MongoDB instance that instantiated only during 'test' phrase with some test data.
And I did not find any out-of-the-box option available in Spring Boot for this purpose.
Some people advice to use for test data pre-populating tools like mongobee or mongoprefill or nosql-unit but for me, it seems like overhead or workaround, do not want to introduce any new dependencies even in test scope.
So could you please advice: In the current Spring Boot ecosystem, what is the right way to pre-populate MongoDB for testing purpose, when we are talking about integration (end-to-end) testing with #SpringBootTest?

There are multiple ways to pre-populate data:
Use the JUnit lifecycle methods like #BeforeEach, #BeforeAll to fill in data
You could disable the Spring Boot autoconfiguration for the embedded MongoDB and do it on your own and insert data after creating the connection
You could somehow mirror the #Sql feature we have for testing relational databases and write something similar using the AsbtractTestExectuionListener. For this have a look at the Spring class SqlScriptsTestExecutionListener
Provide a class that implements the CommandLineRunner interface and only activate this bean for your integration test profile with #Profile("integration-test")

Related

Is it possible to use both Spring Data R2DBC and Spring Data JPA in a single Spring boot application?

I have an application which is using Spring data JPA and hibernate envers for db auditing.
Since R2DBC doesn't support auditing yet, is it possible to use combination of both in a single application?
If yes, Plan is to use Spring Data JPA for insert, update and delete operations, so that all DB auditing will be handled by hibernate envers. And use R2DBC for reactive non-blocking API's to read data.
If no, Is there any suggestions on how to achieve both reactive API's and auditing?
Spring provided simple Auditing via #EnableR2dbcAuditing, check my example.
Mix JPA in a reactive application is also possible, I have an example to demo run JPA in a reactive application, but not added r2dbc in it.
For your plan, a better solution is applying the cqrs pattern on the database topology, use database cluster for your application.
JPA for applying changes, use the main/master database to accept the modification, and sync changes to the secondary/slave database.
r2dbc for queries as you expected, use the secondary/slave database to query.
use a gateway at the front for the query and command service.
Update: I have created a sample to demo JPA and R2dbc coexistence in a single webflux application. But I do not suggest using it in real-world applications. Consider the 3rd solution above if you really need it.
Yes it is possible however you will probably face two issues, one is that multiple repos modules handling needs to be explicited (e.g specify the paths of the respective modules).
Secondly, the JDBC/JPA Spring Boot autoconfiguration will be disabled and you need to import it back.
Those issues have been reported in Reactive and Non Reactive repository in a Spring Boot application with H2 database
And a solution to them and the thought process can be found in this issue:
https://github.com/spring-projects/spring-boot/issues/28025

What are admin UIs for spring-boot with mongoDB, providing CRUD operation like django-admin?

I am implementing a new spring-boot application with mongoDB, earlier worked with djangoAdmin. Looking for a similar solution which can easily generate management UI for all classes decorated with #document.
Have you seen JHipster?
It uses an Angular frontend with a Spring Boot backend but comes with a ton of admin functionality right out of the box. This includes the turn-key CRUD operations like you mentioned.
There is http://lightadmin.org project. But it currently supports only Spring Data JPA.

Using Different Database Providers for Development vs Production with JPA

I have seen some documentation on connecting to more than one database/data source via JPA, but this is not what I want to do exactly. I want to use one database provider for development and a completely different database provider for production (and probably QA). This question would also be applicable to supporting different production environments with different database providers (ie, DB2 for Client 1, Oracle for Client 2, MySQL for Client 3, etc).
In my case, I would like to do the following:
use JPA with Apache Derby for development purposes
use JPA with IBM DB2 for production purposes
We do not use Spring :(
With MyBatis, I could simply provide XML mappings for each database I wanted to support. And map all of results back to the same entities/POJOs.
So far it seems to me, that JPA will require custom everything up to maybe the Service layer (with entities, DAO, sql all being custom per database).
I want to know how you would go about setting up your application to use Derby for development and DB2 for production. Can I get by with using the same entities for both, should I separate the DAO layer into separate projects, can I use one persistence.xml, etc.?
Please let know what approach you would suggest to accommodate this.
This is a pretty common scenario, you don't need to duplicate anything if you are using JPA properly. You can use the same #Entity classes and DAOs.
If you are using Maven you can make the connection settings specific to some Maven profile, e.g. create profile for each environment and then let Maven resource filtering populate properties in your persistence.xml file (see this answer).

Difference between ReactiveMongo , Mongo-Module and MongoHelper

Mongo with Play-Framework is new for me. I have Spring-Framework background, In which we create Controller, Business and DAO layer structure applications. Now, i want to taste the flavor of Play-Framework with ReactiveMongo. For create our sample Application, my first target is to follow Controller, Business and DAO layer structure. Play-Framework already provide the basic structure of application, which is really good. If we use Mongodb with Play-Framework, I found three types of MongoDB Drivers. These are as below:
Mongo Module (https://www.playframework.com/modules/mongo-1.1/home)
MongoHelper (https://gist.github.com/harmeetsingh0013/fdc9c20b056a62f303f5)
ReactiveMongo(http://reactivemongo.org/)
ReactiveMongo, which is really awesome for implementing non-blocking I/O. In reactive mongo there are some problem with sync the data from multiple queries. I have some queries for creating sample application as below:
My first target is to implement MVC using ReactiveMongo, which i think ReactiveMongo-Extension provide me Dao layer. But i still want to confirm, ReactiveMongo-Extension is a good approach ? or I need to go for MongoHelper? What is the benefit of Mongo-Module over reactive mongo?
For dependency Injection we need to go for Spring-Dependency Injection or Guice or other Scala Dependency Injection Framework ?
If i am go for ReactiveMongo, we need to go for JSONCollection because BSONCollection is MongoDB Dependency. What is the support for JSONCollection available?
Current modules repository on playframework.com is just for Play 1.x. So Mongo Module is not an option for you.
I am the author of ReactiveMongo-Extensions and it is highly inspired from MongoHelper. You can use which one you want. If you choose ReactiveMongo-Extensions JsonDao is your friend. There is also an example application which you can check out: https://github.com/mertkavi/play-reactivemongo-extensions-sample
BTW I am currently developing a newer driver from scratch. You may also give it a try: https://github.com/fehmicansaglam/tepkin

Spring Data JPA and SimpleJPA/SimpleDB

I'm wondering if it is possible to specify SimpleJPA as the persistence provider used by Spring-Data-JPA. I'm not a JPA expert so I hope this question isn't silly. Is it as easy as just specifying SimpleJPA's entity manager factory in the persistence.xml? I have yet to find anywhere online where someone has used Spring-Data-JPA to connect to Amazon's SimpleDB, I would love it if someone could point me in the right direction.
I just found this project and got it working for our Spring Data + SimpleDB needs:
https://github.com/3pillarlabs/spring-data-simpledb
The documentation is pretty good and getting things up and running was pretty straight-forward. I was accessing SimpleDB via Spring Data with about 10 minutes of work.
Generally setting up your persistence provider is just a matter of setting up the EntityManagerFactory through your Spring context (in case you use the Spring container). Thus have a look at how to configure EntityManagerFactory instances in Spring.
There might be the need to implement a custom JpaVendorAdapter to let Spring use the SimpleDB JPA implementation correctly. For some advanced functionality (e.g. using pagination with manually defined queries) we'd have to tweak the Spring Data JPA codebase a bit. If you'd like to see that supported feel free to open a ticket in our JIRA.