Skip to content

Task 3-3: Integration Test - Return Early & Expiry Batch

Phase: 3 - Testing Priority: Medium Depends on: task-1-1, task-1-2 Reference: docs/BountyHunter-Backend/details/feature-nft-management/SPEC.md

Background

Test các flow kết thúc rental ngoài happy path: renter trả về sớm và batch xử lý expired rentals.

Test scenarios

Return early

  • [ ] Create rental → confirm → wait 1 ngày (mock thời gian bằng Clock injection hoặc set rental_start_time trong past) → PUT /return-rental-early/{id}
  • [ ] Verify: status=RETURN_EARLY
  • [ ] Verify pro-rated refund calculation với example cụ thể:
  • Duration = 7 ngày, fee = 700
  • Return sau 1 ngày → owner giữ 100 (1/7 × 700), renter nhận lại 600 (6/7 × 700)
  • Confirm balance changes đúng (owner +100, renter refund +600)
  • [ ] Verify: product status=AVAILABLE sau return
  • [ ] Verify: owner nhận notification "NFT returned early"

Return early (minted NFT)

  • [ ] Return early với minted Bounty Ball → NodeServerService được gọi để update on-chain state
  • Mock NodeServerService trong test để verify call và không cần real blockchain
  • [ ] Return early với unminted → chỉ DB update, NodeServerService không được gọi

Expiry batch

  • [ ] Create rental → confirm → mock expiry (set rental_end_time = past, ví dụ: Instant.now().minusSeconds(3600))
  • [ ] Chạy NftRentalExpirationCheckBatch manually (trigger via test context hoặc direct method call)
  • [ ] Verify: status=FINISHED (hoặc EXPIRED_HANDLED — xác nhận tên status thực tế trong code)
  • [ ] Verify: product status=AVAILABLE
  • [ ] Verify: notifications sent tới owner và renter

Batch idempotency

  • [ ] Chạy batch 2 lần cho cùng expired rental:
  • First run: status RENTINGFINISHED
  • Second run: status đã FINISHED → không thay đổi, không lỗi
  • [ ] Verify: không có duplicate notifications

Verification / Acceptance Criteria

  • [ ] Return early: pro-rated refund tính đúng (owner nhận proportional, renter nhận remainder)
  • [ ] Minted NFT return: NodeServerService được invoke; unminted: không invoke
  • [ ] Expiry batch: status đúng, product available, notifications sent
  • [ ] Batch idempotency: chạy 2+ lần cho cùng record → no-op, no error, no duplicate side effects
  • [ ] rental_end_time được set đúng khi confirm (dùng cho expiry check)

Files to Modify

  • Test files trong webmarketplace/src/test/ hoặc integration test module