mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 15:25:49 +00:00
updated
This commit is contained in:
parent
7124afe527
commit
9244518ff9
11 changed files with 45 additions and 36 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -8,7 +8,7 @@ import java.io.IOException;
|
||||||
public interface PetVaccinationService {
|
public interface PetVaccinationService {
|
||||||
|
|
||||||
@WithSpan
|
@WithSpan
|
||||||
VaccinnationRecord[] AllVaccines() throws JSONException, IOException;
|
VaccinnationRecord[] allVaccines() throws JSONException, IOException;
|
||||||
|
|
||||||
@WithSpan
|
@WithSpan
|
||||||
VaccinnationRecord VaccineRecord(int vaccinationRecordId) throws JSONException, IOException;
|
VaccinnationRecord VaccineRecord(int vaccinationRecordId) throws JSONException, IOException;
|
||||||
|
|
|
@ -12,7 +12,6 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@ -20,7 +19,7 @@ public class PetVaccinationServiceFacade implements PetVaccinationService {
|
||||||
|
|
||||||
public static final String VACCINES_RECORDS_URL = "https://647f4bb4c246f166da9084c7.mockapi.io/api/vetcheck/vaccines";
|
public static final String VACCINES_RECORDS_URL = "https://647f4bb4c246f166da9084c7.mockapi.io/api/vetcheck/vaccines";
|
||||||
|
|
||||||
private String MakeHttpCall(String url) throws IOException {
|
private String makeHttpCall(String url) throws IOException {
|
||||||
|
|
||||||
Request getAllVaccinesRequest = new Request.Builder().url(url).build();
|
Request getAllVaccinesRequest = new Request.Builder().url(url).build();
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
@ -30,9 +29,9 @@ public class PetVaccinationServiceFacade implements PetVaccinationService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@WithSpan
|
@WithSpan
|
||||||
public VaccinnationRecord[] AllVaccines() throws JSONException, IOException {
|
public VaccinnationRecord[] allVaccines() throws JSONException, IOException {
|
||||||
|
|
||||||
var vaccineListString = MakeHttpCall(VACCINES_RECORDS_URL);
|
var vaccineListString = makeHttpCall(VACCINES_RECORDS_URL);
|
||||||
JSONArray jArr = new JSONArray(vaccineListString);
|
JSONArray jArr = new JSONArray(vaccineListString);
|
||||||
var vaccinnationRecords = new ArrayList<VaccinnationRecord>();
|
var vaccinnationRecords = new ArrayList<VaccinnationRecord>();
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ public class PetVaccinationServiceFacade implements PetVaccinationService {
|
||||||
|
|
||||||
var idUrl = VACCINES_RECORDS_URL + "/" + vaccinationRecordId;
|
var idUrl = VACCINES_RECORDS_URL + "/" + vaccinationRecordId;
|
||||||
|
|
||||||
var vaccineListString = MakeHttpCall(idUrl);
|
var vaccineListString = makeHttpCall(idUrl);
|
||||||
|
|
||||||
JSONObject vaccineJson = new JSONObject(vaccineListString);
|
JSONObject vaccineJson = new JSONObject(vaccineListString);
|
||||||
return parseVaccinationRecord(vaccineJson);
|
return parseVaccinationRecord(vaccineJson);
|
||||||
|
|
|
@ -5,7 +5,6 @@ import io.opentelemetry.instrumentation.annotations.WithSpan;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.adapters.PetVaccinationService;
|
import org.springframework.samples.petclinic.adapters.PetVaccinationService;
|
||||||
import org.springframework.samples.petclinic.adapters.PetVaccinationServiceFacade;
|
|
||||||
import org.springframework.samples.petclinic.adapters.VaccinnationRecord;
|
import org.springframework.samples.petclinic.adapters.VaccinnationRecord;
|
||||||
import org.springframework.samples.petclinic.owner.Pet;
|
import org.springframework.samples.petclinic.owner.Pet;
|
||||||
import org.springframework.samples.petclinic.owner.PetVaccine;
|
import org.springframework.samples.petclinic.owner.PetVaccine;
|
||||||
|
@ -21,12 +20,13 @@ public class PetVaccinationStatusService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PetVaccinationService adapter;
|
private PetVaccinationService adapter;
|
||||||
|
|
||||||
|
|
||||||
@WithSpan
|
@WithSpan
|
||||||
public void UpdateVaccinationStatus(Pet[] pets) {
|
public void UpdateVaccinationStatus(Pet[] pets) {
|
||||||
|
|
||||||
for (Pet pet : pets) {
|
for (Pet pet : pets) {
|
||||||
try {
|
try {
|
||||||
var vaccinationRecords = this.adapter.AllVaccines();
|
var vaccinationRecords = this.adapter.allVaccines();
|
||||||
for (VaccinnationRecord record : vaccinationRecords) {
|
for (VaccinnationRecord record : vaccinationRecords) {
|
||||||
|
|
||||||
var recordInfo = this.adapter.VaccineRecord(record.recordId());
|
var recordInfo = this.adapter.VaccineRecord(record.recordId());
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import io.opentelemetry.instrumentation.annotations.WithSpan;
|
import io.opentelemetry.instrumentation.annotations.WithSpan;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -54,14 +56,15 @@ class OwnerController {
|
||||||
|
|
||||||
private OwnerValidation validator;
|
private OwnerValidation validator;
|
||||||
|
|
||||||
|
|
||||||
public OwnerController(OwnerRepository clinicService) {
|
public OwnerController(OwnerRepository clinicService) {
|
||||||
this.owners = clinicService;
|
this.owners = clinicService;
|
||||||
var otelTracer = getTracer("OwnerController");
|
var otelTracer = getTracer("OwnerController");
|
||||||
|
|
||||||
validator = new OwnerValidation(otelTracer);
|
validator = new OwnerValidation(otelTracer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@InitBinder
|
@InitBinder
|
||||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
|
@ -72,6 +75,7 @@ class OwnerController {
|
||||||
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
|
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/owners/new")
|
@GetMapping("/owners/new")
|
||||||
public String initCreationForm(Map<String, Object> model) {
|
public String initCreationForm(Map<String, Object> model) {
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,10 @@
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.domain.PetVaccinationStatusService;
|
import org.springframework.samples.petclinic.domain.PetVaccinationStatusService;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -40,7 +43,7 @@ import jakarta.validation.Valid;
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/owners/{ownerId}")
|
@RequestMapping("/owners/{ownerId}")
|
||||||
class PetController {
|
class PetController implements InitializingBean {
|
||||||
|
|
||||||
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
||||||
|
|
||||||
|
@ -49,10 +52,14 @@ class PetController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PetVaccinationStatusService petVaccinationStatus;
|
private PetVaccinationStatusService petVaccinationStatus;
|
||||||
|
|
||||||
|
private ExecutorService executorService;
|
||||||
|
|
||||||
|
|
||||||
public PetController(OwnerRepository owners) {
|
public PetController(OwnerRepository owners) {
|
||||||
this.owners = owners;
|
this.owners = owners;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ModelAttribute("types")
|
@ModelAttribute("types")
|
||||||
public Collection<PetType> populatePetTypes() {
|
public Collection<PetType> populatePetTypes() {
|
||||||
return this.owners.findPetTypes();
|
return this.owners.findPetTypes();
|
||||||
|
@ -101,7 +108,9 @@ class PetController {
|
||||||
|
|
||||||
|
|
||||||
this.owners.save(owner);
|
this.owners.save(owner);
|
||||||
|
this.executorService.submit(() -> {
|
||||||
petVaccinationStatus.UpdateVaccinationStatus(owner.getPets().toArray(Pet[]::new));
|
petVaccinationStatus.UpdateVaccinationStatus(owner.getPets().toArray(Pet[]::new));
|
||||||
|
});
|
||||||
|
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
@ -125,4 +134,9 @@ class PetController {
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
this.executorService = Executors.newFixedThreadPool(5);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,6 @@ public class OwnerControllerTests {
|
||||||
|
|
||||||
Owner owner = CreateOwner();
|
Owner owner = CreateOwner();
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
|
|
||||||
String newPetName = faker.dog().name();
|
String newPetName = faker.dog().name();
|
||||||
given().contentType("multipart/form-data")
|
given().contentType("multipart/form-data")
|
||||||
|
@ -85,20 +84,13 @@ public class OwnerControllerTests {
|
||||||
.post(String.format("/owners/%s/pets/new", owner.getId()))
|
.post(String.format("/owners/%s/pets/new", owner.getId()))
|
||||||
.then()
|
.then()
|
||||||
.statusCode(Matchers.not(Matchers.greaterThan(499)));
|
.statusCode(Matchers.not(Matchers.greaterThan(499)));
|
||||||
try {
|
|
||||||
Thread.sleep(800);
|
|
||||||
}
|
|
||||||
catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// var updatedOwner = ownerRepository.findById(owner.getId());
|
var updatedOwner = ownerRepository.findById(owner.getId());
|
||||||
// assertThat(updatedOwner.getPets())
|
assertThat(updatedOwner.getPets())
|
||||||
// .hasSize(2)
|
.hasSize(2)
|
||||||
// .extracting(Pet::getName)
|
.extracting(Pet::getName)
|
||||||
// .contains(newPetName);
|
.contains(newPetName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue