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

Arquillian Test #14

Open
raulsperoni opened this issue Dec 1, 2017 · 2 comments
Open

Arquillian Test #14

raulsperoni opened this issue Dec 1, 2017 · 2 comments

Comments

@raulsperoni
Copy link

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:

de.flapdoodle.embed de.flapdoodle.embed.mongo 2.0.0 test

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.

@dozd
Copy link
Owner

dozd commented Dec 3, 2017

Hello, I'd need more details about it.

@raulsperoni
Copy link
Author

raulsperoni commented Dec 4, 2017

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants