diff --git a/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts b/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts index c500c16afc61..93544a4a35d2 100644 --- a/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts +++ b/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts @@ -66,7 +66,7 @@ function getRelatedRelationships(relationships, entityNames) { } if ( jdlRelationship.to === entityName && - (jdlRelationship.injectedFieldInTo || Object.keys(jdlRelationship.options.destination).length !== 0) + (jdlRelationship.injectedFieldInTo || Object.keys(jdlRelationship.options.source).length !== 0) ) { relationshipsRelatedToEntity.to.push(jdlRelationship); } diff --git a/jdl/integration-test.spec.ts b/jdl/integration-test.spec.ts index 208dc110c8b4..3d0b85744569 100644 --- a/jdl/integration-test.spec.ts +++ b/jdl/integration-test.spec.ts @@ -21,12 +21,14 @@ import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; import { expect } from 'chai'; +import { jestExpect } from 'esmocha'; import { applicationTypes } from './jhipster/index.mjs'; -import { parseFromFiles } from './readers/jdl-reader.js'; +import { parseFromContent, parseFromFiles } from './readers/jdl-reader.js'; import DocumentParser from './converters/parsed-jdl-to-jdl-object/parsed-jdl-to-jdl-object-converter.js'; import exportToJDL from './exporters/jdl-exporter.js'; import { basicHelpers as helpers } from '../test/support/index.mjs'; +import { convert } from './converters/jdl-to-json/jdl-without-application-to-json-converter.js'; const { MONOLITH } = applicationTypes; const __filename = fileURLToPath(import.meta.url); @@ -57,4 +59,252 @@ describe('jdl - integration tests', () => { expect(writtenContent.toString()).to.equal(originalContent.toString()); }); }); + + context('when parsing entities JDL', () => { + const applicationName = 'jhipster'; + const jdl = ` +entity A {} +entity B {} +relationship ManyToOne { + A to B +} +`; + context('with bidirectional relationship', () => { + let result: Map; + + beforeEach(() => { + result = convert({ + applicationName, + databaseType: 'sql', + jdlObject: DocumentParser.parseFromConfigurationObject({ + parsedContent: parseFromContent(jdl), + applicationType: MONOLITH, + }), + }); + }); + + it('should add relationship at both sides', () => { + jestExpect(result.get('jhipster')![0].relationships.length).toBe(1); + jestExpect(result.get('jhipster')![1].relationships.length).toBe(1); + }); + + it('should result matching', () => { + jestExpect(result).toMatchInlineSnapshot(` +Map { + "jhipster" => [ + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "a", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "A", + "pagination": undefined, + "readOnly": undefined, + "relationships": [ + { + "otherEntityName": "b", + "otherEntityRelationshipName": "a", + "relationshipName": "b", + "relationshipSide": "left", + "relationshipType": "many-to-one", + }, + ], + "service": undefined, + }, + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "b", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "B", + "pagination": undefined, + "readOnly": undefined, + "relationships": [ + { + "otherEntityName": "a", + "otherEntityRelationshipName": "b", + "relationshipName": "a", + "relationshipSide": "right", + "relationshipType": "one-to-many", + }, + ], + "service": undefined, + }, + ], +} +`); + }); + }); + + context('with unidirectional relationship and annotation at destination', () => { + let result: Map; + const jdl = ` +entity A {} +entity B {} +relationship ManyToOne { + A{b} to @AnnotationAtASide B +} +`; + + beforeEach(() => { + result = convert({ + applicationName, + databaseType: 'sql', + jdlObject: DocumentParser.parseFromConfigurationObject({ + parsedContent: parseFromContent(jdl), + applicationType: MONOLITH, + }), + }); + }); + + it('should add relationship at one side', () => { + jestExpect(result.get('jhipster')![0].relationships.length).toBe(1); + jestExpect(result.get('jhipster')![1].relationships.length).toBe(0); + }); + + it('should result matching', () => { + jestExpect(result).toMatchInlineSnapshot(` +Map { + "jhipster" => [ + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "a", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "A", + "pagination": undefined, + "readOnly": undefined, + "relationships": [ + { + "options": { + "annotationAtASide": true, + }, + "otherEntityName": "b", + "relationshipName": "b", + "relationshipSide": "left", + "relationshipType": "many-to-one", + }, + ], + "service": undefined, + }, + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "b", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "B", + "pagination": undefined, + "readOnly": undefined, + "relationships": [], + "service": undefined, + }, + ], +} +`); + }); + }); + + context('with unidirectional relationship and annotation at both sides', () => { + let result: Map; + const jdl = ` +entity A {} +entity B {} +relationship ManyToOne { + @AnnotationAtBSide A{b} to @AnnotationAtASide B +} +`; + + beforeEach(() => { + result = convert({ + applicationName, + databaseType: 'sql', + jdlObject: DocumentParser.parseFromConfigurationObject({ + parsedContent: parseFromContent(jdl), + applicationType: MONOLITH, + }), + }); + }); + + it('should add relationship at both sides', () => { + jestExpect(result.get('jhipster')![0].relationships.length).toBe(1); + jestExpect(result.get('jhipster')![1].relationships.length).toBe(1); + }); + + it('should result matching', () => { + jestExpect(result).toMatchInlineSnapshot(` +Map { + "jhipster" => [ + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "a", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "A", + "pagination": undefined, + "readOnly": undefined, + "relationships": [ + { + "options": { + "annotationAtASide": true, + }, + "otherEntityName": "b", + "relationshipName": "b", + "relationshipSide": "left", + "relationshipType": "many-to-one", + }, + ], + "service": undefined, + }, + JSONEntity { + "applications": "*", + "documentation": undefined, + "dto": undefined, + "embedded": undefined, + "entityTableName": "b", + "fields": [], + "fluentMethods": undefined, + "jpaMetamodelFiltering": undefined, + "name": "B", + "pagination": undefined, + "readOnly": undefined, + "relationships": [ + { + "options": { + "annotationAtBSide": true, + }, + "otherEntityName": "a", + "otherEntityRelationshipName": "b", + "relationshipName": "a", + "relationshipSide": "right", + "relationshipType": "one-to-many", + }, + ], + "service": undefined, + }, + ], +} +`); + }); + }); + }); });