-
Notifications
You must be signed in to change notification settings - Fork 11
/
JiraInputPluginTest.java
240 lines (208 loc) · 10.2 KB
/
JiraInputPluginTest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.embulk.input.jira;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.embulk.EmbulkTestRuntime;
import org.embulk.config.ConfigDiff;
import org.embulk.config.ConfigSource;
import org.embulk.config.TaskReport;
import org.embulk.config.TaskSource;
import org.embulk.input.jira.JiraInputPlugin.PluginTask;
import org.embulk.input.jira.client.JiraClient;
import org.embulk.spi.InputPlugin;
import org.embulk.spi.PageBuilder;
import org.embulk.spi.Schema;
import org.embulk.spi.TestPageBuilderReader.MockPageOutput;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.embulk.input.jira.JiraInputPlugin.CONFIG_MAPPER;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class JiraInputPluginTest
{
@Rule
public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
private JiraInputPlugin plugin;
private JiraClient jiraClient;
private JsonObject data;
private ConfigSource config;
private final CloseableHttpClient client = Mockito.mock(CloseableHttpClient.class);
private final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
private final StatusLine statusLine = Mockito.mock(StatusLine.class);
private final MockPageOutput output = new MockPageOutput();
private PageBuilder pageBuilder;
@Before
public void setUp() throws IOException
{
if (plugin == null) {
plugin = Mockito.spy(new JiraInputPlugin());
jiraClient = Mockito.spy(new JiraClient());
data = TestHelpers.getJsonFromFile("jira_input_plugin.json");
config = TestHelpers.config();
CONFIG_MAPPER.map(config, PluginTask.class);
pageBuilder = Mockito.mock(PageBuilder.class);
}
when(plugin.getJiraClient()).thenReturn(jiraClient);
when(jiraClient.createHttpClient()).thenReturn(client);
when(client.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response);
when(response.getStatusLine()).thenReturn(statusLine);
doReturn(pageBuilder).when(plugin).getPageBuilder(Mockito.any(), Mockito.any());
}
@Test
public void test_run_withEmptyResult() throws IOException
{
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("emptyResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(config, new Control());
// Check credential 1 + getTotal 1 + loadData 0
verify(jiraClient, times(2)).createHttpClient();
verify(pageBuilder, times(0)).addRecord();
verify(pageBuilder, times(1)).finish();
}
public void test_runDynamicSchema_withEmptyResult() throws IOException
{
final JsonObject searchResponse = data.get("emptyResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(TestHelpers.dynamicSchemaConfig(), new Control());
verify(pageBuilder, times(0)).addRecord();
}
@Test
public void test_runDynamicSchema_withResult() throws IOException
{
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("oneRecordResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(searchResponse.get("statusCode").getAsInt())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(searchResponse.get("body").toString()))
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(TestHelpers.dynamicSchemaConfig(), new Control());
// Check credential 1 + getTotal 1 + loadData 2
verify(jiraClient, times(4)).createHttpClient();
verify(pageBuilder, times(1)).addRecord();
verify(pageBuilder, times(1)).finish();
}
@Test
public void test_run_with1RecordsResult() throws IOException
{
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("oneRecordResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(config, new Control());
// Check credential 1 + getTotal 1 + loadData 1
verify(jiraClient, times(3)).createHttpClient();
verify(pageBuilder, times(1)).addRecord();
verify(pageBuilder, times(1)).finish();
}
@Test
public void test_run_with2PagesResult() throws IOException
{
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("2PagesResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(config, new Control());
// Check credential 1 + getTotal 1 + loadData 2
verify(jiraClient, times(4)).createHttpClient();
verify(pageBuilder, times(2)).addRecord();
verify(pageBuilder, times(1)).finish();
}
@Test
public void test_preview_withEmptyResult() throws IOException
{
when(plugin.isPreview()).thenReturn(true);
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("emptyResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(config, new Control());
// Check credential 1 + loadData 1
verify(jiraClient, times(2)).createHttpClient();
verify(pageBuilder, times(0)).addRecord();
verify(pageBuilder, times(1)).finish();
}
@Test
public void test_preview_with1RecordsResult() throws IOException
{
when(plugin.isPreview()).thenReturn(true);
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("oneRecordResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
plugin.transaction(config, new Control());
// Check credential 1 + loadData 1
verify(jiraClient, times(2)).createHttpClient();
verify(pageBuilder, times(1)).addRecord();
verify(pageBuilder, times(1)).finish();
}
@Test
public void test_guess() throws IOException
{
final ConfigSource configSource = TestHelpers.config();
final JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
final JsonObject searchResponse = data.get("guessDataResult").getAsJsonObject();
when(statusLine.getStatusCode())
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
.thenReturn(searchResponse.get("statusCode").getAsInt());
when(response.getEntity())
.thenReturn(new StringEntity(searchResponse.get("body").toString()))
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
final ConfigDiff result = plugin.guess(configSource);
final JsonElement expected = data.get("guessResult").getAsJsonObject();
final JsonElement actual = new JsonParser().parse(result.toString());
assertEquals(expected, actual);
}
private class Control implements InputPlugin.Control
{
@Override
public List<TaskReport> run(final TaskSource taskSource, final Schema schema, final int taskCount)
{
final List<TaskReport> reports = new ArrayList<>();
for (int i = 0; i < taskCount; i++) {
reports.add(plugin.run(taskSource, schema, i, output));
}
return reports;
}
}
}