TF-832 Add Set method for forward and unit test
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
import 'package:forward/forward/capability_forward.dart';
|
||||||
|
import 'package:forward/forward/tmail_forward.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/account_id_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/set/set_method_properties_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/method/request/set_method.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart';
|
||||||
|
|
||||||
|
class SetForwardMethod extends SetMethod<TMailForward> with OptionalUpdateSingleton<TMailForward>{
|
||||||
|
SetForwardMethod(AccountId accountId) : super(accountId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
MethodName get methodName => MethodName('Forward/set');
|
||||||
|
|
||||||
|
@override
|
||||||
|
Set<CapabilityIdentifier> get requiredCapabilities => {
|
||||||
|
CapabilityIdentifier.jmapCore,
|
||||||
|
capabilityForward,
|
||||||
|
};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
final val = <String, dynamic>{
|
||||||
|
'accountId': const AccountIdConverter().toJson(accountId),
|
||||||
|
};
|
||||||
|
|
||||||
|
void writeNotNull(String key, dynamic value) {
|
||||||
|
if (value != null) {
|
||||||
|
val[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeNotNull('ifInState', ifInState?.value);
|
||||||
|
writeNotNull('update', updateSingleton
|
||||||
|
?.map((id, update) => SetMethodPropertiesConverter().fromMapIdToJson(id, update.toJson())));
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [accountId, ifInState, update];
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import 'package:forward/forward/tmail_forward.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/account_id_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/id_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/state_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/http/converter/state_nullable_converter.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/method/response/set_response.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||||
|
|
||||||
|
class SetForwardResponse extends SetResponse<TMailForward> {
|
||||||
|
|
||||||
|
SetForwardResponse(
|
||||||
|
AccountId accountId,
|
||||||
|
State newState, {
|
||||||
|
State? oldState,
|
||||||
|
Map<Id, TMailForward>? created,
|
||||||
|
Map<Id, TMailForward?>? updated,
|
||||||
|
Set<Id>? destroyed,
|
||||||
|
Map<Id, SetError>? notCreated,
|
||||||
|
Map<Id, SetError>? notUpdated,
|
||||||
|
Map<Id, SetError>? notDestroyed
|
||||||
|
}) : super(
|
||||||
|
accountId,
|
||||||
|
newState,
|
||||||
|
oldState: oldState,
|
||||||
|
created: created,
|
||||||
|
updated: updated,
|
||||||
|
destroyed: destroyed,
|
||||||
|
notCreated: notCreated,
|
||||||
|
notUpdated: notUpdated,
|
||||||
|
notDestroyed: notDestroyed
|
||||||
|
);
|
||||||
|
|
||||||
|
static SetForwardResponse deserialize(Map<String, dynamic> json) {
|
||||||
|
return SetForwardResponse(
|
||||||
|
const AccountIdConverter().fromJson(json['accountId'] as String),
|
||||||
|
const StateConverter().fromJson(json['newState'] as String),
|
||||||
|
oldState: const StateNullableConverter().fromJson(json['oldState'] as String?),
|
||||||
|
created: (json['created'] as Map<String, dynamic>?)
|
||||||
|
?.map((key, value) => MapEntry(
|
||||||
|
const IdConverter().fromJson(key),
|
||||||
|
TMailForward.fromJson(value as Map<String, dynamic>))),
|
||||||
|
updated: (json['updated'] as Map<String, dynamic>?)
|
||||||
|
?.map((key, value) => MapEntry(
|
||||||
|
const IdConverter().fromJson(key),
|
||||||
|
value != null ? TMailForward.fromJson(value as Map<String, dynamic>) : null)),
|
||||||
|
destroyed: (json['destroyed'] as List<dynamic>?)
|
||||||
|
?.map((id) => const IdConverter().fromJson(id)).toSet(),
|
||||||
|
notCreated: (json['notCreated'] as Map<String, dynamic>?)
|
||||||
|
?.map((key, value) => MapEntry(
|
||||||
|
const IdConverter().fromJson(key),
|
||||||
|
SetError.fromJson(value))),
|
||||||
|
notUpdated: (json['notUpdated'] as Map<String, dynamic>?)
|
||||||
|
?.map((key, value) => MapEntry(
|
||||||
|
const IdConverter().fromJson(key),
|
||||||
|
SetError.fromJson(value))),
|
||||||
|
notDestroyed: (json['notDestroyed'] as Map<String, dynamic>?)
|
||||||
|
?.map((key, value) => MapEntry(
|
||||||
|
const IdConverter().fromJson(key),
|
||||||
|
SetError.fromJson(value))),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
oldState,
|
||||||
|
newState,
|
||||||
|
created,
|
||||||
|
updated,
|
||||||
|
destroyed,
|
||||||
|
notCreated,
|
||||||
|
notUpdated,
|
||||||
|
notDestroyed
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -22,6 +22,17 @@ class TMailForward extends Forward {
|
|||||||
|
|
||||||
Map<String, dynamic> toJson() => _$TMailForwardToJson(this);
|
Map<String, dynamic> toJson() => _$TMailForwardToJson(this);
|
||||||
|
|
||||||
|
TMailForward copyWith({
|
||||||
|
ForwardId? id,
|
||||||
|
bool? localCopy,
|
||||||
|
Set<String>? forwards,
|
||||||
|
}) {
|
||||||
|
return TMailForward(
|
||||||
|
id: id ?? this.id,
|
||||||
|
localCopy: localCopy ?? this.localCopy,
|
||||||
|
forwards: forwards ?? this.forwards,
|
||||||
|
);
|
||||||
|
}
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [
|
List<Object?> get props => [
|
||||||
id,
|
id,
|
||||||
|
|||||||
@@ -0,0 +1,139 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:forward/forward/forward_id.dart';
|
||||||
|
import 'package:forward/forward/get/get_forward_method.dart';
|
||||||
|
import 'package:forward/forward/get/get_forward_response.dart';
|
||||||
|
import 'package:forward/forward/set/set_forward_method.dart';
|
||||||
|
import 'package:forward/forward/tmail_forward.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/jmap_request.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('test to json set forward method', () {
|
||||||
|
final expectedUpdated = TMailForward(
|
||||||
|
id: ForwardIdSingleton.forwardIdSingleton,
|
||||||
|
localCopy: true,
|
||||||
|
forwards: {'dab@domain.com', 'vuda@gmail.com'}
|
||||||
|
);
|
||||||
|
|
||||||
|
test('set forward method and response parsing', () 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": [
|
||||||
|
[
|
||||||
|
"Forward/set",
|
||||||
|
{
|
||||||
|
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
|
||||||
|
"newState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
|
||||||
|
"updated": {
|
||||||
|
"singleton": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"c0"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Forward/get",
|
||||||
|
{
|
||||||
|
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
|
||||||
|
"notFound": [],
|
||||||
|
"state": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"id": "singleton",
|
||||||
|
"localCopy": true,
|
||||||
|
"forwards": [
|
||||||
|
"dab@domain.com",
|
||||||
|
"vuda@gmail.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"c1"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
data: {
|
||||||
|
"using": [
|
||||||
|
"urn:ietf:params:jmap:core",
|
||||||
|
"com:linagora:params:jmap:forward"
|
||||||
|
],
|
||||||
|
"methodCalls": [
|
||||||
|
[
|
||||||
|
"Forward/set",
|
||||||
|
{
|
||||||
|
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
|
||||||
|
"update": {
|
||||||
|
"singleton": {
|
||||||
|
"id": "singleton",
|
||||||
|
"localCopy": true,
|
||||||
|
"forwards": [
|
||||||
|
"dab@domain.com",
|
||||||
|
"vuda@gmail.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"c0"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Forward/get",
|
||||||
|
{
|
||||||
|
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
|
||||||
|
"ids": [
|
||||||
|
"singleton"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"c1"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
"accept": "application/json;jmapVersion=rfc-8621",
|
||||||
|
"content-type": "application/json; charset=utf-8",
|
||||||
|
"content-length": 420
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
final accountId = AccountId(Id('0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555'));
|
||||||
|
final httpClient = HttpClient(dio);
|
||||||
|
final processingInvocation = ProcessingInvocation();
|
||||||
|
|
||||||
|
final setForwardMethod = SetForwardMethod(accountId)
|
||||||
|
..addUpdatesSingleton({
|
||||||
|
ForwardIdSingleton.forwardIdSingleton.id : TMailForward(
|
||||||
|
id: ForwardIdSingleton.forwardIdSingleton,
|
||||||
|
localCopy: true,
|
||||||
|
forwards: {'dab@domain.com', 'vuda@gmail.com'}
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
final requestBuilder = JmapRequestBuilder(httpClient, processingInvocation)
|
||||||
|
..invocation(setForwardMethod);
|
||||||
|
|
||||||
|
final getForwardMethod = GetForwardMethod(accountId)
|
||||||
|
..addIds({ForwardIdSingleton.forwardIdSingleton.id});
|
||||||
|
final getForwardInvocation = requestBuilder.invocation(getForwardMethod);
|
||||||
|
|
||||||
|
final response = await (requestBuilder
|
||||||
|
..usings(setForwardMethod.requiredCapabilities))
|
||||||
|
.build()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
final getForwardResponse = response.parse<GetForwardResponse>(
|
||||||
|
getForwardInvocation.methodCallId,
|
||||||
|
GetForwardResponse.deserialize);
|
||||||
|
|
||||||
|
expect(getForwardResponse!.list.length, equals(1));
|
||||||
|
expect(getForwardResponse.list, containsAll({expectedUpdated}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user