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

Adds more entity types to CI (#199) #213

Merged
merged 8 commits into from
Jan 22, 2021
2 changes: 2 additions & 0 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
- gradle-oauth-angular
- mvn-ms-consul-jwt
- gradle-ms-eureka-oauth
- jdl-default-app
include:
- app-type: gradle-jwt-react
- app-type: mvn-jwt-angular
Expand All @@ -55,6 +56,7 @@ jobs:
- app-type: gradle-oauth-angular
- app-type: mvn-ms-consul-jwt
- app-type: gradle-ms-eureka-oauth
- app-type: jdl-default-app
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ _%>
import com.fasterxml.jackson.annotation.JsonIgnore;
<%_ } if (importJsonIgnoreProperties === true) { _%>
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
<%_ } if (!hasDto && typeof javadoc != 'undefined') { _%>
import io.swagger.annotations.ApiModel;
<%_ } if (!hasDto && importApiModelProperty === true) { _%>
import io.swagger.annotations.ApiModelProperty;
<%_ } if (!hasDto && (typeof javadoc != 'undefined' || importApiModelProperty)) { _%>
import io.swagger.v3.oas.annotations.media.Schema;
<%_ } if (enableHibernateCache) { _%>
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
Expand Down Expand Up @@ -128,7 +126,7 @@ import static <%= packageName %>.config.Constants.ID_DELIMITER;
<%_ } else { _%>
<%- formatAsClassJavadoc(javadoc) %>
<%_ if (!hasDto) { _%>
@ApiModel(description = "<%- formatAsApiDescription(javadoc) %>")
@Schema(description = "<%- formatAsApiDescription(javadoc) %>")
<%_ } _%>
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
Expand Down Expand Up @@ -199,7 +197,7 @@ public class <%= asEntity(entityClass) %> implements Serializable {
<%- include('../common/field_validators') -%>
<%_ } _%>
<%_ if (!hasDto && typeof fields[idx].javadoc != 'undefined') { _%>
@ApiModelProperty(value = "<%- formatAsApiDescription(fields[idx].javadoc) %>"<% if (required) { %>, required = true<% } %>)
@Schema(description = "<%- formatAsApiDescription(fields[idx].javadoc) %>"<% if (required) { %>, required = true<% } %>)
<%_ } _%>
<%_ if (databaseType === 'sql') {
if (fields[idx].fieldIsEnum) { _%>
Expand Down Expand Up @@ -265,7 +263,7 @@ public class <%= asEntity(entityClass) %> implements Serializable {
if (typeof relationships[idx].javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(relationships[idx].javadoc) %>
<%_ if (!hasDto) { _%>
@ApiModelProperty(value = "<%- formatAsApiDescription(relationships[idx].javadoc) %>")
@Schema(description = "<%- formatAsApiDescription(relationships[idx].javadoc) %>")
<%_ } _%>
<%_ }
if (relationshipType === 'one-to-many') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import io.micronaut.core.annotation.Introspected;
<%_
let { importApiModelProperty, importJsonIgnore, importJsonIgnoreProperties, importSet, uniqueEnums } = _processJavaEntityImports(fields, relationships);
_%>
<%_ if (typeof javadoc != 'undefined') { _%>
import io.swagger.annotations.ApiModel;
<%_ } if (importApiModelProperty === true) { _%>
import io.swagger.annotations.ApiModelProperty;
<%_ if (typeof javadoc != 'undefined' || importApiModelProperty) { _%>
import io.swagger.v3.oas.annotations.media.Schema;
<%_ } _%>
<%_ if (fieldsContainInstant) { _%>
import java.time.Instant;
Expand Down Expand Up @@ -68,7 +66,7 @@ import <%= packageName %>.domain.enumeration.<%= element %>;
* A DTO for the {@link <%= packageName %>.domain.<%= asEntity(entityClass) %>} entity.
*/
<%_ if (typeof javadoc !== 'undefined') { _%>
@ApiModel(description = "<%- formatAsApiDescription(javadoc) %>")
@Schema(description = "<%- formatAsApiDescription(javadoc) %>")
<%_ } _%>
@Introspected
public class <%= asDto(entityClass) %> implements Serializable {
Expand Down Expand Up @@ -99,7 +97,7 @@ public class <%= asDto(entityClass) %> implements Serializable {
<%- include('../../common/field_validators') -%>
<%_ } _%>
<%_ if (typeof fields[idx].javadoc != 'undefined') { _%>
@ApiModelProperty(value = "<%- formatAsApiDescription(fields[idx].javadoc) %>"<% if (required) { %>, required = true<% } %>)
@Schema(description = "<%- formatAsApiDescription(fields[idx].javadoc) %>"<% if (required) { %>, required = true<% } %>)
<%_ } _%>
<%_ if (fieldType === 'byte[]' && databaseType === 'sql') { _%>
@Lob
Expand All @@ -125,7 +123,7 @@ public class <%= asDto(entityClass) %> implements Serializable {
const ownerSide = relationships[idx].ownerSide;
if (typeof relationships[idx].javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(relationships[idx].javadoc) %>
@ApiModelProperty(value = "<%- formatAsApiDescription(relationships[idx].javadoc) %>")
@Schema(description = "<%- formatAsApiDescription(relationships[idx].javadoc) %>")
<%_ }
if (relationshipType === 'many-to-many' && ownerSide === true) { _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') {
let enumValue1;
let enumValue2;
if (isEnum) {
const values = fields[idx].fieldValues.replace(/\s/g, '').split(',');
enumValue1 = values[0];
if (values.length > 1) {
enumValue2 = values[1];
const enumValues = getEnumValuesWithCustomValues(fields[idx].fieldValues);
enumValue1 = enumValues[0];
if (enumValues.length > 1) {
enumValue2 = enumValues[1];
} else {
enumValue2 = enumValue1;
}
Expand Down Expand Up @@ -308,8 +308,8 @@ public class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') {
private static final String <%= updatedValueName %>_CONTENT_TYPE = "image/png";
<%_ } else if (isEnum) { _%>

private static final <%= fieldType %> <%= defaultValueName %> = <%= fieldType %>.<%= enumValue1 %>;
private static final <%= fieldType %> <%= updatedValueName %> = <%= fieldType %>.<%= enumValue2 %>;
private static final <%= fieldType %> <%= defaultValueName %> = <%= fieldType %>.<%= enumValue1.name %>;
private static final <%= fieldType %> <%= updatedValueName %> = <%= fieldType %>.<%= enumValue2.name %>;
<%_ } } _%>

<%_ if (usesDto) { _%>
Expand Down Expand Up @@ -394,7 +394,7 @@ public class <%= entityClass %>ResourceIT <% if (databaseType === 'cassandra') {
// 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(transactionManager);
<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.createEntity(transactionManager, em);
<%_ if (databaseType === 'sql' && !reactive) { _%>
transactionManager.executeWrite(status -> {
em.persist(<%= otherEntityName %>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
<%_ if (databaseType === 'sql') { _%>
import io.micronaut.transaction.TransactionOperations;
<%_ } _%>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

<%_ if (databaseType === 'sql') { _%>
import javax.persistence.EntityManager;
Expand All @@ -29,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public final class TestUtil {

private static final ObjectMapper mapper = createObjectMapper();
private static final Logger log = LoggerFactory.getLogger(TestUtil.class);

private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -109,13 +112,17 @@ public final class TestUtil {
* @param <T>
*/
public static <T> void removeAll(TransactionOperations<Connection> transactionManager, EntityManager em, Class<T> clss) {
transactionManager.executeWrite(status -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<T> query = cb.createCriteriaDelete(clss);
Root<T> root = query.from(clss);
query.where(root.isNotNull());
return em.createQuery(query).executeUpdate();
});
try {
transactionManager.executeWrite(status -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<T> query = cb.createCriteriaDelete(clss);
Root<T> root = query.from(clss);
query.where(root.isNotNull());
return em.createQuery(query).executeUpdate();
});
} catch (Exception e) {
log.warn("Failed to remove instances", e);
}
}

<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
<%_ if (databaseType === 'sql') { _%>
import io.micronaut.transaction.SynchronousTransactionManager;
import io.micronaut.transaction.TransactionOperations;
<%_ } _%>
import io.micronaut.test.annotation.MockBean;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.*;
Expand All @@ -38,6 +42,12 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;

import javax.inject.Inject;
<%_ if (databaseType === 'sql' && !reactive) { _%>
import javax.persistence.EntityManager;
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
import java.sql.Connection;
<%_ } _%>
import java.time.Instant;
import java.util.*;

Expand Down Expand Up @@ -86,6 +96,14 @@ public class UserResourceIT {

@Inject
AuthorityRepository authorityRepository;
<%_ if (databaseType === 'sql') { _%>

@Inject
private EntityManager em;

@Inject
SynchronousTransactionManager<Connection> transactionManager;
<%_ } _%>

@Inject
private UserMapper userMapper;
Expand All @@ -100,7 +118,7 @@ public class UserResourceIT {

private User user;

public static User createEntity() {
public static User createEntity(<% if (databaseType === 'sql') { %>TransactionOperations<Connection> transactionManager, EntityManager em<% } %>) {
User user = new User();
user.setLogin(DEFAULT_LOGIN + RandomStringUtils.randomAlphabetic(5));
<%_ if (authenticationType !== 'oauth2') { _%>
Expand All @@ -116,10 +134,22 @@ public class UserResourceIT {
user.setLangKey(DEFAULT_LANGKEY);
return user;
}
<%_ if (databaseType === 'sql' && !reactive) { _%>

/**
* Delete all User entities.
*
* 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 void deleteAll(TransactionOperations<Connection> transactionManager, EntityManager em) {
TestUtil.removeAll(transactionManager, em, User.class);
}
<%_ } _%>

@BeforeAll
public void initTest() {
user = createEntity();
user = createEntity(transactionManager, em);
user.setLogin(DEFAULT_LOGIN);
user.setEmail(DEFAULT_EMAIL);
userRepository.saveAndFlush(user);
Expand Down Expand Up @@ -150,7 +180,7 @@ public class UserResourceIT {


private void resetDefaultUser() {
User reInsert = createEntity();
User reInsert = createEntity(transactionManager, em);
reInsert.setLogin(DEFAULT_LOGIN);
reInsert.setEmail(DEFAULT_EMAIL);
userRepository.deleteById(user.getId());
Expand Down
Loading