Ich möchte in einer Entity ein Password Feld so kennzeichnen, dass es zwar null sein darf, aber nicht Blank. Wie kann ich soetwas realisieren?
Ich habe im Netz zwar etwas gefunden
Meine konkrete Entity sieht wie folgt aus
meine Testklasse scheint nicht zu greifen
Die Test Funktion testPasswordWithNull liefert Test-Fehler.
Ich habe im Netz zwar etwas gefunden
Code:
@ConstraintComposition(CompositionType.OR)
@Constraint(validatedBy = {})
@Null
@NotBlank
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface NullOrNotBlank {
String message() default "Must be null or not blank";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Meine konkrete Entity sieht wie folgt aus
Code:
Entity(name="persons")
@Table(indexes = { @Index(name = "IDX_EMAIL_PASSWORD", columnList = "email, password") })
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreationTimestamp
private Date created_at;
@Null
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="created_by")
private Person created_by;
@UpdateTimestamp
private Date updated_at;
@Null
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="updated_by")
private Person updated_by;
@NotNull
@NotBlank
@Length(min=3, max=255)
private String firstname;
@NotNull
@NotBlank
@Length(min=3, max=255)
private String lastname;
@NotNull
@NotBlank
@Length(min=3, max=255)
@Email(message = "Email should be valid")
private String email;
@NullOrNotBlank
@Length(min=6, max=255)
private String password;
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreatedAt() {
return created_at;
}
public void setCreatedAt(Date created_at) {
this.created_at = created_at;
}
public Person getCreatedBy() {
return created_by;
}
public void setCreatedBy(Person created_by) {
this.created_by = created_by;
}
public Date getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(Date updated_at) {
this.updated_at = updated_at;
}
public Person getUpdatedBy() {
return updated_by;
}
public void setUpdatedBy(Person updated_by) {
this.updated_by = updated_by;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Person [id=" + id + ", created_at=" + created_at + ", created_by=" + created_by + ", updated_at="
+ updated_at + ", updated_by=" + updated_by + ", firstname=" + firstname + ", lastname=" + lastname
+ ", email=" + email + ", password=" + password + "]";
}
}
meine Testklasse scheint nicht zu greifen
Code:
import static org.junit.Assert.*;
import java.util.Date;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.project.entities.Person;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonRepositoryTest {
@Autowired
PersonRepository personRepository;
@Before
public void setUp() throws Exception {
}
/**
* test a correct person class password with null
*
* @return void
*/
@Test
public void testPasswordWithNull() {
Date dateValue = new Date();
String firstnameValue = "firstname";
String lastnameValue = "lastname";
String emailValue = "email@email.de";
String passwordValue = null; // "a pasword";
Person person = new Person();
person.setFirstname(firstnameValue);
person.setLastname(lastnameValue);
person.setEmail(emailValue);
person.setPassword(passwordValue);
Boolean exception = false;
try {
personRepository.save(person);
} catch (Exception e) { // password is null
exception = true;
}
assertFalse(exception);
}
/**
* test a correct person class passwword with blank
*
* @return void
*/
@Test
public void testPasswordWithBlank() {
Date dateValue = new Date();
String firstnameValue = "firstname";
String lastnameValue = "lastname";
String emailValue = "email@email.de";
String passwordValue = ""; // "a pasword";
Person person = new Person();
person.setFirstname(firstnameValue);
person.setLastname(lastnameValue);
person.setEmail(emailValue);
person.setPassword(passwordValue);
Boolean exception = false;
try {
personRepository.save(person);
} catch (Exception e) { // password is blank
exception = true;
}
assertTrue(exception);
}
/**
* test a correct person class passwword with empty String
*
* @return void
*/
@Test
public void testPasswordWithEmptyString() {
Date dateValue = new Date();
String firstnameValue = "firstname";
String lastnameValue = "lastname";
String emailValue = "email@email.de";
String passwordValue = " "; // "a pasword";
Person person = new Person();
person.setFirstname(firstnameValue);
person.setLastname(lastnameValue);
person.setEmail(emailValue);
person.setPassword(passwordValue);
Boolean exception = false;
try {
personRepository.save(person);
} catch (Exception e) { // password has a empty string
exception = true;
}
assertTrue(exception);
}
}
Die Test Funktion testPasswordWithNull liefert Test-Fehler.