<%#
 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.
-%>
<%_ const collectionTypeName = ormTool === 'hibernate' ? 'HashSet' : 'ArrayList'; _%>
package <%= entityAbsolutePackage %>.domain;

import static <%= entityAbsolutePackage %>.domain.<%= persistClass %>TestSamples.*;
<%_ for (const otherEntity of otherEntities.filter(otherEntity => !otherEntity.builtIn)) { _%>
import static <%= otherEntity.entityAbsolutePackage %>.domain.<%= otherEntity.persistClass %>TestSamples.*;
<%_ } _%>
<%_ for (const otherEntity of otherEntities.filter(otherEntity => entityPackage !== otherEntity.entityPackage)) { _%>
import <%= otherEntity.entityAbsolutePackage %>.domain.<%= otherEntity.persistClass %>;
<%_ } _%>
import java.util.*;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import <%= packageName %>.web.rest.TestUtil;

class <%= persistClass %>Test {

    @Test
    void equalsVerifier() throws Exception {
        TestUtil.equalsVerifier(<%= persistClass %>.class);
<%_if (!embedded) { _%>
        <%= persistClass %> <%= persistInstance %>1 = get<%= persistClass %>Sample1();
        <%= persistClass %> <%= persistInstance %>2 = new <%= persistClass %>();
        assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);

        <%= persistInstance %>2.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
        assertThat(<%= persistInstance %>1).isEqualTo(<%= persistInstance %>2);

        <%= persistInstance %>2 = get<%= persistClass %>Sample2();
        assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
<%_ } _%>
    }
