diff --git a/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs b/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs index 42550cecfe40..46a142aa528d 100644 --- a/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs +++ b/generators/entity-server/templates/src/test/java/package/web/rest/EntityResourceIT.java.ejs @@ -409,23 +409,24 @@ _%> .setValidator(validator).build(); } +<%_ ['DEFAULT_', 'UPDATED_'].forEach((fieldStatus) => { _%> /** - * Create an entity for this test. + * 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 <%= asEntity(entityClass) %> createEntity(<% if (databaseType === 'sql') { %>EntityManager em<% } %>) { + public static <%= asEntity(entityClass) %> create<% if (fieldStatus === 'UPDATED_') { _%>Updated<%_ } %>Entity(<% if (databaseType === 'sql') { %>EntityManager em<% } %>) { <%_ if (fluentMethods) { _%> <%= asEntity(entityClass) %> <%= asEntity(entityInstance) %> = new <%= asEntity(entityClass) %>()<% for (idx in fields) { %> - .<%= fields[idx].fieldName %>(<%='DEFAULT_' + fields[idx].fieldNameUnderscored.toUpperCase()%>)<% if ((fields[idx].fieldType === 'byte[]' || fields[idx].fieldType === 'ByteBuffer') && fields[idx].fieldTypeBlobContent !== 'text') { %> - .<%= fields[idx].fieldName %>ContentType(<%='DEFAULT_' + fields[idx].fieldNameUnderscored.toUpperCase()%>_CONTENT_TYPE)<% } %><% } %>; + .<%= fields[idx].fieldName %>(<%=fieldStatus + fields[idx].fieldNameUnderscored.toUpperCase()%>)<% if ((fields[idx].fieldType === 'byte[]' || fields[idx].fieldType === 'ByteBuffer') && fields[idx].fieldTypeBlobContent !== 'text') { %> + .<%= fields[idx].fieldName %>ContentType(<%=fieldStatus + fields[idx].fieldNameUnderscored.toUpperCase()%>_CONTENT_TYPE)<% } %><% } %>; <%_ } else { _%> <%= asEntity(entityClass) %> <%= asEntity(entityInstance) %> = new <%= asEntity(entityClass) %>(); <%_ for (idx in fields) { _%> - <%= asEntity(entityInstance) %>.set<%= fields[idx].fieldInJavaBeanMethod %>(<%='DEFAULT_' + fields[idx].fieldNameUnderscored.toUpperCase() %>); + <%= asEntity(entityInstance) %>.set<%= fields[idx].fieldInJavaBeanMethod %>(<%=fieldStatus + fields[idx].fieldNameUnderscored.toUpperCase() %>); <%_ if ((fields[idx].fieldType === 'byte[]' || fields[idx].fieldType === 'ByteBuffer') && fields[idx].fieldTypeBlobContent !== 'text') { _%> - <%= asEntity(entityInstance) %>.set<%= fields[idx].fieldInJavaBeanMethod %>ContentType(<%='DEFAULT_' + fields[idx].fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE); + <%= asEntity(entityInstance) %>.set<%= fields[idx].fieldInJavaBeanMethod %>ContentType(<%=fieldStatus + fields[idx].fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE); <%_ } _%> <%_ } _%> <%_ } _%> @@ -442,6 +443,7 @@ _%> if ((relationshipValidate !== null && relationshipValidate === true) || mapsIdUse === true) { _%> // Add required entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> + <%_ if (otherEntityName === 'user') { // TODO or other entity has no unique fields _%> <%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (databaseType === 'sql') { %>em<% } %>); <%_ if (databaseType === 'sql') { _%> em.persist(<%= otherEntityName %>); @@ -450,6 +452,21 @@ _%> <%_ if (databaseType === 'mongodb') { _%> <%= otherEntityName %>.setId("fixed-id-for-tests"); <%_ } _%> + <%_ } else { _%> + <%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>; + if (TestUtil.findAll(em, <%= otherEntityNameCapitalized %>.class).isEmpty()) { + <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (databaseType === 'sql') { %>em<% } %>); + <%_ if (databaseType === 'sql') { _%> + em.persist(<%= otherEntityName %>); + em.flush(); + <%_ } _%> + <%_ if (databaseType === 'mongodb') { _%> + <%= otherEntityName %>.setId("fixed-id-for-tests"); + <%_ } _%> + } else { + <%= otherEntityName %> = TestUtil.findAll(em, <%= otherEntityNameCapitalized %>.class).get(0); + } + <%_ } _%> <%_ } _%> <%_ if (relationshipType === 'many-to-many' || relationshipType === 'one-to-many') { _%> <%= asEntity(entityInstance) %>.get<%= relationshipNameCapitalizedPlural %>().add(<%= otherEntityName %>); @@ -460,6 +477,7 @@ _%> <%_ } } _%> return <%= asEntity(entityInstance) %>; } +<%_ }); _%> @BeforeEach public void initTest() { @@ -560,7 +578,7 @@ _%> if (mapsIdUse === true) { _%> // Add a new parent entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> - <%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.createEntity(em); + <%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (otherEntityName !== 'user') { _%>Updated<%_ } %>Entity(em); em.persist(<%= otherEntityName %>); em.flush(); <%_ } _%> @@ -813,6 +831,10 @@ _%> @Test<% if (databaseType === 'sql') { %> @Transactional<% } %> public void getAll<%= entityClassPlural %>By<%= relationship.relationshipNameCapitalized %>IsEqualToSomething() throws Exception { + <%_ if (relationship.relationshipValidate === true || relationship.useJPADerivedIdentifier === true) { _%> + // Get already existing entity + <%= asEntity(relationship.otherEntityNameCapitalized) %> <%= relationship.relationshipFieldName %> = <%= asEntity(entityInstance) %>.get<%= relationship.relationshipNameCapitalized %>(); + <%_ } else { _%> // Initialize the database <%= asEntity(relationship.otherEntityNameCapitalized) %> <%= relationship.relationshipFieldName %> = <%= relationship.otherEntityNameCapitalized %>ResourceIT.createEntity(em); em.persist(<%= relationship.relationshipFieldName %>); @@ -825,6 +847,7 @@ _%> <%= relationship.relationshipFieldName %>.set<%= relationship.otherEntityRelationshipNameCapitalized %>(<%= asEntity(entityInstance) %>); <%_ } _%> <%_ } _%> + <%_ } _%> <%= entityInstance %>Repository.saveAndFlush(<%= asEntity(entityInstance) %>); <% if (authenticationType === 'oauth2' && relationship.relationshipFieldName === 'user') { _%>String<%_ } else { _%>Long<% } %> <%= relationship.relationshipFieldName %>Id = <%= relationship.relationshipFieldName %>.getId(); diff --git a/generators/server/templates/src/test/java/package/web/rest/TestUtil.java.ejs b/generators/server/templates/src/test/java/package/web/rest/TestUtil.java.ejs index 25bc7f98faac..92af04a611e8 100644 --- a/generators/server/templates/src/test/java/package/web/rest/TestUtil.java.ejs +++ b/generators/server/templates/src/test/java/package/web/rest/TestUtil.java.ejs @@ -35,6 +35,15 @@ import org.springframework.security.test.context.TestSecurityContextHolder; import java.io.IOException; import java.time.ZonedDateTime; import java.time.format.DateTimeParseException; +<%_ if (databaseType === 'sql') { _%> +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +<%_ } _%> import static org.assertj.core.api.Assertions.assertThat; @@ -164,5 +173,23 @@ public final class TestUtil { return dfcs; } +<%_ if (databaseType === 'sql') { _%> + /** + * Makes a an executes a query to the EntityManager finding all stored objects. + * @param The type of objects to be searched + * @param em The instance of the EntityManager + * @param clss The class type to be searched + * @return A list of all found objects + */ + public static List findAll(EntityManager em, Class clss) { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(clss); + Root rootEntry = cq.from(clss); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + return allQuery.getResultList(); + } +<%_ } _%> + private TestUtil() {} } diff --git a/test-integration/samples/jdl-default/app.jdl b/test-integration/samples/jdl-default/app.jdl index 7732c8c03df2..5a437a71cfde 100644 --- a/test-integration/samples/jdl-default/app.jdl +++ b/test-integration/samples/jdl-default/app.jdl @@ -105,6 +105,18 @@ entity Task { description String } +entity GoldenBadge { + name String +} + +entity SilverBadge { + name String +} + +entity Identifier { + name String required unique +} + entity Country { name String } @@ -114,7 +126,8 @@ entity Region { } relationship OneToOne { - Department{location} to Location + Department{location} to Location, + Employee{user(login)} to User with jpaDerivedIdentifier } relationship OneToMany { @@ -138,25 +151,28 @@ relationship OneToMany { Employee{department}, Employee{job} to Job{emp(lastName)}, Location{country} to Country, - Country{area(region)} to Region + Country{area(name)} to Region } relationship ManyToOne { - Employee{user(login)} to User{employee}, - Employee{manager(lastName)} to Employee + Employee{manager(lastName)} to Employee, + Employee{sibag(name) required} to SilverBadge, + Employee{gobag(name) required} to GoldenBadge, + SilverBadge{iden(name) required} to Identifier, + GoldenBadge{iden(name) required} to Identifier } relationship ManyToMany { JobHistory to Department, JobHistory to Job{history}, - JobHistory{emp(employee)} to Employee{history}, - Job{chore(title)} to Task{linkedJob(JobTitle)} + JobHistory{emp(firstName)} to Employee{history}, + Job{chore(title)} to Task{linkedJob(title)} } -dto BankAccount, Employee, Department, Location, Country, Region with mapstruct +dto BankAccount, Employee, Department, Location, Country, Region, SilverBadge, GoldenBadge, Identifier with mapstruct angularSuffix BankAccount with mySuffix -filter BankAccount +filter BankAccount, Employee clientRootFolder BankAccount, TheLabel, Operation with test-root paginate TheLabel, Job with pagination