202 lines
5.9 KiB
Dart
202 lines
5.9 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:votianlt_app/models/acknowledgment_message.dart';
|
|
|
|
void main() {
|
|
group('AcknowledgmentMessage', () {
|
|
group('fromJson', () {
|
|
test('parses all required fields correctly', () {
|
|
final json = {
|
|
'messageId': 'msg-123',
|
|
'status': 'RECEIVED',
|
|
'timestamp': '2024-01-15T10:30:00.000Z',
|
|
};
|
|
|
|
final ack = AcknowledgmentMessage.fromJson(json);
|
|
|
|
expect(ack.messageId, 'msg-123');
|
|
expect(ack.status, AcknowledgmentStatus.received);
|
|
expect(ack.timestamp, DateTime.utc(2024, 1, 15, 10, 30, 0));
|
|
expect(ack.errorMessage, isNull);
|
|
});
|
|
|
|
test('parses errorMessage when present', () {
|
|
final json = {
|
|
'messageId': 'msg-123',
|
|
'status': 'FAILED',
|
|
'timestamp': '2024-01-15T10:30:00.000Z',
|
|
'errorMessage': 'Connection timeout',
|
|
};
|
|
|
|
final ack = AcknowledgmentMessage.fromJson(json);
|
|
|
|
expect(ack.status, AcknowledgmentStatus.failed);
|
|
expect(ack.errorMessage, 'Connection timeout');
|
|
});
|
|
|
|
test('parses PROCESSED status', () {
|
|
final json = {
|
|
'messageId': 'msg-123',
|
|
'status': 'PROCESSED',
|
|
'timestamp': '2024-01-15T10:30:00.000Z',
|
|
};
|
|
|
|
final ack = AcknowledgmentMessage.fromJson(json);
|
|
|
|
expect(ack.status, AcknowledgmentStatus.processed);
|
|
});
|
|
});
|
|
|
|
group('toJson', () {
|
|
test('serializes all fields correctly', () {
|
|
final ack = AcknowledgmentMessage(
|
|
messageId: 'msg-123',
|
|
status: AcknowledgmentStatus.received,
|
|
timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0),
|
|
);
|
|
|
|
final json = ack.toJson();
|
|
|
|
expect(json['messageId'], 'msg-123');
|
|
expect(json['status'], 'RECEIVED');
|
|
expect(json['timestamp'], '2024-01-15T10:30:00.000Z');
|
|
expect(json.containsKey('errorMessage'), false);
|
|
});
|
|
|
|
test('includes errorMessage when present', () {
|
|
final ack = AcknowledgmentMessage(
|
|
messageId: 'msg-123',
|
|
status: AcknowledgmentStatus.failed,
|
|
timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0),
|
|
errorMessage: 'Processing error',
|
|
);
|
|
|
|
final json = ack.toJson();
|
|
|
|
expect(json['errorMessage'], 'Processing error');
|
|
});
|
|
});
|
|
|
|
group('fromJson/toJson roundtrip', () {
|
|
test('preserves all data through serialization', () {
|
|
final original = AcknowledgmentMessage(
|
|
messageId: 'roundtrip-msg',
|
|
status: AcknowledgmentStatus.processed,
|
|
timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0),
|
|
);
|
|
|
|
final json = original.toJson();
|
|
final restored = AcknowledgmentMessage.fromJson(json);
|
|
|
|
expect(restored.messageId, original.messageId);
|
|
expect(restored.status, original.status);
|
|
expect(restored.timestamp, original.timestamp);
|
|
expect(restored.errorMessage, original.errorMessage);
|
|
});
|
|
|
|
test('preserves errorMessage through serialization', () {
|
|
final original = AcknowledgmentMessage(
|
|
messageId: 'error-msg',
|
|
status: AcknowledgmentStatus.failed,
|
|
timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0),
|
|
errorMessage: 'Something went wrong',
|
|
);
|
|
|
|
final json = original.toJson();
|
|
final restored = AcknowledgmentMessage.fromJson(json);
|
|
|
|
expect(restored.errorMessage, 'Something went wrong');
|
|
});
|
|
});
|
|
|
|
group('toString', () {
|
|
test('returns readable representation', () {
|
|
final ack = AcknowledgmentMessage(
|
|
messageId: 'msg-123',
|
|
status: AcknowledgmentStatus.received,
|
|
timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0),
|
|
);
|
|
|
|
final str = ack.toString();
|
|
|
|
expect(str, contains('msg-123'));
|
|
expect(str, contains('RECEIVED'));
|
|
});
|
|
});
|
|
});
|
|
|
|
group('AcknowledgmentStatus', () {
|
|
group('fromString', () {
|
|
test('parses RECEIVED', () {
|
|
expect(
|
|
AcknowledgmentStatus.fromString('RECEIVED'),
|
|
AcknowledgmentStatus.received,
|
|
);
|
|
});
|
|
|
|
test('parses PROCESSED', () {
|
|
expect(
|
|
AcknowledgmentStatus.fromString('PROCESSED'),
|
|
AcknowledgmentStatus.processed,
|
|
);
|
|
});
|
|
|
|
test('parses FAILED', () {
|
|
expect(
|
|
AcknowledgmentStatus.fromString('FAILED'),
|
|
AcknowledgmentStatus.failed,
|
|
);
|
|
});
|
|
|
|
test('handles lowercase input', () {
|
|
expect(
|
|
AcknowledgmentStatus.fromString('received'),
|
|
AcknowledgmentStatus.received,
|
|
);
|
|
expect(
|
|
AcknowledgmentStatus.fromString('processed'),
|
|
AcknowledgmentStatus.processed,
|
|
);
|
|
expect(
|
|
AcknowledgmentStatus.fromString('failed'),
|
|
AcknowledgmentStatus.failed,
|
|
);
|
|
});
|
|
|
|
test('defaults to received for unknown values', () {
|
|
expect(
|
|
AcknowledgmentStatus.fromString('UNKNOWN'),
|
|
AcknowledgmentStatus.received,
|
|
);
|
|
expect(
|
|
AcknowledgmentStatus.fromString(''),
|
|
AcknowledgmentStatus.received,
|
|
);
|
|
});
|
|
});
|
|
|
|
group('toString', () {
|
|
test('returns RECEIVED for received', () {
|
|
expect(AcknowledgmentStatus.received.toString(), 'RECEIVED');
|
|
});
|
|
|
|
test('returns PROCESSED for processed', () {
|
|
expect(AcknowledgmentStatus.processed.toString(), 'PROCESSED');
|
|
});
|
|
|
|
test('returns FAILED for failed', () {
|
|
expect(AcknowledgmentStatus.failed.toString(), 'FAILED');
|
|
});
|
|
});
|
|
|
|
group('fromString/toString roundtrip', () {
|
|
test('preserves status through conversion', () {
|
|
for (final status in AcknowledgmentStatus.values) {
|
|
final str = status.toString();
|
|
final restored = AcknowledgmentStatus.fromString(str);
|
|
expect(restored, status);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|