Skip to content

Commit

Permalink
Testing - Edit Medical record feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Codinab committed Apr 5, 2024
1 parent 9c940c4 commit 5467fff
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.MedicalRecord;
import cat.udl.eps.softarch.demo.domain.Pet;
import cat.udl.eps.softarch.demo.repository.MedicalRecordRepository;
import cat.udl.eps.softarch.demo.repository.PetRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class EditMedicalRecordStepDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
MedicalRecordRepository medicalRecordRepository;

@Autowired
PetRepository petRepository;

@And("a medical record exists for the pet")
public void aMedicalRecordExistsForThePet() {
Pet pet = petRepository.findAll().iterator().next();

MedicalRecord existingMedicalRecord;
existingMedicalRecord = new MedicalRecord();
existingMedicalRecord.setPet(pet);
existingMedicalRecord.setIssue("Initial Issue");
existingMedicalRecord.setDescription("Initial Description");
existingMedicalRecord.setDate(ZonedDateTime.now());
medicalRecordRepository.save(existingMedicalRecord);

}

@When("I edit the medical record for a pet with new issue {string}, new description {string}, and new date {string}")
public void iEditTheMedicalRecordForAPetWithNewIssueNewDescriptionAndNewDate(String issue, String description, String date) throws Throwable {
MedicalRecord existingMedicalRecord = medicalRecordRepository.findAll().iterator().next();
existingMedicalRecord.setIssue(issue);
existingMedicalRecord.setDescription(description);
existingMedicalRecord.setDate(ZonedDateTime.parse(date));

stepDefs.result = stepDefs.mockMvc.perform(
put("/medicalRecords/" + existingMedicalRecord.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingMedicalRecord))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

@When("I edit the medical record for a pet with new issue {string}, new description {string}")
public void iEditTheMedicalRecordForAPetWithNewIssueNewDescription(String issue, String description) throws Throwable {
MedicalRecord existingMedicalRecord = medicalRecordRepository.findAll().iterator().next();

existingMedicalRecord.setIssue(issue);
existingMedicalRecord.setDescription(description);
// Keeping the original date

stepDefs.result = stepDefs.mockMvc.perform(
put("/medicalRecords/" + existingMedicalRecord.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingMedicalRecord))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

@When("I try to edit a medical record for a pet")
public void iTryToEditAMedicalRecordForAPet() throws Exception {
MedicalRecord existingMedicalRecord = medicalRecordRepository.findAll().iterator().next();

stepDefs.result = stepDefs.mockMvc.perform(
put("/medicalRecords/" + existingMedicalRecord.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(existingMedicalRecord))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
}
43 changes: 43 additions & 0 deletions src/test/resources/features/EditMedicalRecord.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Feature: Edit Medical Record
In order to keep pet health history up to date
As a shelter volunteer
I want to edit existing medical records in pets' profiles

Background:
Given a pet exists in the system
And a medical record exists for the pet
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given There is a registered admin with name "admin" and password "password" and email "[email protected]"
Given There is a registered volunteer with name "volunteer" and password "password" and email "[email protected]"


Scenario: Edit an existing medical record as Volunteer
Given I login as "volunteer" with password "password"
When I edit the medical record for a pet with new issue "Allergy Update", new description "Updated seasonal allergy treatment", and new date "2024-03-08T14:00:00Z"
Then The response code is 204

Scenario: Edit a medical record with empty issue as Volunteer
Given I login as "volunteer" with password "password"
When I edit the medical record for a pet with new issue "", new description "Revaccination", and new date "2024-03-08T14:00:00Z"
Then The response code is 400

Scenario: Edit a medical record with empty description as Volunteer
Given I login as "volunteer" with password "password"
When I edit the medical record for a pet with new issue "Vaccination Update", new description "", and new date "2024-03-08T14:00:00Z"
Then The response code is 400

Scenario: Edit a medical record without changing the date as Volunteer
Given I login as "volunteer" with password "password"
When I edit the medical record for a pet with new issue "Injury Update", new description "Healed cut on paw"
Then The response code is 204

Scenario: Attempt to edit a medical record as a normal user
Given I login as "user" with password "password"
When I try to edit a medical record for a pet
Then The response code is 403
And The error message is "Unauthorized access"

Scenario: Attempt to edit a medical record as an admin
Given I login as "admin" with password "password"
When I try to edit a medical record for a pet
Then The response code is 204

0 comments on commit 5467fff

Please sign in to comment.