Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
*/
package org.springframework.samples.petclinic.owner;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;
import org.springframework.util.Assert;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -31,8 +23,14 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;
import org.springframework.util.Assert;

/**
* Simple JavaBean domain object representing an owner.
Expand All @@ -58,7 +56,8 @@ public class Owner extends Person {

@Column
@NotBlank
@Pattern(regexp = "\\d{10}", message = "{telephone.invalid}")
@Pattern(regexp = "^[0-9+\\-\\s()]{7,20}$", message = "{telephone.invalid}")
@jakarta.validation.constraints.Size(min = 7, max = 15)
private String telephone;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
Expand Down Expand Up @@ -162,7 +161,6 @@ public String toString() {
* @param visit the visit to add, must not be {@literal null}.
*/
public void addVisit(Integer petId, Visit visit) {

Assert.notNull(petId, "Pet identifier must not be null!");
Assert.notNull(visit, "Visit must not be null!");

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ somethingHappened=Something happened...
pets=Pets
home=Home
error=Error
telephone.invalid=Telephone must be a 10-digit number
telephone.invalid=Invalid phone number. Use international format (e.g. +34123456789)
layoutTitle=PetClinic :: a Spring Framework demonstration
pet=Pet
birthDate=Birth Date
type=Type
previousVisits=Previous Visits
date=Date
description=Description
new=New
new=New
addVisit=Add Visit
editPet=Edit Pet
ownerInformation=Owner Information
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=Etwas ist passiert...
pets=Haustiere
home=Startseite
error=Fehler
telephone.invalid=Telefonnummer muss aus 10 Ziffern bestehen
telephone.invalid=Ungültige Telefonnummer. Verwenden Sie das internationale Format (z.B. +34123456789)
layoutTitle=PetClinic :: eine Demonstration des Spring Frameworks
pet=Haustier
birthDate=Geburtsdatum
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=Algo pasó...
pets=Mascotas
home=Inicio
error=Error
telephone.invalid=El número de teléfono debe tener 10 dígitos
telephone.invalid=Número de teléfono inválido. Use el formato internacional (ej. +34123456789)
layoutTitle=PetClinic :: una demostración de Spring Framework
pet=Mascota
birthDate=Fecha de nacimiento
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=مشکلی پیش آمد...
pets=حیوانات خانگی
home=خانه
error=خطا
telephone.invalid=شماره تلفن باید ۱۰ رقمی باشد
telephone.invalid=شماره تلفن نامعتبر است. از فرمت بین‌المللی استفاده کنید (مثال: +34123456789)
layoutTitle=PetClinic :: یک نمایش از Spring Framework
pet=حیوان خانگی
birthDate=تاریخ تولد
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_ko.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=문제가 발생했습니다...
pets=반려동물
home=홈
error=오류
telephone.invalid=전화번호는 10자리 숫자여야 합니다
telephone.invalid=잘못된 전화번호입니다. 국제 형식을 사용하세요 (예: +34123456789)
layoutTitle=PetClinic :: Spring Framework 데모
pet=반려동물
birthDate=생년월일
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_pt.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=Algo aconteceu...
pets=Animais de estimação
home=Início
error=Erro
telephone.invalid=O número de telefone deve conter 10 dígitos
telephone.invalid=Número de telefone inválido. Use o formato internacional (ex.: +34123456789)
layoutTitle=PetClinic :: uma demonstração do Spring Framework
pet=Animal de estimação
birthDate=Data de nascimento
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=Что-то пошло не так...
pets=Питомцы
home=Главная
error=Ошибка
telephone.invalid=Телефон должен содержать 10 цифр
telephone.invalid=Неверный номер телефона. Используйте международный формат (например, +34123456789)
layoutTitle=PetClinic :: демонстрация Spring Framework
pet=Питомец
birthDate=Дата рождения
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ somethingHappened=Bir şey oldu...
pets=Evcil Hayvanlar
home=Ana Sayfa
error=Hata
telephone.invalid=Telefon numarası 10 basamaklı olmalıdır
telephone.invalid=Geçersiz telefon numarası. Uluslararası formatı kullanın (örn. +34123456789)
layoutTitle=PetClinic :: bir Spring Framework demosu
pet=Evcil Hayvan
birthDate=Doğum Tarihi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@

package org.springframework.samples.petclinic.owner;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
Expand All @@ -49,6 +32,22 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

/**
* Test class for {@link OwnerController}
*
Expand Down Expand Up @@ -89,7 +88,6 @@ private Owner george() {

@BeforeEach
void setup() {

Owner george = george();
given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))
.willReturn(new PageImpl<>(List.of(george)));
Expand All @@ -98,7 +96,6 @@ void setup() {
Visit visit = new Visit();
visit.setDate(LocalDate.now());
george.getPet("Max").getVisits().add(visit);

}

@Test
Expand Down Expand Up @@ -164,7 +161,6 @@ void processFindFormNoOwnersFound() throws Exception {
.andExpect(model().attributeHasFieldErrors("owner", "lastName"))
.andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
.andExpect(view().name("owners/findOwners"));

}

@Test
Expand Down Expand Up @@ -248,4 +244,19 @@ void processUpdateOwnerFormWithIdMismatch() throws Exception {
.andExpect(flash().attributeExists("error"));
}

@Test
void processCreationFormInvalidTelephone() throws Exception {
mockMvc
.perform(post("/owners/new").param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("address", "123 Caramel Street")
.param("city", "London")
.param("telephone", "ABC12345")) // longitud OK, patrón inválido
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner"))
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))
.andExpect(model().attributeHasFieldErrorCode("owner", "telephone", "Pattern"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}

}