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

Add entity mapper test and domain assertion utilities to be used in tests. #25151

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions generators/entities/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,27 @@ exports[`generator - entities regenerating all entities should match snapshot 1`
"src/main/webapp/i18n/en/user-management.json": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/SkipAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/SkipTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -631,12 +640,18 @@ exports[`generator - entities regenerating some entities should match snapshot 1
"src/main/webapp/i18n/en/user-management.json": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
Expand Down
12 changes: 12 additions & 0 deletions generators/java/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,30 @@ exports[`generator - java with default config should match files snapshot 1`] =
"src/main/java/com/mycompany/myapp/package-info.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
Expand Down
6 changes: 5 additions & 1 deletion generators/java/entity-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export const entityServerFiles: WriteFileSection = {
modelTestFiles: [
javaTestPackageTemplatesBlock({
condition: ctx => ctx.entityDomainLayer,
templates: ['_entityPackage_/domain/_persistClass_Test.java', '_entityPackage_/domain/_persistClass_TestSamples.java'],
templates: [
'_entityPackage_/domain/_persistClass_Asserts.java',
'_entityPackage_/domain/_persistClass_Test.java',
'_entityPackage_/domain/_persistClass_TestSamples.java',
],
}),
],
server: [
Expand Down
5 changes: 4 additions & 1 deletion generators/java/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Generator from './generator.js';
import { WriteFileSection } from '../base/api.js';
import { JavaApplication } from './types.js';
import { SERVER_MAIN_SRC_DIR } from '../generator-constants.js';
import { moveToJavaPackageSrcDir } from './support/index.js';
import { javaTestPackageTemplatesBlock, moveToJavaPackageSrcDir } from './support/index.js';

const files: WriteFileSection<Generator, JavaApplication> = {
baseFiles: [
Expand All @@ -29,6 +29,9 @@ const files: WriteFileSection<Generator, JavaApplication> = {
renameTo: moveToJavaPackageSrcDir,
templates: ['GeneratedByJHipster.java'],
},
javaTestPackageTemplatesBlock({
templates: ['domain/AssertUtils.java'],
}),
],
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<%#
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 <%= entityAbsolutePackage %>.domain;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.assertj.core.api.Assertions.assertThat;

<%_ if (anyFieldIsBigDecimal) { _%>
import static <%- packageName %>.domain.AssertUtils.bigDecimalCompareTo;
<%_ } _%>
<%_ if (anyFieldIsZonedDateTime) { _%>
import static <%- packageName %>.domain.AssertUtils.zonedDataTimeSameInstant;
<%_ } _%>

public class <%= persistClass %>Asserts {

public static void assert<%- persistClass %>AllPropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
assert<%- persistClass %>AutoGeneratedPropertiesEquals(expected, actual);
assert<%- persistClass %>AllUpdatablePropertiesEquals(expected, actual);
}

public static void assert<%- persistClass %>AllUpdatablePropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
assert<%- persistClass %>UpdatableFieldsEquals(expected, actual);
assert<%- persistClass %>UpdatableRelationshipsEquals(expected, actual);
}

public static void assert<%- persistClass %>AutoGeneratedPropertiesEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (fields.some(field => !field.transient && field.autoGenerate)) { _%>
assertThat(expected)
.as("Verify <%- persistClass %> auto generated properties")
<%_ for (const field of fields.filter(field => !field.transient && field.autoGenerate)) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>()))
<%_ } _%>
<%_ for (const relationship of relationships.filter(relationship => relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
.satisfies(e -> assertThat(e.get<%- relationship.propertyJavaBeanName %>()).as("check <%- relationship.propertyName %>").isEqualTo(actual.get<%- relationship.propertyJavaBeanName %>()))
<%_ } _%>
;
<%_ } _%>
}

public static void assert<%- persistClass %>UpdatableFieldsEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (fields.some(field => !field.transient && !field.autoGenerate)) { _%>
assertThat(expected)
.as("Verify <%- persistClass %> relevant properties")
<%_ for (const field of fields.filter(field => !field.transient && !field.autoGenerate)) { _%>
<%_ if (field.fieldTypeZonedDateTime) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").usingComparator(zonedDataTimeSameInstant).isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>()))
<%_ } else if (field.fieldTypeBigDecimal) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").usingComparator(bigDecimalCompareTo).isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>()))
<%_ } else { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>()))
<%_ if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>ContentType()).as("check <%- field.propertyName %> contenty type").isEqualTo(actual.get<%- field.fieldInJavaBeanMethod %>ContentType()))
<%_ } -%>
<%_ } -%>
<%_ } -%>
;
<%_ } _%>
}

public static void assert<%- persistClass %>UpdatableRelationshipsEquals(<%- persistClass %> expected, <%- persistClass %> actual) {
<%_ if (relationships.some(relationship => !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
assertThat(expected)
.as("Verify <%- persistClass %> relationships")
<%_ for (const relationship of relationships.filter(relationship => !relationship.autoGenerate && !relationship.otherEntity.builtInUser)) { _%>
.satisfies(e -> assertThat(e.get<%- relationship.propertyJavaBeanName %>()).as("check <%- relationship.propertyName %>").isEqualTo(actual.get<%- relationship.propertyJavaBeanName %>()))
<%_ } _%>
;
<%_ } _%>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package <%= packageName %>.domain;
Copy link
Member

@DanielFran DanielFran Feb 9, 2024

Choose a reason for hiding this comment

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

@mshima Missing copyright header


import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.Objects;

public class AssertUtils {

public static Comparator<ZonedDateTime> zonedDataTimeSameInstant = (ZonedDateTime e, ZonedDateTime a) -> Objects.compare(e, a, (e1, a2) -> e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC)));

public static Comparator<BigDecimal> bigDecimalCompareTo = (BigDecimal e, BigDecimal a) -> Objects.compare(e, a, (e1, a2) -> e1.compareTo(a2));

}
21 changes: 21 additions & 0 deletions generators/server/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ exports[`generator - server composing databaseType option no with jwt should mat
"src/test/java/com/mycompany/myapp/config/WebConfigurerTestController.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/management/SecurityMetersServiceTests.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -442,6 +445,9 @@ exports[`generator - server composing databaseType option no with oauth2 should
"src/test/java/com/mycompany/myapp/config/WebConfigurerTestController.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/security/SecurityUtilsUnitTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -657,6 +663,9 @@ exports[`generator - server composing databaseType option no with session should
"src/test/java/com/mycompany/myapp/config/WebConfigurerTestController.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/security/SecurityUtilsUnitTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1121,18 +1130,30 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/test/java/com/mycompany/myapp/config/timezone/HibernateTimeZoneIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/FooTest.java": {
"stateCleared": "modified",
},
Expand Down
6 changes: 6 additions & 0 deletions generators/server/support/__snapshots__/needles.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ exports[`generator - server - support - needles generated project should match s
"src/test/java/com/mycompany/myapp/config/timezone/HibernateTimeZoneIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@
-%>
package <%= entityAbsolutePackage %>.service.mapper;

import static <%= entityAbsoluteClass %>Asserts.*;
import static <%= entityAbsoluteClass %>TestSamples.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
<%_ if (primaryKey && primaryKey.hasUUID) { _%>
import java.util.UUID;
<%_ } _%>

class <%= entityClass %>MapperTest {

private <%= entityClass %>Mapper <%= entityInstance %>Mapper;

@BeforeEach
public void setUp() {
void setUp() {
<%= entityInstance %>Mapper = new <%= entityClass %>MapperImpl();
}

@Test
void shouldConvertToDtoAndBack() {
var expected = get<%= persistClass %>Sample1();
var actual = <%= entityInstance %>Mapper.toEntity(<%= entityInstance %>Mapper.toDto(expected));
assert<%= persistClass %>AllPropertiesEquals(expected, actual);
}
}
9 changes: 9 additions & 0 deletions generators/spring-boot/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ exports[`generator - spring-boot with jwt should match generated files snapshot
"src/test/java/com/mycompany/myapp/config/WebConfigurerTestController.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityAsserts.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -586,6 +592,9 @@ exports[`generator - spring-boot with oauth2 should match generated files snapsh
"src/test/java/com/mycompany/myapp/config/WebConfigurerTestController.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AssertUtils.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/security/SecurityUtilsUnitTest.java": {
"stateCleared": "modified",
},
Expand Down
Loading
Loading