Skip to content

Commit

Permalink
Merge pull request #23531 from mshima/skip_ci-integer
Browse files Browse the repository at this point in the history
add tests for Integer ids
  • Loading branch information
DanielFran authored Sep 17, 2023
2 parents f50c823 + 0d6cb68 commit 8860d9f
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ _%>
<%_ if (noPropertyEntity) { _%>
if (form.controls.<%= primaryKey.name %>.disabled) {
// form.value returns <%= primaryKey.name %> with null value for FormGroup with only one FormControl
return {};
return { <%= primaryKey.name %>: null };
}
<%_ } _%>
<%_ if (anyFieldIsTimeDerived) { _%>
Expand Down
4 changes: 3 additions & 1 deletion generators/base-application/support/prepare-entity.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const { GATEWAY, MICROSERVICE } = applicationTypes;
const { OAUTH2 } = authenticationTypes;
const { CommonDBTypes } = fieldTypes;

const { BOOLEAN, LONG, STRING, UUID } = CommonDBTypes;
const { BOOLEAN, LONG, STRING, UUID, INTEGER } = CommonDBTypes;
const { NO: NO_DTO, MAPSTRUCT } = MapperTypes;
const { PAGINATION, INFINITE_SCROLL } = PaginationTypes;
const { SERVICE_IMPL } = ServiceTypes;
Expand Down Expand Up @@ -268,9 +268,11 @@ export function derivedPrimaryKeyProperties(primaryKey) {
_.defaults(primaryKey, {
hasUUID: primaryKey.fields && primaryKey.fields.some(field => field.fieldType === UUID),
hasLong: primaryKey.fields && primaryKey.fields.some(field => field.fieldType === LONG),
hasInteger: primaryKey.fields && primaryKey.fields.some(field => field.fieldType === INTEGER),
typeUUID: primaryKey.type === UUID,
typeString: primaryKey.type === STRING,
typeLong: primaryKey.type === LONG,
typeInteger: primaryKey.type === INTEGER,
typeNumeric: !primaryKey.composite && primaryKey.fields[0].fieldTypeNumeric,
});
}
Expand Down
12 changes: 12 additions & 0 deletions generators/bootstrap-application/generator.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ describe(`generator - ${generator}`, () => {
"id": true,
"javaFieldType": "UUID",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
"loadColumnType": "\${uuidType}",
"nullable": true,
"path": [
Expand Down Expand Up @@ -580,6 +582,7 @@ describe(`generator - ${generator}`, () => {
"derived": false,
"derivedFields": Any<Array>,
"fields": Any<Array>,
"hasInteger": false,
"hasLong": false,
"hasUUID": true,
"ids": [
Expand All @@ -601,6 +604,7 @@ describe(`generator - ${generator}`, () => {
"relationships": [],
"tsType": "string",
"type": "UUID",
"typeInteger": false,
"typeLong": false,
"typeNumeric": false,
"typeString": false,
Expand Down Expand Up @@ -785,6 +789,8 @@ describe(`generator - ${generator}`, () => {
"id": true,
"javaFieldType": "UUID",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
"loadColumnType": "\${uuidType}",
"nullable": true,
"path": [
Expand Down Expand Up @@ -840,6 +846,7 @@ describe(`generator - ${generator}`, () => {
"derived": false,
"derivedFields": Any<Array>,
"fields": Any<Array>,
"hasInteger": false,
"hasLong": false,
"hasUUID": true,
"ids": [
Expand All @@ -861,6 +868,7 @@ describe(`generator - ${generator}`, () => {
"relationships": [],
"tsType": "string",
"type": "UUID",
"typeInteger": false,
"typeLong": false,
"typeNumeric": false,
"typeString": false,
Expand Down Expand Up @@ -1097,6 +1105,8 @@ describe(`generator - ${generator}`, () => {
"id": true,
"javaFieldType": "UUID",
"jpaGeneratedValue": true,
"jpaGeneratedValueIdentity": false,
"jpaGeneratedValueSequence": false,
"loadColumnType": "\${uuidType}",
"nullable": true,
"path": [
Expand Down Expand Up @@ -1152,6 +1162,7 @@ describe(`generator - ${generator}`, () => {
"derived": false,
"derivedFields": Any<Array>,
"fields": Any<Array>,
"hasInteger": false,
"hasLong": false,
"hasUUID": true,
"ids": [
Expand All @@ -1173,6 +1184,7 @@ describe(`generator - ${generator}`, () => {
"relationships": [],
"tsType": "string",
"type": "UUID",
"typeInteger": false,
"typeLong": false,
"typeNumeric": false,
"typeString": false,
Expand Down
4 changes: 2 additions & 2 deletions generators/liquibase/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { fieldTypes } from '../../jdl/jhipster/index.mjs';
import command from './command.mjs';

const {
CommonDBTypes: { LONG: TYPE_LONG },
CommonDBTypes: { LONG: TYPE_LONG, INTEGER: TYPE_INTEGER },
} = fieldTypes;

export default class LiquibaseGenerator extends BaseEntityChangesGenerator {
Expand Down Expand Up @@ -595,7 +595,7 @@ export default class LiquibaseGenerator extends BaseEntityChangesGenerator {
return;
}
let data;
if (field.id && field.fieldType === TYPE_LONG) {
if (field.id && [TYPE_INTEGER, TYPE_LONG].includes(field.fieldType)) {
data = rowNumber + 1;
} else {
data = field.generateFakeData();
Expand Down
6 changes: 3 additions & 3 deletions generators/liquibase/support/post-prepare-entity.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { LiquibaseEntity } from '../types.mjs';
import { GeneratorDefinition } from '../../base-application/generator.mjs';

const { CommonDBTypes } = fieldTypes;
const { LONG: TYPE_LONG } = CommonDBTypes;
const { LONG: TYPE_LONG, INTEGER: TYPE_INTEGER } = CommonDBTypes;

export default function postPrepareEntity({
application,
Expand All @@ -34,8 +34,8 @@ export default function postPrepareEntity({
const idFieldName = idField.fieldName ?? 'id';
const liquibaseFakeData = application.generateUserManagement
? [
{ [idFieldName]: userIdType === TYPE_LONG ? 1 : idField.generateFakeData() },
{ [idFieldName]: userIdType === TYPE_LONG ? 2 : idField.generateFakeData() },
{ [idFieldName]: [TYPE_INTEGER, TYPE_LONG].includes(userIdType) ? 1 : idField.generateFakeData() },
{ [idFieldName]: [TYPE_INTEGER, TYPE_LONG].includes(userIdType) ? 2 : idField.generateFakeData() },
]
: [];
(entity as LiquibaseEntity).liquibaseFakeData = liquibaseFakeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<%_ fields.filter(field => field.liquibaseCustomSequenceGenerator).forEach((field, idx) => { _%>
<changeSet id="<%= changelogDate %>-seq-<%- idx %>" author="jhipster">
<createSequence sequenceName="<%- field.liquibaseSequenceGeneratorName %>" startValue="1050" incrementBy="50"/>
</changeSet>
<%_ }); _%>
<!--
Added the entity <%= entity.entityClass %>.
-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@
<changeSet id="00000000000000" author="jhipster">
<createSequence sequenceName="sequence_generator" startValue="1050" incrementBy="50"/>
</changeSet>
<%_ } _%>
<%_
if (generateBuiltInUserEntity) {
const idField = user.primaryKey.fields[0];
if (idField.liquibaseCustomSequenceGenerator) {
_%>
<changeSet id="00000000000000-seq-user" author="jhipster">
<createSequence sequenceName="<%- idField.liquibaseSequenceGeneratorName %>" startValue="1050" incrementBy="50"/>
</changeSet>
<%_
}
}
_%>
<!--
JHipster core tables.
The initial schema has the '00000000000001' id, so that it is over-written if we re-generate it.
Expand Down
14 changes: 10 additions & 4 deletions generators/server/support/prepare-field.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const { MYSQL, SQL } = databaseTypes;
const { MapperTypes } = entityOptions;

const { MAPSTRUCT } = MapperTypes;
const { LONG, UUID } = CommonDBTypes;
const { INTEGER, LONG, UUID } = CommonDBTypes;

const { snakeCase, upperFirst } = _;

Expand All @@ -51,7 +51,7 @@ export default function prepareField(entityWithConfig, field, generator) {

if (field.id && entityWithConfig.primaryKey) {
if (field.autoGenerate === undefined) {
field.autoGenerate = !entityWithConfig.primaryKey.composite && [LONG, UUID].includes(field.fieldType);
field.autoGenerate = !entityWithConfig.primaryKey.composite && [INTEGER, LONG, UUID].includes(field.fieldType);
}

if (!field.autoGenerate) {
Expand All @@ -76,13 +76,19 @@ export default function prepareField(entityWithConfig, field, generator) {
field.readonly = true;
} else {
const defaultGenerationType = entityWithConfig.prodDatabaseType === MYSQL ? 'identity' : 'sequence';
field.jpaGeneratedValue = field.jpaGeneratedValue || field.fieldType === LONG ? defaultGenerationType : true;
field.jpaGeneratedValue = field.jpaGeneratedValue || [INTEGER, LONG].includes(field.fieldType) ? defaultGenerationType : true;
field.jpaGeneratedValueSequence = field.jpaGeneratedValue === 'sequence';
field.jpaGeneratedValueIdentity = field.jpaGeneratedValue === 'identity';
field.autoGenerateByService = false;
field.autoGenerateByRepository = true;
field.requiresPersistableImplementation = false;
field.readonly = true;
if (field.jpaGeneratedValue === 'identity') {
if (field.jpaGeneratedValueIdentity) {
field.liquibaseAutoIncrement = true;
} else if (field.jpaGeneratedValueSequence) {
field.jpaSequenceGeneratorName = field.sequenceGeneratorName ?? 'sequenceGenerator';
field.liquibaseSequenceGeneratorName = snakeCase(field.jpaSequenceGeneratorName);
field.liquibaseCustomSequenceGenerator = field.liquibaseSequenceGeneratorName !== 'sequence_generator';
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions generators/server/support/templates/field-values.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { fieldTypes, databaseTypes } from '../../../../jdl/jhipster/index.mjs';

const dbTypes = fieldTypes;
const { STRING, UUID, LONG } = dbTypes.CommonDBTypes;
const { STRING, UUID, LONG, INTEGER } = dbTypes.CommonDBTypes;
const { SQL } = databaseTypes;

/**
Expand All @@ -33,7 +33,7 @@ export const getJavaValueGeneratorForType = (type) => {
if (type === UUID) {
return 'UUID.randomUUID()';
}
if (type === LONG) {
if (type === LONG || type === INTEGER) {
return 'count.incrementAndGet()';
}
throw new Error(`Java type ${type} does not have a random generator implemented`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class <%= persistClass %>Test {
TestUtil.equalsVerifier(<%= persistClass %>.class);
<%_if (!embedded) { _%>
<%= persistClass %> <%= persistInstance %>1 = new <%= persistClass %>();
<%= persistInstance %>1.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeLong) { %>1L<% } else if (primaryKey.typeString) { %>"id1"<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } %>);
<%= 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 %>2 = new <%= persistClass %>();
<%= persistInstance %>2.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
assertThat(<%= persistInstance %>1).isEqualTo(<%= persistInstance %>2);
<%= persistInstance %>2.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeLong) { %>2L<% } else if (primaryKey.typeString) { %>"id2"<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } %>);
<%= 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);
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (databaseTypeSql && reactive) {
createEntityPostfix = ').block()';
}
let idValue = `${persistInstance}.get${primaryKey.nameCapitalized}()`;
if (primaryKey.typeLong) {
if (primaryKey.typeLong || primaryKey.typeInteger) {
idValue = idValue + '.intValue()';
} else if (primaryKey.typeUUID) {
idValue = idValue + '.toString()';
Expand Down Expand Up @@ -200,9 +200,13 @@ import java.util.List;
<%_ if (anyFieldIsUUID || primaryKey.typeString || otherEntityPrimaryKeyTypesIncludesUUID) { _%>
import java.util.UUID;
<%_ } _%>
<%_ if (!embedded && primaryKey.hasLong) { _%>
<%_ if (!embedded && (primaryKey.hasLong || primaryKey.hasInteger)) { _%>
import java.util.Random;
<%_ if (primaryKey.hasLong) { _%>
import java.util.concurrent.atomic.AtomicLong;
<%_ } else if (primaryKey.hasInteger) { _%>
import java.util.concurrent.atomic.AtomicInteger;
<%_ } _%>
<%_ } _%>

<%_ if (anyFieldIsBigDecimal) { _%>
Expand Down Expand Up @@ -431,10 +435,14 @@ if (field.fieldTypeString || field.blobContentTypeText) {
<%_ if (searchEngineAny) { _%>
private static final String ENTITY_SEARCH_API_URL = "/api/_search/<%= entityApiUrl %>";
<%_ } _%>
<%_ if (!embedded && primaryKey.hasLong) { _%>
<%_ if (!embedded && (primaryKey.hasLong || primaryKey.hasInteger)) { _%>

private static Random random = new Random();
<%_ if (primaryKey.hasLong) { _%>
private static AtomicLong count = new AtomicLong(random.nextInt() + ( 2 * Integer.MAX_VALUE ));
<%_ } else if (primaryKey.hasInteger) { _%>
private static AtomicInteger count = new AtomicInteger(random.nextInt() + ( 2 * Short.MAX_VALUE ));
<%_ } _%>
<%_ } _%>

@Autowired
Expand Down Expand Up @@ -692,7 +700,7 @@ _%>
<%_ if (primaryKey.typeUUID && databaseTypeSql) { _%>
<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>;
<%_ } else { _%>
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } else if (primaryKey.typeLong) { %>1L<% } else { %>"existing_id"<% } %>);
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<% if (primaryKey.typeUUID) { %>UUID.randomUUID()<% } else if (primaryKey.typeLong) { %>1L<% } else if (primaryKey.typeInteger) { %>1<% } else { %>"existing_id"<% } %>);
<%_ } _%>
<%_ if (dtoMapstruct) { _%>
<%= dtoClass %> <%= dtoInstance %> = <%= entityInstance %>Mapper.toDto(<%= persistInstance %>);
Expand Down Expand Up @@ -1037,7 +1045,7 @@ _%>
default<%= entityClass %>ShouldBeFound("<%= primaryKey.name %>.equals=" + id);
default<%= entityClass %>ShouldNotBeFound("<%= primaryKey.name %>.notEquals=" + id);

<%_ if (primaryKey.typeLong) { _%>
<%_ if (primaryKey.typeLong || primaryKey.typeInteger) { _%>
default<%= entityClass %>ShouldBeFound("<%= primaryKey.name %>.greaterThanOrEqual=" + id);
default<%= entityClass %>ShouldNotBeFound("<%= primaryKey.name %>.greaterThan=" + id);

Expand Down Expand Up @@ -1363,12 +1371,12 @@ _%>
void getNonExisting<%= entityClass %>() <% if (!reactive) { %>throws Exception <% } %>{
// Get the <%= entityInstance %>
<%_ if (reactive) { _%>
webTestClient.get().uri(ENTITY_API_URL_ID, <% if (primaryKey.typeLong || primaryKey.typeString) { %>Long.MAX_VALUE<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID().toString()<% } %>)
webTestClient.get().uri(ENTITY_API_URL_ID, <% if (primaryKey.typeInteger) { %>Integer.MAX_VALUE<% } else if (primaryKey.typeLong || primaryKey.typeString) { %>Long.MAX_VALUE<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID().toString()<% } %>)
.accept(MediaType.APPLICATION_PROBLEM_JSON)
.exchange()
.expectStatus().isNotFound();
<%_ } else { _%>
rest<%= entityClass %>MockMvc.perform(get(ENTITY_API_URL_ID, <% if (primaryKey.typeLong || primaryKey.typeString) { %>Long.MAX_VALUE<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID().toString()<% } %>))
rest<%= entityClass %>MockMvc.perform(get(ENTITY_API_URL_ID, <% if (primaryKey.typeInteger) { %>Integer.MAX_VALUE<% } else if (primaryKey.typeLong || primaryKey.typeString) { %>Long.MAX_VALUE<% } else if (primaryKey.typeUUID) { %>UUID.randomUUID().toString()<% } %>))
.andExpect(status().isNotFound());
<%_ } _%>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public class <%= user.persistClass %><% if (databaseTypeSql || databaseTypeMongo
@GeneratedValue(strategy = GenerationType.IDENTITY)
<%_ } else if (user.primaryKey.fields[0].jpaGeneratedValue === 'sequence') { _%>
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator"<% if (user.primaryKey.fields[0].liquibaseCustomSequenceGenerator) { %>, sequenceName = "<%- user.primaryKey.fields[0].jpaSequenceGeneratorName %>"<% } %>)
<%_ } else if (user.primaryKey.fields[0].jpaGeneratedValue) { _%>
@GeneratedValue
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import org.hibernate.type.SqlTypes;
@GeneratedValue(strategy = GenerationType.IDENTITY)
<%_ } else if (field.jpaGeneratedValue === 'sequence') { _%>
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator"<% if (field.liquibaseCustomSequenceGenerator) { %>, sequenceName = "<%- field.jpaSequenceGeneratorName %>"<% } %>)
<%_ } else if (field.jpaGeneratedValue) { _%>
@GeneratedValue
<%_ } _%>
Expand Down
19 changes: 19 additions & 0 deletions test-integration/samples/jdl-entities/custom-id.jdl
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Integer custom id
*/
@ChangelogDate(20200804035100)
entity EntityIntegerId {
id Integer
name String
}

/*
* Integer custom id and sequence
*/
@ChangelogDate(20200804035200)
entity EntityCustomSequence {
@SequenceGeneratorName(entityIntegerIdSeq)
id Long
name String
}

/*
* UUID custom id
*/
Expand Down

0 comments on commit 8860d9f

Please sign in to comment.