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

Generated java tests fails when a field has unique validation, entity has filter and is required #9666

Closed
1 task done
Gabrui opened this issue May 4, 2019 · 4 comments · Fixed by #9669
Closed
1 task done

Comments

@Gabrui
Copy link
Contributor

Gabrui commented May 4, 2019

Overview of the issue

Generated java tests fails when a field has unique validation and filter:

[ERROR] Errors: 
[ERROR]   EmployeeResourceIT.getAllEmployeesByLocationIsEqualToSomething:324 » Persistence
[INFO] 
[ERROR] Tests run: 141, Failures: 0, Errors: 1, Skipped: 0
[ERROR] Tests run: 20, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.312 s <<< FAILURE! - in com.mycompany.myapp.web.rest.EmployeeResourceIT
[ERROR] getAllEmployeesByLocationIsEqualToSomething  Time elapsed: 0.137 s  <<< ERROR!
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
	at com.mycompany.myapp.web.rest.EmployeeResourceIT.getAllEmployeesByLocationIsEqualToSomething(EmployeeResourceIT.java:324)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
	at com.mycompany.myapp.web.rest.EmployeeResourceIT.getAllEmployeesByLocationIsEqualToSomething(EmployeeResourceIT.java:324)
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: 
Unique index or primary key violation: "PUBLIC.UX_LOCATION_NAME_INDEX_9 ON PUBLIC.LOCATION(NAME) VALUES 4"; SQL statement:
insert into location (id, description, name) values (null, ?, ?) [23505-199]
	at com.mycompany.myapp.web.rest.EmployeeResourceIT.getAllEmployeesByLocationIsEqualToSomething(EmployeeResourceIT.java:324)
Motivation for or Use Case

The generated tests should be passing.

Reproduce the error

Create a new project with default settings jhipster, import JDL jhipster import-jdl bug.jdl, run ./mvnw clean install.

Related issues

It was also happening in #9563 , but I hadn't identified the cause and it was also happening with other bugs.

Suggest a Fix

The createEntity shouldn't create entities that have the same value for the field if it is unique, as it could be created twice by other entities that require it.

Another simple solution is not to create a new entity if it was already created. I will try to make a PR to fix it.

JHipster Version(s)
teste-6-simples@0.0.0 /home/gabriel/projetos/teste6simples
└── generator-jhipster@6.0.0 

JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "generator-jhipster": {
    "promptValues": {
      "packageName": "com.mycompany.myapp",
      "nativeLanguage": "pt-br"
    },
    "jhipsterVersion": "6.0.0",
    "applicationType": "monolith",
    "baseName": "teste6simples",
    "packageName": "com.mycompany.myapp",
    "packageFolder": "com/mycompany/myapp",
    "serverPort": "8080",
    "authenticationType": "jwt",
    "cacheProvider": "ehcache",
    "enableHibernateCache": true,
    "websocket": false,
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "mysql",
    "searchEngine": false,
    "messageBroker": false,
    "serviceDiscoveryType": false,
    "buildTool": "maven",
    "enableSwaggerCodegen": false,
    "clientFramework": "angularX",
    "clientTheme": "none",
    "clientThemeVariant": "",
    "useSass": true,
    "clientPackageManager": "npm",
    "testFrameworks": [],
    "jhiPrefix": "jhi",
    "entitySuffix": "",
    "dtoSuffix": "DTO",
    "otherModules": [],
    "enableTranslation": true,
    "nativeLanguage": "pt-br",
    "languages": ["pt-br"]
  }
}
JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
entity Employee {
  name String required,
  age Integer required
}
entity Location {
  name String required unique,
  description String
}
relationship ManyToOne {
  Employee{location(name) required} to Location
}
filter Employee
Environment and Tools
java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

git version 2.11.0

node: v10.15.3

npm: 6.4.1

yeoman: 2.0.6

yarn: 1.15.2

Docker version 18.09.5, build e8ff056dbc

