From 819b1cb25bf024a75070f76383f7d0d26075092e Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 22 Aug 2024 20:11:22 -0300 Subject: [PATCH] fix some sonar issues --- generators/base-application/types/entity.d.ts | 5 ++++ .../bootstrap-application-base/generator.ts | 3 +++ .../web/rest/_entityClass_ResourceIT.java.ejs | 26 ++++++++++--------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/generators/base-application/types/entity.d.ts b/generators/base-application/types/entity.d.ts index 326c5beaf181..d30d16d74561 100644 --- a/generators/base-application/types/entity.d.ts +++ b/generators/base-application/types/entity.d.ts @@ -128,6 +128,11 @@ type Entity = Required & */ anyFieldHasFileBasedContentType: boolean; + /** + * Any relationship is required or id + */ + anyRelationshipIsRequired: boolean; + dtoMapstruct: boolean; }; diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index c1ced6a2c432..bbfa5917a902 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -382,6 +382,9 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { get postPreparingEachEntity() { return this.asPostPreparingEachEntityTaskGroup({ + hasRequiredRelationship({ entity }) { + entity.anyRelationshipIsRequired = entity.relationships.some(rel => rel.relationshipRequired || rel.id); + }, checkForCircularRelationships({ entity }) { const detectCyclicRequiredRelationship = (entity, relatedEntities) => { if (relatedEntities.has(entity)) return true; diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs index 1ba83316dc5d..a1224e6a7196 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs @@ -522,9 +522,9 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( * This is a static method, as tests for other entities might also need it, * if they test an entity which requires the current entity. */ - public static <%= persistClass %> create<% if (fieldStatus === 'UPDATED_') { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql) { %>EntityManager em<% } %>) { + public static <%= persistClass %> create<% if (fieldStatus === 'UPDATED_') { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>EntityManager em<% } %>) { <%_ if (fluentMethods) { _%> - <% if (persistableRelationships.length === 0) { %>return <% } else { %><%= persistClass %> <%= persistInstance %> = <% } %>new <%= persistClass %>() + <% if (!anyRelationshipIsRequired) { %>return <% } else { %><%= persistClass %> <%= persistInstance %> = <% } %>new <%= persistClass %>() <%_ if (reactive && databaseTypeSql && primaryKey.typeUUID && !isUsingMapsId) { _%> .<%= primaryKey.name %>(UUID.randomUUID()) <%_ } _%> @@ -537,7 +537,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( .<%= field.fieldName %>ContentType(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE) <%_ } _%> <%_ } _%>; - <%_ if (persistableRelationships.length === 0) { _%> + <%_ if (!anyRelationshipIsRequired) { _%> } <%_ return; _%> <%_ } _%> @@ -559,6 +559,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( <%_ const alreadyGeneratedEntities = []; for (relationship of persistableRelationships) { + const otherEntity = relationship.otherEntity; const relationshipValidate = relationship.relationshipValidate; const otherEntityName = relationship.otherEntityName; const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; @@ -569,7 +570,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( // Add required entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> <%_ if (relationship.otherEntityUser) { /* TODO or other entity has no unique fields */ _%> - <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (databaseTypeSql) { %>em<% } %>)<%= createEntityPostfix %>; + <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (databaseTypeSql && otherEntity.anyRelationshipIsRequired) { %>em<% } %>)<%= createEntityPostfix %>; <%_ if (databaseTypeSql && !reactive) { _%> em.persist(<%= otherEntityName %>); em.flush(); @@ -583,7 +584,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( <%_ if (!isUsingMapsId || fieldStatus !== "UPDATED_") { _%> if (TestUtil.findAll(em, <%= relationship.otherEntity.persistClass %>.class).isEmpty()) { <%_ } _%> - <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(em)<%= createEntityPostfix %>; + <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (relationship.otherEntity.anyRelationshipIsRequired) { %>em<% } %>)<%= createEntityPostfix %>; em.persist(<%= otherEntityName %>); em.flush(); <%_ if (!isUsingMapsId || fieldStatus !== "UPDATED_") { _%> @@ -592,7 +593,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( } <%_ } _%> <%_ } else { _%> - <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (databaseType === 'sql') { %>em<% } %>)<%= createEntityPostfix %>; + <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (databaseType === 'sql' && otherEntity.anyRelationshipIsRequired) { %>em<% } %>)<%= createEntityPostfix %>; <%_ } _%> <%_ if (databaseTypeMongodb) { _%> <%= otherEntityName %>.set<%= primaryKey.nameCapitalized %>("fixed-id-for-tests"); @@ -627,7 +628,7 @@ _%> } <%_ relationships.forEach(function(rel) { if ((rel.relationshipValidate || rel.id) && !alreadyGeneratedDeletionCalls.includes(rel.otherEntityName)) { _%> - <%= rel.otherEntityNameCapitalized %>ResourceIT.deleteEntities(em); + <%= rel.otherEntityNameCapitalized %>ResourceIT.deleteEntities(<% if (rel.otherEntity.anyRelationshipIsRequired) { %>em<% } %>); <%_ alreadyGeneratedDeletionCalls.push(rel.otherEntityName); } }); _%> @@ -643,7 +644,7 @@ _%> <%_ } _%> @BeforeEach public void initTest() { - <%= persistInstance %> = createEntity(<% if (databaseTypeSql) { %>em<% } %>); + <%= persistInstance %> = createEntity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>em<% } %>); } @AfterEach @@ -656,7 +657,7 @@ _%> inserted<%= persistClass %> = null; } <%_ if (databaseTypeSql && reactive) { _%> - deleteEntities(em); + deleteEntities(<% if (anyRelationshipIsRequired) { %>em<% } %>); <%_ } _%> <%_ if (requiresDeleteAllUsers && hasUserRepository) { _%> <%_ if (databaseTypeSql && reactive) { _%> @@ -796,13 +797,14 @@ _%> int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>); <%_ } _%> <%_ for (relationship of relationships) { + const otherEntity = relationship.otherEntity; const otherEntityName = relationship.otherEntityName; const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; const mapsIdUse = relationship.id; if (mapsIdUse) { _%> // Add a new parent entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> - <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (!relationship.otherEntityUser) { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql) { %>em<% } %>); + <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (!relationship.otherEntityUser) { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql && otherEntity.anyRelationshipIsRequired) { %>em<% } %>); <%_ if (databaseTypeSql && !reactive) { _%> em.persist(<%= otherEntityName %>); em.flush(); @@ -1239,12 +1241,12 @@ _%> <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %>; if (TestUtil.findAll(em, <%= relationship.otherEntity.persistClass %>.class).isEmpty()) { <%= entityInstance %>Repository.saveAndFlush(<%= persistInstance %>); - <%= relationship.relationshipFieldName %> = <%= createEntityPrefix %><%= relationship.otherEntityNameCapitalized %>ResourceIT.createEntity(em); + <%= relationship.relationshipFieldName %> = <%= createEntityPrefix %><%= relationship.otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (relationship.otherEntity.anyRelationshipIsRequired) { %>em<% } %>); } else { <%= relationship.relationshipFieldName %> = TestUtil.findAll(em, <%= relationship.otherEntity.persistClass %>.class).get(0); } <%_ } else { _%> - <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %> = <%= relationship.otherEntityNameCapitalized %>ResourceIT.createEntity(em); + <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %> = <%= relationship.otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (relationship.otherEntity.anyRelationshipIsRequired) { %>em<% } %>); <%_ } _%> <%_ if(reactive) { _%> <%= relationship.otherEntity.persistInstance %>Repository.<%= saveMethod %>(<%= relationship.relationshipFieldName %>)<%= callBlock %>;