Skip to content

Commit a84adb3

Browse files
ManhNTManhNT
authored andcommitted
create query operator and create query operator email test
1 parent 987ed3d commit a84adb3

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
2+
import 'package:json_annotation/json_annotation.dart';
3+
4+
class FilterConverter implements JsonConverter<Filter, Map<String, dynamic>> {
5+
const FilterConverter();
6+
7+
@override
8+
Filter fromJson(Map<String, dynamic> json) {
9+
throw UnimplementedError();
10+
}
11+
12+
@override
13+
Map<String, dynamic> toJson(Filter object) {
14+
return object.toJson();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:jmap_dart_client/jmap/core/filter/filter_operator.dart';
2+
import 'package:json_annotation/json_annotation.dart';
3+
4+
class FilterOperatorConverter implements JsonConverter<Operator, String> {
5+
const FilterOperatorConverter();
6+
7+
@override
8+
Operator fromJson(String json) {
9+
return Operator.values.firstWhere((e) => e.name == json);
10+
}
11+
12+
@override
13+
String toJson(Operator object) {
14+
return object.name;
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:jmap_dart_client/http/converter/filter_conveter.dart';
2+
import 'package:jmap_dart_client/http/converter/filter_operator_converter.dart';
3+
import 'package:jmap_dart_client/jmap/core/filter/filter_operator.dart';
4+
import 'package:json_annotation/json_annotation.dart';
5+
6+
part 'logic_filter_operator.g.dart';
7+
8+
@FilterConverter()
9+
@FilterOperatorConverter()
10+
@JsonSerializable(explicitToJson: true)
11+
class LogicFilterOperator extends FilterOperator {
12+
LogicFilterOperator(Operator operator, conditions)
13+
: super(operator,conditions);
14+
15+
@override
16+
List<Object?> get props => [
17+
operator,
18+
conditions,
19+
];
20+
21+
@override
22+
Map<String, dynamic> toJson() => _$LogicFilterOperatorToJson(this);
23+
24+
factory LogicFilterOperator.fromJson(Map<String, dynamic> json) =>
25+
_$LogicFilterOperatorFromJson(json);
26+
}

lib/jmap/core/filter/operator/logic_filter_operator.g.dart

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:http_mock_adapter/http_mock_adapter.dart';
4+
import 'package:jmap_dart_client/http/http_client.dart';
5+
import 'package:jmap_dart_client/jmap/account_id.dart';
6+
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
7+
import 'package:jmap_dart_client/jmap/core/filter/filter_operator.dart';
8+
import 'package:jmap_dart_client/jmap/core/filter/operator/logic_filter_operator.dart';
9+
import 'package:jmap_dart_client/jmap/core/id.dart';
10+
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
11+
import 'package:jmap_dart_client/jmap/core/request/reference_path.dart';
12+
import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart';
13+
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
14+
import 'package:jmap_dart_client/jmap/core/utc_date.dart';
15+
import 'package:jmap_dart_client/jmap/jmap_request.dart';
16+
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
17+
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
18+
import 'package:jmap_dart_client/jmap/mail/email/email_comparator.dart';
19+
import 'package:jmap_dart_client/jmap/mail/email/email_comparator_property.dart';
20+
import 'package:jmap_dart_client/jmap/mail/email/email_filter_condition.dart';
21+
import 'package:jmap_dart_client/jmap/mail/email/get/get_email_method.dart';
22+
import 'package:jmap_dart_client/jmap/mail/email/get/get_email_response.dart';
23+
import 'package:jmap_dart_client/jmap/mail/email/query/query_email_method.dart';
24+
25+
void main() {
26+
group('get list email test', () {
27+
final expectMail1 = Email(
28+
EmailId(Id("382312d0-fa5c-11eb-b647-2fef1ee78d9e")),
29+
preview: "Dear QA,I attached image here",
30+
hasAttachment: false,
31+
subject: "test inline image",
32+
from: {EmailAddress("DatPH", "[email protected]")},
33+
sentAt: UTCDate(DateTime.parse("2021-08-11T04:25:34Z")),
34+
receivedAt: UTCDate(DateTime.parse("2021-08-11T04:25:55Z")));
35+
36+
final expectMail2 = Email(
37+
EmailId(Id("bc8a5320-fa58-11eb-b647-2fef1ee78d9e")),
38+
preview:
39+
"This event is about to begin Noti check TimeFriday 23 October 2020 12:00 - 12:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - User A <[email protected]> (Organizer) - Lê Nguyễn <[email protected]> - User C <[email protected]",
40+
hasAttachment: false,
41+
subject: "Notification: Noti check",
42+
from: {EmailAddress(null, "[email protected]")},
43+
sentAt: UTCDate(DateTime.parse("2021-08-10T09:45:01Z")),
44+
receivedAt: UTCDate(DateTime.parse("2021-08-11T04:00:59Z")));
45+
46+
final expectMail3 = Email(
47+
EmailId(Id("ba7e0860-fa58-11eb-b647-2fef1ee78d9e")),
48+
preview:
49+
"This event is about to begin Recurrencr TimeWednesday 26 August 2020 05:30 - 06:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - [email protected] <[email protected]> (Organizer) - User A <[email protected]> Resourc",
50+
hasAttachment: false,
51+
subject: "Notification: Recurrencr",
52+
from: {EmailAddress(null, "[email protected]")},
53+
sentAt: UTCDate(DateTime.parse("2021-08-11T03:00:00Z")),
54+
receivedAt: UTCDate(DateTime.parse("2021-08-11T04:00:55Z")));
55+
56+
final expectMail4 = Email(
57+
EmailId(Id("d9b3b880-fa6f-11eb-b647-2fef1ee78d9e")),
58+
preview: "alo -- desktop signature",
59+
hasAttachment: true,
60+
subject: "test attachment",
61+
from: {EmailAddress("Haaheoo", "[email protected]")},
62+
sentAt: UTCDate(DateTime.parse("2021-08-11T06:46:25Z")),
63+
receivedAt: UTCDate(DateTime.parse("2021-08-11T06:46:26Z")));
64+
65+
final expectMail5 = Email(
66+
EmailId(Id("637f1ef0-fa5d-11eb-b647-2fef1ee78d9e")),
67+
preview: "Dear, test inline Thanks and BRs-- desktop signature",
68+
hasAttachment: false,
69+
subject: "test inline image",
70+
from: {EmailAddress("Haaheoo", "[email protected]")},
71+
sentAt: UTCDate(DateTime.parse("2021-08-11T04:34:13Z")),
72+
receivedAt: UTCDate(DateTime.parse("2021-08-11T04:34:17Z")));
73+
74+
test('get email in a mailbox correctly', () async {
75+
final baseOption = BaseOptions(method: 'POST');
76+
final dio = Dio(baseOption)..options.baseUrl = 'http://domain.com';
77+
final dioAdapter = DioAdapter(dio: dio);
78+
dioAdapter.onPost(
79+
'/jmap',
80+
(server) => server.reply(200, {
81+
"sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
82+
"methodResponses": [
83+
[
84+
"Email/query",
85+
{
86+
"accountId":
87+
"3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12",
88+
"queryState": "248a9026",
89+
"canCalculateChanges": false,
90+
"ids": [
91+
"d9b3b880-fa6f-11eb-b647-2fef1ee78d9e",
92+
"637f1ef0-fa5d-11eb-b647-2fef1ee78d9e",
93+
"382312d0-fa5c-11eb-b647-2fef1ee78d9e",
94+
"ba7e0860-fa58-11eb-b647-2fef1ee78d9e",
95+
"bc8a5320-fa58-11eb-b647-2fef1ee78d9e"
96+
],
97+
"position": 0
98+
},
99+
"c2"
100+
],
101+
[
102+
"Email/get",
103+
{
104+
"accountId":
105+
"3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12",
106+
"notFound": [],
107+
"state": "05114a40-fa7c-11eb-a9c2-2fef1ee78d9e",
108+
"list": [
109+
{
110+
"preview": "Dear QA,I attached image here",
111+
"hasAttachment": false,
112+
"subject": "test inline image",
113+
"from": [
114+
{
115+
"name": "DatPH",
116+
"email": "[email protected]"
117+
}
118+
],
119+
"sentAt": "2021-08-11T04:25:34Z",
120+
"id": "382312d0-fa5c-11eb-b647-2fef1ee78d9e",
121+
"receivedAt": "2021-08-11T04:25:55Z"
122+
},
123+
{
124+
"preview":
125+
"This event is about to begin Noti check TimeFriday 23 October 2020 12:00 - 12:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - User A <[email protected]> (Organizer) - Lê Nguyễn <[email protected]> - User C <[email protected]",
126+
"hasAttachment": false,
127+
"subject": "Notification: Noti check",
128+
"from": [
129+
{"email": "[email protected]"}
130+
],
131+
"sentAt": "2021-08-10T09:45:01Z",
132+
"id": "bc8a5320-fa58-11eb-b647-2fef1ee78d9e",
133+
"receivedAt": "2021-08-11T04:00:59Z"
134+
},
135+
{
136+
"preview":
137+
"This event is about to begin Recurrencr TimeWednesday 26 August 2020 05:30 - 06:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - [email protected] <[email protected]> (Organizer) - User A <[email protected]> Resourc",
138+
"hasAttachment": false,
139+
"subject": "Notification: Recurrencr",
140+
"from": [
141+
{"email": "[email protected]"}
142+
],
143+
"sentAt": "2021-08-11T03:00:00Z",
144+
"id": "ba7e0860-fa58-11eb-b647-2fef1ee78d9e",
145+
"receivedAt": "2021-08-11T04:00:55Z"
146+
},
147+
{
148+
"preview": "alo -- desktop signature",
149+
"hasAttachment": true,
150+
"subject": "test attachment",
151+
"from": [
152+
{
153+
"name": "Haaheoo",
154+
"email": "[email protected]"
155+
}
156+
],
157+
"sentAt": "2021-08-11T06:46:25Z",
158+
"id": "d9b3b880-fa6f-11eb-b647-2fef1ee78d9e",
159+
"receivedAt": "2021-08-11T06:46:26Z"
160+
},
161+
{
162+
"preview":
163+
"Dear, test inline Thanks and BRs-- desktop signature",
164+
"hasAttachment": false,
165+
"subject": "test inline image",
166+
"from": [
167+
{
168+
"name": "Haaheoo",
169+
"email": "[email protected]"
170+
}
171+
],
172+
"sentAt": "2021-08-11T04:34:13Z",
173+
"id": "637f1ef0-fa5d-11eb-b647-2fef1ee78d9e",
174+
"receivedAt": "2021-08-11T04:34:17Z"
175+
}
176+
]
177+
},
178+
"c3"
179+
]
180+
]
181+
}),
182+
data: {
183+
"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
184+
"methodCalls": [
185+
[
186+
"Email/query",
187+
{
188+
"accountId":
189+
"93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c",
190+
"filter": {
191+
"operator": "OR",
192+
"conditions": [
193+
{"hasKeyword": "music"},
194+
{"hasKeyword": "video"}
195+
]
196+
},
197+
"sort": [
198+
{"isAscending": false, "property": "sentAt"}
199+
],
200+
"limit": 20,
201+
},
202+
"c2"
203+
],
204+
[
205+
"Email/get",
206+
{
207+
"accountId":
208+
"93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c",
209+
"#ids": {
210+
"resultOf": "c2",
211+
"name": "Email/query",
212+
"path": "ids/*"
213+
},
214+
"properties": [
215+
"id",
216+
"subject",
217+
"from",
218+
"receivedAt",
219+
"sentAt",
220+
"preview",
221+
"hasAttachment"
222+
]
223+
},
224+
"c3"
225+
]
226+
]
227+
},
228+
headers: {
229+
"accept": "application/json;jmapVersion=rfc-8621",
230+
"content-type": "application/json; charset=utf-8",
231+
"content-length": 581
232+
});
233+
234+
final httpClient = HttpClient(dio);
235+
final processingInvocation = ProcessingInvocation();
236+
final jmapRequestBuilder =
237+
JmapRequestBuilder(httpClient, processingInvocation);
238+
final accountId = AccountId(Id(
239+
'93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c'));
240+
241+
final queryEmailMethod = QueryEmailMethod(accountId)
242+
..addLimit(UnsignedInt(20))
243+
..addSorts({
244+
EmailComparator(EmailComparatorProperty.sentAt)..setIsAscending(false)
245+
})
246+
..addFilters(LogicFilterOperator(
247+
Operator.OR,
248+
Set<Filter>.from([
249+
EmailFilterCondition(hasKeyword: "music"),
250+
EmailFilterCondition(hasKeyword: "video"),
251+
])));
252+
final queryEmailInvocation = jmapRequestBuilder
253+
.invocation(queryEmailMethod, methodCallId: MethodCallId('c2'));
254+
255+
final getEmailMethod = GetEmailMethod(accountId)
256+
..addProperties(Properties({
257+
"id",
258+
"subject",
259+
"from",
260+
"receivedAt",
261+
"sentAt",
262+
"preview",
263+
"hasAttachment"
264+
}))
265+
..addReferenceIds(processingInvocation.createResultReference(
266+
queryEmailInvocation.methodCallId, ReferencePath.idsPath));
267+
final getEmailInvocation = jmapRequestBuilder.invocation(getEmailMethod,
268+
methodCallId: MethodCallId('c3'));
269+
270+
final result = await (jmapRequestBuilder
271+
..usings(getEmailMethod.requiredCapabilities))
272+
.build()
273+
.execute();
274+
275+
final resultList = result.parse<GetEmailResponse>(
276+
getEmailInvocation.methodCallId, GetEmailResponse.deserialize);
277+
278+
expect(resultList?.list.length, equals(5));
279+
expect(
280+
resultList?.list,
281+
containsAll({
282+
expectMail1,
283+
expectMail2,
284+
expectMail3,
285+
expectMail4,
286+
expectMail5
287+
}));
288+
});
289+
});
290+
}

0 commit comments

Comments
 (0)