embulk-input-marketo 0.6.0 → 0.6.1.alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +132 -12
- data/build.gradle +1 -1
- data/src/main/java/org/embulk/input/marketo/MarketoServiceImpl.java +115 -44
- data/src/main/java/org/embulk/input/marketo/MarketoUtils.java +83 -0
- data/src/main/java/org/embulk/input/marketo/delegate/MarketoBaseBulkExtractInputPlugin.java +17 -3
- data/src/main/java/org/embulk/input/marketo/model/BulkExtractRangeHeader.java +26 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoBaseRestClient.java +8 -3
- data/src/main/java/org/embulk/input/marketo/rest/MarketoInputStreamResponseEntityReader.java +1 -1
- data/src/main/java/org/embulk/input/marketo/rest/MarketoResponseJetty92EntityReader.java +2 -4
- data/src/main/java/org/embulk/input/marketo/rest/MarketoRestClient.java +47 -20
- data/src/test/java/org/embulk/input/marketo/MarketoServiceImplTest.java +60 -57
- data/src/test/java/org/embulk/input/marketo/delegate/ActivityBulkExtractInputPluginTest.java +16 -15
- data/src/test/java/org/embulk/input/marketo/delegate/CampaignInputPluginTest.java +10 -10
- data/src/test/java/org/embulk/input/marketo/delegate/LeadBulkExtractInputPluginTest.java +22 -21
- data/src/test/java/org/embulk/input/marketo/delegate/LeadWithListInputPluginTest.java +22 -20
- data/src/test/java/org/embulk/input/marketo/delegate/LeadWithProgramInputPluginTest.java +22 -20
- data/src/test/java/org/embulk/input/marketo/delegate/MarketoBaseBulkExtractInputPluginTest.java +27 -28
- data/src/test/java/org/embulk/input/marketo/rest/MarketoBaseRestClientTest.java +56 -57
- data/src/test/java/org/embulk/input/marketo/rest/MarketoRestClientTest.java +136 -125
- metadata +6 -5
@@ -64,6 +64,8 @@ public abstract class MarketoBaseBulkExtractInputPlugin<T extends MarketoBaseBul
|
|
64
64
|
|
65
65
|
private static final int MARKETO_MAX_RANGE_EXTRACT = 30;
|
66
66
|
|
67
|
+
private static final String IMPORTED = "imported";
|
68
|
+
|
67
69
|
public interface PluginTask extends MarketoBaseInputPluginDelegate.PluginTask, CsvTokenizer.PluginTask
|
68
70
|
{
|
69
71
|
@Config("from_date")
|
@@ -145,6 +147,7 @@ public abstract class MarketoBaseBulkExtractInputPlugin<T extends MarketoBaseBul
|
|
145
147
|
Long currentLatestFetchTime = 0L;
|
146
148
|
Set latestUIds = null;
|
147
149
|
String incrementalColumn = task.getIncrementalColumn().orNull();
|
150
|
+
int imported = 0;
|
148
151
|
if (incrementalColumn != null && task.getIncremental()) {
|
149
152
|
DateFormat df = new SimpleDateFormat(MarketoUtils.MARKETO_DATE_SIMPLE_DATE_FORMAT);
|
150
153
|
for (TaskReport taskReport : taskReports) {
|
@@ -156,12 +159,21 @@ public abstract class MarketoBaseBulkExtractInputPlugin<T extends MarketoBaseBul
|
|
156
159
|
currentLatestFetchTime = latestFetchTime;
|
157
160
|
latestUIds = taskReport.get(Set.class, LATEST_UID_LIST);
|
158
161
|
}
|
159
|
-
else if (currentLatestFetchTime
|
162
|
+
else if (currentLatestFetchTime.equals(latestFetchTime)) {
|
160
163
|
latestUIds.addAll(taskReport.get(Set.class, LATEST_UID_LIST));
|
161
164
|
}
|
165
|
+
if (taskReport.has(IMPORTED)) {
|
166
|
+
imported = imported + taskReport.get(Integer.class, IMPORTED);
|
167
|
+
}
|
162
168
|
}
|
163
169
|
// in case of we didn't import anything but search range is entirely in the past. Then we should move the the range anyway.
|
164
|
-
|
170
|
+
if (imported == 0) {
|
171
|
+
configDiff.set(FROM_DATE, df.format(task.getToDate().orNull()));
|
172
|
+
}
|
173
|
+
else {
|
174
|
+
// Otherwise it's should start from the currentLastFetchTime. If Marketo could guaranteed that they always return data of full range we could just set it to_date
|
175
|
+
configDiff.set(FROM_DATE, df.format(new Date(currentLatestFetchTime)));
|
176
|
+
}
|
165
177
|
configDiff.set(LATEST_FETCH_TIME, currentLatestFetchTime);
|
166
178
|
configDiff.set(LATEST_UID_LIST, latestUIds);
|
167
179
|
}
|
@@ -193,6 +205,7 @@ public abstract class MarketoBaseBulkExtractInputPlugin<T extends MarketoBaseBul
|
|
193
205
|
if (Exec.isPreview()) {
|
194
206
|
csvRecords = Iterators.limit(csvRecords, PREVIEW_RECORD_LIMIT);
|
195
207
|
}
|
208
|
+
int imported = 0;
|
196
209
|
while (csvRecords.hasNext()) {
|
197
210
|
Map<String, String> csvRecord = csvRecords.next();
|
198
211
|
if (task.getIncremental()) {
|
@@ -221,12 +234,13 @@ public abstract class MarketoBaseBulkExtractInputPlugin<T extends MarketoBaseBul
|
|
221
234
|
}
|
222
235
|
}
|
223
236
|
}
|
224
|
-
|
225
237
|
ObjectNode objectNode = MarketoUtils.OBJECT_MAPPER.valueToTree(csvRecord);
|
226
238
|
recordImporter.importRecord(new AllStringJacksonServiceRecord(objectNode), pageBuilder);
|
239
|
+
imported = imported + 1;
|
227
240
|
}
|
228
241
|
taskReport.set(LATEST_FETCH_TIME, currentTimestamp);
|
229
242
|
taskReport.set(LATEST_UID_LIST, latestUids);
|
243
|
+
taskReport.set(IMPORTED, imported);
|
230
244
|
return taskReport;
|
231
245
|
}
|
232
246
|
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
package org.embulk.input.marketo.model;
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Created by tai.khuu on 10/12/17.
|
5
|
+
*/
|
6
|
+
public class BulkExtractRangeHeader
|
7
|
+
{
|
8
|
+
private Long start;
|
9
|
+
private Long end;
|
10
|
+
|
11
|
+
public BulkExtractRangeHeader(long start)
|
12
|
+
{
|
13
|
+
this.start = start;
|
14
|
+
}
|
15
|
+
|
16
|
+
public BulkExtractRangeHeader(long start, long end)
|
17
|
+
{
|
18
|
+
this.start = start;
|
19
|
+
this.end = end;
|
20
|
+
}
|
21
|
+
|
22
|
+
public String toRangeHeaderValue()
|
23
|
+
{
|
24
|
+
return "bytes=" + start + "-" + (end != null ? end : "");
|
25
|
+
}
|
26
|
+
}
|
@@ -141,7 +141,12 @@ public class MarketoBaseRestClient implements AutoCloseable
|
|
141
141
|
if (content != null) {
|
142
142
|
contentProvider = new StringContentProvider(APPLICATION_JSON, content, StandardCharsets.UTF_8);
|
143
143
|
}
|
144
|
-
return
|
144
|
+
return doPost(target, headers, params, responseReader, contentProvider);
|
145
|
+
}
|
146
|
+
|
147
|
+
protected <T> T doPost(final String target, final Map<String, String> headers, final Multimap<String, String> params, Jetty92ResponseReader<T> responseReader, final ContentProvider content)
|
148
|
+
{
|
149
|
+
return doRequest(target, HttpMethod.POST, headers, params, content, responseReader);
|
145
150
|
}
|
146
151
|
|
147
152
|
protected <T> T doRequest(final String target, final HttpMethod method, final Map<String, String> headers, final Multimap<String, String> params, final ContentProvider contentProvider, Jetty92ResponseReader<T> responseReader)
|
@@ -152,7 +157,6 @@ public class MarketoBaseRestClient implements AutoCloseable
|
|
152
157
|
public void requestOnce(HttpClient client, Response.Listener responseListener)
|
153
158
|
{
|
154
159
|
Request request = client.newRequest(target).method(method);
|
155
|
-
|
156
160
|
if (headers != null) {
|
157
161
|
for (String key : headers.keySet()) {
|
158
162
|
request.header(key, headers.get(key));
|
@@ -187,7 +191,8 @@ public class MarketoBaseRestClient implements AutoCloseable
|
|
187
191
|
if (exception instanceof ExecutionException) {
|
188
192
|
this.toRetry((Exception) exception.getCause());
|
189
193
|
}
|
190
|
-
|
194
|
+
//Anything that is EOFException or cause by EOF exception
|
195
|
+
if (exception instanceof EOFException || exception.getCause() instanceof EOFException) {
|
191
196
|
return true;
|
192
197
|
}
|
193
198
|
if (exception instanceof MarketoAPIException) {
|
data/src/main/java/org/embulk/input/marketo/rest/MarketoInputStreamResponseEntityReader.java
CHANGED
@@ -29,13 +29,13 @@ public class MarketoInputStreamResponseEntityReader implements Jetty92ResponseRe
|
|
29
29
|
|
30
30
|
public MarketoInputStreamResponseEntityReader(long timeout)
|
31
31
|
{
|
32
|
-
this.listener = new InputStreamResponseListener();
|
33
32
|
this.timeout = timeout;
|
34
33
|
}
|
35
34
|
|
36
35
|
@Override
|
37
36
|
public Response.Listener getListener()
|
38
37
|
{
|
38
|
+
this.listener = new InputStreamResponseListener();
|
39
39
|
return this.listener;
|
40
40
|
}
|
41
41
|
|
@@ -36,7 +36,6 @@ public class MarketoResponseJetty92EntityReader<T> implements Jetty92ResponseRea
|
|
36
36
|
|
37
37
|
public MarketoResponseJetty92EntityReader(long timeout)
|
38
38
|
{
|
39
|
-
this.listener = new InputStreamResponseListener();
|
40
39
|
this.timeout = timeout;
|
41
40
|
javaType = OBJECT_MAPPER.getTypeFactory().constructParametrizedType(MarketoResponse.class, MarketoResponse.class, ObjectNode.class);
|
42
41
|
}
|
@@ -51,7 +50,8 @@ public class MarketoResponseJetty92EntityReader<T> implements Jetty92ResponseRea
|
|
51
50
|
@Override
|
52
51
|
public Response.Listener getListener()
|
53
52
|
{
|
54
|
-
|
53
|
+
this.listener = new InputStreamResponseListener();
|
54
|
+
return this.listener;
|
55
55
|
}
|
56
56
|
|
57
57
|
@Override
|
@@ -83,8 +83,6 @@ public class MarketoResponseJetty92EntityReader<T> implements Jetty92ResponseRea
|
|
83
83
|
InputStream inputStream = this.listener.getInputStream();
|
84
84
|
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
|
85
85
|
String reponseContent = CharStreams.toString(inputStreamReader);
|
86
|
-
//Reset this listener
|
87
|
-
this.listener = new InputStreamResponseListener();
|
88
86
|
return reponseContent;
|
89
87
|
}
|
90
88
|
}
|
@@ -6,11 +6,13 @@ import com.google.common.collect.ArrayListMultimap;
|
|
6
6
|
import com.google.common.collect.ImmutableListMultimap;
|
7
7
|
import com.google.common.collect.ImmutableMap;
|
8
8
|
import com.google.common.collect.Multimap;
|
9
|
-
import org.
|
9
|
+
import org.eclipse.jetty.client.util.FormContentProvider;
|
10
|
+
import org.eclipse.jetty.util.Fields;
|
10
11
|
import org.embulk.config.Config;
|
11
12
|
import org.embulk.config.ConfigDefault;
|
12
13
|
import org.embulk.config.Task;
|
13
14
|
import org.embulk.input.marketo.MarketoUtils;
|
15
|
+
import org.embulk.input.marketo.model.BulkExtractRangeHeader;
|
14
16
|
import org.embulk.input.marketo.model.MarketoBulkExtractRequest;
|
15
17
|
import org.embulk.input.marketo.model.MarketoError;
|
16
18
|
import org.embulk.input.marketo.model.MarketoField;
|
@@ -45,6 +47,12 @@ public class MarketoRestClient extends MarketoBaseRestClient
|
|
45
47
|
|
46
48
|
private static final String MAX_RETURN = "maxReturn";
|
47
49
|
|
50
|
+
private static final String MAX_BATCH_SIZE = "300";
|
51
|
+
|
52
|
+
private static final String DEFAULT_MAX_RETURN = "200";
|
53
|
+
|
54
|
+
private static final String RANGE_HEADER = "Range";
|
55
|
+
|
48
56
|
private String endPoint;
|
49
57
|
|
50
58
|
private Integer batchSize;
|
@@ -119,9 +127,11 @@ public class MarketoRestClient extends MarketoBaseRestClient
|
|
119
127
|
for (int i = 0; i < fields.size(); i++) {
|
120
128
|
ObjectNode field = fields.get(i);
|
121
129
|
String dataType = field.get("dataType").asText();
|
122
|
-
|
123
|
-
|
124
|
-
|
130
|
+
if (field.has("rest")) {
|
131
|
+
ObjectNode restField = (ObjectNode) field.get("rest");
|
132
|
+
String name = restField.get("name").asText();
|
133
|
+
marketoFields.add(new MarketoField(name, dataType));
|
134
|
+
}
|
125
135
|
}
|
126
136
|
return marketoFields;
|
127
137
|
}
|
@@ -261,42 +271,48 @@ public class MarketoRestClient extends MarketoBaseRestClient
|
|
261
271
|
}
|
262
272
|
}
|
263
273
|
|
264
|
-
public InputStream getLeadBulkExtractResult(String exportId)
|
274
|
+
public InputStream getLeadBulkExtractResult(String exportId, BulkExtractRangeHeader bulkExtractRangeHeader)
|
265
275
|
{
|
266
|
-
return getBulkExtractResult(MarketoRESTEndpoint.GET_LEAD_EXPORT_RESULT, exportId);
|
276
|
+
return getBulkExtractResult(MarketoRESTEndpoint.GET_LEAD_EXPORT_RESULT, exportId, bulkExtractRangeHeader);
|
267
277
|
}
|
268
278
|
|
269
|
-
public InputStream getActivitiesBulkExtractResult(String exportId)
|
279
|
+
public InputStream getActivitiesBulkExtractResult(String exportId, BulkExtractRangeHeader bulkExtractRangeHeader)
|
270
280
|
{
|
271
|
-
return getBulkExtractResult(MarketoRESTEndpoint.GET_ACTIVITY_EXPORT_RESULT, exportId);
|
281
|
+
return getBulkExtractResult(MarketoRESTEndpoint.GET_ACTIVITY_EXPORT_RESULT, exportId, bulkExtractRangeHeader);
|
272
282
|
}
|
273
283
|
|
274
|
-
private InputStream getBulkExtractResult(MarketoRESTEndpoint endpoint, String exportId)
|
284
|
+
private InputStream getBulkExtractResult(MarketoRESTEndpoint endpoint, String exportId, BulkExtractRangeHeader bulkExtractRangeHeader)
|
275
285
|
{
|
276
|
-
|
286
|
+
LOGGER.info("Download bulk export job [{}]", exportId);
|
287
|
+
Map<String, String> headers = new HashMap<>();
|
288
|
+
if (bulkExtractRangeHeader != null) {
|
289
|
+
headers.put(RANGE_HEADER, bulkExtractRangeHeader.toRangeHeaderValue());
|
290
|
+
LOGGER.info("Range header value [{}]", bulkExtractRangeHeader.toRangeHeaderValue());
|
291
|
+
}
|
292
|
+
return doGet(this.endPoint + endpoint.getEndpoint(new ImmutableMap.Builder().put("export_id", exportId).build()), headers, null, new MarketoInputStreamResponseEntityReader(READ_TIMEOUT_MILLIS));
|
277
293
|
}
|
278
294
|
|
279
295
|
public RecordPagingIterable<ObjectNode> getLists()
|
280
296
|
{
|
281
|
-
return getRecordWithTokenPagination(endPoint + MarketoRESTEndpoint.GET_LISTS.getEndpoint(),
|
297
|
+
return getRecordWithTokenPagination(endPoint + MarketoRESTEndpoint.GET_LISTS.getEndpoint(), new ImmutableListMultimap.Builder<String, String>().put(BATCH_SIZE, MAX_BATCH_SIZE).build(), ObjectNode.class);
|
282
298
|
}
|
283
299
|
|
284
300
|
public RecordPagingIterable<ObjectNode> getPrograms()
|
285
301
|
{
|
286
|
-
return getRecordWithOffsetPagination(endPoint + MarketoRESTEndpoint.GET_PROGRAMS.getEndpoint(),
|
302
|
+
return getRecordWithOffsetPagination(endPoint + MarketoRESTEndpoint.GET_PROGRAMS.getEndpoint(), new ImmutableListMultimap.Builder<String, String>().put(MAX_RETURN, DEFAULT_MAX_RETURN).build(), ObjectNode.class);
|
287
303
|
}
|
288
304
|
|
289
|
-
public RecordPagingIterable<ObjectNode> getLeadsByProgram(String programId,
|
305
|
+
public RecordPagingIterable<ObjectNode> getLeadsByProgram(String programId, String fieldNames)
|
290
306
|
{
|
291
307
|
Multimap<String, String> multimap = ArrayListMultimap.create();
|
292
|
-
multimap.put("fields",
|
308
|
+
multimap.put("fields", fieldNames);
|
293
309
|
return getRecordWithTokenPagination(endPoint + MarketoRESTEndpoint.GET_LEADS_BY_PROGRAM.getEndpoint(new ImmutableMap.Builder().put("program_id", programId).build()), multimap, ObjectNode.class);
|
294
310
|
}
|
295
311
|
|
296
|
-
public RecordPagingIterable<ObjectNode> getLeadsByList(String listId,
|
312
|
+
public RecordPagingIterable<ObjectNode> getLeadsByList(String listId, String fieldNames)
|
297
313
|
{
|
298
314
|
Multimap<String, String> multimap = ArrayListMultimap.create();
|
299
|
-
multimap.put("fields",
|
315
|
+
multimap.put("fields", fieldNames);
|
300
316
|
return getRecordWithTokenPagination(endPoint + MarketoRESTEndpoint.GET_LEADS_BY_LIST.getEndpoint(new ImmutableMap.Builder().put("list_id", listId).build()), multimap, ObjectNode.class);
|
301
317
|
}
|
302
318
|
|
@@ -348,17 +364,28 @@ public class MarketoRestClient extends MarketoBaseRestClient
|
|
348
364
|
return getTokenPage(null);
|
349
365
|
}
|
350
366
|
|
367
|
+
@SuppressWarnings("unchecked")
|
351
368
|
private RecordPagingIterable.TokenPage<T> getTokenPage(RecordPagingIterable.TokenPage page)
|
352
369
|
{
|
353
370
|
ImmutableListMultimap.Builder params = new ImmutableListMultimap.Builder<>();
|
371
|
+
params.put("_method", "GET");
|
372
|
+
Fields fields = new Fields();
|
354
373
|
if (page != null) {
|
355
|
-
|
374
|
+
fields.add(NEXT_PAGE_TOKEN, page.getNextPageToken());
|
356
375
|
}
|
357
|
-
|
376
|
+
fields.add(BATCH_SIZE, String.valueOf(batchSize));
|
358
377
|
if (parameters != null) {
|
359
|
-
|
378
|
+
for (String key : parameters.keySet()) {
|
379
|
+
//params that is passed in should overwrite default
|
380
|
+
fields.remove(key);
|
381
|
+
for (String value : parameters.get(key)) {
|
382
|
+
fields.add(key, value);
|
383
|
+
}
|
384
|
+
}
|
360
385
|
}
|
361
|
-
|
386
|
+
//Let do GET Disguise in POST here to overcome Marketo URI Too long error
|
387
|
+
FormContentProvider formContentProvider = new FormContentProvider(fields);
|
388
|
+
MarketoResponse<T> marketoResponse = doPost(endPoint, null, params.build(), new MarketoResponseJetty92EntityReader<>(READ_TIMEOUT_MILLIS, recordClass), formContentProvider);
|
362
389
|
return new RecordPagingIterable.TokenPage<>(marketoResponse.getResult(), marketoResponse.getNextPageToken(), marketoResponse.getNextPageToken() != null);
|
363
390
|
}
|
364
391
|
});
|
@@ -4,20 +4,24 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
4
4
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
5
5
|
import com.google.common.io.ByteStreams;
|
6
6
|
import org.embulk.EmbulkTestRuntime;
|
7
|
+
import org.embulk.input.marketo.model.BulkExtractRangeHeader;
|
7
8
|
import org.embulk.input.marketo.model.MarketoField;
|
8
9
|
import org.embulk.input.marketo.rest.MarketoRestClient;
|
9
10
|
import org.embulk.input.marketo.rest.RecordPagingIterable;
|
11
|
+
import org.junit.Assert;
|
10
12
|
import org.junit.Before;
|
11
13
|
import org.junit.Rule;
|
12
14
|
import org.junit.Test;
|
15
|
+
import org.mockito.Mockito;
|
13
16
|
|
14
17
|
import java.io.ByteArrayInputStream;
|
15
18
|
import java.io.File;
|
16
19
|
import java.io.FileInputStream;
|
17
|
-
import java.util
|
18
|
-
|
19
|
-
import
|
20
|
-
import
|
20
|
+
import java.util.ArrayList;
|
21
|
+
import java.util.Arrays;
|
22
|
+
import java.util.Date;
|
23
|
+
import java.util.Iterator;
|
24
|
+
import java.util.List;
|
21
25
|
|
22
26
|
/**
|
23
27
|
* Created by tai.khuu on 10/9/17.
|
@@ -33,7 +37,7 @@ public class MarketoServiceImplTest
|
|
33
37
|
@Before
|
34
38
|
public void prepare()
|
35
39
|
{
|
36
|
-
mockMarketoRestClient = mock(MarketoRestClient.class);
|
40
|
+
mockMarketoRestClient = Mockito.mock(MarketoRestClient.class);
|
37
41
|
marketoService = new MarketoServiceImpl(mockMarketoRestClient);
|
38
42
|
}
|
39
43
|
|
@@ -45,13 +49,13 @@ public class MarketoServiceImplTest
|
|
45
49
|
List<String> extractedFields = Arrays.asList("field1", "field2");
|
46
50
|
String filerField = "field1";
|
47
51
|
String exportId = "exportId";
|
48
|
-
when(mockMarketoRestClient.createLeadBulkExtract(eq(startDate), eq(endDate), eq(extractedFields), eq(filerField))).thenReturn(exportId);
|
52
|
+
Mockito.when(mockMarketoRestClient.createLeadBulkExtract(Mockito.eq(startDate), Mockito.eq(endDate), Mockito.eq(extractedFields), Mockito.eq(filerField))).thenReturn(exportId);
|
49
53
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("Test File Content".getBytes());
|
50
|
-
when(mockMarketoRestClient.getLeadBulkExtractResult(eq(exportId))).thenReturn(byteArrayInputStream);
|
54
|
+
Mockito.when(mockMarketoRestClient.getLeadBulkExtractResult(Mockito.eq(exportId), Mockito.any(BulkExtractRangeHeader.class))).thenReturn(byteArrayInputStream);
|
51
55
|
File file = marketoService.extractLead(startDate, endDate, extractedFields, filerField, 1, 3);
|
52
|
-
assertEquals("Test File Content", new String(ByteStreams.toByteArray(new FileInputStream(file))));
|
53
|
-
verify(mockMarketoRestClient, times(1)).startLeadBulkExtract(eq(exportId));
|
54
|
-
verify(mockMarketoRestClient, times(1)).waitLeadExportJobComplete(eq(exportId), eq(1), eq(3));
|
56
|
+
Assert.assertEquals("Test File Content", new String(ByteStreams.toByteArray(new FileInputStream(file))));
|
57
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).startLeadBulkExtract(Mockito.eq(exportId));
|
58
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).waitLeadExportJobComplete(Mockito.eq(exportId), Mockito.eq(1), Mockito.eq(3));
|
55
59
|
}
|
56
60
|
|
57
61
|
@Test
|
@@ -60,100 +64,99 @@ public class MarketoServiceImplTest
|
|
60
64
|
Date startDate = new Date(1507223374000L);
|
61
65
|
Date endDate = new Date(1507655374000L);
|
62
66
|
String exportId = "exportId";
|
63
|
-
when(mockMarketoRestClient.createActivityExtract(eq(startDate), eq(endDate))).thenReturn(exportId);
|
67
|
+
Mockito.when(mockMarketoRestClient.createActivityExtract(Mockito.eq(startDate), Mockito.eq(endDate))).thenReturn(exportId);
|
64
68
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("Test File Content".getBytes());
|
65
|
-
when(mockMarketoRestClient.getActivitiesBulkExtractResult(eq(exportId))).thenReturn(byteArrayInputStream);
|
69
|
+
Mockito.when(mockMarketoRestClient.getActivitiesBulkExtractResult(Mockito.eq(exportId), Mockito.any(BulkExtractRangeHeader.class))).thenReturn(byteArrayInputStream);
|
66
70
|
File file = marketoService.extractAllActivity(startDate, endDate, 1, 3);
|
67
|
-
assertEquals("Test File Content", new String(ByteStreams.toByteArray(new FileInputStream(file))));
|
68
|
-
verify(mockMarketoRestClient, times(1)).startActitvityBulkExtract(eq(exportId));
|
69
|
-
verify(mockMarketoRestClient, times(1)).waitActitvityExportJobComplete(eq(exportId), eq(1), eq(3));
|
71
|
+
Assert.assertEquals("Test File Content", new String(ByteStreams.toByteArray(new FileInputStream(file))));
|
72
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).startActitvityBulkExtract(Mockito.eq(exportId));
|
73
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).waitActitvityExportJobComplete(Mockito.eq(exportId), Mockito.eq(1), Mockito.eq(3));
|
70
74
|
}
|
71
75
|
|
72
76
|
@Test
|
73
77
|
public void getAllListLead() throws Exception
|
74
78
|
{
|
75
79
|
List<String> extractFields = Arrays.asList("field1", "field2");
|
76
|
-
RecordPagingIterable<ObjectNode> listObjectNodes = mock(RecordPagingIterable.class);
|
77
|
-
Iterator listIterator = mock(Iterator.class);
|
78
|
-
when(listIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
|
79
|
-
when(listIterator.next()).thenReturn(OBJECT_MAPPER.readTree("{\"id\":1}")).thenReturn(OBJECT_MAPPER.readTree("{\"id\":2}"));
|
80
|
-
when(listObjectNodes.iterator()).thenReturn(listIterator);
|
80
|
+
RecordPagingIterable<ObjectNode> listObjectNodes = Mockito.mock(RecordPagingIterable.class);
|
81
|
+
Iterator listIterator = Mockito.mock(Iterator.class);
|
82
|
+
Mockito.when(listIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
|
83
|
+
Mockito.when(listIterator.next()).thenReturn(OBJECT_MAPPER.readTree("{\"id\":1}")).thenReturn(OBJECT_MAPPER.readTree("{\"id\":2}"));
|
84
|
+
Mockito.when(listObjectNodes.iterator()).thenReturn(listIterator);
|
81
85
|
List<ObjectNode> leadList1 = new ArrayList<>();
|
82
86
|
leadList1.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead1\"}"));
|
83
87
|
List<ObjectNode> leadList2 = new ArrayList<>();
|
84
88
|
leadList2.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead2\"}"));
|
85
|
-
when(mockMarketoRestClient.getLists()).thenReturn(listObjectNodes);
|
86
|
-
RecordPagingIterable leadIterable1 = mock(RecordPagingIterable.class);
|
87
|
-
RecordPagingIterable leadsIterable2 = mock(RecordPagingIterable.class);
|
88
|
-
when(leadIterable1.iterator()).thenReturn(leadList1.iterator());
|
89
|
-
when(leadsIterable2.iterator()).thenReturn(leadList2.iterator());
|
90
|
-
when(mockMarketoRestClient.getLeadsByList(eq("1"), eq(
|
91
|
-
when(mockMarketoRestClient.getLeadsByList(eq("2"), eq(
|
89
|
+
Mockito.when(mockMarketoRestClient.getLists()).thenReturn(listObjectNodes);
|
90
|
+
RecordPagingIterable leadIterable1 = Mockito.mock(RecordPagingIterable.class);
|
91
|
+
RecordPagingIterable leadsIterable2 = Mockito.mock(RecordPagingIterable.class);
|
92
|
+
Mockito.when(leadIterable1.iterator()).thenReturn(leadList1.iterator());
|
93
|
+
Mockito.when(leadsIterable2.iterator()).thenReturn(leadList2.iterator());
|
94
|
+
Mockito.when(mockMarketoRestClient.getLeadsByList(Mockito.eq("1"), Mockito.eq("field1,field2"))).thenReturn(leadIterable1);
|
95
|
+
Mockito.when(mockMarketoRestClient.getLeadsByList(Mockito.eq("2"), Mockito.eq("field1,field2"))).thenReturn(leadsIterable2);
|
92
96
|
Iterable<ObjectNode> allListLead = marketoService.getAllListLead(extractFields);
|
93
|
-
assertEquals(leadList1.get(0), allListLead.iterator().next());
|
94
|
-
assertEquals(leadList2.get(0), allListLead.iterator().next());
|
97
|
+
Assert.assertEquals(leadList1.get(0), allListLead.iterator().next());
|
98
|
+
Assert.assertEquals(leadList2.get(0), allListLead.iterator().next());
|
95
99
|
}
|
96
100
|
|
97
101
|
@Test
|
98
102
|
public void getAllProgramLead() throws Exception
|
99
103
|
{
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
when(listIterator.
|
104
|
-
when(
|
105
|
-
when(listObjectNodes.iterator()).thenReturn(listIterator);
|
104
|
+
RecordPagingIterable<ObjectNode> listObjectNodes = Mockito.mock(RecordPagingIterable.class);
|
105
|
+
Iterator listIterator = Mockito.mock(Iterator.class);
|
106
|
+
Mockito.when(listIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
|
107
|
+
Mockito.when(listIterator.next()).thenReturn(OBJECT_MAPPER.readTree("{\"id\":1}")).thenReturn(OBJECT_MAPPER.readTree("{\"id\":2}"));
|
108
|
+
Mockito.when(listObjectNodes.iterator()).thenReturn(listIterator);
|
106
109
|
List<ObjectNode> leadList1 = new ArrayList<>();
|
107
110
|
leadList1.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead1\"}"));
|
108
111
|
List<ObjectNode> leadList2 = new ArrayList<>();
|
109
112
|
leadList2.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead2\"}"));
|
110
|
-
when(mockMarketoRestClient.getPrograms()).thenReturn(listObjectNodes);
|
111
|
-
RecordPagingIterable leadIterable1 = mock(RecordPagingIterable.class);
|
112
|
-
RecordPagingIterable leadsIterable2 = mock(RecordPagingIterable.class);
|
113
|
-
when(leadIterable1.iterator()).thenReturn(leadList1.iterator());
|
114
|
-
when(leadsIterable2.iterator()).thenReturn(leadList2.iterator());
|
115
|
-
when(mockMarketoRestClient.getLeadsByProgram(eq("1"), eq(
|
116
|
-
when(mockMarketoRestClient.getLeadsByProgram(eq("2"), eq(
|
117
|
-
Iterable<ObjectNode> allListLead = marketoService.getAllProgramLead(
|
118
|
-
assertEquals(leadList1.get(0), allListLead.iterator().next());
|
119
|
-
assertEquals(leadList2.get(0), allListLead.iterator().next());
|
113
|
+
Mockito.when(mockMarketoRestClient.getPrograms()).thenReturn(listObjectNodes);
|
114
|
+
RecordPagingIterable leadIterable1 = Mockito.mock(RecordPagingIterable.class);
|
115
|
+
RecordPagingIterable leadsIterable2 = Mockito.mock(RecordPagingIterable.class);
|
116
|
+
Mockito.when(leadIterable1.iterator()).thenReturn(leadList1.iterator());
|
117
|
+
Mockito.when(leadsIterable2.iterator()).thenReturn(leadList2.iterator());
|
118
|
+
Mockito.when(mockMarketoRestClient.getLeadsByProgram(Mockito.eq("1"), Mockito.eq("field1,field2"))).thenReturn(leadIterable1);
|
119
|
+
Mockito.when(mockMarketoRestClient.getLeadsByProgram(Mockito.eq("2"), Mockito.eq("field1,field2"))).thenReturn(leadsIterable2);
|
120
|
+
Iterable<ObjectNode> allListLead = marketoService.getAllProgramLead(Arrays.asList("field1", "field2"));
|
121
|
+
Assert.assertEquals(leadList1.get(0), allListLead.iterator().next());
|
122
|
+
Assert.assertEquals(leadList2.get(0), allListLead.iterator().next());
|
120
123
|
}
|
121
124
|
|
122
125
|
@Test
|
123
126
|
public void getCampaign() throws Exception
|
124
127
|
{
|
125
128
|
marketoService.getCampaign();
|
126
|
-
verify(mockMarketoRestClient, times(1)).getCampaign();
|
129
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).getCampaign();
|
127
130
|
}
|
128
131
|
|
129
132
|
@Test
|
130
133
|
public void describeLead() throws Exception
|
131
134
|
{
|
132
135
|
marketoService.describeLead();
|
133
|
-
verify(mockMarketoRestClient, times(1)).describeLead();
|
136
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).describeLead();
|
134
137
|
}
|
135
138
|
|
136
139
|
@Test
|
137
140
|
public void describeLeadByProgram() throws Exception
|
138
141
|
{
|
139
142
|
List<MarketoField> marketoFields = new ArrayList<>();
|
140
|
-
when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
143
|
+
Mockito.when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
141
144
|
marketoService.describeLeadByProgram();
|
142
|
-
verify(mockMarketoRestClient, times(1)).describeLead();
|
143
|
-
assertEquals(1, marketoFields.size());
|
144
|
-
assertEquals("programId", marketoFields.get(0).getName());
|
145
|
-
assertEquals(MarketoField.MarketoDataType.STRING, marketoFields.get(0).getMarketoDataType());
|
145
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).describeLead();
|
146
|
+
Assert.assertEquals(1, marketoFields.size());
|
147
|
+
Assert.assertEquals("programId", marketoFields.get(0).getName());
|
148
|
+
Assert.assertEquals(MarketoField.MarketoDataType.STRING, marketoFields.get(0).getMarketoDataType());
|
146
149
|
}
|
147
150
|
|
148
151
|
@Test
|
149
152
|
public void describeLeadByLists() throws Exception
|
150
153
|
{
|
151
154
|
List<MarketoField> marketoFields = new ArrayList<>();
|
152
|
-
when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
155
|
+
Mockito.when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
153
156
|
marketoService.describeLeadByLists();
|
154
|
-
verify(mockMarketoRestClient, times(1)).describeLead();
|
155
|
-
assertEquals(1, marketoFields.size());
|
156
|
-
assertEquals("listId", marketoFields.get(0).getName());
|
157
|
-
assertEquals(MarketoField.MarketoDataType.STRING, marketoFields.get(0).getMarketoDataType());
|
157
|
+
Mockito.verify(mockMarketoRestClient, Mockito.times(1)).describeLead();
|
158
|
+
Assert.assertEquals(1, marketoFields.size());
|
159
|
+
Assert.assertEquals("listId", marketoFields.get(0).getName());
|
160
|
+
Assert.assertEquals(MarketoField.MarketoDataType.STRING, marketoFields.get(0).getMarketoDataType());
|
158
161
|
}
|
159
162
|
}
|