embulk-input-jira 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +10 -5
- data/.travis.yml +4 -34
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +5 -4
- data/build.gradle +116 -0
- data/config/checkstyle/checkstyle.xml +128 -0
- data/config/checkstyle/default.xml +108 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +5 -0
- data/gradlew +172 -0
- data/gradlew.bat +84 -0
- data/lib/embulk/guess/jira.rb +24 -0
- data/lib/embulk/input/jira.rb +3 -169
- data/src/main/java/org/embulk/input/jira/AuthenticateMethod.java +27 -0
- data/src/main/java/org/embulk/input/jira/Constant.java +17 -0
- data/src/main/java/org/embulk/input/jira/Issue.java +150 -0
- data/src/main/java/org/embulk/input/jira/JiraInputPlugin.java +226 -0
- data/src/main/java/org/embulk/input/jira/client/JiraClient.java +254 -0
- data/src/main/java/org/embulk/input/jira/util/JiraException.java +18 -0
- data/src/main/java/org/embulk/input/jira/util/JiraUtil.java +264 -0
- data/src/test/java/org/embulk/input/jira/IssueTest.java +278 -0
- data/src/test/java/org/embulk/input/jira/JiraInputPluginTest.java +204 -0
- data/src/test/java/org/embulk/input/jira/JiraPluginTestRuntime.java +133 -0
- data/src/test/java/org/embulk/input/jira/TestHelpers.java +41 -0
- data/src/test/java/org/embulk/input/jira/client/JiraClientTest.java +222 -0
- data/src/test/java/org/embulk/input/jira/util/JiraUtilTest.java +318 -0
- data/src/test/resources/config.yml +13 -0
- data/src/test/resources/issue_flatten.json +129 -0
- data/src/test/resources/issue_flatten_expected.json +73 -0
- data/src/test/resources/issue_get.json +36 -0
- data/src/test/resources/issue_get_expected.json +62 -0
- data/src/test/resources/jira_client.json +81 -0
- data/src/test/resources/jira_input_plugin.json +114 -0
- data/src/test/resources/jira_util.json +26 -0
- metadata +55 -175
- data/Gemfile +0 -3
- data/LICENSE +0 -13
- data/Rakefile +0 -15
- data/embulk-input-jira.gemspec +0 -27
- data/gemfiles/embulk-0.8.0-latest +0 -4
- data/gemfiles/embulk-0.8.7 +0 -4
- data/gemfiles/embulk-0.8.8 +0 -4
- data/gemfiles/embulk-latest +0 -4
- data/gemfiles/template.erb +0 -4
- data/lib/embulk/input/jira_api.rb +0 -9
- data/lib/embulk/input/jira_api/client.rb +0 -144
- data/lib/embulk/input/jira_api/issue.rb +0 -133
- data/lib/embulk/input/jira_input_plugin_utils.rb +0 -58
- data/spec/embulk/input/jira-input-plugin-utils_spec.rb +0 -89
- data/spec/embulk/input/jira_api/client_spec.rb +0 -224
- data/spec/embulk/input/jira_api/issue_spec.rb +0 -394
- data/spec/embulk/input/jira_spec.rb +0 -322
- data/spec/embulk_spec.rb +0 -32
- data/spec/spec_helper.rb +0 -26
- data/spec/support/stdout_and_err_capture.rb +0 -45
@@ -0,0 +1,204 @@
|
|
1
|
+
package org.embulk.input.jira;
|
2
|
+
|
3
|
+
import com.google.gson.JsonElement;
|
4
|
+
import com.google.gson.JsonObject;
|
5
|
+
import com.google.gson.JsonParser;
|
6
|
+
import org.apache.http.HttpResponse;
|
7
|
+
import org.apache.http.StatusLine;
|
8
|
+
import org.apache.http.client.HttpClient;
|
9
|
+
import org.apache.http.client.methods.HttpUriRequest;
|
10
|
+
import org.apache.http.entity.StringEntity;
|
11
|
+
import org.embulk.config.ConfigDiff;
|
12
|
+
import org.embulk.config.ConfigSource;
|
13
|
+
import org.embulk.config.TaskReport;
|
14
|
+
import org.embulk.config.TaskSource;
|
15
|
+
import org.embulk.input.jira.JiraInputPlugin.PluginTask;
|
16
|
+
import org.embulk.input.jira.client.JiraClient;
|
17
|
+
import org.embulk.spi.InputPlugin;
|
18
|
+
import org.embulk.spi.PageBuilder;
|
19
|
+
import org.embulk.spi.Schema;
|
20
|
+
import org.embulk.spi.TestPageBuilderReader.MockPageOutput;
|
21
|
+
import org.junit.Before;
|
22
|
+
import org.junit.Rule;
|
23
|
+
import org.junit.Test;
|
24
|
+
import org.mockito.Mockito;
|
25
|
+
|
26
|
+
import java.io.IOException;
|
27
|
+
import java.util.ArrayList;
|
28
|
+
import java.util.List;
|
29
|
+
|
30
|
+
import static org.junit.Assert.assertEquals;
|
31
|
+
import static org.mockito.Mockito.doReturn;
|
32
|
+
import static org.mockito.Mockito.times;
|
33
|
+
import static org.mockito.Mockito.verify;
|
34
|
+
import static org.mockito.Mockito.when;
|
35
|
+
|
36
|
+
public class JiraInputPluginTest
|
37
|
+
{
|
38
|
+
@Rule
|
39
|
+
public JiraPluginTestRuntime runtime = new JiraPluginTestRuntime();
|
40
|
+
|
41
|
+
private JiraInputPlugin plugin;
|
42
|
+
private JiraClient jiraClient;
|
43
|
+
private JsonObject data;
|
44
|
+
private ConfigSource config;
|
45
|
+
private HttpClient client = Mockito.mock(HttpClient.class);
|
46
|
+
private HttpResponse response = Mockito.mock(HttpResponse.class);
|
47
|
+
private StatusLine statusLine = Mockito.mock(StatusLine.class);
|
48
|
+
|
49
|
+
private MockPageOutput output = new MockPageOutput();
|
50
|
+
private PageBuilder pageBuilder;
|
51
|
+
|
52
|
+
@Before
|
53
|
+
public void setUp() throws IOException
|
54
|
+
{
|
55
|
+
if (plugin == null) {
|
56
|
+
plugin = Mockito.spy(new JiraInputPlugin());
|
57
|
+
jiraClient = Mockito.spy(new JiraClient());
|
58
|
+
data = TestHelpers.getJsonFromFile("jira_input_plugin.json");
|
59
|
+
config = TestHelpers.config();
|
60
|
+
config.loadConfig(PluginTask.class);
|
61
|
+
pageBuilder = Mockito.mock(PageBuilder.class);
|
62
|
+
//pageBuilder = new PageBuilder(Exec.getBufferAllocator(), config.loadConfig(PluginTask.class).getColumns().toSchema(), output);
|
63
|
+
}
|
64
|
+
when(plugin.getJiraClient()).thenReturn(jiraClient);
|
65
|
+
when(jiraClient.createHttpClient()).thenReturn(client);
|
66
|
+
when(client.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response);
|
67
|
+
when(response.getStatusLine()).thenReturn(statusLine);
|
68
|
+
doReturn(pageBuilder).when(plugin).getPageBuilder(Mockito.any(), Mockito.any());
|
69
|
+
}
|
70
|
+
|
71
|
+
@Test
|
72
|
+
public void test_run_withEmptyResult() throws IOException
|
73
|
+
{
|
74
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
75
|
+
JsonObject searchResponse = data.get("emptyResult").getAsJsonObject();
|
76
|
+
|
77
|
+
when(statusLine.getStatusCode())
|
78
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
79
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
80
|
+
when(response.getEntity())
|
81
|
+
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
|
82
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
83
|
+
|
84
|
+
plugin.transaction(config, new Control());
|
85
|
+
// Check credential 1 + getTotal 1 + loadData 0
|
86
|
+
verify(jiraClient, times(2)).createHttpClient();
|
87
|
+
verify(pageBuilder, times(0)).addRecord();
|
88
|
+
verify(pageBuilder, times(1)).finish();
|
89
|
+
}
|
90
|
+
|
91
|
+
@Test
|
92
|
+
public void test_run_with1RecordsResult() throws IOException
|
93
|
+
{
|
94
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
95
|
+
JsonObject searchResponse = data.get("oneRecordResult").getAsJsonObject();
|
96
|
+
|
97
|
+
when(statusLine.getStatusCode())
|
98
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
99
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
100
|
+
when(response.getEntity())
|
101
|
+
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
|
102
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
103
|
+
|
104
|
+
plugin.transaction(config, new Control());
|
105
|
+
// Check credential 1 + getTotal 1 + loadData 1
|
106
|
+
verify(jiraClient, times(3)).createHttpClient();
|
107
|
+
verify(pageBuilder, times(1)).addRecord();
|
108
|
+
verify(pageBuilder, times(1)).finish();
|
109
|
+
}
|
110
|
+
|
111
|
+
@Test
|
112
|
+
public void test_run_with2PagesResult() throws IOException
|
113
|
+
{
|
114
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
115
|
+
JsonObject searchResponse = data.get("2PagesResult").getAsJsonObject();
|
116
|
+
|
117
|
+
when(statusLine.getStatusCode())
|
118
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
119
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
120
|
+
when(response.getEntity())
|
121
|
+
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
|
122
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
123
|
+
|
124
|
+
plugin.transaction(config, new Control());
|
125
|
+
// Check credential 1 + getTotal 1 + loadData 2
|
126
|
+
verify(jiraClient, times(4)).createHttpClient();
|
127
|
+
verify(pageBuilder, times(2)).addRecord();
|
128
|
+
verify(pageBuilder, times(1)).finish();
|
129
|
+
}
|
130
|
+
|
131
|
+
@Test
|
132
|
+
public void test_preview_withEmptyResult() throws IOException
|
133
|
+
{
|
134
|
+
when(plugin.isPreview()).thenReturn(true);
|
135
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
136
|
+
JsonObject searchResponse = data.get("emptyResult").getAsJsonObject();
|
137
|
+
|
138
|
+
when(statusLine.getStatusCode())
|
139
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
140
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
141
|
+
when(response.getEntity())
|
142
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
143
|
+
|
144
|
+
plugin.transaction(config, new Control());
|
145
|
+
// Check credential 1 + loadData 1
|
146
|
+
verify(jiraClient, times(2)).createHttpClient();
|
147
|
+
verify(pageBuilder, times(0)).addRecord();
|
148
|
+
verify(pageBuilder, times(1)).finish();
|
149
|
+
}
|
150
|
+
|
151
|
+
@Test
|
152
|
+
public void test_preview_with1RecordsResult() throws IOException
|
153
|
+
{
|
154
|
+
when(plugin.isPreview()).thenReturn(true);
|
155
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
156
|
+
JsonObject searchResponse = data.get("oneRecordResult").getAsJsonObject();
|
157
|
+
|
158
|
+
when(statusLine.getStatusCode())
|
159
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
160
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
161
|
+
when(response.getEntity())
|
162
|
+
.thenReturn(new StringEntity(authorizeResponse.get("body").toString()))
|
163
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
164
|
+
|
165
|
+
plugin.transaction(config, new Control());
|
166
|
+
// Check credential 1 + loadData 1
|
167
|
+
verify(jiraClient, times(2)).createHttpClient();
|
168
|
+
verify(pageBuilder, times(1)).addRecord();
|
169
|
+
verify(pageBuilder, times(1)).finish();
|
170
|
+
}
|
171
|
+
|
172
|
+
@Test
|
173
|
+
public void test_guess() throws IOException
|
174
|
+
{
|
175
|
+
ConfigSource configSource = TestHelpers.config();
|
176
|
+
JsonObject authorizeResponse = data.get("authenticateSuccess").getAsJsonObject();
|
177
|
+
JsonObject searchResponse = data.get("guessDataResult").getAsJsonObject();
|
178
|
+
|
179
|
+
when(statusLine.getStatusCode())
|
180
|
+
.thenReturn(authorizeResponse.get("statusCode").getAsInt())
|
181
|
+
.thenReturn(searchResponse.get("statusCode").getAsInt());
|
182
|
+
when(response.getEntity())
|
183
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()))
|
184
|
+
.thenReturn(new StringEntity(searchResponse.get("body").toString()));
|
185
|
+
|
186
|
+
ConfigDiff result = plugin.guess(configSource);
|
187
|
+
JsonElement expected = data.get("guessResult").getAsJsonObject();
|
188
|
+
JsonElement actual = new JsonParser().parse(result.toString());
|
189
|
+
assertEquals(expected, actual);
|
190
|
+
}
|
191
|
+
|
192
|
+
private class Control implements InputPlugin.Control
|
193
|
+
{
|
194
|
+
@Override
|
195
|
+
public List<TaskReport> run(final TaskSource taskSource, final Schema schema, final int taskCount)
|
196
|
+
{
|
197
|
+
List<TaskReport> reports = new ArrayList<>();
|
198
|
+
for (int i = 0; i < taskCount; i++) {
|
199
|
+
reports.add(plugin.run(taskSource, schema, i, output));
|
200
|
+
}
|
201
|
+
return reports;
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
package org.embulk.input.jira;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
5
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
6
|
+
import com.google.inject.Binder;
|
7
|
+
import com.google.inject.Injector;
|
8
|
+
import com.google.inject.Module;
|
9
|
+
import org.embulk.GuiceBinder;
|
10
|
+
import org.embulk.RandomManager;
|
11
|
+
import org.embulk.TestPluginSourceModule;
|
12
|
+
import org.embulk.TestUtilityModule;
|
13
|
+
import org.embulk.config.ConfigSource;
|
14
|
+
import org.embulk.config.DataSourceImpl;
|
15
|
+
import org.embulk.config.ModelManager;
|
16
|
+
import org.embulk.exec.ExecModule;
|
17
|
+
import org.embulk.exec.ExtensionServiceLoaderModule;
|
18
|
+
import org.embulk.exec.SystemConfigModule;
|
19
|
+
import org.embulk.jruby.JRubyScriptingModule;
|
20
|
+
import org.embulk.plugin.BuiltinPluginSourceModule;
|
21
|
+
import org.embulk.plugin.PluginClassLoaderFactory;
|
22
|
+
import org.embulk.plugin.PluginClassLoaderModule;
|
23
|
+
import org.embulk.spi.BufferAllocator;
|
24
|
+
import org.embulk.spi.Exec;
|
25
|
+
import org.embulk.spi.ExecAction;
|
26
|
+
import org.embulk.spi.ExecSession;
|
27
|
+
import org.junit.runner.Description;
|
28
|
+
import org.junit.runners.model.Statement;
|
29
|
+
|
30
|
+
import java.util.Random;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* This is a clone from {@link org.embulk.EmbulkTestRuntime}, since there is no easy way to extend it.
|
34
|
+
* The only modification is on the provided systemConfig, enable tests to run `embulk/guess/jira.rb`
|
35
|
+
*/
|
36
|
+
public class JiraPluginTestRuntime extends GuiceBinder
|
37
|
+
{
|
38
|
+
private static ConfigSource getSystemConfig()
|
39
|
+
{
|
40
|
+
ObjectNode configNode = JsonNodeFactory.instance.objectNode();
|
41
|
+
configNode.set("jruby_load_path", JsonNodeFactory.instance.arrayNode().add("lib"));
|
42
|
+
|
43
|
+
return new DataSourceImpl(new ModelManager(null, new ObjectMapper()), configNode);
|
44
|
+
}
|
45
|
+
|
46
|
+
public static class TestRuntimeModule implements Module
|
47
|
+
{
|
48
|
+
@Override
|
49
|
+
public void configure(Binder binder)
|
50
|
+
{
|
51
|
+
ConfigSource systemConfig = getSystemConfig();
|
52
|
+
new SystemConfigModule(systemConfig).configure(binder);
|
53
|
+
new ExecModule().configure(binder);
|
54
|
+
new ExtensionServiceLoaderModule(systemConfig).configure(binder);
|
55
|
+
new BuiltinPluginSourceModule().configure(binder);
|
56
|
+
new JRubyScriptingModule(systemConfig).configure(binder);
|
57
|
+
new PluginClassLoaderModule(systemConfig).configure(binder);
|
58
|
+
new TestUtilityModule().configure(binder);
|
59
|
+
new TestPluginSourceModule().configure(binder);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
private ExecSession exec;
|
64
|
+
|
65
|
+
public JiraPluginTestRuntime()
|
66
|
+
{
|
67
|
+
super(new TestRuntimeModule());
|
68
|
+
Injector injector = getInjector();
|
69
|
+
ConfigSource execConfig = new DataSourceImpl(injector.getInstance(ModelManager.class));
|
70
|
+
this.exec = ExecSession.builder(injector).fromExecConfig(execConfig).build();
|
71
|
+
}
|
72
|
+
|
73
|
+
public ExecSession getExec()
|
74
|
+
{
|
75
|
+
return exec;
|
76
|
+
}
|
77
|
+
|
78
|
+
public BufferAllocator getBufferAllocator()
|
79
|
+
{
|
80
|
+
return getInstance(BufferAllocator.class);
|
81
|
+
}
|
82
|
+
|
83
|
+
public ModelManager getModelManager()
|
84
|
+
{
|
85
|
+
return getInstance(ModelManager.class);
|
86
|
+
}
|
87
|
+
|
88
|
+
public Random getRandom()
|
89
|
+
{
|
90
|
+
return getInstance(RandomManager.class).getRandom();
|
91
|
+
}
|
92
|
+
|
93
|
+
public PluginClassLoaderFactory getPluginClassLoaderFactory()
|
94
|
+
{
|
95
|
+
return getInstance(PluginClassLoaderFactory.class);
|
96
|
+
}
|
97
|
+
|
98
|
+
@Override
|
99
|
+
public Statement apply(Statement base, Description description)
|
100
|
+
{
|
101
|
+
final Statement superStatement = JiraPluginTestRuntime.super.apply(base, description);
|
102
|
+
return new Statement() {
|
103
|
+
public void evaluate() throws Throwable
|
104
|
+
{
|
105
|
+
try {
|
106
|
+
Exec.doWith(exec, (ExecAction<Void>) () -> {
|
107
|
+
try {
|
108
|
+
superStatement.evaluate();
|
109
|
+
}
|
110
|
+
catch (Throwable ex) {
|
111
|
+
throw new RuntimeExecutionException(ex);
|
112
|
+
}
|
113
|
+
return null;
|
114
|
+
});
|
115
|
+
}
|
116
|
+
catch (RuntimeException ex) {
|
117
|
+
throw ex.getCause();
|
118
|
+
}
|
119
|
+
finally {
|
120
|
+
exec.cleanup();
|
121
|
+
}
|
122
|
+
}
|
123
|
+
};
|
124
|
+
}
|
125
|
+
|
126
|
+
private static class RuntimeExecutionException extends RuntimeException
|
127
|
+
{
|
128
|
+
public RuntimeExecutionException(Throwable cause)
|
129
|
+
{
|
130
|
+
super(cause);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
package org.embulk.input.jira;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
+
import com.fasterxml.jackson.datatype.guava.GuavaModule;
|
5
|
+
import com.fasterxml.jackson.datatype.joda.JodaModule;
|
6
|
+
import com.google.common.io.Resources;
|
7
|
+
import com.google.gson.JsonObject;
|
8
|
+
import com.google.gson.JsonParser;
|
9
|
+
import com.google.gson.stream.JsonReader;
|
10
|
+
|
11
|
+
import org.embulk.config.ConfigLoader;
|
12
|
+
import org.embulk.config.ConfigSource;
|
13
|
+
import org.embulk.config.ModelManager;
|
14
|
+
|
15
|
+
import java.io.File;
|
16
|
+
import java.io.FileReader;
|
17
|
+
import java.io.IOException;
|
18
|
+
|
19
|
+
public final class TestHelpers
|
20
|
+
{
|
21
|
+
private TestHelpers() {}
|
22
|
+
|
23
|
+
public static JsonObject getJsonFromFile(String fileName) throws IOException
|
24
|
+
{
|
25
|
+
String path = Resources.getResource(fileName).getPath();
|
26
|
+
try (JsonReader reader = new JsonReader(new FileReader(path))) {
|
27
|
+
JsonParser parser = new JsonParser();
|
28
|
+
return parser.parse(reader).getAsJsonObject();
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
public static ConfigSource config() throws IOException
|
33
|
+
{
|
34
|
+
String path = Resources.getResource("config.yml").getPath();
|
35
|
+
ObjectMapper mapper = new ObjectMapper()
|
36
|
+
.registerModule(new GuavaModule())
|
37
|
+
.registerModule(new JodaModule());
|
38
|
+
ConfigLoader configLoader = new ConfigLoader(new ModelManager(null, mapper));
|
39
|
+
return configLoader.fromYamlFile(new File(path));
|
40
|
+
}
|
41
|
+
}
|
@@ -0,0 +1,222 @@
|
|
1
|
+
package org.embulk.input.jira.client;
|
2
|
+
|
3
|
+
import com.google.gson.JsonObject;
|
4
|
+
|
5
|
+
import org.apache.http.HttpResponse;
|
6
|
+
import org.apache.http.StatusLine;
|
7
|
+
import org.apache.http.client.HttpClient;
|
8
|
+
import org.apache.http.entity.StringEntity;
|
9
|
+
import org.embulk.EmbulkTestRuntime;
|
10
|
+
import org.embulk.config.ConfigException;
|
11
|
+
import org.embulk.input.jira.Issue;
|
12
|
+
import org.embulk.input.jira.JiraInputPlugin.PluginTask;
|
13
|
+
import org.embulk.input.jira.TestHelpers;
|
14
|
+
|
15
|
+
import org.junit.Before;
|
16
|
+
import org.junit.Rule;
|
17
|
+
import org.junit.Test;
|
18
|
+
import org.mockito.Mockito;
|
19
|
+
|
20
|
+
import java.io.IOException;
|
21
|
+
import java.util.List;
|
22
|
+
|
23
|
+
import static org.junit.Assert.assertEquals;
|
24
|
+
import static org.junit.Assert.assertThrows;
|
25
|
+
import static org.mockito.Mockito.times;
|
26
|
+
import static org.mockito.Mockito.verify;
|
27
|
+
import static org.mockito.Mockito.when;
|
28
|
+
|
29
|
+
public class JiraClientTest
|
30
|
+
{
|
31
|
+
@Rule
|
32
|
+
public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
|
33
|
+
private JiraClient jiraClient;
|
34
|
+
private PluginTask task;
|
35
|
+
|
36
|
+
private HttpClient client = Mockito.mock(HttpClient.class);
|
37
|
+
private HttpResponse response = Mockito.mock(HttpResponse.class);
|
38
|
+
private StatusLine statusLine = Mockito.mock(StatusLine.class);
|
39
|
+
private JsonObject data;
|
40
|
+
|
41
|
+
@Before
|
42
|
+
public void setUp() throws IOException
|
43
|
+
{
|
44
|
+
if (jiraClient == null) {
|
45
|
+
jiraClient = Mockito.spy(new JiraClient());
|
46
|
+
response = Mockito.mock(HttpResponse.class);
|
47
|
+
task = TestHelpers.config().loadConfig(PluginTask.class);
|
48
|
+
data = TestHelpers.getJsonFromFile("jira_client.json");
|
49
|
+
}
|
50
|
+
when(jiraClient.createHttpClient()).thenReturn(client);
|
51
|
+
when(client.execute(Mockito.any())).thenReturn(response);
|
52
|
+
when(response.getStatusLine()).thenReturn(statusLine);
|
53
|
+
}
|
54
|
+
|
55
|
+
@Test
|
56
|
+
public void test_checkUserCredentials_success() throws IOException
|
57
|
+
{
|
58
|
+
String dataName = "credentialSuccess";
|
59
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
60
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
61
|
+
String body = messageResponse.get("body").toString();
|
62
|
+
|
63
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
64
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
65
|
+
|
66
|
+
jiraClient.checkUserCredentials(task);
|
67
|
+
}
|
68
|
+
|
69
|
+
@Test
|
70
|
+
public void test_checkUserCredentials_failOn400() throws IOException
|
71
|
+
{
|
72
|
+
String dataName = "credentialFail400";
|
73
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
74
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
75
|
+
String body = messageResponse.get("body").toString();
|
76
|
+
|
77
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
78
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
79
|
+
|
80
|
+
assertThrows("Could not authorize with your credential.", ConfigException.class, () -> jiraClient.checkUserCredentials(task));
|
81
|
+
}
|
82
|
+
|
83
|
+
@Test
|
84
|
+
public void test_checkUserCredentials_failOn401() throws IOException
|
85
|
+
{
|
86
|
+
String dataName = "credentialFail401";
|
87
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
88
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
89
|
+
String body = messageResponse.get("body").toString();
|
90
|
+
|
91
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
92
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
93
|
+
|
94
|
+
assertThrows("Could not authorize with your credential.", ConfigException.class, () -> jiraClient.checkUserCredentials(task));
|
95
|
+
}
|
96
|
+
|
97
|
+
@Test
|
98
|
+
public void test_checkUserCredentials_failOn429() throws IOException
|
99
|
+
{
|
100
|
+
String dataName = "credentialFail429";
|
101
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
102
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
103
|
+
String body = messageResponse.get("body").toString();
|
104
|
+
|
105
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
106
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
107
|
+
|
108
|
+
assertThrows("Could not authorize with your credential due to problems when contacting JIRA API.", ConfigException.class, () -> jiraClient.checkUserCredentials(task));
|
109
|
+
}
|
110
|
+
|
111
|
+
@Test
|
112
|
+
public void test_checkUserCredentials_failOn500() throws IOException
|
113
|
+
{
|
114
|
+
String dataName = "credentialFail500";
|
115
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
116
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
117
|
+
String body = messageResponse.get("body").toString();
|
118
|
+
|
119
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
120
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
121
|
+
|
122
|
+
assertThrows("Could not authorize with your credential due to problems when contacting JIRA API.", ConfigException.class, () -> jiraClient.checkUserCredentials(task));
|
123
|
+
}
|
124
|
+
|
125
|
+
@Test
|
126
|
+
public void test_getTotalCount_success() throws IOException
|
127
|
+
{
|
128
|
+
String dataName = "totalCountSuccess";
|
129
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
130
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
131
|
+
String body = messageResponse.get("body").toString();
|
132
|
+
|
133
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
134
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
135
|
+
|
136
|
+
int totalCount = jiraClient.getTotalCount(task);
|
137
|
+
assertEquals(totalCount, messageResponse.get("body").getAsJsonObject().get("total").getAsInt());
|
138
|
+
}
|
139
|
+
|
140
|
+
@Test
|
141
|
+
public void test_getTotalCount_failOnRetry() throws IOException
|
142
|
+
{
|
143
|
+
String dataName = "totalCountFailAllTime";
|
144
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
145
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
146
|
+
String body = messageResponse.get("body").toString();
|
147
|
+
|
148
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
149
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
150
|
+
|
151
|
+
assertThrows(RuntimeException.class, () -> jiraClient.getTotalCount(task));
|
152
|
+
|
153
|
+
// First try + 3 retry_limit
|
154
|
+
int expectedInvocation = 3 + 1;
|
155
|
+
verify(jiraClient, times(expectedInvocation)).createHttpClient();
|
156
|
+
verify(statusLine, times(expectedInvocation)).getStatusCode();
|
157
|
+
}
|
158
|
+
|
159
|
+
@Test
|
160
|
+
public void test_getTotalCount_doNotRetryOn400Status() throws IOException
|
161
|
+
{
|
162
|
+
String dataName = "totalCountFail400";
|
163
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
164
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
165
|
+
String body = messageResponse.get("body").toString();
|
166
|
+
|
167
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
168
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
169
|
+
|
170
|
+
assertThrows(RuntimeException.class, () -> jiraClient.getTotalCount(task));
|
171
|
+
|
172
|
+
// No retry
|
173
|
+
int expectedInvocation = 1;
|
174
|
+
verify(jiraClient, times(expectedInvocation)).createHttpClient();
|
175
|
+
verify(statusLine, times(expectedInvocation)).getStatusCode();
|
176
|
+
}
|
177
|
+
|
178
|
+
@Test
|
179
|
+
public void test_getTotalCount_retryOnIOException() throws IOException
|
180
|
+
{
|
181
|
+
when(client.execute(Mockito.any())).thenThrow(new IOException("test exeception"));
|
182
|
+
|
183
|
+
assertThrows(RuntimeException.class, () -> jiraClient.getTotalCount(task));
|
184
|
+
|
185
|
+
// First try + 3 retry_limit
|
186
|
+
int expectedInvocation = 3 + 1;
|
187
|
+
verify(jiraClient, times(expectedInvocation)).createHttpClient();
|
188
|
+
// getStatusCode is not triggered
|
189
|
+
verify(statusLine, times(0)).getStatusCode();
|
190
|
+
}
|
191
|
+
|
192
|
+
@Test
|
193
|
+
public void test_searchIssues() throws IOException
|
194
|
+
{
|
195
|
+
String dataName = "searchIssuesSuccess";
|
196
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
197
|
+
|
198
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
199
|
+
String body = messageResponse.get("body").toString();
|
200
|
+
|
201
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
202
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
203
|
+
|
204
|
+
List<Issue> issues = jiraClient.searchIssues(task, 0, 50);
|
205
|
+
assertEquals(issues.size(), 2);
|
206
|
+
}
|
207
|
+
|
208
|
+
@Test
|
209
|
+
public void test_searchIssues_failJql() throws IOException
|
210
|
+
{
|
211
|
+
String dataName = "searchIssuesFailJql";
|
212
|
+
JsonObject messageResponse = data.get(dataName).getAsJsonObject();
|
213
|
+
|
214
|
+
int statusCode = messageResponse.get("statusCode").getAsInt();
|
215
|
+
String body = messageResponse.get("body").toString();
|
216
|
+
|
217
|
+
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
218
|
+
when(response.getEntity()).thenReturn(new StringEntity(body));
|
219
|
+
|
220
|
+
assertThrows(ConfigException.class, () -> jiraClient.searchIssues(task, 0, 50));
|
221
|
+
}
|
222
|
+
}
|