Skip to content

Commit

Permalink
add samples builder for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 6, 2024
1 parent 848305e commit ec1a2d7
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class <%= persistClass %>Test {
void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(<%= persistClass %>.class);
<%_if (!embedded) { _%>
<%= persistClass %> <%= persistInstance %>1 = get<%= persistClass %>Sample1();
<%= persistClass %> <%= persistInstance %>1 = build<%= persistClass %>Sample1();
<%= persistClass %> <%= persistInstance %>2 = new <%= persistClass %>();
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);

<%= persistInstance %>2.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
assertThat(<%= persistInstance %>1).isEqualTo(<%= persistInstance %>2);

<%= persistInstance %>2 = get<%= persistClass %>Sample2();
<%= persistInstance %>2 = build<%= persistClass %>Sample2();
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
<%_ } _%>
}
Expand All @@ -57,7 +57,7 @@ class <%= persistClass %>Test {
<%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
assertThat(<%= persistInstance %>.hashCode()).isZero();

<%= persistClass %> <%= persistInstance %>1 = get<%= persistClass %>Sample1();
<%= persistClass %> <%= persistInstance %>1 = build<%= persistClass %>Sample1();
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
assertThat(<%= persistInstance %>).hasSameHashCodeAs(<%= persistInstance %>1);
}
Expand All @@ -66,8 +66,8 @@ class <%= persistClass %>Test {

@Test
void <%- relationship.relationshipName %>Test() throws Exception {
<%= persistClass %> <%= persistInstance %> = get<%= persistClass %>RandomSampleGenerator();
<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntity.persistInstance %>Back = get<%= relationship.otherEntity.persistClass %>RandomSampleGenerator();
<%= persistClass %> <%= persistInstance %> = build<%= persistClass %>RandomSampleGenerator();
<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntity.persistInstance %>Back = build<%= relationship.otherEntity.persistClass %>RandomSampleGenerator();
<%_ if (relationship.collection) { _%>

<%= persistInstance %>.add<%- relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import java.util.concurrent.atomic.AtomicInteger;
<%_ } _%>

<%_ const sampleFields = fields.filter(field => !field.mapstructExpression && (field.fieldTypeInteger || field.fieldTypeLong || field.fieldTypeString || field.fieldTypeUUID)); _%>
<%_ const autoGenerateSampleFields = sampleFields.filter(field => field.autoGenerate); _%>
<%_ const manualGenerateSampleFields = sampleFields.filter(field => !field.autoGenerate); _%>
public class <%= persistClass %>TestSamples {

<%_ if (fields.some(field => field.fieldTypeLong || field.fieldTypeInteger)) { _%>
Expand All @@ -47,39 +49,80 @@ public class <%= persistClass %>TestSamples {
private static final AtomicInteger intCount = new AtomicInteger(random.nextInt() + ( 2 * Short.MAX_VALUE ));
<%_ } _%>

public static <%= persistClass %> get<%= persistClass %>Sample1() {
private <%= persistClass %>TestSamples() {}

public static <%= persistClass %> build<%= persistClass %>InsertableSample1() {
<%_ if (fluentMethods) { _%>
return new <%= persistClass %>()<% sampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample1 %>)<% }) %>;
return new <%= persistClass %>()<% manualGenerateSampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample1 %>)<% }) %>;
<%_ } else { _%>
<%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
<%_ for (const field of sampleFields) { _%>
<%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- field.javaValueSample1 %>);
<%_ for (const field of manualGenerateSampleFields) { _%>
<%= persistInstance %>.set<%= field.propertyJavaBeanName %>(<%- field.javaValueSample1 %>);
<%_ } _%>
return <%= persistInstance %>;
<%_ } _%>
}

public static <%= persistClass %> get<%= persistClass %>Sample2() {
public static <%= persistClass %> build<%= persistClass %>Sample1() {
<%_ if (fluentMethods) { _%>
return new <%= persistClass %>()<% sampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample2 %>)<% }) %>;
return build<%= persistClass %>InsertableSample1()<% autoGenerateSampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample1 %>)<% }) %>;
<%_ } else { _%>
<%= persistClass %> <%= persistInstance %> = build<%= persistClass %>InsertableSample1();
<%_ for (const field of autoGenerateSampleFields) { _%>
<%= persistInstance %>.set<%= field.propertyJavaBeanName %>(<%- field.javaValueSample1 %>);
<%_ } _%>
return <%= persistInstance %>;
<%_ } _%>
}

