Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generated java tests now passes with unique validation #9669

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh still missing 1 space: %>updated <%

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fixed directly in your branch

*
* 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);
<%_ } _%>
<%_ } _%>
<%_ } _%>
Expand All @@ -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 %>);
Expand All @@ -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 %>);
Expand All @@ -460,6 +477,7 @@ _%>
<%_ } } _%>
return <%= asEntity(entityInstance) %>;
}
<%_ }); _%>

@BeforeEach
public void initTest() {
Expand Down Expand Up @@ -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();
<%_ } _%>
Expand Down Expand Up @@ -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 %>);
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <T> 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 <T> List<T> findAll(EntityManager em, Class<T> clss) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(clss);
Root<T> rootEntry = cq.from(clss);
CriteriaQuery<T> all = cq.select(rootEntry);
TypedQuery<T> allQuery = em.createQuery(all);
return allQuery.getResultList();
}
<%_ } _%>

private TestUtil() {}
}
32 changes: 24 additions & 8 deletions test-integration/samples/jdl-default/app.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down