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

Proposal to possibly simplify writing test cases for template generation #72

Open
AlexanderSkrock opened this issue Oct 21, 2023 · 0 comments

Comments

@AlexanderSkrock
Copy link

AlexanderSkrock commented Oct 21, 2023

When I tried to write testcases for another issue that I created, I realized it is not that easy to write test cases currently. Also there are two approaches to test cases at the moment. I would like to propose a possible take on this and would like to discuss possible improvements.

First, I spotted a couple aspects that influence the difficulty of writing tests:

  • the Generator class uses maven plugin specific code, e. g. MojoException
  • the Generator seems to do to many things to be tested easily, but testing GeneratorParser solely seems not to be enough as validating the created object structure is not our main goal
  • the Generator class needs a package name which makes it difficulty to test a single template class
  • the Generator class writes it output as file, thus we need to include the file system for testing.

Before writing some proposals, we need to maintain the core api classes as-is, including their location, so that any users are not affected. These are all annotations to define templates. From the current project structure the Generator classes are not part of the public api as the functionality is provided as maven plugin only.

From my perception we have the following blocks of functionality

  1. annotations for describing templates
  2. dicovery to find annotated classes within a specific scope
  3. parser to translate annotation drive descriptions into java objects
  4. converter to generate json templates from our java objects
  5. validator that uses the specified json schema to verify the generated templates
  6. export functionality to write the generated json to files
  7. maven plugin that bundles everything together and makes these functions accessibly

I would not neccessarily say that we need to decouple of all those. From a testing perspective I would rate tests that generate templates for a specific class and verify that generated json is as expected. This would test the afore-mentioned bullets from 3 and 4 which are roughly 70% to 90% of this code base.
Obviously, it also makes sense to have a few test cases for class disovery (bullet 2) and the maven plugin (bullet 7). For the latter I am not sure if there exists utility already to test maven plugin integration. For schema validations (bulett 5), we could put some of the previously released version into the test resources and write tests as well.

To simplify writing the majority of tests, as desribed earlier containing the logic within bullets 3 and 4, we would need to achieve two things:

  • Extract classgraph specific types for the generation call and prefer a class as input
  • Instead of writing to files we should return a json string

With these changes tests could look as simple as:

Template template = xxx.parse(MyExampleClass.class)
String templateJson = xxx.generateJson(template);
assertThat(templateJson).isEqualTo("""
{
    id: "ID"
}
""");

I know this is a lot of text and also a proposal to do major changes. You also might say, bigger refactorings are not in the current scope of development. Still, I wanted to write down my thoughts and possibly discuss approaches and alternatives.

Kind regards
Alexander Skrock

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

1 participant