Loading data models from the classpath in unit tests

So we want to use unit tests that make use of the real data models in our project, so we would like to load the data models (and eventually documents as well) from the classpath without the need of a running “dataservices server” that connects to the database.

For that I used the internal classes from dataservices-core as a blueprint and basically copied the things found AbstractKernelAwareTest, KernelTestSupport AbstractTestResourcesModelLoader and adapted them to what we need in our project. It works for the most part, but it fails to resolve our included type definitions.

The class TestResourcesDocumentModelResolver does most of the work and it uses the expand() method to resolved includes and that’s where the loading fails when including our TypeDefs.json with the error message:

Cannot expand import because the root group of the imported model [=MyProjectTypeDef] contains elements.

I guess I implemented the “include” mechanism incorrectly and I need to use a different serializer for typedef includes, but I couldn’t find any sample code to do that.

Any ideas?

From the message I’d guess it’s a problem with the model NovaTypeDef. Looks like an invalid model?

Your setup might be simplified a lot if you add an include expansion as a pre-processing, for instance via gradle task, that expands all models before executing the tests.

Your tests can then simply use/load the effective models and the respective data. Test execution performance should also improve.

I remember that, in the past, we had a batch API from kernel that did the expansion for a set of models.

The NovaTypeDef file is created and maintained in the SME, so I assume the file is valid.

Hmm, but which code would I use inside the gradle task to expand the models?

I am not a fan of generation steps that are required before the code can run, so I would really like to have code that does this “on the fly”. Can you point me to that “batch API” from the kernel? Maybe I can re-use that.

The batch API class is the following:
com.mgmtp.a12.kernel.md.facade.cli.BatchDocumentModelExpander

I also noticed that there is fat-jar specifically for that API:

<dependency>
    <groupId>com.mgmtp.a12.kernel</groupId>
    <artifactId>kernel-md-facade</artifactId>
    <version>30.1.1</version>
    <classifier>BatchDocumentModelExpander</classifier>
</dependency>

or

compile(group: 'com.mgmtp.a12.kernel', name: 'kernel-md-facade', version: '30.1.1', classifier: 'BatchDocumentModelExpander')

Thanks, that put me on the right track.

All I had to add to my code was a call to ExternalDocumentModelService.expand() using a FilePathIncludedDocumentModelResolver.