Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating Mongo to DevServices #213

Closed
agoncal opened this issue Jun 22, 2021 · 6 comments · Fixed by #247
Closed

Migrating Mongo to DevServices #213

agoncal opened this issue Jun 22, 2021 · 6 comments · Fixed by #247
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $300 https://www.jhipster.tech/bug-bounties/

Comments

@agoncal
Copy link
Contributor

agoncal commented Jun 22, 2021

Describe the bug

Today the generated application that use MongoDB as a primary database, use Mongock for testing. We should use DevServices instead. But some migration is needed

Migrating Configuration

Today the Mongo configuration is as follow:

jhi.mongodb.port=27017
%test.jhi.mongodb.port=37017
jhi.mongodb.host=localhost
%prod.jhi.mongodb.host=localhost
quarkus.mongodb.connection-string=mongodb://${jhi.mongodb.host}:${jhi.mongodb.port}
quarkus.mongodb.database=dummy

The end result should look like:

%test.quarkus.mongodb.devservices.image-name=mongo:4.2.7
%test.quarkus.mongodb.devservices.port=27017
quarkus.mongodb.database=dummy
%prod.quarkus.mongodb.connection-string=mongodb://localhost:27017

Migrating Test Configuration

MongoDbTestResource.

Pom.xml

We can get all the dependencies on MongoDB and Mongock and only leave quarkus-mongodb-panache

@agoncal agoncal mentioned this issue Jun 22, 2021
17 tasks
@agoncal
Copy link
Contributor Author

agoncal commented Jun 23, 2021

There is an authentication problem. All the tests in UserResourceTest fail with

status=401, title="Unauthorized", detail="HTTP 401 Unauthorized"

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <401>.

@agoncal
Copy link
Contributor Author

agoncal commented Jun 27, 2021

Here are some differences between the Mongo and SQL setup and code:

  • InitialSetupMigration.java: Creates the initial database setup
  • MongockConfiguration.java
  • reflect-config-mongo.json
  • MongoDbTestResource.java

The entire Liquibase configuration disapears in Mongo. But it looks like there is a Liquibase extension: https://github.com/liquibase/liquibase-mongodb

@agoncal
Copy link
Contributor Author

agoncal commented Jun 28, 2021

InitialSetupMigration can be simplified as follow:

public class InitialSetupMigration {

    @ConfigProperty(name = "quarkus.mongodb.database")
    String databaseName;

    public void onStart(@Observes StartupEvent ev) {
        MongoClient mongoClient = MongoClients.create(MongoClientSettings.builder().build());
        MongoDatabase db = mongoClient.getDatabase(databaseName);

But now there is another exception, due to the new version of BSON

Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'User'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.

Caused by: org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.

It looks like this is due to a wrong type of id. ATM the id is a String instead of an ObjectId

@MongoEntity(collection="jhi_user")
public class User extends PanacheMongoEntityBase implements Serializable {

    @BsonId
    public String id;

But that's because it inherits from PanacheMongoEntityBase instead of PanacheMongoEntity

public abstract class PanacheMongoEntity extends PanacheMongoEntityBase {

    public ObjectId id;

I checked the behaviour of the Spring version of the JHipster generator. It uses the version 3.11.2 of the org.mongodb and the Quarkus one uses 4.2.3.

Another exception that happens is:

invalid hexadecimal representation of an ObjectId: [user-1]

For that we need to create an id as an ObjectId (in hexa) and not just a string. We can do it with :

    User userUser = new User();
    userUser.id = new ObjectId();

But that means that we need an id of type ObjectId in User (therefore, inherit from PanacheMongoEntity):

@MongoEntity(collection="jhi_user")
public class User extends PanacheMongoEntity implements Serializable {

But that means that the DTO has to be changed too:

public class UserDTO {
    public ObjectId id;

And specify an ID in the test

anonymousUser.id = new ObjectId("60dacbd22a7680665f8b1111");

@mraible mraible added $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $300 https://www.jhipster.tech/bug-bounties/ labels Sep 21, 2021
@mraible
Copy link
Contributor

mraible commented Sep 21, 2021

Added a bug bounty to increase motivation.

@mraible
Copy link
Contributor

mraible commented Sep 10, 2022

@agoncal Are you interested in continuing this work? I'd like to see this blueprint succeed and I'm willing to help make it happen.

@agoncal
Copy link
Contributor Author

agoncal commented Sep 14, 2022

@mraible unfortunatelly I won't be able to work on that, too much on my plate :o(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $300 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants