From e64a81a0d90e5a0334bdd8805f6d5ef9778eb205 Mon Sep 17 00:00:00 2001 From: ManhNT Date: Tue, 14 Jun 2022 13:15:42 +0700 Subject: [PATCH 1/2] TF-626: add explicitToJson in GetMailboxMethod --- lib/jmap/mail/mailbox/get/get_mailbox_method.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jmap/mail/mailbox/get/get_mailbox_method.dart b/lib/jmap/mail/mailbox/get/get_mailbox_method.dart index fa8fd7f..e147006 100644 --- a/lib/jmap/mail/mailbox/get/get_mailbox_method.dart +++ b/lib/jmap/mail/mailbox/get/get_mailbox_method.dart @@ -13,7 +13,7 @@ part 'get_mailbox_method.g.dart'; @IdConverter() @AccountIdConverter() @PropertiesConverter() -@JsonSerializable() +@JsonSerializable(explicitToJson: true) class GetMailboxMethod extends GetMethod { GetMailboxMethod(AccountId accountId) : super(accountId); From a8b8f871074a684c8f358b4de699fad35a0ee280 Mon Sep 17 00:00:00 2001 From: ManhNT Date: Tue, 14 Jun 2022 13:24:37 +0700 Subject: [PATCH 2/2] TF-626: create unit test for test case get changes mailbox --- .../mailbox/get/get_mailbox_method.g.dart | 36 +-- .../changes/get_changes_mailbox_test.dart | 210 ++++++++++++++++++ 2 files changed, 228 insertions(+), 18 deletions(-) create mode 100644 test/jmap/mailbox/changes/get_changes_mailbox_test.dart diff --git a/lib/jmap/mail/mailbox/get/get_mailbox_method.g.dart b/lib/jmap/mail/mailbox/get/get_mailbox_method.g.dart index 67c577d..a608027 100644 --- a/lib/jmap/mail/mailbox/get/get_mailbox_method.g.dart +++ b/lib/jmap/mail/mailbox/get/get_mailbox_method.g.dart @@ -6,22 +6,22 @@ part of 'get_mailbox_method.dart'; // JsonSerializableGenerator // ************************************************************************** -GetMailboxMethod _$GetMailboxMethodFromJson(Map json) { - return GetMailboxMethod( - const AccountIdConverter().fromJson(json['accountId'] as String), - ) - ..ids = (json['ids'] as List?) - ?.map((e) => const IdConverter().fromJson(e as String)) - .toSet() - ..referenceIds = json['#ids'] == null - ? null - : ResultReference.fromJson(json['#ids'] as Map) - ..properties = const PropertiesConverter() - .fromJson(json['properties'] as List?) - ..referenceProperties = json['#properties'] == null - ? null - : ResultReference.fromJson(json['#properties'] as Map); -} +GetMailboxMethod _$GetMailboxMethodFromJson(Map json) => + GetMailboxMethod( + const AccountIdConverter().fromJson(json['accountId'] as String), + ) + ..ids = (json['ids'] as List?) + ?.map((e) => const IdConverter().fromJson(e as String)) + .toSet() + ..referenceIds = json['#ids'] == null + ? null + : ResultReference.fromJson(json['#ids'] as Map) + ..properties = const PropertiesConverter() + .fromJson(json['properties'] as List?) + ..referenceProperties = json['#properties'] == null + ? null + : ResultReference.fromJson( + json['#properties'] as Map); Map _$GetMailboxMethodToJson(GetMailboxMethod instance) { final val = { @@ -35,9 +35,9 @@ Map _$GetMailboxMethodToJson(GetMailboxMethod instance) { } writeNotNull('ids', instance.ids?.map(const IdConverter().toJson).toList()); - writeNotNull('#ids', instance.referenceIds); + writeNotNull('#ids', instance.referenceIds?.toJson()); writeNotNull( 'properties', const PropertiesConverter().toJson(instance.properties)); - writeNotNull('#properties', instance.referenceProperties); + writeNotNull('#properties', instance.referenceProperties?.toJson()); return val; } diff --git a/test/jmap/mailbox/changes/get_changes_mailbox_test.dart b/test/jmap/mailbox/changes/get_changes_mailbox_test.dart new file mode 100644 index 0000000..42e565c --- /dev/null +++ b/test/jmap/mailbox/changes/get_changes_mailbox_test.dart @@ -0,0 +1,210 @@ +import 'package:dio/dio.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:http_mock_adapter/http_mock_adapter.dart'; +import 'package:jmap_dart_client/http/http_client.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/id.dart'; +import 'package:jmap_dart_client/jmap/core/request/reference_path.dart'; +import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart'; +import 'package:jmap_dart_client/jmap/core/state.dart'; +import 'package:jmap_dart_client/jmap/core/unsigned_int.dart'; +import 'package:jmap_dart_client/jmap/jmap_request.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/changes/changes_mailbox_method.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_method.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; +import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox_rights.dart'; + +void main() { + group('[MailBox/changes]', () { + final expectedUpdated = Mailbox( + id: MailboxId(Id('c2543650-32cf-11eb-995c-a3ae66e9f96a')), + role: Role('inbox'), + name: MailboxName('INBOX'), + sortOrder: SortOrder(sortValue: 10), + totalEmails: TotalEmails(UnsignedInt(4079)), + unreadEmails: UnreadEmails(UnsignedInt(10)), + totalThreads: TotalThreads(UnsignedInt(4079)), + unreadThreads: UnreadThreads(UnsignedInt(10)), + myRights: MailboxRights( + true, + true, + true, + true, + true, + true, + true, + true, + true, + ), + isSubscribed: IsSubscribed(true), + ); + + test('get changes email', () async { + final baseOption = BaseOptions(method: 'POST'); + final dio = Dio(baseOption)..options.baseUrl = 'http://domain.com'; + final dioAdapter = DioAdapter(dio: dio); + dioAdapter.onPost( + '/jmap', + (server) => server.reply(200, { + "sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943", + "methodResponses": [ + [ + "Mailbox/changes", + { + "accountId": + "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "oldState": "94bbff21-e87c-11ec-aae4-43ebf0340ebd", + "newState": "4a523070-eb89-11ec-aae4-43ebf0340ebd", + "hasMoreChanges": false, + "updatedProperties": null, + "created": [], + "updated": ["c2543650-32cf-11eb-995c-a3ae66e9f96a"], + "destroyed": ["7dc7cb50-3f63-11eb-bf20-f1a8da6866c8"] + }, + "c0" + ], + [ + "Mailbox/get", + { + "accountId": + "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "notFound": [], + "state": "4a523070-eb89-11ec-aae4-43ebf0340ebd", + "list": [ + { + "id": "c2543650-32cf-11eb-995c-a3ae66e9f96a", + "name": "INBOX", + "role": "inbox", + "sortOrder": 10, + "totalEmails": 4079, + "unreadEmails": 10, + "totalThreads": 4079, + "unreadThreads": 10, + "myRights": { + "mayReadItems": true, + "mayAddItems": true, + "mayRemoveItems": true, + "maySetSeen": true, + "maySetKeywords": true, + "mayCreateChild": true, + "mayRename": true, + "mayDelete": true, + "maySubmit": true + }, + "isSubscribed": true + } + ] + }, + "c1" + ], + [ + "Mailbox/get", + { + "accountId": + "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "notFound": ["7dc7cb50-3f63-11eb-bf20-f1a8da6866c8"], + "state": "4a523070-eb89-11ec-aae4-43ebf0340ebd", + "list": [] + }, + "c2" + ] + ] + }), + data: { + "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], + "methodCalls": [ + [ + "Mailbox/changes", + { + "accountId": + "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "sinceState": "94bbff21-e87c-11ec-aae4-43ebf0340ebd" + }, + "c0" + ], + [ + "Mailbox/get", + { + "accountId": + "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "#ids": { + "resultOf": "c0", + "name": "Mailbox/changes", + "path": "updated/*" + } + }, + "c1" + ], + [ + "Mailbox/get", + { + "accountId": "0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb", + "#ids": { + "resultOf": "c0", + "name": "Mailbox/changes", + "path": "destroyed/*" + } + }, + "c2" + ] + ] + }, + headers: { + "accept": "application/json;jmapVersion=rfc-8621", + "content-type": "application/json; charset=utf-8", + "content-length": 585 + }); + + final HttpClient httpClient = HttpClient(dio); + final processingInvocation = ProcessingInvocation(); + final jmapRequestBuilder = + JmapRequestBuilder(httpClient, processingInvocation); + final accountId = AccountId(Id( + '0eacc7a5c74b27ab36a823bc5c34da36e16c093705f241d6ed5f48ee73a4ecfb')); + final state = State('94bbff21-e87c-11ec-aae4-43ebf0340ebd'); + + final changesMailboxMethod = ChangesMailboxMethod(accountId, state); + final changesMailboxInvocation = jmapRequestBuilder.invocation( + changesMailboxMethod, + methodCallId: MethodCallId('c0'), + ); + + final getMailboxMethodForUpdate = GetMailboxMethod(accountId) + ..addReferenceIds(processingInvocation.createResultReference( + changesMailboxInvocation.methodCallId, + ReferencePath.updatedPath, + )); + + final getMailboxForUpdateInvocation = jmapRequestBuilder.invocation( + getMailboxMethodForUpdate, + methodCallId: MethodCallId('c1')); + + final getMailboxMethodForDestroyed = GetMailboxMethod(accountId) + ..addReferenceIds(processingInvocation.createResultReference( + changesMailboxInvocation.methodCallId, + ReferencePath('destroyed/*'), + )); + final getMailboxForDestroyInvocation = jmapRequestBuilder.invocation( + getMailboxMethodForDestroyed, + methodCallId: MethodCallId('c2'), + ); + + final result = await (jmapRequestBuilder + ..usings(getMailboxMethodForUpdate.requiredCapabilities)) + .build() + .execute(); + + final resultUpdated = result.parse( + getMailboxForUpdateInvocation.methodCallId, + GetMailboxResponse.deserialize); + + final resultDestroyed = result.parse( + getMailboxForDestroyInvocation.methodCallId, + GetMailboxResponse.deserialize); + + expect(resultUpdated!.list[0], equals(expectedUpdated)); + expect(resultDestroyed!.list.length, equals(0)); + }); + }); +}