import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; import 'package:mockito/annotations.dart'; import 'package:dartz/dartz.dart'; import 'package:superport/data/models/license/license_dto.dart'; import 'package:superport/data/repositories/license_repository.dart'; import 'package:superport/domain/usecases/base_usecase.dart'; import 'package:superport/domain/usecases/license/create_license_usecase.dart'; import 'package:superport/core/errors/failures.dart'; import 'create_license_usecase_test.mocks.dart'; @GenerateMocks([LicenseRepository]) void main() { late CreateLicenseUseCase useCase; late MockLicenseRepository mockRepository; setUp(() { mockRepository = MockLicenseRepository(); useCase = CreateLicenseUseCase(mockRepository); }); group('CreateLicenseUseCase', () { final validParams = CreateLicenseParams( equipmentId: 1, companyId: 1, licenseType: 'maintenance', startDate: DateTime(2025, 1, 1), expiryDate: DateTime(2025, 12, 31), description: 'Test license', cost: 1000.0, ); final mockLicense = LicenseDto( id: 1, licenseKey: 'TEST-LICENSE-KEY', productName: 'Test Product', vendor: 'Test Vendor', licenseType: 'maintenance', userCount: 10, purchaseDate: DateTime(2025, 1, 1), expiryDate: DateTime(2025, 12, 31), purchasePrice: 1000.0, companyId: 1, branchId: 1, assignedUserId: 1, remark: 'Test license', isActive: true, createdAt: DateTime.now(), updatedAt: DateTime.now(), ); test('라이선스 생성 성공', () async { // arrange when(mockRepository.createLicense(any)) .thenAnswer((_) async => mockLicense); // act final result = await useCase(validParams); // assert expect(result.isRight(), true); result.fold( (failure) => fail('Should not return failure'), (license) => expect(license, equals(mockLicense)), ); verify(mockRepository.createLicense(validParams.toMap())).called(1); }); test('만료일이 시작일보다 이전인 경우 검증 실패', () async { // arrange final invalidParams = CreateLicenseParams( equipmentId: 1, companyId: 1, licenseType: 'maintenance', startDate: DateTime(2025, 12, 31), expiryDate: DateTime(2025, 1, 1), // 시작일보다 이전 description: 'Test license', cost: 1000.0, ); // act final result = await useCase(invalidParams); // assert expect(result.isLeft(), true); result.fold( (failure) { expect(failure, isA()); expect(failure.message, contains('만료일은 시작일 이후여야 합니다')); }, (license) => fail('Should not return license'), ); verifyNever(mockRepository.createLicense(any)); }); test('라이선스 기간이 30일 미만인 경우 검증 실패', () async { // arrange final invalidParams = CreateLicenseParams( equipmentId: 1, companyId: 1, licenseType: 'maintenance', startDate: DateTime(2025, 1, 1), expiryDate: DateTime(2025, 1, 15), // 15일 기간 description: 'Test license', cost: 1000.0, ); // act final result = await useCase(invalidParams); // assert expect(result.isLeft(), true); result.fold( (failure) { expect(failure, isA()); expect(failure.message, contains('라이선스 기간은 최소 30일 이상이어야 합니다')); }, (license) => fail('Should not return license'), ); verifyNever(mockRepository.createLicense(any)); }); test('Repository에서 예외 발생 시 ServerFailure 반환', () async { // arrange when(mockRepository.createLicense(any)) .thenThrow(Exception('Server error')); // act final result = await useCase(validParams); // assert expect(result.isLeft(), true); result.fold( (failure) { expect(failure, isA()); expect(failure.message, contains('Server error')); }, (license) => fail('Should not return license'), ); verify(mockRepository.createLicense(validParams.toMap())).called(1); }); test('파라미터를 올바른 Map으로 변환', () { // arrange final params = CreateLicenseParams( equipmentId: 1, companyId: 2, licenseType: 'maintenance', startDate: DateTime(2025, 1, 1), expiryDate: DateTime(2025, 12, 31), description: 'Test description', cost: 5000.0, ); // act final map = params.toMap(); // assert expect(map['equipment_id'], equals(1)); expect(map['company_id'], equals(2)); expect(map['license_type'], equals('maintenance')); expect(map['start_date'], equals(DateTime(2025, 1, 1).toIso8601String())); expect(map['expiry_date'], equals(DateTime(2025, 12, 31).toIso8601String())); expect(map['description'], equals('Test description')); expect(map['cost'], equals(5000.0)); }); test('옵셔널 파라미터가 null인 경우에도 정상 처리', () async { // arrange final paramsWithNulls = CreateLicenseParams( equipmentId: 1, companyId: 1, licenseType: 'maintenance', startDate: DateTime(2025, 1, 1), expiryDate: DateTime(2025, 12, 31), description: null, cost: null, ); when(mockRepository.createLicense(any)) .thenAnswer((_) async => mockLicense); // act final result = await useCase(paramsWithNulls); // assert expect(result.isRight(), true); final map = paramsWithNulls.toMap(); expect(map['description'], isNull); expect(map['cost'], isNull); }); }); }