Skip to content

Commit

Permalink
✅ New tests for constructor rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lengors committed Aug 31, 2024
1 parent 5fa38d9 commit b0288e5
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 0 deletions.
170 changes: 170 additions & 0 deletions src/test/java/io/github/lengors/js2pets/rules/ConstructorRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package io.github.lengors.js2pets.rules;

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.rules.Rule;
import org.jsonschema2pojo.rules.RuleFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JDefinedClass;

import io.github.lengors.js2pets.annotators.Annotator;
import lombok.Getter;

/**
* Unit tests for the {@link ConstructorRule} class.
*
* This test class verifies the correct behavior of the {@link ConstructorRule}, particularly its ability to include or
* exclude no-args constructors based on configuration and to notify annotators if required.
*
* @author lengors
*/
@ExtendWith(MockitoExtension.class)
public class ConstructorRuleTest implements ConstructorRuleTestSuite {

/**
* Mock rule used to simulate the super constructor rule behavior.
*/
@Mock
@Getter
@MonotonicNonNull
private Rule<JDefinedClass, JDefinedClass> superConstructorRule;

/**
* Mock JSON node used for testing.
*/
@Mock
@Getter
@MonotonicNonNull
private JsonNode node;

/**
* Mock schema used for testing.
*/
@Mock
@Getter
@MonotonicNonNull
private Schema currentSchema;

/**
* Mock rule factory used to provide configuration and utilities for testing.
*/
@Mock
@Getter
@MonotonicNonNull
private RuleFactory ruleFactory;

/**
* Tests that the rule correctly includes a no-args constructor when configured to do so.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyIncludeNoArgsConstructor() throws JClassAlreadyExistsException {
testForSuccessWithoutPluginImplementation(true);
}

/**
* Tests that the rule correctly excludes a no-args constructor when configured to do so.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyExcludeNoArgsConstructor() throws JClassAlreadyExistsException {
testForSuccessWithoutPluginImplementation(false);
}

/**
* Tests that the rule fails to infer the inclusion of a no-args constructor when the Mojo configuration is not used.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldFailToInferNoArgsConstructorInclusionDueToMojoNotUsed() throws JClassAlreadyExistsException {
ConstructorRuleTestRunner
.prepareWithoutPluginImplementation(this, null)
.testForFailure();
}

/**
* Tests that the rule correctly infers the inclusion of a no-args constructor based on plugin configuration.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyInferIncludeNoArgsConstructor() throws JClassAlreadyExistsException {
testForSuccessWithPluginImplementation(true);
}

/**
* Tests that the rule correctly infers the exclusion of a no-args constructor based on plugin configuration.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyInferExcludeNoArgsConstructor() throws JClassAlreadyExistsException {
testForSuccessWithPluginImplementation(false);
}

/**
* Tests that the rule correctly infers the default inclusion of a no-args constructor.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyInferDefaultInclusionOfNoArgsConstructor() throws JClassAlreadyExistsException {
ConstructorRuleTestRunner
.prepare(this, 0)
.testForSuccess(true);
}

/**
* Tests that the rule fails to infer the inclusion of a no-args constructor due to multiple executions.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldFailToInferNoArgsConstructorInclusionDueToMultipleExecutions() throws JClassAlreadyExistsException {
ConstructorRuleTestRunner
.prepare(this, 2)
.testForFailure();
}

/**
* Tests that the rule correctly notifies the annotator about the constructor if configured.
*
* @throws JClassAlreadyExistsException if a class with the same name already exists.
*/
@Test
public void shouldCorrectlyNotifyAnnotator() throws JClassAlreadyExistsException {
final var annotator = Mockito.mock(Annotator.class);

final var includeNoArgsConstructor = false;
final var runner = ConstructorRuleTestRunner.prepareWithoutPluginImplementation(this, includeNoArgsConstructor);
runner.testForSuccess(annotator, includeNoArgsConstructor);

Mockito
.verify(annotator, Mockito.only())
.constructor(runner.getArgsConstructor());
}

private void testForSuccessWithoutPluginImplementation(final boolean includeNoArgsConstructor)
throws JClassAlreadyExistsException {
ConstructorRuleTestRunner
.prepareWithoutPluginImplementation(this, includeNoArgsConstructor)
.testForSuccess(includeNoArgsConstructor);
}

private void testForSuccessWithPluginImplementation(final boolean includeNoArgsConstructor)
throws JClassAlreadyExistsException {
ConstructorRuleTestRunner
.prepare(this, includeNoArgsConstructor)
.testForSuccess(includeNoArgsConstructor);
}
}
Loading

0 comments on commit b0288e5

Please sign in to comment.