<%_if (!updatableEntity) { _%>

    @Test
    void hashCodeVerifier() {
        <%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
        assertThat(<%= persistInstance %>.hashCode()).isZero();

        <%= persistClass %> <%= persistInstance %>1 = get<%= persistClass %>Sample1();
        <%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
        assertThat(<%= persistInstance %>).hasSameHashCodeAs(<%= persistInstance %>1);
    }
<%_ } _%>
<%# modify by begcode _%>
<%_ for (const relationship of relationships
        .filter(rel => !rel.otherEntity.builtIn  && (!embedded || (rel.otherEntity.embedded && rel.ownerSide)))
        .filter(rel => !(rel.relationshipRightSide && rel?.bcConfig?.leftUnidirectional && (rel?.bcConfig?.leftCountByPrimaryKey || rel?.bcConfig?.leftRelateByIdEntity || rel?.relationshipManyToMany || rel?.relationshipOneToOne)))
        .filter(rel => !(!rel.ownerSide && rel.relationshipTypeOneToMany && (rel?.bcConfig?.leftUnidirectional && !(rel?.bcConfig?.leftCountByPrimaryKey || rel?.bcConfig?.leftRelateByIdEntity)))))
    { _%>

    @Test
    void <%- relationship.relationshipName %>Test() {
        <%= persistClass %> <%= persistInstance %> = get<%= persistClass %>RandomSampleGenerator();
        <%= relationship.otherEntity.persistClass %> <%= relationship.otherEntity.persistInstance %>Back = get<%= relationship.otherEntity.persistClass %>RandomSampleGenerator();
  <%_ if (relationship.collection) { _%>

       // todo <%= persistInstance %>.add<%- relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
       // assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).containsOnly(<%= relationship.otherEntity.persistInstance %>Back);
  <%_ } else { _%>

        <%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
        assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).isEqualTo(<%= relationship.otherEntity.persistInstance %>Back);
  <%_ } _%>
  <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && !relationship.otherEntity.embedded) { _%>
    <%_ if (relationship.otherRelationship.collection) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).containsOnly(<%= persistInstance %>);
    <%_ } else { _%>
      <%_ if (relationship.otherRelationship.relationshipRightSide && relationship.otherRelationship.bcConfig.leftRelateByIdEntity) { _%>
        <%_ if (ormTool === 'hibernate') { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.getOwner()).isEqualTo(<%= persistInstance %>);
        <%_ } _%>
      <%_ } else if (relationship.otherRelationship.propertyNameCapitalized) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isEqualTo(<%= persistInstance %>);
      <%_ } _%>
    <%_ } _%>
  <%_ } _%>
  <%_ if (relationship.collection) { _%>

        // <%= persistInstance %>.remove<%- relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistInstance %>Back);
        // assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).doesNotContain(<%= relationship.otherEntity.persistInstance %>Back);
  <%_ } else { _%>

    <%_ if (fluentMethods) { _%>
        <%= persistInstance %>.<%- relationship.propertyName %>(null);
    <%_ } else { _%>
        <%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(null);
    <%_ } _%>
        assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).isNull();
  <%_ } _%>
  <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && !relationship.otherEntity.embedded) { _%>
    <%_ if (relationship.otherRelationship.collection) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).doesNotContain(<%= persistInstance %>);
    <%_ } else { _%>
      <%_ if (relationship.otherRelationship.relationshipRightSide && relationship.otherRelationship.bcConfig.leftRelateByIdEntity) { _%>
        <%_ if (ormTool === 'hibernate') { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.getOwner()).isNull();
        <%_ } _%>
      <%_ } else if (relationship.otherRelationship.propertyNameCapitalized) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isNull();
      <%_ } _%>
    <%_ } _%>
  <%_ } _%>
  <%_ if (relationship.collection && !(relationship.bcConfig?.leftUnidirectional && (relationship?.bcConfig?.leftCountByPrimaryKey || relationship?.bcConfig?.leftRelateByIdEntity))) { _%>

    <%_ if (fluentMethods) { _%>
        <%= persistInstance %>.<%- relationship.propertyName %>(new <%= collectionTypeName %><>(<%= javaVersion === '8' ? 'Arrays.asList' : 'Set.of'%>(<%= relationship.otherEntity.persistInstance %>Back)));
    <%_ } else { _%>
        <%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(new <%= collectionTypeName %><>(Set.of(<%= relationship.otherEntity.persistInstance %>Back)));
    <%_ } _%>
        assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).containsOnly(<%= relationship.otherEntity.persistInstance %>Back);
    <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && !relationship.otherEntity.embedded) { _%>
      <%_ if (relationship.otherRelationship.collection) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).containsOnly(<%= persistInstance %>);
      <%_ } else { _%>
          <%_ if (relationship.otherRelationship.relationshipRightSide && relationship.otherRelationship.bcConfig.leftRelateByIdEntity) { _%>
            <%_ if (ormTool === 'hibernate') { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.getOwner()).isEqualTo(<%= persistInstance %>);
            <%_ } _%>
          <%_ } else { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isEqualTo(<%= persistInstance %>);
          <%_ } _%>
      <%_ } _%>
    <%_ } _%>

        <%= persistInstance %>.set<%- relationship.propertyNameCapitalized %>(new <%= collectionTypeName %><>());
        assertThat(<%= persistInstance %>.get<%- relationship.propertyNameCapitalized %>()).doesNotContain(<%= relationship.otherEntity.persistInstance %>Back);
    <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && !relationship.otherEntity.embedded) { _%>
      <%_ if (relationship.otherRelationship.collection) { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).doesNotContain(<%= persistInstance %>);
      <%_ } else { _%>
      <%_ if (relationship.otherRelationship.relationshipRightSide && relationship.otherRelationship.bcConfig.leftRelateByIdEntity) { _%>
        <%_ if (ormTool === 'hibernate') { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.getOwner()).isNull();
        <%_ } _%>
      <%_ } else { _%>
        assertThat(<%= relationship.otherEntity.persistInstance %>Back.get<%- relationship.otherRelationship.propertyNameCapitalized %>()).isNull();
      <%_ } _%>
      <%_ } _%>
    <%_ } _%>
  <%_ } _%>
    }
<%_ } _%>
}
