From ca5ecee28569e2957aa10ad0ec008316ce0bd7ca Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Fri, 23 Aug 2024 11:48:09 -0300 Subject: [PATCH] fix some sonar issues (#27039) --- .github/labeler.yml | 3 ++ generators/base-application/types/entity.d.ts | 5 +++ .../bootstrap-application-base/generator.ts | 3 ++ .../bootstrap-application/generator.spec.ts | 3 ++ .../web/rest/PublicUserResourceIT.java.ejs | 10 +---- .../web/rest/UserResourceIT.java.ejs | 8 ++-- .../web/rest/_entityClass_ResourceIT.java.ejs | 39 ++++++++++--------- 7 files changed, 40 insertions(+), 31 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 72444e7be8b8..bb4d8ac7ebec 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -75,7 +75,10 @@ - changed-files: - any-glob-to-any-file: - generators/common/** + - generators/java/**/* + - generators/liquibase/**/* - generators/server/**/* + - generators/spring-*/**/* 'theme: upgrade': - changed-files: 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/bootstrap-application/generator.spec.ts b/generators/bootstrap-application/generator.spec.ts index e29f6f97be78..463a3df159fe 100644 --- a/generators/bootstrap-application/generator.spec.ts +++ b/generators/bootstrap-application/generator.spec.ts @@ -189,6 +189,7 @@ describe(`generator - ${generator}`, () => { "anyFieldIsUUID": true, "anyFieldIsZonedDateTime": false, "anyPropertyHasValidation": true, + "anyRelationshipIsRequired": false, "applicationType": "monolith", "auditableEntity": true, "authenticationType": "jwt", @@ -1089,6 +1090,7 @@ describe(`generator - ${generator}`, () => { "anyFieldIsUUID": true, "anyFieldIsZonedDateTime": false, "anyPropertyHasValidation": false, + "anyRelationshipIsRequired": false, "applicationType": "monolith", "authenticationType": "jwt", "baseName": "jhipster", @@ -1432,6 +1434,7 @@ describe(`generator - ${generator}`, () => { "anyFieldIsUUID": true, "anyFieldIsZonedDateTime": false, "anyPropertyHasValidation": false, + "anyRelationshipIsRequired": false, "applicationType": "monolith", "authenticationType": "jwt", "baseName": "jhipster", diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs index b845922e078d..404a8143e018 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/PublicUserResourceIT.java.ejs @@ -75,9 +75,6 @@ import org.springframework.transaction.annotation.Transactional; import reactor.core.publisher.Mono; <%_ } _%> -<%_ if (databaseTypeSql && !reactive) { _%> -import jakarta.persistence.EntityManager; -<%_ } _%> <%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%> import java.util.stream.Stream; <%_ } _%> @@ -130,11 +127,6 @@ class PublicUserResourceIT { @Autowired private UserSearchRepository mockUserSearchRepository; <%_ } _%> -<%_ if (databaseTypeSql) { _%> - - @Autowired - private EntityManager em; -<%_ } _%> <%_ if (cacheProviderAny) { _%> @Autowired @@ -168,7 +160,7 @@ class PublicUserResourceIT { <%_ } _%> @BeforeEach public void initTest() { - user = UserResourceIT.initTestUser(<% if (databaseTypeSql) { %>em<% } %>); + user = UserResourceIT.initTestUser(); } @AfterEach diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/UserResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/UserResourceIT.java.ejs index 3eb7946ce45c..e845f16e570c 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/UserResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/UserResourceIT.java.ejs @@ -226,7 +226,7 @@ class UserResourceIT { * This is a static method, as tests for other entities might also need it, * if they test an entity which has a required relationship to the User entity. */ - public static <%= user.persistClass %> createEntity(<% if (databaseTypeSql) { %>EntityManager em<% } %>) { + public static <%= user.persistClass %> createEntity() { <%= user.persistClass %> persistUser = new <%= user.persistClass %>(); <%_ if (databaseTypeCassandra || (authenticationTypeOauth2 && !databaseTypeCouchbase)) { _%> persistUser.setId(UUID.randomUUID().toString()); @@ -260,8 +260,8 @@ class UserResourceIT { /** * Setups the database with one user. */ - public static <%= user.persistClass %> initTestUser(<% if (databaseTypeSql) { %>EntityManager em<% } _%>) { - <%= user.persistClass %> persistUser = createEntity(<% if (databaseTypeSql ) { %>em<% } _%>); + public static <%= user.persistClass %> initTestUser() { + <%= user.persistClass %> persistUser = createEntity(); <%_ if (databaseTypeSql) { _%> persistUser.setLogin(DEFAULT_LOGIN); persistUser.setEmail(DEFAULT_EMAIL); @@ -271,7 +271,7 @@ class UserResourceIT { @BeforeEach public void initTest() { - user = initTestUser(<% if (databaseTypeSql) { %>em<% } _%>); + user = initTestUser(); } @AfterEach 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..18719bb62579 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 @@ -516,15 +516,16 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( <%_ }); }_%> <%_ ['DEFAULT_', 'UPDATED_'].forEach((fieldStatus) => { _%> +<%_ const createInstance = fieldStatus === 'UPDATED_' ? `updated${persistClass}` : persistInstance; _%> /** * Create an <% if (fieldStatus === 'UPDATED_') { %>updated <% } %>entity for this test. * * 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 %> <%= createInstance %> = <% } %>new <%= persistClass %>() <%_ if (reactive && databaseTypeSql && primaryKey.typeUUID && !isUsingMapsId) { _%> .<%= primaryKey.name %>(UUID.randomUUID()) <%_ } _%> @@ -537,28 +538,29 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( .<%= field.fieldName %>ContentType(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE) <%_ } _%> <%_ } _%>; - <%_ if (persistableRelationships.length === 0) { _%> + <%_ if (!anyRelationshipIsRequired) { _%> } <%_ return; _%> <%_ } _%> <%_ } else { _%> - <%= persistClass %> <%= persistInstance %> = new <%= persistClass %>(); + <%= persistClass %> <%= createInstance %> = new <%= persistClass %>(); <%_ if (reactive && databaseTypeSql && primaryKey.typeUUID && !isUsingMapsId) { _%> - <%= persistInstance %>.set<%= primaryKey.fields[0].fieldInJavaBeanMethod %>(UUID.randomUUID()); + <%= createInstance %>.set<%= primaryKey.fields[0].fieldInJavaBeanMethod %>(UUID.randomUUID()); <%_ } _%> <%_ if (primaryKey.typeString && !isUsingMapsId && !primaryKey.autoGenerate) { _%> - <%= persistInstance %>.set<%= primaryKey.fields[0].fieldInJavaBeanMethod %>(UUID.randomUUID().toString()); + <%= createInstance %>.set<%= primaryKey.fields[0].fieldInJavaBeanMethod %>(UUID.randomUUID().toString()); <%_ } _%> <%_ for (field of fieldsToTest) { _%> - <%= persistInstance %>.set<%= field.fieldInJavaBeanMethod %>(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>); + <%= createInstance %>.set<%= field.fieldInJavaBeanMethod %>(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>); <%_ if (field.fieldTypeBinary && !field.blobContentTypeText) { _%> - <%= persistInstance %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE); + <%= createInstance %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE); <%_ } _%> <%_ } _%> <%_ } _%> <%_ const alreadyGeneratedEntities = []; for (relationship of persistableRelationships) { + const otherEntity = relationship.otherEntity; const relationshipValidate = relationship.relationshipValidate; const otherEntityName = relationship.otherEntityName; const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; @@ -569,7 +571,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 +585,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 +594,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"); @@ -600,14 +602,14 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( <%_ } _%> <%_ } _%> <%_ if (relationship.relationshipManyToMany || relationship.relationshipOneToMany) { _%> - <%= persistInstance %>.get<%= relationshipNameCapitalizedPlural %>().add(<%= otherEntityName %>); + <%= createInstance %>.get<%= relationshipNameCapitalizedPlural %>().add(<%= otherEntityName %>); <%_ } else { _%> - <%= persistInstance %>.set<%= relationshipNameCapitalized %>(<%= otherEntityName %>); + <%= createInstance %>.set<%= relationshipNameCapitalized %>(<%= otherEntityName %>); <%_ } _%> <%_ alreadyGeneratedEntities.push(otherEntityName) _%> <%_ } _%> <%_ } _%> - return <%= persistInstance %>; + return <%= createInstance %>; } <%_ }); _%> @@ -643,7 +645,7 @@ _%> <%_ } _%> @BeforeEach public void initTest() { - <%= persistInstance %> = createEntity(<% if (databaseTypeSql) { %>em<% } %>); + <%= persistInstance %> = createEntity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>em<% } %>); } @AfterEach @@ -796,13 +798,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 +1242,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 %>;