TF-1204 Add FirebaseRegistration/set method/response and write unit test
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import 'package:fcm/model/firebase_registration.dart';
|
||||
import 'package:fcm/utils/firebase_utils.dart';
|
||||
import 'package:jmap_dart_client/http/converter/id_converter.dart';
|
||||
import 'package:jmap_dart_client/http/converter/set/set_method_properties_converter.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 FirebaseRegistrationSetMethod extends SetMethodNoNeedAccountId<FirebaseRegistration> {
|
||||
|
||||
FirebaseRegistrationSetMethod() : super();
|
||||
|
||||
@override
|
||||
MethodName get methodName => MethodName('FirebaseRegistration/set');
|
||||
|
||||
@override
|
||||
Set<CapabilityIdentifier> get requiredCapabilities => {
|
||||
CapabilityIdentifier.jmapCore,
|
||||
FirebaseUtils.capabilityLinagoraFirebase,
|
||||
};
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
final val = <String, dynamic>{};
|
||||
|
||||
void writeNotNull(String key, dynamic value) {
|
||||
if (value != null) {
|
||||
val[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
writeNotNull('create', create
|
||||
?.map((id, create) => SetMethodPropertiesConverter().fromMapIdToJson(id, create.toJson())));
|
||||
writeNotNull('update', update
|
||||
?.map((id, update) => SetMethodPropertiesConverter().fromMapIdToJson(id, update.toJson())));
|
||||
writeNotNull('destroy', destroy
|
||||
?.map((destroyId) => const IdConverter().toJson(destroyId)).toList());
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [create, update, destroy];
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import 'package:fcm/model/firebase_registration.dart';
|
||||
import 'package:jmap_dart_client/http/converter/id_converter.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';
|
||||
|
||||
class FirebaseRegistrationSetResponse extends SetResponseNoAccount<FirebaseRegistration> {
|
||||
FirebaseRegistrationSetResponse({
|
||||
Map<Id, FirebaseRegistration>? created,
|
||||
Map<Id, FirebaseRegistration?>? updated,
|
||||
Set<Id>? destroyed,
|
||||
Map<Id, SetError>? notCreated,
|
||||
Map<Id, SetError>? notUpdated,
|
||||
Map<Id, SetError>? notDestroyed
|
||||
}) : super(
|
||||
created: created,
|
||||
updated: updated,
|
||||
destroyed: destroyed,
|
||||
notCreated: notCreated,
|
||||
notUpdated: notUpdated,
|
||||
notDestroyed: notDestroyed
|
||||
);
|
||||
|
||||
static FirebaseRegistrationSetResponse deserialize(Map<String, dynamic> json) {
|
||||
return FirebaseRegistrationSetResponse(
|
||||
created: (json['created'] as Map<String, dynamic>?)?.map((key, value) => MapEntry(
|
||||
const IdConverter().fromJson(key),
|
||||
FirebaseRegistration.fromJson(value as Map<String, dynamic>)
|
||||
)),
|
||||
updated: (json['updated'] as Map<String, dynamic>?)?.map((key, value) => MapEntry(
|
||||
const IdConverter().fromJson(key),
|
||||
value != null ? FirebaseRegistration.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 => [
|
||||
created,
|
||||
updated,
|
||||
destroyed,
|
||||
notCreated,
|
||||
notUpdated,
|
||||
notDestroyed
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:fcm/method/set/firebase_registration_set_method.dart';
|
||||
import 'package:fcm/method/set/firebase_registration_set_response.dart';
|
||||
import 'package:fcm/model/device_client_id.dart';
|
||||
import 'package:fcm/model/firebase_expired_time.dart';
|
||||
import 'package:fcm/model/firebase_registration.dart';
|
||||
import 'package:fcm/model/firebase_registration_id.dart';
|
||||
import 'package:fcm/model/firebase_token.dart';
|
||||
import 'package:fcm/model/type_name.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/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/utc_date.dart';
|
||||
import 'package:jmap_dart_client/jmap/jmap_request.dart';
|
||||
|
||||
void main() {
|
||||
group('test to json firebase registration set method', () {
|
||||
final expectedFirebaseRegistrationCreated = FirebaseRegistration(
|
||||
id: FirebaseRegistrationId(Id('175dbd70-93d1-11ec-984e-e3f8b83572b4')),
|
||||
expires: FirebaseExpiredTime(UTCDate(DateTime.parse('2022-03-31T02:14:29Z'))),
|
||||
);
|
||||
|
||||
test('firebase registration set method and response parsing', () async {
|
||||
final baseOption = BaseOptions(method: 'POST');
|
||||
final dio = Dio(baseOption)
|
||||
..options.baseUrl = 'http://domain.com/jmap';
|
||||
final dioAdapter = DioAdapter(dio: dio);
|
||||
dioAdapter.onPost(
|
||||
'',
|
||||
(server) => server.reply(200, {
|
||||
"sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
|
||||
"methodResponses": [
|
||||
[
|
||||
"FirebaseRegistration/set",
|
||||
{
|
||||
"created": {
|
||||
"dab246": {
|
||||
"id": "175dbd70-93d1-11ec-984e-e3f8b83572b4",
|
||||
"expires": "2022-03-31T02:14:29Z"
|
||||
}
|
||||
}
|
||||
},
|
||||
"c0"
|
||||
]
|
||||
]
|
||||
}),
|
||||
data: {
|
||||
"using": [
|
||||
"urn:ietf:params:jmap:core",
|
||||
"com:linagora:params:jmap:firebase:push"
|
||||
],
|
||||
"methodCalls": [
|
||||
[
|
||||
"FirebaseRegistration/set",
|
||||
{
|
||||
"create": {
|
||||
"dab246": {
|
||||
"token": "token1",
|
||||
"deviceClientId": "a123-b123-c123",
|
||||
"types": ["Mailbox"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"c0"
|
||||
]
|
||||
]
|
||||
},
|
||||
headers: {
|
||||
"accept": "application/json;jmapVersion=rfc-8621",
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
"content-length": 225
|
||||
}
|
||||
);
|
||||
|
||||
final createRequestId = Id('dab246');
|
||||
|
||||
final firebaseRegistrationSetMethod = FirebaseRegistrationSetMethod()
|
||||
..addCreate(
|
||||
createRequestId,
|
||||
FirebaseRegistration(
|
||||
deviceClientId: DeviceClientId('a123-b123-c123'),
|
||||
token: FirebaseToken('token1'),
|
||||
types: [TypeName.mailboxType]
|
||||
)
|
||||
);
|
||||
|
||||
final httpClient = HttpClient(dio);
|
||||
final requestBuilder = JmapRequestBuilder(httpClient, ProcessingInvocation());
|
||||
final firebaseRegistrationSetInvocation = requestBuilder.invocation(firebaseRegistrationSetMethod);
|
||||
final response = await (requestBuilder
|
||||
..usings(firebaseRegistrationSetMethod.requiredCapabilities))
|
||||
.build()
|
||||
.execute();
|
||||
|
||||
final firebaseRegistrationSetResponse = response.parse<FirebaseRegistrationSetResponse>(
|
||||
firebaseRegistrationSetInvocation.methodCallId,
|
||||
FirebaseRegistrationSetResponse.deserialize);
|
||||
|
||||
expect(
|
||||
firebaseRegistrationSetResponse!.created![createRequestId]!.id,
|
||||
equals(expectedFirebaseRegistrationCreated.id)
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user