Skip to content

Task 3-3: Integration Test - Feature Toggle Mechanism

Phase: 3 - Testing Priority: Medium Depends on: Không có Reference: SPEC.md — xem mục 4 (ADM-F-05 Feature toggles) và mục 5 (Acceptance Criteria: "Feature toggle ON → feature available; OFF → feature disabled") và mục 2.10 (FeatureToggleController)

Background

Test feature toggle: admin bật/tắt → effect đúng + cache invalidation.

Test scenarios

Setup note: Tạo test class FeatureToggleIntegrationTest trong admin/src/test/java/. Cần: - Một admin account có quyền gọi PUT /api/admin/feature-toggles/{key} (kiểm tra rule cần thiết qua RuleEnum) - Seed ít nhất một feature toggle record trong test DB trước mỗi test (@BeforeEach) - Nếu feature toggle dùng Redis cache, test cần có embedded Redis hoặc mock RedisTemplate

Toggle ON

  • [ ] PUT /api/admin/feature-toggles/{key} với enabled=true
  • [ ] Verify DB: feature_toggle.enabled = true

    Query DB trực tiếp qua repository trong test, hoặc gọi GET /api/admin/feature-toggles/{key} và verify response

  • [ ] Verify Redis cache invalidated (hoặc updated)

    Nếu dùng @Cacheable/@CacheEvict, verify bằng cách gọi toggle, rồi query lại — response phải reflect giá trị mới, không phải cached value cũ

  • [ ] Verify feature available trong request tiếp theo

Toggle OFF

  • [ ] PUT /api/admin/feature-toggles/{key} với enabled=false
  • [ ] Verify feature disabled

    Nếu feature toggle gate một API endpoint hoặc logic cụ thể, gọi API đó sau khi toggle OFF và verify hành vi disabled

  • [ ] Clients check feature → false

    Test bằng cách gọi GET trên feature toggle endpoint hoặc endpoint bị gate

Cache behavior

  • [ ] Toggle changed → cache invalidated ngay lập tức (không phải sau TTL)

    Test sequence: toggle → immediate GET → verify fresh value (không phải stale cache)

  • [ ] Multiple app instances: toggle change → tất cả instances thấy change (via Redis)

    Nếu không thể test multi-instance trong unit test, document rằng cần manual verification hoặc dùng test container với multiple app instances

Non-existent key

  • [ ] Toggle key không tồn tại → error FEATURE_TOGGLE_NOT_FOUND

    Verify HTTP 404 (hoặc 400 tùy implementation) với error code FEATURE_TOGGLE_NOT_FOUND trong response body

Verification / Acceptance Criteria

  • [ ] Toggle ON → DB persisted: Sau PUT với enabled=true, query DB trả về enabled = true
  • [ ] Toggle OFF → DB persisted: Sau PUT với enabled=false, query DB trả về enabled = false
  • [ ] Cache invalidation ngay lập tức: Sau toggle, gọi ngay API phụ thuộc vào feature toggle → nhận giá trị mới (không phải stale)
  • [ ] Non-existent key trả lỗi đúng: HTTP 404/400 với message FEATURE_TOGGLE_NOT_FOUND
  • [ ] Tất cả test scenarios pass: Chạy FeatureToggleIntegrationTest → 0 failures
  • [ ] Test class tham khảo: FeatureToggleIntegrationTest — đặt trong admin/src/test/java/.../controller/; kiểm tra cả DB state và cache behavior

Files to Modify

(Test-only task — không modify production code, chỉ tạo/cập nhật test files)

  • admin/src/test/java/.../controller/FeatureToggleIntegrationTest.java (new hoặc update)