Skip to content

Commit

Permalink
fix some sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 22, 2024
1 parent b2e93c4 commit 819b1cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -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())
<%_ } _%>
Expand All @@ -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; _%>
<%_ } _%>
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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_") { _%>
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}); _%>
Expand All @@ -643,7 +644,7 @@ _%>
<%_ } _%>
@BeforeEach
public void initTest() {
<%= persistInstance %> = createEntity(<% if (databaseTypeSql) { %>em<% } %>);
<%= persistInstance %> = createEntity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>em<% } %>);
}

@AfterEach
Expand All @@ -656,7 +657,7 @@ _%>
inserted<%= persistClass %> = null;
}
<%_ if (databaseTypeSql && reactive) { _%>
deleteEntities(em);
deleteEntities(<% if (anyRelationshipIsRequired) { %>em<% } %>);
<%_ } _%>
<%_ if (requiresDeleteAllUsers && hasUserRepository) { _%>
<%_ if (databaseTypeSql && reactive) { _%>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 %>;
Expand Down

0 comments on commit 819b1cb

Please sign in to comment.