From 55c9a52946d88e39a3affd68dabb41633f30d408 Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Thu, 15 Apr 2021 16:43:46 +0200 Subject: [PATCH] use generator methods in IT --- .../partials/it_patch_update.partial.java.ejs | 6 -- .../main/java/package/domain/Entity.java.ejs | 1 - .../web/rest/EntityResourceIT.java.ejs | 79 ++++++++++++------- generators/generator-base-private.js | 8 +- utils/entity.js | 4 +- 5 files changed, 56 insertions(+), 42 deletions(-) diff --git a/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs b/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs index 5d07b713785..a2825430a72 100644 --- a/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs +++ b/generators/entity-server/templates/partials/it_patch_update.partial.java.ejs @@ -16,12 +16,6 @@ See the License for the specific language governing permissions and limitations under the License. -%> -// Initialize the database -<%_ if (!primaryKey.autoGenerate && !databaseTypeSql) { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<%- getPrimaryKeyValue(primaryKey.type, databaseType) -%>); -<%_ } _%> -<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; - int databaseSizeBeforeUpdate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size(); // Update the <%= entityInstance %> using partial update diff --git a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs index ad4099f257e..95534a45052 100644 --- a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs +++ b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs @@ -207,7 +207,6 @@ public class <%= persistClass %> implements Serializable { let required = false; let unique = false; const fieldValidate = field.fieldValidate; - const fieldValidateRules = field.fieldValidateRules; const fieldValidateRulesMaxlength = field.fieldValidateRulesMaxlength; const fieldType = field.fieldType; const fieldName = field.fieldName; diff --git a/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs b/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs index 5c3592840d8..9d235d968cc 100644 --- a/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs +++ b/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs @@ -27,16 +27,7 @@ package <%= packageName %>.web.rest; * @returns {string} java primary key value */ function getJavaValueGeneratorForType(type) { - if (type === 'String') { - return 'UUID.randomUUID().toString()'; - } - if (type === 'UUID') { - return 'UUID.randomUUID()'; - } - if (type === 'Long') { - return 'count.incrementAndGet()'; - } - throw new Error(`Java type ${type} does not have a random generator implemented`); + return getPrimaryKeyValue(type, databaseType, 'count.incrementAndGet()', false); } let mapsIdEntity; @@ -810,9 +801,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte @Test void getAll<%= entityClassPlural %>AsStream() { // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.save(<%= persistInstance %>)<%= callBlock %>; List<<%= entityClass %>> <%= entityInstance %>List = webTestClient.get().uri(ENTITY_API_URL) @@ -850,9 +843,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte @Test<%= transactionalAnnotation %> void getAll<%= entityClassPlural %>() <% if (!reactive) { %>throws Exception <% } %>{ // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; // Get all the <%= entityInstance %>List @@ -941,9 +936,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte @Test<%= transactionalAnnotation %> void get<%= entityClass %>() <% if (!reactive) { %>throws Exception <% } %>{ // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; // Get the <%= entityInstance %> @@ -1326,9 +1323,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte .thenAnswer(invocation -> Mono.just(invocation.getArgument(0))); <%_ } _%> // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; int databaseSizeBeforeUpdate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size(); @@ -1514,14 +1513,30 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte %> @Test<%= transactionalAnnotation %> void partialUpdate<%= entityClass %>WithPatch() throws Exception { + // Initialize the database +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> + <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; + <% const fieldsToIncludeInPartialPatchTest = fields.filter(field => !field.id && !field.transient).map(field => prepareFieldForPatchTest(field, () => faker.datatype.boolean())); %> -<%- include('/partials/it_patch_update.partial.java.ejs', {fields: fieldsToIncludeInPartialPatchTest, saveMethod, asEntity, callBlock, callListBlock, getPrimaryKeyValue}); -%> +<%- include('/partials/it_patch_update.partial.java.ejs', {fields: fieldsToIncludeInPartialPatchTest, saveMethod, asEntity, callBlock, callListBlock}); -%> } @Test<%= transactionalAnnotation %> void fullUpdate<%= entityClass %>WithPatch() throws Exception { + // Initialize the database +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> + <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; + <% const fieldsToIncludeInFullPatchTest = fields.filter(field => !field.id && !field.transient).map(field => prepareFieldForPatchTest(field, () => true)); %> -<%- include('/partials/it_patch_update.partial.java.ejs', {fields: fieldsToIncludeInFullPatchTest, saveMethod, asEntity, callBlock, callListBlock, getPrimaryKeyValue}); -%> +<%- include('/partials/it_patch_update.partial.java.ejs', {fields: fieldsToIncludeInFullPatchTest, saveMethod, asEntity, callBlock, callListBlock}); -%> } @Test<%= transactionalAnnotation %> @@ -1646,9 +1661,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte when(mock<%= entityClass %>SearchRepository.deleteById(any<%= primaryKey.type %>())).thenReturn(Mono.empty()); <%_ } _%> // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; int databaseSizeBeforeDelete = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size(); @@ -1693,9 +1710,11 @@ class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') { %>exte <%_ } _%> <%_ } _%> // Initialize the database - <%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%> - <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID()); - <%_ } _%> +<%_ if (!primaryKey.derived) { _%> + <%_ for (field of primaryKey.fields.filter(f => !f.autoGenerateByRepository)) { _%> + <%= persistInstance %>.set<%= field.fieldNameCapitalized %>(<%- getJavaValueGeneratorForType(field.fieldType) %>); + <%_ } _%> +<%_ } _%> <%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>; <%_ if (searchEngine === 'elasticsearch') { _%> <%_ if (reactive) { _%> diff --git a/generators/generator-base-private.js b/generators/generator-base-private.js index e5a870938b3..bc720aa6aa1 100644 --- a/generators/generator-base-private.js +++ b/generators/generator-base-private.js @@ -1248,7 +1248,7 @@ module.exports = class JHipsterBasePrivateGenerator extends Generator { * @param {string} defaultValue - default value * @returns {string} java primary key value */ - getPrimaryKeyValue(primaryKey, databaseType = this.jhipsterConfig.databaseType, defaultValue = 1) { + getPrimaryKeyValue(primaryKey, databaseType = this.jhipsterConfig.databaseType, defaultValue = 1, keepUnit = true) { if (typeof primaryKey === 'object' && primaryKey.composite) { return `new ${primaryKey.type}(${primaryKey.references .map(ref => this.getPrimaryKeyValue(ref.type, databaseType, defaultValue)) @@ -1259,12 +1259,14 @@ module.exports = class JHipsterBasePrivateGenerator extends Generator { if (databaseType === SQL && defaultValue === 0) { return 'UUID.randomUUID().toString()'; } - return `"id${defaultValue}"`; + if (!isNaN(defaultValue)) { + return `"id${defaultValue}"`; + } } if (primaryKeyType === TYPE_UUID) { return 'UUID.randomUUID()'; } - return `${defaultValue}L`; + return keepUnit ? `${defaultValue}L` : defaultValue; } /** diff --git a/utils/entity.js b/utils/entity.js index 9f0c51d2b82..2a5f294748f 100644 --- a/utils/entity.js +++ b/utils/entity.js @@ -30,7 +30,7 @@ const { MapperTypes } = require('../jdl/jhipster/entity-options'); const { OAUTH2 } = require('../jdl/jhipster/authentication-types'); const { CommonDBTypes } = require('../jdl/jhipster/field-types'); -const { BOOLEAN, LONG } = CommonDBTypes; +const { BOOLEAN } = CommonDBTypes; const { MAPSTRUCT } = MapperTypes; const { PAGINATION, INFINITE_SCROLL } = PaginationTypes; const NO_PAGINATION = PaginationTypes.NO; @@ -368,7 +368,7 @@ function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator, enable return [...this.ownFields, ...this.derivedFields]; }, get autoGenerate() { - return this.composite || primaryKeyType !== LONG ? false : this.fields[0].autoGenerate; + return this.composite ? false : this.fields[0].autoGenerate; }, // Fields inherited from id relationships. get derivedFields() {