Skip to content

Commit

Permalink
cleanup Java integration tests. (jhipster#25152)
Browse files Browse the repository at this point in the history
* rework entity validations
  • Loading branch information
mshima authored Feb 9, 2024
1 parent 86bacac commit 1d0fadb
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,47 @@ import static <%- packageName %>.domain.AssertUtils.zonedDataTimeSameInstant;
public class <%= persistClass %>Asserts {
/**
* Asserts that the entity has all properties (fields/relationships) set.
*
* @param expected the expected entity
* @param actual the actual entity
*/
public static void assert<%- persistClass %>AllPropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
assert<%- persistClass %>AutoGeneratedPropertiesEquals(expected, actual);
assert<%- persistClass %>AllUpdatablePropertiesEquals(expected, actual);
}
/**
* Asserts that the entity has all updatable properties (fields/relationships) set.
*
* @param expected the expected entity
* @param actual the actual entity
*/
public static void assert<%- persistClass %>AllUpdatablePropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
assert<%- persistClass %>UpdatableFieldsEquals(expected, actual);
assert<%- persistClass %>UpdatableRelationshipsEquals(expected, actual);
}
<%_ if (!embedded && primaryKey.derived) { _%>
/**
* Asserts that the derived primary key is set correctly.
*
* @param entityToPersist the entity used to persist
* @param persisted the persisted entity
*/
public static void assert<%- persistClass %>MapsIdRelationshipPersistedValue(<%- persistClass %> entityToPersist, <%- persistClass %> persisted) {
// Validate the id for MapsId, the ids must be same
assertThat(entityToPersist.get<%= primaryKey.relationships[0].propertyJavaBeanName %>().get<%= primaryKey.nameCapitalized %>()).isEqualTo(persisted.get<%= primaryKey.nameCapitalized %>());
}
<%_ } _%>
/**
* Asserts that the entity has all the auto generated properties (fields/relationships) set.
*
* @param expected the expected entity
* @param actual the actual entity
*/
public static void assert<%- persistClass %>AutoGeneratedPropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (fields.some(field => !field.transient && field.autoGenerate)) { _%>
assertThat(expected)
Expand All @@ -54,6 +85,12 @@ public class <%= persistClass %>Asserts {
<%_ } _%>
}
/**
* Asserts that the entity has all the updatable fields set.
*
* @param expected the expected entity
* @param actual the actual entity
*/
public static void assert<%- persistClass %>UpdatableFieldsEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (fields.some(field => !field.transient && !field.autoGenerate)) { _%>
assertThat(expected)
Expand All @@ -74,11 +111,17 @@ public class <%= persistClass %>Asserts {
<%_ } _%>
}
/**
* Asserts that the entity has all the updatable relationships set.
*
* @param expected the expected entity
* @param actual the actual entity
*/
public static void assert<%- persistClass %>UpdatableRelationshipsEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (relationships.some(relationship => !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
<%_ if (relationships.some(relationship => relationship.persistableRelationship && !relationship.id && !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
assertThat(expected)
.as("Verify <%- persistClass %> relationships")
<%_ for (const relationship of relationships.filter(relationship => !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
<%_ for (const relationship of relationships.filter(relationship => relationship.persistableRelationship && !relationship.id && !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
.satisfies(e -> assertThat(e.get<%- relationship.propertyJavaBeanName %>()).as("check <%- relationship.propertyName %>").isEqualTo(actual.get<%- relationship.propertyJavaBeanName %>()))
<%_ } _%>
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.domain;

import java.math.BigDecimal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,3 @@ rest<%= entityClass %>MockMvc.perform(patch(ENTITY_API_URL_ID, partialUpdated<%=
<%_ if (databaseTypeCouchbase) { _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertSameRepositoryCount(databaseSizeBeforeUpdate);
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);
<%_ for (field of fields) { _%>
<%_ if (field.fieldTypeZonedDateTime) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
<%_ } else if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>ContentType()).isEqualTo(<%= field.testWithConstant %>_CONTENT_TYPE);
<%_ } else if (field.fieldTypeBigDecimal) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualByComparingTo(<%= field.testWithConstant %>);
<%_ } else { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
<%_ } _%>
<%_ } _%>
Loading

0 comments on commit 1d0fadb

Please sign in to comment.