mongounit / mongounit

MongoUnit is a data driven integration testing framework for Spring Boot based applications that use MongoDB for persistence. The framework enables the developer to test the data access logic with relative ease.
https://mongoUnit.org
Apache License 2.0
24 stars 7 forks source link

Error comparing DB_POINTER attributes #31

Open arisnegro opened 1 year ago

arisnegro commented 1 year ago

Hi!

When the expected value is a DB_POINTER, MongoUnit fails loading the dataset.

Expected data: "fieldDbPointer": { "$$DB_POINTER": { "namespace": "categories", "objectId": "5db7545b7b615c739732c773" }}

Error: org.bson.BsonInvalidOperationException: Value expected to be of type OBJECT_ID is of unexpected type DB_POINTER

at org.bson.BsonValue.throwIfInvalidType(BsonValue.java:419)
at org.bson.BsonValue.asObjectId(BsonValue.java:150)
at org.mongounit.MongoUnitUtil.getFieldValue(MongoUnitUtil.java:493)
at org.mongounit.MongoUnitUtil.getDocument(MongoUnitUtil.java:331)

Bye!!

ychaikin commented 1 year ago

Will investigate... Can you provide an example of an export of mongoDB dataset that uses DB_POINTER type?

arisnegro commented 1 year ago

Hi!!

The example is there:

"fieldDbPointer": { "$$DB_POINTER": { "namespace": "categories", "objectId": "5db7545b7b615c739732c773" }}

:) :)

For example, you can use a data set file like this one:

[ {
  "collectionName" : "categories",
  "documents" : [ {
    "name" : "Category_1",
    "_id" : {
      "$$OBJECT_ID" : "5db7545b7b615c739732c773"
    }
  }, {
    "name" : "Category_2",
    "_id" : {
      "$$OBJECT_ID" : "5db7545b7b615c739732c774"
    }
  } ]
}, {
  "collectionName" : "items",
  "documents" : [ {
    "name" : "Item 2",
    "_id" : 2
  }, {
    "name" : "Item 1",
    "_id" : 1,
    "fieldDbPointer": { "$$DB_POINTER": { "namespace": "categories", "objectId": "5db7545b7b615c739732c773" }}
  } ]
} ]

If you use this file to feed and assert the database, you will get this exception:

org.bson.BsonInvalidOperationException: Value expected to be of type OBJECT_ID is of unexpected type DB_POINTER

    at org.bson.BsonValue.throwIfInvalidType(BsonValue.java:419)
    at org.bson.BsonValue.asObjectId(BsonValue.java:150)
    at org.mongounit.MongoUnitUtil.getFieldValue(MongoUnitUtil.java:493)
    at org.mongounit.MongoUnitUtil.getDocument(MongoUnitUtil.java:331)
    at org.mongounit.MongoUnitUtil.getMongoUnitDocuments(MongoUnitUtil.java:302)
    at org.mongounit.MongoUnitUtil.fromDatabase(MongoUnitUtil.java:118)

The problem could be in the method org.mongounit.MongoUnitUtil#getFieldValue:

...

      case DB_POINTER:
        String namespace = bsonValue.asDBPointer().getNamespace();
        String objectId = bsonValue.asObjectId().getValue().toHexString();

        Map dbPointerValueMap = new HashMap<>();
        dbPointerValueMap.put("namespace", namespace);
...

The invocation bsonValue.asObjectId() seems to not be valid.