-
Notifications
You must be signed in to change notification settings - Fork 462
/
GetMessageListMethodStepdefs.java
156 lines (133 loc) · 6.76 KB
/
GetMessageListMethodStepdefs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.jmap.methods.integration.cucumber;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import javax.inject.Inject;
import org.apache.james.mailbox.model.MailboxConstants;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.model.MessageId;
import org.apache.james.modules.MailboxProbeImpl;
import com.github.steveash.guavate.Guavate;
import com.google.common.base.Joiner;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.runtime.java.guice.ScenarioScoped;
@ScenarioScoped
public class GetMessageListMethodStepdefs {
private static final String ARGUMENTS = "[0][1]";
private final MainStepdefs mainStepdefs;
private final HttpClient httpClient;
private final MessageIdStepdefs messageIdStepdefs;
private final UserStepdefs userStepdefs;
@Inject
private GetMessageListMethodStepdefs(MainStepdefs mainStepdefs, HttpClient httpClient, MessageIdStepdefs messageIdStepdefs, UserStepdefs userStepdefs) {
this.mainStepdefs = mainStepdefs;
this.httpClient = httpClient;
this.messageIdStepdefs = messageIdStepdefs;
this.userStepdefs = userStepdefs;
}
@When("^\"([^\"]*)\" asks for message list in mailboxes \"([^\"]*)\" with flag \"([^\"]*)\"$")
public void getMessageListWithFlag(String username, List<String> mailboxes, String flag) throws Exception {
String mailboxIds = Joiner.on("\",\"")
.join(mailboxes.stream()
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));
httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" +
" \"inMailboxes\":[\"%s\"]," +
" \"hasKeyword\":\"%s\"" +
"}}, \"#0\"]]",
mailboxIds,
flag));
}
@When("^\"([^\"]*)\" asks for message list in (?:mailboxes|mailbox) \"([^\"]*)\"$")
public void getMessageList(String username, List<String> mailboxes) throws Exception {
String mailboxIds = Joiner.on("\",\"")
.join(mailboxes.stream()
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));
getMessageListFromMailboxIds(mailboxIds);
}
@When("^\"([^\"]*)\" asks for message list in delegated (?:mailboxes|mailbox) \"([^\"]*)\" from \"([^\"]*)\"$")
public void getMessageListFromDelegated(String sharee, List<String> mailboxes, String sharer) throws Exception {
String mailboxIds = Joiner.on("\",\"")
.join(mailboxes.stream()
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, sharer, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));
userStepdefs.execWithUser(sharee, () -> getMessageListFromMailboxIds(mailboxIds));
}
private void getMessageListFromMailboxIds(String mailboxIds) throws Exception {
httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" +
" \"inMailboxes\":[\"%s\"]" +
"}}, \"#0\"]]",
mailboxIds));
}
@When("^\"([^\"]*)\" asks for message list in mailbox \"([^\"]*)\" with flag \"([^\"]*)\"$")
public void getMessageList(String username, String mailbox, String flag) throws Exception {
MailboxId mailboxId = mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId();
httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" +
" \"inMailboxes\":[\"%s\"]," +
" \"hasKeyword\":\"%s\"" +
"}}, \"#0\"]]",
mailboxId.serialize(),
flag));
}
@When("^the user asks for message list with flag \"([^\"]*)\"$")
public void getMessageList(String flag) throws Exception {
httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" +
" \"hasKeyword\":\"%s\"" +
"}}, \"#0\"]]",
flag));
}
@Then("^the message list is empty$")
public void assertEmpty() {
assertThat(httpClient.response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".messageIds")).isEmpty();
}
@Then("^the message list has size (\\d+)")
public void assertSize(int size) {
assertThat(httpClient.response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".messageIds")).hasSize(size);
}
@Then("^the message list contains \"([^\"]*)\"")
public void assertContains(String message) {
MessageId messageId = messageIdStepdefs.getMessageId(message);
assertThat(httpClient.response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".messageIds")).contains(messageId.serialize());
}
}