Skip to content

Commit

Permalink
use generator methods in IT
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed Apr 15, 2021
1 parent 9cca793 commit 55c9a52
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 %>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 %>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) { _%>
Expand Down
8 changes: 5 additions & 3 deletions generators/generator-base-private.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions utils/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 55c9a52

Please sign in to comment.