Skip to content

Commit

Permalink
fix some sonar issues (#27039)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Aug 23, 2024
1 parent eeb62e5 commit ca5ecee
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions generators/base-application/types/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ type Entity = Required<BaseEntity> &
*/
anyFieldHasFileBasedContentType: boolean;

/**
* Any relationship is required or id
*/
anyRelationshipIsRequired: boolean;

dtoMapstruct: boolean;
};

Expand Down
3 changes: 3 additions & 0 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": true,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"auditableEntity": true,
"authenticationType": "jwt",
Expand Down Expand Up @@ -1089,6 +1090,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": false,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"authenticationType": "jwt",
"baseName": "jhipster",
Expand Down Expand Up @@ -1432,6 +1434,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": false,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"authenticationType": "jwt",
"baseName": "jhipster",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
<%_ } _%>
Expand Down Expand Up @@ -130,11 +127,6 @@ class PublicUserResourceIT {
@Autowired
private UserSearchRepository mockUserSearchRepository;
<%_ } _%>
<%_ if (databaseTypeSql) { _%>
@Autowired
private EntityManager em;
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
@Autowired
Expand Down Expand Up @@ -168,7 +160,7 @@ class PublicUserResourceIT {
<%_ } _%>
@BeforeEach
public void initTest() {
user = UserResourceIT.initTestUser(<% if (databaseTypeSql) { %>em<% } %>);
user = UserResourceIT.initTestUser();
}
@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -271,7 +271,7 @@ class UserResourceIT {

@BeforeEach
public void initTest() {
user = initTestUser(<% if (databaseTypeSql) { %>em<% } _%>);
user = initTestUser();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
<%_ } _%>
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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_") { _%>
Expand All @@ -592,22 +594,22 @@ 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");
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ 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 %>;
}
<%_ }); _%>

Expand Down Expand Up @@ -643,7 +645,7 @@ _%>
<%_ } _%>
@BeforeEach
public void initTest() {
<%= persistInstance %> = createEntity(<% if (databaseTypeSql) { %>em<% } %>);
<%= persistInstance %> = createEntity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>em<% } %>);
}

@AfterEach
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 %>;
Expand Down

0 comments on commit ca5ecee

Please sign in to comment.