docker-compose version 1.24.0, build 0aa59064

Browsers and Operating System

Debian 9

  • Checking this box is mandatory (this is just to show you read everything)
@Gabrui
Copy link
Contributor Author

Gabrui commented May 4, 2019

Using the jpaDerivedIdentifier has the same result as with the required and filter:

This JDL also fails

entity Employee {
  name String required,
  age Integer required
}
entity IdentityCard {
  number String required unique,
  description String
}
relationship OneToOne {
  Employee to IdentityCard with jpaDerivedIdentifier
}

@Gabrui
Copy link
Contributor Author

Gabrui commented May 5, 2019

There may be dependencies deep into the creation tree:

This JDL also fails, it has no filter nor jpaDerivedIdentifier.

entity Employee {
  name String,
}
entity Location {
  name String
}
entity Badge {
  name String
}
entity Identifier {
  name String required unique
}
relationship ManyToOne {
  Employee{loc(name) required} to Location,
  Employee{badg(name) required} to Badge,
  Location{iden(name) required} to Identifier,
  Badge{iden(name) required} to Identifier,
}

Gabrui added a commit to Gabrui/generator-jhipster that referenced this issue May 5, 2019
Checks if a required entity is already created in the createEntity method,
adds the creatUpdatedEntity using the UPDATED_ constants,
adds the findAll method to TestUtils,

Fix jhipster#9666
Gabrui added a commit to Gabrui/generator-jhipster that referenced this issue May 5, 2019
Checks if a required entity is already created in the createEntity method,
adds the creatUpdatedEntity using the UPDATED_ constants and
adds the findAll method to TestUtils.

Fix jhipster#9666
Gabrui added a commit to Gabrui/generator-jhipster that referenced this issue May 5, 2019
Checks if a required entity is already created in the createEntity method,
adds the creatUpdatedEntity using the UPDATED_ constants and
adds the findAll method to TestUtils.

Fix jhipster#9666
@Gabrui
Copy link
Contributor Author

Gabrui commented May 5, 2019

The additions I made to the app.jdl catches the behaviour: in the current version these are the errors of the app.jdl updated:

[ERROR] getEmployee  Time elapsed: 0.087 s  <<< ERROR!
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.createEntity(EmployeeResourceIT.java:143)
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.initTest(EmployeeResourceIT.java:152)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.createEntity(EmployeeResourceIT.java:143)
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.initTest(EmployeeResourceIT.java:152)
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: 
Unique index or primary key violation: "PUBLIC.UX_IDENTIFIER_NAME_INDEX_B ON PUBLIC.IDENTIFIER(NAME) VALUES 23"; SQL statement:
insert into identifier (id, name) values (null, ?) [23505-199]
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.createEntity(EmployeeResourceIT.java:143)
	at io.github.jhipster.sample.web.rest.EmployeeResourceIT.initTest(EmployeeResourceIT.java:152)


[ERROR]   EmployeeResourceIT.initTest:152->createEntity:143 » Persistence org.hibernate....
[INFO] 
[ERROR] Tests run: 335, Failures: 0, Errors: 46, Skipped: 0

Prior to my edit, this JDL already had an warning, but it didn't result in an error. I don't know if all dependent entities should have a DTO or not.

WARNING! This entity has the DTO option, and it has a relationship with entity "operation" that doesn't have the DTO option. This will result in an error.
WARNING! This entity has the DTO option, and it has a relationship with entity "jobHistory" that doesn't have the DTO option. This will result in an error.
WARNING! This entity has the DTO option, and it has a relationship with entity "job" that doesn't have the DTO option. This will result in an error.
WARNING! This entity has the DTO option, and it has a relationship with entity "jobHistory" that doesn't have the DTO option. This will result in an error.

@pascalgrimaud
Copy link
Member

I confirm the issue and can reproduce.
Thanks a lot for your analyze, @Gabrui !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants