fix: add shift features #51
@@ -1,5 +1,156 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import com.drinkool.InternalValue;
|
||||
import com.drinkool.entities.ShiftEntity;
|
||||
import com.drinkool.entities.ShiftRegistrationEntity;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.time.LocalTime;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
public class ShiftServiceTest {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
@Transactional
|
||||
void setup() {
|
||||
ShiftRegistrationEntity.deleteAll();
|
||||
ShiftEntity.deleteAll();
|
||||
}
|
||||
|
||||
// --- TEST QUERIES ---
|
||||
|
||||
@Test
|
||||
void testGetAllShifts() {
|
||||
createMockShift("Ca Sáng", "08:00:00", "12:00:00");
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("{ \"query\": \"{ allShifts { name startTime } }\" }")
|
||||
.when()
|
||||
.post("/graphql")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("data.allShifts", hasSize(1))
|
||||
.body("data.allShifts[0].name", is("Ca Sáng"));
|
||||
}
|
||||
|
||||
// --- TEST MUTATIONS ---
|
||||
|
||||
@Test
|
||||
void testCreateShift_AsManager() {
|
||||
String mutation =
|
||||
"mutation { createShift(shiftInput: { " +
|
||||
"name: \"Ca Chiều\", " +
|
||||
"startTime: \"13:00:00\", " +
|
||||
"endTime: \"17:00:00\", " +
|
||||
"maxStaff: 3 }) { id name } }";
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("{ \"query\": \"" + mutation + "\" }")
|
||||
.when()
|
||||
.post("/graphql")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("data.createShift.name", is("Ca Chiều"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRegisterShift_Success() {
|
||||
ShiftEntity shift = createMockShift("Ca Tối", "18:00:00", "22:00:00");
|
||||
UUID staffId = UUID.randomUUID();
|
||||
|
||||
String mutation = String.format(
|
||||
"mutation { registerShift(registration: { shiftId: \\\"%s\\\" }) { id staffId } }",
|
||||
shift.id
|
||||
);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header(InternalValue.userId, staffId.toString()) // Mock user ID
|
||||
.body("{ \"query\": \"" + mutation + "\" }")
|
||||
.when()
|
||||
.post("/graphql")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("data.registerShift.staffId", is(staffId.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRegisterShift_Overlap_Fail() {
|
||||
UUID staffId = UUID.randomUUID();
|
||||
|
||||
// Tạo 2 ca bị trùng giờ nhau
|
||||
ShiftEntity shift1 = createMockShift("Ca 1", "08:00:00", "12:00:00");
|
||||
ShiftEntity shift2 = createMockShift("Ca 2", "10:00:00", "14:00:00");
|
||||
|
||||
// Đăng ký ca 1 trước
|
||||
registerStaffToShift(staffId, shift1);
|
||||
|
||||
// Đăng ký ca 2 (sẽ bị overlap)
|
||||
String mutation = String.format(
|
||||
"mutation { registerShift(registration: { shiftId: \\\"%s\\\" }) { id } }",
|
||||
shift2.id
|
||||
);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header(InternalValue.userId, staffId.toString())
|
||||
.body("{ \"query\": \"" + mutation + "\" }")
|
||||
.when()
|
||||
.post("/graphql")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("errors", notNullValue())
|
||||
.body("errors[0].message", containsString("OverlappingShift"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteShift_WithRegistrations_Fail() {
|
||||
ShiftEntity shift = createMockShift("Ca Xóa", "08:00:00", "12:00:00");
|
||||
registerStaffToShift(UUID.randomUUID(), shift);
|
||||
|
||||
String mutation = String.format(
|
||||
"mutation { deleteShift(id: \\\"%s\\\") }",
|
||||
shift.id
|
||||
);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("{ \"query\": \"" + mutation + "\" }")
|
||||
.when()
|
||||
.post("/graphql")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("errors[0].message", is("CannotDeleteShiftWithRegistrations"));
|
||||
}
|
||||
|
||||
// --- HELPER METHODS ---
|
||||
|
||||
@Transactional
|
||||
ShiftEntity createMockShift(String name, String start, String end) {
|
||||
ShiftEntity shift = new ShiftEntity();
|
||||
shift.id = UUID.randomUUID();
|
||||
shift.name = name;
|
||||
shift.startTime = LocalTime.parse(start);
|
||||
shift.endTime = LocalTime.parse(end);
|
||||
shift.maxStaff = 5;
|
||||
shift.persist();
|
||||
return shift;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
void registerStaffToShift(UUID staffId, ShiftEntity shift) {
|
||||
ShiftRegistrationEntity reg = new ShiftRegistrationEntity();
|
||||
reg.id = UUID.randomUUID();
|
||||
reg.staffId = staffId;
|
||||
reg.shift = shift;
|
||||
reg.persist();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user