From aa5e2a96325528dd80e310b8320be82d0dca935d Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Tue, 14 May 2024 22:43:41 +0200 Subject: [PATCH] baby step in JDL to generator types matching --- .../app_with_entities/.yo-rc.json | 1 + .../.jhipster/Country.json | 26 ++++++++++++++ .../.jhipster/Department.json | 32 +++++++++++++++++ .../.yo-rc.json | 36 +++++++++++++++++++ jdl/converters/json-to-jdl-converter.spec.ts | 8 +++++ jdl/converters/json-to-jdl-converter.ts | 27 ++++++++------ jdl/converters/types.ts | 9 +++++ 7 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Country.json create mode 100644 jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Department.json create mode 100644 jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.yo-rc.json diff --git a/jdl/__test-files__/json_to_jdl_converter/app_with_entities/.yo-rc.json b/jdl/__test-files__/json_to_jdl_converter/app_with_entities/.yo-rc.json index 1a0941f6eb74..39380d6953c3 100644 --- a/jdl/__test-files__/json_to_jdl_converter/app_with_entities/.yo-rc.json +++ b/jdl/__test-files__/json_to_jdl_converter/app_with_entities/.yo-rc.json @@ -1,4 +1,5 @@ { + "entities": [ "Country", "Department", "Employee", "Job", "JobHistory", "Location", "Region", "Task"], "generator-jhipster": { "promptValues": { "packageName": "com.mycompany.myapp" diff --git a/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Country.json b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Country.json new file mode 100644 index 000000000000..2dd5d2a9d615 --- /dev/null +++ b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Country.json @@ -0,0 +1,26 @@ +{ + "fluentMethods": false, + "jpaMetamodelFiltering": false, + "relationships": [ + { + "relationshipType": "one-to-one", + "relationshipValidateRules": "required", + "relationshipName": "region", + "otherEntityName": "region", + "ownerSide": true, + "otherEntityRelationshipName": "country" + } + ], + "fields": [ + { + "fieldName": "countryName", + "fieldType": "String" + } + ], + "changelogDate": "20190524215046", + "dto": "no", + "pagination": "pager", + "service": "no", + "documentation": "", + "entityTableName": "country" +} diff --git a/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Department.json b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Department.json new file mode 100644 index 000000000000..3c2ca1b54300 --- /dev/null +++ b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.jhipster/Department.json @@ -0,0 +1,32 @@ +{ + "fluentMethods": false, + "jpaMetamodelFiltering": false, + "relationships": [ + { + "relationshipType": "one-to-many", + "relationshipName": "employee", + "otherEntityName": "employee", + "otherEntityRelationshipName": "department" + }, + { + "relationshipType": "one-to-one", + "relationshipValidateRules": "required", + "relationshipName": "location", + "otherEntityName": "location", + "ownerSide": true, + "otherEntityRelationshipName": "department" + } + ], + "fields": [ + { + "fieldName": "departmentName", + "fieldType": "String" + } + ], + "changelogDate": "20190524215041", + "dto": "no", + "pagination": "no", + "service": "no", + "documentation": "", + "entityTableName": "department" +} diff --git a/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.yo-rc.json b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.yo-rc.json new file mode 100644 index 000000000000..f09c49d2e282 --- /dev/null +++ b/jdl/__test-files__/json_to_jdl_converter/app_with_inconsistent_information/.yo-rc.json @@ -0,0 +1,36 @@ +{ + "entities": [ "Country"], + "generator-jhipster": { + "promptValues": { + "packageName": "com.mycompany.myapp" + }, + "jhipsterVersion": "6.0.1", + "applicationType": "microservice", + "baseName": "truc", + "packageName": "com.mycompany.myapp", + "packageFolder": "com/mycompany/myapp", + "serverPort": "8081", + "authenticationType": "jwt", + "cacheProvider": "hazelcast", + "enableHibernateCache": true, + "websocket": "no", + "databaseType": "sql", + "devDatabaseType": "h2Disk", + "prodDatabaseType": "mysql", + "searchEngine": "no", + "messageBroker": "no", + "serviceDiscoveryType": "eureka", + "buildTool": "maven", + "enableSwaggerCodegen": false, + "jwtSecretKey": "HIDDEN", + "testFrameworks": [], + "jhiPrefix": "jhi", + "entitySuffix": "", + "dtoSuffix": "DTO", + "enableTranslation": false, + "clientPackageManager": "npm", + "skipClient": true, + "nativeLanguage": "en", + "skipUserManagement": true + } +} diff --git a/jdl/converters/json-to-jdl-converter.spec.ts b/jdl/converters/json-to-jdl-converter.spec.ts index a31f56174def..5927bae86cbf 100644 --- a/jdl/converters/json-to-jdl-converter.spec.ts +++ b/jdl/converters/json-to-jdl-converter.spec.ts @@ -85,6 +85,14 @@ describe('jdl - JSONToJDLConverter', () => { `); }); }); + describe('with entities inconsistent informations', () => { + it('should fail with an error', () => { + dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_inconsistent_information'); + jdlFilename = 'app.jdl'; + expect(() => convertToJDL(dir)).to.throw(); + }); + }); + describe('with entities', () => { beforeEach(() => { dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_entities'); diff --git a/jdl/converters/json-to-jdl-converter.ts b/jdl/converters/json-to-jdl-converter.ts index ca05af679479..05a27a16d4f4 100644 --- a/jdl/converters/json-to-jdl-converter.ts +++ b/jdl/converters/json-to-jdl-converter.ts @@ -26,7 +26,7 @@ import { readJSONFile } from '../readers/json-file-reader.js'; import { convertApplicationToJDL } from './json-to-jdl-application-converter.js'; import { convertEntitiesToJDL } from './json-to-jdl-entity-converter.js'; import exportJDLObject from '../exporters/jdl-exporter.js'; -import { JSONEntity } from './types.js'; +import { JSONEntity, JSONRootObject } from './types.js'; import { removeFieldsWithNullishValues } from '../../generators/base/support/config.js'; import { GENERATOR_JHIPSTER } from '../../generators/generator-constants.js'; @@ -40,13 +40,13 @@ export default { * @param directory the directory to find JHipster files. * @param output the file where the JDL will be written */ -export function convertToJDL(directory = '.', output: string | false = 'app.jdl') { +export function convertToJDL(directory = '.', output: string | false = 'app.jdl'): JDLObject | undefined { let jdlObject: JDLObject; if (doesFileExist(path.join(directory, '.yo-rc.json'))) { - const yoRcFileContent = readJSONFile(path.join(directory, '.yo-rc.json')); - let entities; + const yoRcFileContent: Partial = readJSONFile(path.join(directory, '.yo-rc.json')); + let entities: Map | undefined; if (doesDirectoryExist(path.join(directory, '.jhipster'))) { - entities = getJSONEntityFiles(directory); + entities = getJSONEntityFiles(directory, yoRcFileContent); } jdlObject = getJDLObjectFromSingleApplication(yoRcFileContent, entities); } else { @@ -63,11 +63,11 @@ export function convertToJDL(directory = '.', output: string | false = 'app.jdl' return jdlObject; } -export function convertSingleContentToJDL(yoRcFileContent: Record, entities?: Map) { +export function convertSingleContentToJDL(yoRcFileContent: Record, entities?: Map): string { return getJDLObjectFromSingleApplication(yoRcFileContent, entities).toString(); } -function getJDLObjectFromMultipleApplications(directory: string) { +function getJDLObjectFromMultipleApplications(directory: string): JDLObject { const subDirectories = getSubdirectories(directory); if (subDirectories.length === 0) { throw new Error('There are no subdirectories.'); @@ -75,10 +75,10 @@ function getJDLObjectFromMultipleApplications(directory: string) { let jdlObject = new JDLObject(); subDirectories.forEach(subDirectory => { const applicationDirectory = path.join(directory, subDirectory); - const yoRcFileContent = readJSONFile(path.join(applicationDirectory, '.yo-rc.json')); + const yoRcFileContent: Partial = readJSONFile(path.join(applicationDirectory, '.yo-rc.json')); let entities: Map = new Map(); if (doesDirectoryExist(path.join(applicationDirectory, '.jhipster'))) { - entities = getJSONEntityFiles(applicationDirectory); + entities = getJSONEntityFiles(applicationDirectory, yoRcFileContent); } jdlObject = getJDLObjectFromSingleApplication(yoRcFileContent, entities, jdlObject); }); @@ -116,12 +116,17 @@ function cleanYoRcFileContent(yoRcFileContent: Record) { return yoRcFileContent; } -function getJSONEntityFiles(applicationDirectory: string) { +function getJSONEntityFiles(applicationDirectory: string, yoRcFileContent: Partial): Map { const entities: Map = new Map(); fs.readdirSync(path.join(applicationDirectory, '.jhipster')).forEach(file => { + const entityName = file.slice(0, file.indexOf('.json')); + if (!yoRcFileContent.entities?.includes(entityName)) { + throw new Error( + `Mismatch identified between the application description amount of entity listed and the entity found in the .jhipster folder for entity name: ${entityName}`, + ); + } const jsonFilePath = path.join(applicationDirectory, '.jhipster', file); if (fs.statSync(jsonFilePath).isFile() && file.endsWith('.json')) { - const entityName = file.slice(0, file.indexOf('.json')); entities.set(entityName, readJSONFile(jsonFilePath)); } }); diff --git a/jdl/converters/types.ts b/jdl/converters/types.ts index a501d412edc4..2936f8a7e2fd 100644 --- a/jdl/converters/types.ts +++ b/jdl/converters/types.ts @@ -30,3 +30,12 @@ export type JSONEntity = { fields?: JSONField[]; relationships?: JSONRelationship[]; } & Record; + +export type JSONGeneratorJhipsterContent = { + applicationType: string; +} & Record; + +export type JSONRootObject = { + entities: string[]; + ['generator-jhipster']: JSONGeneratorJhipsterContent; +};