Skip to content

Commit

Permalink
java: add javaSampleValues (#27831)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Nov 7, 2024
1 parent 64960c3 commit 2cb9d34
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 117 deletions.
2 changes: 1 addition & 1 deletion generators/base/support/jhipster7-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const jhipster7deprecatedProperties = {
get: () => getDBCExtraOption,
},
getPrimaryKeyValue: {
replacement: 'current generator this.getPrimaryKeyValue',
replacement: 'current generator primaryKey.javaSampleValues[0|1|2]',
get: () => getPrimaryKeyValue,
},
getJoinTableName: {
Expand Down
23 changes: 23 additions & 0 deletions generators/bootstrap-application-server/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { loadRequiredConfigIntoEntity, prepareEntityPrimaryKeyForTemplates } from '../base-application/support/index.js';
import {
addEntitiesOtherRelationships,
getPrimaryKeyValue,
hibernateSnakeCase,
loadDerivedServerConfig,
loadRequiredConfigDerivedProperties,
Expand Down Expand Up @@ -223,4 +224,26 @@ export default class BoostrapApplicationServer extends BaseApplicationGenerator
get [BaseApplicationGenerator.POST_PREPARING_EACH_ENTITY]() {
return this.postPreparingEachEntity;
}

get default() {
return this.asDefaultTaskGroup({
async postPreparingEntity({ application, entities }) {
if (!application.backendTypeJavaAny) return;
for (const entity of entities) {
if (entity.primaryKey) {
entity.resetFakerSeed(`${application.baseName}post-prepare-server`);
entity.primaryKey.javaSampleValues ??= [
getPrimaryKeyValue(entity.primaryKey, application.databaseType!, 1),
getPrimaryKeyValue(entity.primaryKey, application.databaseType!, 2),
getPrimaryKeyValue(entity.primaryKey, application.databaseType!, entity.faker.number.int({ min: 10, max: 100 })),
];
}
}
},
});
}

get [BaseApplicationGenerator.DEFAULT]() {
return this.default;
}
}
15 changes: 15 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,11 @@ describe(`generator - ${generator}`, () => {
"setter": "setId",
},
],
"javaSampleValues": [
"UUID.randomUUID()",
"UUID.randomUUID()",
"UUID.randomUUID()",
],
"name": "id",
"nameCapitalized": "Id",
"ownFields": Any<Array>,
Expand Down Expand Up @@ -1351,6 +1356,11 @@ describe(`generator - ${generator}`, () => {
"setter": "setId",
},
],
"javaSampleValues": [
"UUID.randomUUID()",
"UUID.randomUUID()",
"UUID.randomUUID()",
],
"name": "id",
"nameCapitalized": "Id",
"ownFields": Any<Array>,
Expand Down Expand Up @@ -1718,6 +1728,11 @@ describe(`generator - ${generator}`, () => {
"setter": "setId",
},
],
"javaSampleValues": [
"UUID.randomUUID()",
"UUID.randomUUID()",
"UUID.randomUUID()",
],
"name": "id",
"nameCapitalized": "Id",
"ownFields": Any<Array>,
Expand Down
94 changes: 1 addition & 93 deletions generators/server/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,7 @@ import { createNeedleCallback, mutateData } from '../base/support/index.js';
import { isReservedPaginationWords } from '../../lib/jhipster/reserved-keywords.js';
import { loadStoredAppOptions } from '../app/support/index.js';
import { isReservedH2Keyword } from '../spring-data-relational/support/h2-reserved-keywords.js';
import {
getJavaValueGeneratorForType as getJavaValueForType,
getPrimaryKeyValue as getPKValue,
hibernateSnakeCase,
javaBeanCase as javaBeanClassNameFormat,
buildJavaGet as javaGetCall,
buildJavaGetter as javaGetter,
buildJavaSetter as javaSetter,
} from './support/index.js';

const dbTypes = fieldTypes;
const {
STRING: TYPE_STRING,
INTEGER: TYPE_INTEGER,
LONG: TYPE_LONG,
BIG_DECIMAL: TYPE_BIG_DECIMAL,
FLOAT: TYPE_FLOAT,
DOUBLE: TYPE_DOUBLE,
LOCAL_DATE: TYPE_LOCAL_DATE,
ZONED_DATE_TIME: TYPE_ZONED_DATE_TIME,
INSTANT: TYPE_INSTANT,
DURATION: TYPE_DURATION,
} = dbTypes.CommonDBTypes;
import { hibernateSnakeCase } from './support/index.js';

const { SUPPORTED_VALIDATION_RULES } = validations;
const { isReservedTableName } = reservedKeywords;
Expand Down Expand Up @@ -654,74 +632,4 @@ ${instructions}`,
);
}
}

/**
* @private
* Return the method name which converts the filter to specification
* @param {string} fieldType
*/
getSpecificationBuilder(fieldType) {
if (
[
TYPE_INTEGER,
TYPE_LONG,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_BIG_DECIMAL,
TYPE_LOCAL_DATE,
TYPE_ZONED_DATE_TIME,
TYPE_INSTANT,
TYPE_DURATION,
].includes(fieldType)
) {
return 'buildRangeSpecification';
}
if (fieldType === TYPE_STRING) {
return 'buildStringSpecification';
}
return 'buildSpecification';
}

getJavaValueGeneratorForType(type) {
return getJavaValueForType(type);
}

/**
* @private
* Returns the primary key value based on the primary key type, DB and default value
*
* @param {string} primaryKey - the primary key type
* @param {string} databaseType - the database type
* @param {string} defaultValue - default value
* @returns {string} java primary key value
*/
getPrimaryKeyValue(primaryKey, databaseType = this.jhipsterConfig.databaseType, defaultValue = 1) {
return getPKValue(primaryKey, databaseType, defaultValue);
}

/**
* @private
* Convert to Java bean name case
*
* Handle the specific case when the second letter is capitalized
* See http://stackoverflow.com/questions/2948083/naming-convention-for-getters-setters-in-java
*
* @param {string} beanName name of the class to check
* @return {string}
*/
javaBeanCase(beanName) {
return javaBeanClassNameFormat(beanName);
}

buildJavaGet(reference) {
return javaGetCall(reference);
}

buildJavaGetter(reference, type = reference.type) {
return javaGetter(reference, type);
}

buildJavaSetter(reference, valueDefinition = `${reference.type} ${reference.name}`) {
return javaSetter(reference, valueDefinition);
}
}
10 changes: 6 additions & 4 deletions generators/server/support/templates/field-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*/
import { databaseTypes, fieldTypes } from '../../../../lib/jhipster/index.js';
import type { PrimaryKey } from '../../../../lib/types/application/entity.js';

const dbTypes = fieldTypes;
const { STRING, UUID, LONG, INTEGER } = dbTypes.CommonDBTypes;
Expand Down Expand Up @@ -50,15 +51,16 @@ export const getJavaValueGeneratorForType = (type) => {
* @param {number} defaultValue - default value
* @returns {string} java primary key value
*/
export const getPrimaryKeyValue = (primaryKey, databaseType, defaultValue = 1) => {
export const getPrimaryKeyValue = (primaryKey: PrimaryKey | string, databaseType: string, defaultValue = 1): string => {
if (typeof primaryKey === 'object' && primaryKey.composite) {
return `new ${primaryKey.type}(${primaryKey.references
.map(ref => getPrimaryKeyValue(ref.type, databaseType, defaultValue))
return `new ${primaryKey.type}(${primaryKey.fields
.map(ref => getPrimaryKeyValue(ref.fieldType, databaseType, defaultValue))
.join(', ')})`;
}
const random = ![1,2].includes(defaultValue);
const primaryKeyType = typeof primaryKey === 'string' ? primaryKey : primaryKey.type;
if (primaryKeyType === STRING) {
if (databaseType === SQL && defaultValue === 0) {
if (databaseType === SQL && random) {
return getJavaValueGeneratorForType(primaryKeyType);
}
return `"id${defaultValue}"`;
Expand Down
14 changes: 0 additions & 14 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { ADD_SPRING_MILESTONE_REPOSITORY } from '../generator-constants.js';
import {
addSpringFactory,
getJavaValueGeneratorForType,
getPrimaryKeyValue,
getSpecificationBuildForType,
insertContentIntoApplicationProperties,
javaBeanCase,
Expand Down Expand Up @@ -623,17 +622,4 @@ public void set${javaBeanCase(propertyName)}(${propertyType} ${propertyName}) {
get [BaseApplicationGenerator.END]() {
return this.delegateTasksToBlueprint(() => this.end);
}

/**
* @private
* Returns the primary key value based on the primary key type, DB and default value
*
* @param {string} primaryKey - the primary key type
* @param {string} databaseType - the database type
* @param {string} defaultValue - default value
* @returns {string} java primary key value
*/
getPrimaryKeyValue(primaryKey, databaseType = this.jhipsterConfig.databaseType, defaultValue = 1) {
return getPrimaryKeyValue(primaryKey, databaseType, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import <%= packageName %>.web.rest.TestUtil;
let id1;
let id2;
if (!embedded) {
id1 = this.getPrimaryKeyValue(primaryKey.type, databaseType, '1');
id2 = this.getPrimaryKeyValue(primaryKey.type, databaseType, '2');
id1 = primaryKey.javaSampleValues[0];
id2 = primaryKey.javaSampleValues[1];
} %>
<%_ if (primaryKey && primaryKey.hasUUID) { _%>
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class UserResourceIT {
<%_ if (databaseTypeCouchbase) { _%>
private static final String DEFAULT_ID = DEFAULT_LOGIN;
<%_ } else { _%>
private static final <%= user.primaryKey.type %> DEFAULT_ID = <%- this.getPrimaryKeyValue(user.primaryKey.type) %>;
private static final <%= user.primaryKey.type %> DEFAULT_ID = <%- user.primaryKey.javaSampleValues[0] %>;
<%_ } _%>
private static final String DEFAULT_EMAIL = "johndoe@localhost";
Expand Down Expand Up @@ -898,7 +898,7 @@ class UserResourceIT {
<%= user.persistClass %> user2 = new <%= user.persistClass %>();
user2.setId(user1.getId());
assertThat(user1).isEqualTo(user2);
user2.setId(<%- this.getPrimaryKeyValue(user.primaryKey.type, databaseType, 2) %>);
user2.setId(<%- user.primaryKey.javaSampleValues[1] %>);
assertThat(user1).isNotEqualTo(user2);
user1.setId(null);
assertThat(user1).isNotEqualTo(user2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UserMapperTest {
<%_ if (databaseTypeCouchbase) { _%>
private static final String DEFAULT_ID = DEFAULT_LOGIN;
<%_ } else { _%>
private static final <%= user.primaryKey.type %> DEFAULT_ID = <%- this.getPrimaryKeyValue(user.primaryKey.type) %>;
private static final <%= user.primaryKey.type %> DEFAULT_ID = <%- user.primaryKey.javaSampleValues[0] %>;
<%_ } _%>
private UserMapper userMapper;
Expand Down
1 change: 1 addition & 0 deletions lib/types/application/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type PrimaryKey<F extends BaseField = Field> = {
javaBuildSpecification?: string;

tsSampleValues?: (string | number)[];
javaSampleValues?: string[];
};

type ClientSample = Record<string, string | number | boolean | null>;
Expand Down

0 comments on commit 2cb9d34

Please sign in to comment.