Generator-jhipster: EntityResourceIntTest Failures with Required Relationships

Created on 24 Jun 2016  Â·  19Comments  Â·  Source: jhipster/generator-jhipster

Overview of the issue

There are out-of-the-box test failures when generating entities with required relationships.

Motivation for or Use Case

Tests should not fail on a fresh generated project.

JHipster Version(s)

Master, 3.2.0+ (required relationships added)

JHipster configuration, a .yo-rc.json file generated in the root folder
{
  "generator-jhipster": {
    "jhipsterVersion": "3.4.2",
    "baseName": "test",
    "packageName": "com.mycompany.myapp",
    "packageFolder": "com/mycompany/myapp",
    "serverPort": "8080",
    "authenticationType": "session",
    "hibernateCache": "ehcache",
    "clusteredHttpSession": "no",
    "websocket": "no",
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "mysql",
    "searchEngine": "no",
    "buildTool": "maven",
    "enableSocialSignIn": false,
    "rememberMeKey": "8c073c30fd56d34230694bc40fb1ec253d47c7d7",
    "useSass": false,
    "applicationType": "monolith",
    "testFrameworks": [
      "gatling"
    ],
    "jhiPrefix": "jhi",
    "enableTranslation": true,
    "nativeLanguage": "en",
    "languages": [
      "en"
    ]
  }
}
Entity configuration(s) entityName.json files generated in the .jhipster directory

Foo

{
    "relationships": [
        {
            "relationshipName": "foo",
            "otherEntityName": "foo",
            "relationshipType": "one-to-many",
            "otherEntityRelationshipName": "bar"
        }
    ],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String"
        }
    ],
    "changelogDate": "20160624065546",
    "dto": "mapstruct",
    "service": "serviceClass",
    "entityTableName": "bar",
    "pagination": "pagination"
}

Bar

{
    "relationships": [
        {
            "relationshipName": "bar",
            "otherEntityName": "bar",
            "relationshipType": "many-to-one",
            "relationshipValidateRules": [
                "required"
            ],
            "otherEntityField": "name"
        }
    ],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String"
        }
    ],
    "changelogDate": "20160624065503",
    "dto": "mapstruct",
    "service": "serviceClass",
    "entityTableName": "foo",
    "pagination": "pagination"
}
Reproduce the error

Generate the above entities (or any with a required relationship) and run mvn test

Related issues

1843 - talks about string length and regex validation. There is a note about validation failing is documented in the Validation section https://jhipster.github.io/creating-an-entity/:

JHipster generates unit tests that work for generic entities, without knowing your validation rules: it is possible that the generated tests do not pass the validation rules. In that case, you will need to update the sample values used in your unit tests, so that they pass the validation rules.

Suggest a Fix

Since a Bar entity is required to create/update a Foo entity, I was able to get FooResouceIntTest.java working by creating a Bar in setup(), then adding it to the initTest() and updateFoo() tests. I also needed to inject BarMapper and BarService.

The other option would be to close this issue and point at the documentation, though fixing the tests requires more work than just updating the sample values.

area

Most helpful comment

Ya damn thata gonna be difficult. May be we can try to do for one level and add documentation to ask user to correct if there are nested validations

All 19 comments

It would be easy to fix for one level of dependency but what if entity A requires B, B requires C, C requires D, ... ?

Ya damn thata gonna be difficult. May be we can try to do for one level and add documentation to ask user to correct if there are nested validations

The solution could be to use liquibase to create an instance of each entity with a defined ID. What do you think ?

Ya we could try that

Thanks & regards,
Deepu
On 26 Jun 2016 22:53, "Christophe Bornet" [email protected] wrote:

The solution could be to use liquibase to create an instance of each
entity with a defined ID. What do you think ?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/3769#issuecomment-228605045,
or mute the thread
https://github.com/notifications/unsubscribe/ABDlFwMtjTYVqUMmDnAOM7j9qQhBIXeTks5qPpJigaJpZM4I-GC_
.

I think it would be nice to have a tool which can setup your database before each integration test. For example DB Unit is an option?

You need to have managed entities, so creating them with Liquibase won't help a lot.

Those are like "normal" properties, they should be provided when the object is created. They should be created a the beginning of the test, and they need to be flushed to the database: you need to inject the relationship's repository.

So that's quite a lot of changes, but that should be doable in pure Java.

The problem is you need to build the whole tree of entities. When a relationship is required, and that relationship has another relationship that is required, you need to inject a lot of repositories. DB unit or liquibase can help with this. Then you only have to perform one find on the main entity to load it into the entitymanager.

You are right @evelknievel - I'll have another look now

This is in fact quite hard: using Liquibase doesn't really solve the graph issue, and then the IDs are supposed to be generated by Hibernate, so this needs another trick.

I think I can solve this in Java, and it wouldn't be too bad, I'm trying this now.

I'm not totally satisfied with it, and it requires a lot of testing (I just did a simple relationship for the moment), but I've pushed my idea in this commit: https://github.com/jhipster/generator-jhipster/commit/14e9b339eaa34b1435fa725183f01b0a8851ebae

Won't liquibase use hibernate ?

I don't think it does, for generating the PK from a CSV file.
The issue, anyway, is that the PKs are auto-generated, so it's annoying, as
you need to know the PKs.

Le 8 août 2016 7:21 PM, "Christophe Bornet" [email protected] a
écrit :

Won't liquibase use hibernate ?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/3769#issuecomment-238306893,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AATVo3ws6hISbmUtV5yvd128wHFm2HmOks5qd2WIgaJpZM4I-GC_
.

Indeed...

Is it an issue for hibernate if rows from different table have the same known id ?

You dont have to let hibernate generate the id. You can insert it manually and then use it in other tables.

Yes you can insert it manually, but then you need to be careful that it won't be used by Hibernate afterwards... I usually take a very high number.

I've done more tests with my commit, and it works really well. I also like that it's quite simple, and doesn't change much on what currently works.

For the record, I tested a many-to-many and many-to-one required relationship, as well as a graph of two required relationships.

I'm going to clean it up, and merge it.

I think the hibernate sequence starts at 1000 so you can use ids lower than that. Eg. 100.

@cbornet it's starting with 1 -> we use the "new" hi/lo generator, and I'm guessing you're familiar with the old one (and with our config, the old one would have started at 100 if my memory is good)

Closing as I merged my commits

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RizziCR picture RizziCR  Â·  3Comments

frantzynicolas picture frantzynicolas  Â·  3Comments

tomj0101 picture tomj0101  Â·  3Comments

lsadehaan picture lsadehaan  Â·  3Comments

kaidohallik picture kaidohallik  Â·  3Comments