Spring MongoDB: Auswertung schlägt fehl

TM69

Bekanntes Mitglied
ich habe folgende Entity
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"
 

TM69

Bekanntes Mitglied
Nachtrag:
Ich habe inzwischen die Funktion etwas modifiziert. So
Java:
    public boolean existsCountryByNameOrCode(String country_name, String code) {
        logger.debug("CountryService::existsCountryByNameOrCountryCode(" + country_name + ", " + code + ")");
    
        return     this.existsCountryByName(country_name) || this.existsCountryByCode(code);
        /*
         * CHECK:
         *
        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);
        */
    }

wird der Test vollständig durchgelaufen. Mir persönlich würde aber das "original" besser gefallen, da es in einer Statement abgehandelt wird und nicht in zwei.
 


Schreibe deine Antwort... und nutze den </> Button, wenn du Code posten möchtest...
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Spring MongoDB: Prüfen ob bereits eine Email existiert Datenbankprogrammierung 15
T Spring MongoDB @Indexed(unique=true) Datenbankprogrammierung 0
T Spring MongoDB self-reference Datenbankprogrammierung 2
OnDemand JDBC Client Spring: Pool läuft voll Datenbankprogrammierung 6
T Cast-Fehler: Spring Data exists.... boolean??? Datenbankprogrammierung 1
ma095 value NULL- Datenbank Postgresql Spring - intellij community Datenbankprogrammierung 0
OnDemand Spring Boot Speichern in Threads Datenbankprogrammierung 6
bueseb84 Spring Boot : Update Mysql Datenbank Datenbankprogrammierung 1
M Spring, JPA, Hibernate, H2 Datenbankprogrammierung 2
R JPA, Spring, löschen einer Entity Datenbankprogrammierung 2
J Hibernate + Spring + SQL Server => Performanceprobleme :( Datenbankprogrammierung 4
R Mongodb testen bzw mocken Datenbankprogrammierung 3
R Mongodb Daten werden immer überschrieben Datenbankprogrammierung 7
R Mongodb tree Architektur Datenbankprogrammierung 6
R Mongodb Unterschied MongoClient und Repository. Datenbankprogrammierung 3
R Mongodb Daten in einem bestimmten Document speichern Datenbankprogrammierung 1
R Mongodb Authentication failed Datenbankprogrammierung 6
R Beste Lösung für User Erstellung in mongodb Datenbankprogrammierung 1
6 MongoDB Dokument basierend auf Referenz finden Datenbankprogrammierung 1
MongoDB-Datenbank in Androidstudio einbinden Datenbankprogrammierung 1
N MongoDB Datenbankprogrammierung 5
S MongoDB löschung ohne Cascade Datenbankprogrammierung 1
OnDemand MySQL und mongoDB wann macht was Sinn? Datenbankprogrammierung 11
S MongoDB Community Edition Datenbankprogrammierung 1
S MongoDB - Abfrageergebnis in Array speichern Datenbankprogrammierung 2
C Über Classpath MongoDB Treiber einbinden und korrekte import Pfade Datenbankprogrammierung 8
B MongoDB- Queryception Datenbankprogrammierung 6
M MongoDb Versändnis Fragen: ( multiserver, morphia/morphium ) Datenbankprogrammierung 0
A MongoDB Passwort Problem Datenbankprogrammierung 0
T MongoDB: Morphia REST 505 Exception Tomcat Datenbankprogrammierung 2
G MongoDB - klassisches one to many Datenbankprogrammierung 2
P MongoDB vs. andere DBs Datenbankprogrammierung 0
D Daten posten auf RestApi (Mongodb/NoSQL) Datenbankprogrammierung 0
F MSSql oder MongoDB für die Speicherung von POI Datenbankprogrammierung 9
S Mitarbeiterverwaltung und Auswertung Datenbankprogrammierung 4
M Logfile auswertung Datenbankprogrammierung 4

Ähnliche Java Themen

Neue Themen


Oben