mongo-mapper
mongo-mapper copied to clipboard
Arquillian Test
Hi, I've been using this library for a while now. I'm trying to test some stuff with arquillian using some in memory mongo instance:
The thing is the codecs doesn't seem to be registering so i get:
CodecConfigurationException: Can't find a codec for class coop.magnesium.db.entities.Tool.
any thoughts? thank you.
Hello, I'd need more details about it.
Thanks for the reply, below you can see the code, i think the in memory mongo instance it's working fine since I also tried starting a regular mongo server in localhost getting the exact same error. Let me know if you need to see something else. Thank you.
This is the Mongo Client Provider:
@ApplicationScoped
public class MongoClientProvider {
private static String CONNECTION_STRING = System.getenv("DB_HOST") != null ? System.getenv("DB_HOST") : "localhost";
private static String DATABASE_NAME = System.getenv("DB_NAME") != null ? System.getenv("DB_NAME") : "omicflows";
private static String COLLECTION_TOOL = "tool";
private static String COLLECTION_USER = "user";
private static String COLLECTION_WORKFLOW = "workflow";
private static String COLLECTION_JOB = "job";
@Inject
Logger logger;
private MongoClient mongoClient = null;
@Lock(LockType.READ)
public MongoClient getMongoClient() {
return mongoClient;
}
@Lock(LockType.READ)
public MongoDatabase getDatabase() {
return getMongoClient().getDatabase(DATABASE_NAME);
}
@Lock(LockType.READ)
public MongoCollection<Tool> getToolCollection() {
return getDatabase().getCollection(COLLECTION_TOOL, Tool.class);
}
@Lock(LockType.READ)
public MongoCollection<User> getUserCollection() {
return getDatabase().getCollection(COLLECTION_USER, User.class);
}
@Lock(LockType.READ)
public MongoCollection<Workflow> getWorkflowCollection() {
return getDatabase().getCollection(COLLECTION_WORKFLOW, Workflow.class);
}
@Lock(LockType.READ)
public MongoCollection<Job> getJobsCollection() {
return getDatabase().getCollection(COLLECTION_JOB, Job.class);
}
@PostConstruct
public void init() {
Logger mongoLogger = Logger.getLogger("org.mongodb");
mongoLogger.setLevel(Level.INFO);
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(MongoMapper.getProviders()));
MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry)
.build();
try {
mongoClient = new MongoClient(CONNECTION_STRING, options);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is the test:
@RunWith(Arquillian.class)
public class ToolServiceTest {
@Inject
MongoClientProvider mongoClientProvider;
@Deployment
public static WebArchive createDeployment() {
File[] libs = Maven.resolver()
.loadPomFromFile("pom.xml")
.importRuntimeAndTestDependencies()
.resolve()
.withTransitivity().as(File.class);
return ShrinkWrap.create(WebArchive.class)
.addClass(JAXRSConfiguration.class)
.addClass(ToolServiceTest.class)
.addClass(MongoClientProvider.class)
.addPackage(Tool.class.getPackage())
.addPackage(Logged.class.getPackage())
.addAsResource("endpoints.properties")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsLibraries(libs);
}
@Before
public void init() throws IOException {
MongodStarter starter = MongodStarter.getDefaultInstance();
String bindIp = "localhost";
int port = 27017;
IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
.build();
MongodExecutable mongodExecutable = null;
mongodExecutable = starter.prepare(mongodConfig);
MongodProcess mongod = mongodExecutable.start();
mongoClientProvider.getToolCollection().insertOne(new Tool());
}
@Test
@InSequence(1)
public void createTarea() {
mongoClientProvider.getToolCollection().insertOne(new Tool());
}
}
I get the error in this line:
mongoClientProvider.getToolCollection().insertOne(new Tool());
ERROR [stderr] (default task-2) org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class coop.magnesium.db.entities.User.
ERROR [stderr] (default task-2) at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
ERROR [stderr] (default task-2) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
ERROR [stderr] (default task-2) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
ERROR [stderr] (default task-2) at com.mongodb.MongoCollectionImpl.getCodec(MongoCollectionImpl.java:591)
ERROR [stderr] (default task-2) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:314)
ERROR [stderr] (default task-2) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:307)
15:54:00,974 ERROR [stderr] (default task-2) at coop.magnesium.db.MongoClientProvider.init(MongoClientProvider.java:89)