ich habe folgende Entity
Aus dem Service stammt folgende Funktion, um die es sich dreht
und eine entsprechende Test-Case dazu
Bis zum Fall "test 6" läuft alles ok. Bei
String findName6 = "germany";
String findCountryCode6 = "UK";
liefert die Funktion aber false anstelle des (für mich logischen) true zurück. Erwähnenswert wäre noch, dass beide Datenstäze in der Datenbank enthalten ist.
Java:
@Document(collection="countries")
public class Country {
@Transient
public static final String SEQUENCE_NAME = "countries_sequence";
@Id
private long id; // the id of the user
@DBRef
private User created_by; // the creator
private LocalDateTime created_at; // the created date
@DBRef
private User updated_by; // the creator
private LocalDateTime updated_at; // the update
@DBRef
private User deleted_by; // the user who has this deleted
private LocalDateTime deleted_at; // the date when the user has this deleted
@NotBlank
@Size(min = 1, max = 100)
@Indexed(unique = true)
private String name; // the country name
@NotBlank
@Size(min = 1, max = 2)
@Indexed(unique = true)
private String code; // the country code (ISO Code 3166-Alpha2
private Binary flag; // the flag of the country
public Country(
@NotBlank @Size(min = 1, max = 100) String name,
@NotBlank @Size(min = 1, max = 2) String code,
Binary flag) {
super();
this.name = name;
this.code = code;
this.flag = flag;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getCreatedBy() {
return created_by;
}
public void setCreatedBy(User created_by) {
this.created_by = created_by;
}
public LocalDateTime getCreatedAt() {
return created_at;
}
public void setCreatedAt(LocalDateTime created_at) {
this.created_at = created_at;
}
public User getUpdatedBy() {
return updated_by;
}
public void setUpdatedBy(User updated_by) {
this.updated_by = updated_by;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public User getDeletedBy() {
return deleted_by;
}
public void setDeletedBy(User deleted_by) {
this.deleted_by = deleted_by;
}
public LocalDateTime getDeletedAt() {
return deleted_at;
}
public void setDeletedAt(LocalDateTime deleted_at) {
this.deleted_at = deleted_at;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Binary getFlag() {
return flag;
}
public void setFlag(Binary flag) {
this.flag = flag;
}
@Override
public int hashCode() {
return Objects.hash(code, name);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Country other = (Country) obj;
return Objects.equals(code, other.code) && Objects.equals(name, other.name);
}
@Override
public String toString() {
return "Country [id=" + id + ", created_by=" + created_by + ", created_at=" + created_at + ", updated_by="
+ updated_by + ", updated_at=" + updated_at + ", deleted_by=" + deleted_by + ", deleted_at="
+ deleted_at + ", name=" + name + ", code=" + code + ", flag=" + flag + "]";
}
}
Aus dem Service stammt folgende Funktion, um die es sich dreht
Code:
public boolean existsCountryByNameOrCode(String country_name, String code) {
logger.debug("CountryService::existsCountryByNameOrCountryCode(" + country_name + ", " + code + ")");
Criteria regex = Criteria.where("name").regex(
Pattern.compile(country_name, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
).orOperator(Criteria.where("code").regex(
Pattern.compile(code, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
));
Query query = new Query().addCriteria(regex);
return mongoOperations.exists(query, Country.class);
}
und eine entsprechende Test-Case dazu
Code:
@Test
void testExistsCountryByNameOrCountryCode() throws ResourceNotFoundException, NotDefinedException, DuplicateException {
// create creator
String firstname = "firstname";
String lastname = "lastname";
String email = "EMail@email.com";
String password = "password";
User creator = new User(
firstname, // password
lastname, // lastname
email, // email
password // password
);
User createdCreator = userService.createUser(null, creator);
Optional<User> foundCreator = userService.findUserById(createdCreator.getId());
assertTrue( foundCreator.isPresent(), "testExistsCountryByNameOrCountryCode(): Creator (" + createdCreator + ") not stored");
// create country 1
String country_name1 = "Germany";
String country_code1 = "DE";
Country createCountry1 = new Country(
country_name1,
country_code1,
null);
Country createdCountry1 = countryService.createCountry(creator, createCountry1);
Optional<Country> foundCreatedCountry1 = countryService.findCountryById(createdCountry1.getId());
assertTrue( foundCreatedCountry1.isPresent(), "testExistsCountryByNameOrCountryCode(): Creator (" + createdCountry1 + ") not stored");
// create country 2
String country_name2 = "United Kingdom";
String country_code2 = "UK";
Country createCountry2 = new Country(
country_name2,
country_code2,
null);
Country createdCountry2 = countryService.createCountry(creator, createCountry2);
Optional<Country> foundCreatedCountry2 = countryService.findCountryById(createdCountry2.getId());
assertTrue( foundCreatedCountry2.isPresent(), "testExistsCountryByNameOrCountryCode(): Creator (" + createdCountry1 + ") not stored");
// test 1
String findName1 = "germany";
String findCountryCode1 = "de";
assertTrue( countryService.existsCountryByNameOrCode(findName1, findCountryCode1), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode1 + " or name: " + findName1 );
// test 2
String findName2 = "Germany";
String findCountryCode2 = "de";
assertTrue( countryService.existsCountryByNameOrCode(findName2, findCountryCode2), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry2 + ") not exists a country with code: " + findCountryCode1 + " or name: " + findName2 );
// test 3
String findName3 = "GERMANY";
String findCountryCode3 = "de";
assertTrue( countryService.existsCountryByNameOrCode(findName3, findCountryCode3), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode3 + " or name: " + findName3 );
// test 4
String findName4 = "germany";
String findCountryCode4 = "De";
assertTrue( countryService.existsCountryByNameOrCode(findName4, findCountryCode4), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode4 + " or name: " + findName4 );
// test 5
String findName5 = "germany";
String findCountryCode5 = "DE";
assertTrue( countryService.existsCountryByNameOrCode(findName5, findCountryCode5), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode5 + " or name: " + findName5 );
// test 6
String findName6 = "germany";
String findCountryCode6 = "UK";
assertTrue( countryService.existsCountryByNameOrCode(findName6, findCountryCode6), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode6 + " or name: " + findName6 );
// test 7
String findName7 = "france";
String findCountryCode7 = "UK";
assertTrue( countryService.existsCountryByNameOrCode(findName7, findCountryCode7), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode7 + " or name: " + findName7 );
// test 8
String findName8 = "germany";
String findCountryCode8 = "fr";
assertTrue( countryService.existsCountryByNameOrCode(findName8, findCountryCode8), "testExistsCountryByNameOrCountryCode(): Country (" + createdCountry1 + ") not exists a country with code: " + findCountryCode8 + " or name: " + findName8 );
// clean up
countryService.removeCountry(createdCountry2.getId());
countryService.removeCountry(createdCountry1.getId());
userService.removeUser(creator.getId());
}
Bis zum Fall "test 6" läuft alles ok. Bei
String findName6 = "germany";
String findCountryCode6 = "UK";
liefert die Funktion aber false anstelle des (für mich logischen) true zurück. Erwähnenswert wäre noch, dass beide Datenstäze in der Datenbank enthalten ist.
Code:
_id
2
created_by
DBRef('users', '2')
created_at
2022-09-15T07:33:43.274+00:00
name
"Germany"
code
"DE"
_class
"com.myapp.country.model.Country"
_id
3
created_by
DBRef('users', '2')
created_at
2022-09-15T07:33:43.289+00:00
name
"United Kingdom"
code
"UK"
_class
"com.myapp.country.model.Country"