public static <%= persistClass %> build<%= persistClass %>InsertableSample2() {
<%_ if (fluentMethods) { _%>
return new <%= persistClass %>()<% manualGenerateSampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample2 %>)<% }) %>;
<%_ } else { _%>
<%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
<%_ for (const field of sampleFields) { _%>
<%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- field.javaValueSample2 %>);
<%_ for (const field of manualGenerateSampleFields) { _%>
<%= persistInstance %>.set<%= field.propertyJavaBeanName %>(<%- field.javaValueSample2 %>);
<%_ } _%>
return <%= persistInstance %>;
<%_ } _%>
}

public static <%= persistClass %> build<%= persistClass %>Sample2() {
return apply<%= persistClass %>UpdateSampleToInsertableSample1(build<%= persistClass %>InsertableSample2());
}

public static <%= persistClass %> apply<%= persistClass %>UpdateSampleToInsertableSample1(<%= persistClass %> <%= persistInstance %>) {
<%_ if (fluentMethods) { _%>
return <%= persistInstance %><% autoGenerateSampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueSample2 %>)<% }) %>;
<%_ } else { _%>
<%_ for (const field of autoGenerateSampleFields) { _%>
<%= persistInstance %>.set<%= field.propertyJavaBeanName %>(<%- field.javaValueSample2 %>);
<%_ } _%>
return <%= persistInstance %>;
<%_ } _%>
}

public static <%= persistClass %> get<%= persistClass %>RandomSampleGenerator() {
public static <%= persistClass %> build<%= persistClass %>RandomSampleGenerator() {
<%_ if (fluentMethods) { _%>
return new <%= persistClass %>()<% sampleFields.forEach(field => { %>.<%- field.fieldName %>(<%- field.javaValueGenerator %>)<% }) %>;
<%_ } else { _%>
<%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
<%_ for (const field of sampleFields) { _%>
<%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- field.javaValueGenerator %>);
<%= persistInstance %>.set<%= field.propertyJavaBeanName %>(<%- field.javaValueGenerator %>);
<%_ } _%>
return <%= persistInstance %>;
<%_ } _%>
}

public static <%= persistClass %> clone(<%= persistClass %> <%= persistInstance %>) {
<%_ if (fluentMethods) { _%>
return new <%= persistClass %>()<% sampleFields.forEach(field => { %>.<%- field.fieldName %>(<%= persistInstance %>.get<%= field.propertyJavaBeanName %>())<% }) %>;
<%_ } else { _%>
<%= persistClass %> new<%= persistClass %> = new <%= persistClass %>();
<%_ for (const field of sampleFields) { _%>
new<%= persistClass %>.set<%= field.propertyJavaBeanName %>(<%= persistInstance %>.get<%= field.propertyJavaBeanName %>());
<%_ } _%>
return new<%= persistClass %>;
<%_ } _%>
}
}
11 changes: 11 additions & 0 deletions generators/spring-boot/entity-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ const { MAPSTRUCT } = MapperTypes;
const { ServiceTypes } = entityOptions;
const { SERVICE_CLASS, SERVICE_IMPL } = ServiceTypes;

export const sampleFiles = {
sampleFiles: [
javaTestPackageTemplatesBlock({
relativePath: '_entityPackage_/domain/',
condition: data => !data.embedded && data.databaseTypeSql && !data.reactive,
templates: ['_persistClass_IntegrationTestSamplesBuilder.java'],
}),
],
};

