Skip to content

Commit

Permalink
add java samples class for entities (#24051)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Oct 31, 2023
1 parent e68a3a4 commit 7f811fe
Show file tree
Hide file tree
Showing 18 changed files with 818 additions and 18 deletions.
19 changes: 12 additions & 7 deletions generators/base-application/support/prepare-field.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { fieldTypes, validations } from '../../../jdl/jhipster/index.mjs';
import { getTypescriptType, prepareField as prepareClientFieldForTemplates } from '../../client/support/index.mjs';
import { prepareField as prepareServerFieldForTemplates } from '../../server/support/index.mjs';
import { fieldIsEnum } from './field-utils.mjs';
import { mutateData } from '../../base/support/config.mjs';

const { BlobTypes, CommonDBTypes, RelationalOnlyDBTypes } = fieldTypes;
const {
Expand Down Expand Up @@ -266,14 +267,18 @@ export default function prepareField(entityWithConfig, field, generator) {
}

function prepareCommonFieldForTemplates(entityWithConfig, field, generator) {
_.defaults(field, {
propertyName: field.fieldName,
mutateData(field, {
path: [field.fieldName],
fieldNameCapitalized: _.upperFirst(field.fieldName),
fieldNameUnderscored: _.snakeCase(field.fieldName),
fieldNameHumanized: _.startCase(field.fieldName),
fieldTranslationKey: `${entityWithConfig.i18nKeyPrefix}.${field.fieldName}`,
tsType: getTypescriptType(field.fieldType),
propertyName: field.fieldName,
propertyNameCapitalized: ({ propertyName, propertyNameCapitalized }) => propertyNameCapitalized ?? _.upperFirst(propertyName),
fieldNameCapitalized: ({ fieldName, fieldNameCapitalized }) => fieldNameCapitalized ?? _.upperFirst(fieldName),
fieldNameUnderscored: ({ fieldName, fieldNameUnderscored }) => fieldNameUnderscored ?? _.snakeCase(fieldName),
fieldNameHumanized: ({ fieldName, fieldNameHumanized }) => fieldNameHumanized ?? _.startCase(fieldName),
fieldTranslationKey: ({ fieldName, fieldTranslationKey }) => fieldTranslationKey ?? `${entityWithConfig.i18nKeyPrefix}.${fieldName}`,
tsType: ({ fieldType, tsType }) => tsType ?? getTypescriptType(fieldType),
});

_.defaults(field, {
entity: entityWithConfig,
});
const fieldType = field.fieldType;
Expand Down
4 changes: 3 additions & 1 deletion generators/base-application/support/prepare-relationship.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { upperFirstCamelCase } from '../../base/support/string.mjs';
import { getJoinTableName, hibernateSnakeCase } from '../../server/support/index.mjs';
import { stringifyApplicationData } from './debug.mjs';
import { mutateData } from '../../base/support/config.mjs';

const { isReservedTableName } = reservedKeywords;
const { NEO4J, NO: DATABASE_NO } = databaseTypes;
Expand Down Expand Up @@ -160,8 +161,9 @@ export default function prepareRelationship(entityWithConfig, relationship, gene
otherEntityNameCapitalizedPlural: pluralize(relationship.otherEntityNameCapitalized),
});

_.defaults(relationship, {
mutateData(relationship, {
propertyName: relationship.collection ? relationship.relationshipFieldNamePlural : relationship.relationshipFieldName,
propertyNameCapitalized: ({ propertyName, propertyNameCapitalized }) => propertyNameCapitalized ?? _.upperFirst(propertyName),
});

if (entityWithConfig.dto === MAPSTRUCT) {
Expand Down
24 changes: 24 additions & 0 deletions generators/bootstrap-application/generator.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ describe(`generator - ${generator}`, () => {
"generateFakeData": Any<Function>,
"id": true,
"javaFieldType": "UUID",
"javaValueGenerator": "UUID.randomUUID()",
"javaValueSample1": "UUID.fromString("23d8dc04-a48b-45d9-a01d-4b728f0ad4aa")",
"javaValueSample2": "UUID.fromString("ad79f240-3727-46c3-b89f-2cf6ebd74367")",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
Expand All @@ -319,6 +322,7 @@ describe(`generator - ${generator}`, () => {
"id",
],
"propertyName": "id",
"propertyNameCapitalized": "Id",
"readonly": true,
"reference": Any<Object>,
"relationshipsPath": [],
Expand Down Expand Up @@ -387,12 +391,16 @@ describe(`generator - ${generator}`, () => {
"filterableField": true,
"generateFakeData": Any<Function>,
"javaFieldType": "String",
"javaValueGenerator": "UUID.randomUUID().toString()",
"javaValueSample1": ""login1"",
"javaValueSample2": ""login2"",
"loadColumnType": "string",
"nullable": true,
"path": [
"login",
],
"propertyName": "login",
"propertyNameCapitalized": "Login",
"reference": Any<Object>,
"relationshipsPath": [],
"shouldCreateContentType": false,
Expand Down Expand Up @@ -459,12 +467,16 @@ describe(`generator - ${generator}`, () => {
"filterableField": true,
"generateFakeData": Any<Function>,
"javaFieldType": "String",
"javaValueGenerator": "UUID.randomUUID().toString()",
"javaValueSample1": ""firstName1"",
"javaValueSample2": ""firstName2"",
"loadColumnType": "string",
"nullable": true,
"path": [
"firstName",
],
"propertyName": "firstName",
"propertyNameCapitalized": "FirstName",
"reference": Any<Object>,
"relationshipsPath": [],
"shouldCreateContentType": false,
Expand Down Expand Up @@ -531,12 +543,16 @@ describe(`generator - ${generator}`, () => {
"filterableField": true,
"generateFakeData": Any<Function>,
"javaFieldType": "String",
"javaValueGenerator": "UUID.randomUUID().toString()",
"javaValueSample1": ""lastName1"",
"javaValueSample2": ""lastName2"",
"loadColumnType": "string",
"nullable": true,
"path": [
"lastName",
],
"propertyName": "lastName",
"propertyNameCapitalized": "LastName",
"reference": Any<Object>,
"relationshipsPath": [],
"shouldCreateContentType": false,
Expand Down Expand Up @@ -793,6 +809,9 @@ describe(`generator - ${generator}`, () => {
"generateFakeData": Any<Function>,
"id": true,
"javaFieldType": "UUID",
"javaValueGenerator": "UUID.randomUUID()",
"javaValueSample1": "UUID.fromString("23d8dc04-a48b-45d9-a01d-4b728f0ad4aa")",
"javaValueSample2": "UUID.fromString("ad79f240-3727-46c3-b89f-2cf6ebd74367")",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
Expand All @@ -802,6 +821,7 @@ describe(`generator - ${generator}`, () => {
"id",
],
"propertyName": "id",
"propertyNameCapitalized": "Id",
"readonly": true,
"reference": Any<Object>,
"relationshipsPath": [],
Expand Down Expand Up @@ -1112,6 +1132,9 @@ describe(`generator - ${generator}`, () => {
"generateFakeData": Any<Function>,
"id": true,
"javaFieldType": "UUID",
"javaValueGenerator": "UUID.randomUUID()",
"javaValueSample1": "UUID.fromString("23d8dc04-a48b-45d9-a01d-4b728f0ad4aa")",
"javaValueSample2": "UUID.fromString("ad79f240-3727-46c3-b89f-2cf6ebd74367")",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
Expand All @@ -1121,6 +1144,7 @@ describe(`generator - ${generator}`, () => {
"id",
],
"propertyName": "id",
"propertyNameCapitalized": "Id",
"readonly": true,
"reference": Any<Object>,
"relationshipsPath": [],
Expand Down
15 changes: 15 additions & 0 deletions generators/entities/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,21 @@ exports[`generator - entities regenerating all entities should match snapshot 1`
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/SkipTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/SkipTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/BarResourceIT.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -565,9 +574,15 @@ exports[`generator - entities regenerating some entities should match snapshot 1
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/BarResourceIT.java": {
"stateCleared": "modified",
},
Expand Down
6 changes: 6 additions & 0 deletions generators/java/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ exports[`generator - java with default config should match files snapshot 1`] =
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTestSamples.java": {
"stateCleared": "modified",
},
}
`;
2 changes: 1 addition & 1 deletion generators/java/entity-files.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const entityServerFiles: WriteFileSection = {
],
modelTestFiles: [
javaTestPackageTemplatesBlock({
templates: ['_entityPackage_/domain/_persistClass_Test.java'],
templates: ['_entityPackage_/domain/_persistClass_Test.java', '_entityPackage_/domain/_persistClass_TestSamples.java'],
}),
],
server: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
-%>
package <%= entityAbsolutePackage %>.domain;

import static <%= entityAbsolutePackage %>.domain.<%= persistClass %>TestSamples.*;
<%_ for (const otherEntity of otherEntities.filter(otherEntity => !otherEntity.builtIn)) { _%>
import static <%= otherEntity.entityAbsolutePackage %>.domain.<%= otherEntity.persistClass %>TestSamples.*;
<%_ } _%>
<%_ for (const otherEntity of otherEntities.filter(otherEntity => entityPackage !== otherEntity.entityPackage)) { _%>
import <%= otherEntity.entityAbsolutePackage %>.domain.<%= otherEntity.persistClass %>;
<%_ } _%>
<%_ if (relationships.some(relationship => relationship.collection)) { _%>
import java.util.HashSet;
import java.util.Set;
<%_ } _%>
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import <%= packageName %>.web.rest.TestUtil;
Expand All @@ -31,15 +42,77 @@ class <%= persistClass %>Test {
void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(<%= persistClass %>.class);
<%_if (!embedded) { _%>
<%= persistClass %> <%= persistInstance %>1 = new <%= persistClass %>();
<%= persistInstance %>1.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeInteger) { %>1<% } else if (primaryKey.typeLong) { %>1L<% } else if (primaryKey.typeString) { %>"id1"<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } %>);
<%= persistClass %> <%= persistInstance %>1 = get<%= 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.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeInteger) { %>2<% } else if (primaryKey.typeLong) { %>2L<% } else if (primaryKey.typeString) { %>"id2"<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } %>);
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
<%= persistInstance %>1.set<%= primaryKey.nameCapitalized %>(null);

<%= persistInstance %>2 = get<%= persistClass %>Sample2();
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
<%_} _%>
}
<%_ for (const relationship of relationships.filter(relationship => !relationship.otherEntity.builtIn)) { _%>

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

<%= persistInstance %>.add<%- relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).containsOnly(<%= relationship.otherEntity.persistInstance %>Back);
<%_ } else { _%>

<%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).isEqualTo(<%= relationship.otherEntity.persistInstance %>Back);
<%_ } _%>
<%_ if (relationship.otherRelationship && !relationship.ownerSide && !relationship.otherEntity.embedded) { _%>
<%_ if (relationship.otherRelationship.collection) { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).containsOnly(<%= persistInstance %>);
<%_ } else { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isEqualTo(<%= persistInstance %>);
<%_ } _%>
<%_ } _%>
<%_ if (relationship.collection) { _%>

<%= persistInstance %>.remove<%- relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).doesNotContain(<%= relationship.otherEntity.persistInstance %>Back);
<%_ } else { _%>

<%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(null);
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).isNull();
<%_ } _%>
<%_ if (relationship.otherRelationship && !relationship.ownerSide && !relationship.otherEntity.embedded) { _%>
<%_ if (relationship.otherRelationship.collection) { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).doesNotContain(<%= persistInstance %>);
<%_ } else { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isNull();
<%_ } _%>
<%_ } _%>
<%_ if (relationship.collection) { _%>

<%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(new HashSet<>(Set.of(<%= relationship.otherEntity.persistInstance %>Back)));
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).containsOnly(<%= relationship.otherEntity.persistInstance %>Back);
<%_ if (relationship.otherRelationship && !relationship.ownerSide && !relationship.otherEntity.embedded) { _%>
<%_ if (relationship.otherRelationship.collection) { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).containsOnly(<%= persistInstance %>);
<%_ } else { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isEqualTo(<%= persistInstance %>);
<%_ } _%>
<%_ } _%>

<%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(new HashSet<>());
assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).doesNotContain(<%= relationship.otherEntity.persistInstance %>Back);
<%_ if (relationship.otherRelationship && !relationship.ownerSide && !relationship.otherEntity.embedded) { _%>
<%_ if (relationship.otherRelationship.collection) { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).doesNotContain(<%= persistInstance %>);
<%_ } else { _%>
assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isNull();
<%_ } _%>
<%_ } _%>
<%_ } _%>
}
<%_ } _%>
}
Loading

0 comments on commit 7f811fe

Please sign in to comment.