export const restFiles = {
restFiles: [
{
Expand Down Expand Up @@ -181,6 +191,7 @@ const userFiles = {
};

export const serverFiles = {
...sampleFiles,
...restFiles,
...filteringFiles,
...filteringReactiveFiles,
Expand Down
18 changes: 17 additions & 1 deletion generators/spring-boot/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ import {
GRADLE_BUILD_SRC_MAIN_DIR,
} from '../generator-constants.js';
import { addSectionsCondition, mergeSections } from '../base/support/index.js';
import { moveToJavaPackageSrcDir, moveToJavaPackageTestDir, moveToSrcMainResourcesDir } from '../java/support/index.js';
import {
javaTestPackageTemplatesBlock,
moveToJavaPackageSrcDir,
moveToJavaPackageTestDir,
moveToSrcMainResourcesDir,
} from '../java/support/index.js';

export const sampleFiles = {
sampleFiles: [
javaTestPackageTemplatesBlock({
condition: data => !data.reactive,
relativePath: 'domain/',
templates: ['IntegrationTestSample.java'],
}),
],
};

const imperativeConfigFiles = {
imperativeFiles: [
Expand Down Expand Up @@ -283,6 +298,7 @@ const swaggerFiles = {
* For any other config an object { file:.., method:.., template:.. } can be used
*/
export const baseServerFiles = {
...sampleFiles,
jib: [
{
path: 'src/main/docker/jib/',
Expand Down
10 changes: 9 additions & 1 deletion generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,16 @@ export default class SpringBootGenerator extends BaseApplicationGenerator {

get postPreparingEachEntity() {
return this.asPostPreparingEachEntityTaskGroup({
prepareEntity({ entity }) {
prepareEntity({ application, entity }) {
const { primaryKey } = entity;
(entity as any).newIntegrationTestSample =
application.databaseTypeSql &&
!application.reactive &&
(entity as any).searchEngineNo &&
!(entity as any).jpaMetaModelFiltering &&
!entity.primaryKey.derived &&
entity.primaryKey.autoGenerate;

if (primaryKey) {
primaryKey.javaBuildSpecification = getSpecificationBuildForType(primaryKey.type);
primaryKey.javaValueGenerator = getJavaValueGeneratorForType(primaryKey.type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package <%= entityAbsolutePackage %>.domain;

import <%= entityAbsolutePackage %>.repository.<%= entityClass %>Repository;
<%_ if (entityAbsolutePackage !== packageName) { _%>
import <%= packageName %>.domain.IntegrationTestSample;
<%_ } _%>
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.context.ApplicationContext;

public class <%= persistClass %>IntegrationTestSamplesBuilder {

private <%= persistClass %>IntegrationTestSamplesBuilder() {}

public static IntegrationTestSample<<%= persistClass %>> build<%= persistClass %>IntegrationTestSample(ApplicationContext applicationContext) {
<%= persistClass %> insertable = <%= persistClass %>TestSamples.build<%= persistClass %>InsertableSample1();

var tearDownCallbacks = new ArrayList<IntegrationTestSample<?>>();
<%_ for (const relationship of relationships.filter(relationship => relationship.relationshipRequired)) { _%>

IntegrationTestSample<<%- relationship.otherEntity.persistClass %>> <%- relationship.relationshipName %>IntegrationTestSample = <%- relationship.otherEntity.persistClass %>TestSamplesBuilder.build<%= relationship.otherEntity.persistClass %>IntegrationTestSample(applicationContext);
tearDownCallbacks.add(<%- relationship.relationshipName %>IntegrationTestSample);
insertable.set<%- relationship.propertyJavaBeanName %>(<%- relationship.relationshipName %>IntegrationTestSample.insert());
<%_ } _%>

Supplier<<%= persistClass %>> insertCallback = () -> {
<%= entityClass %>Repository <%= entityInstance %>Repository = applicationContext.getBean(<%= entityClass %>Repository.class);
<%= persistClass %> persisted = <%= entityInstance %>Repository.save(<%= persistClass %>TestSamples.build<%= persistClass %>InsertableSample1());

return <%= persistClass %>TestSamples.clone(persisted);
};

Consumer<<%= persistClass %>> tearDownCallback = (<%= persistClass %> inserted) -> {
<%= entityClass %>Repository <%= entityInstance %>Repository = applicationContext.getBean(<%= entityClass %>Repository.class);
<%= entityInstance %>Repository.delete(inserted);

tearDownCallbacks.forEach((IntegrationTestSample<?> integrationTestSample) -> {
integrationTestSample.tearDown();
});
};

Function<<%= persistClass %>, <%= persistClass %>> updatableCallback = (<%= persistClass %> <%= persistInstance %>) -> {
return <%= persistClass %>TestSamples.apply<%= persistClass %>UpdateSampleToInsertableSample1(<%= persistInstance %>);
};

return IntegrationTestSample.of(insertable, insertCallback, updatableCallback, tearDownCallback);
}
}
Loading

0 comments on commit ec1a2d7

Please sign in to comment.