embulk-input-marketo 0.5.7.alpha.6 → 0.6.0.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/build.gradle +6 -6
- data/src/main/java/org/embulk/input/marketo/MarketoService.java +1 -1
- data/src/main/java/org/embulk/input/marketo/MarketoServiceImpl.java +4 -5
- data/src/main/java/org/embulk/input/marketo/MarketoUtils.java +45 -14
- data/src/main/java/org/embulk/input/marketo/delegate/ActivityBulkExtractInputPlugin.java +12 -11
- data/src/main/java/org/embulk/input/marketo/delegate/CampaignInputPlugin.java +4 -23
- data/src/main/java/org/embulk/input/marketo/delegate/LeadBulkExtractInputPlugin.java +18 -11
- data/src/main/java/org/embulk/input/marketo/delegate/LeadWithListInputPlugin.java +3 -18
- data/src/main/java/org/embulk/input/marketo/delegate/LeadWithProgramInputPlugin.java +5 -20
- data/src/main/java/org/embulk/input/marketo/delegate/MarketoBaseBulkExtractInputPlugin.java +213 -87
- data/src/main/java/org/embulk/input/marketo/delegate/MarketoBaseInputPluginDelegate.java +22 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoBulkExtractRequest.java +12 -1
- data/src/main/java/org/embulk/input/marketo/model/MarketoField.java +28 -2
- data/src/main/java/org/embulk/input/marketo/model/MarketoResponse.java +2 -1
- data/src/main/java/org/embulk/input/marketo/model/filter/DateRangeFilter.java +9 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoBaseRestClient.java +4 -0
- data/src/main/java/org/embulk/input/marketo/rest/{MarketoFileResponseEntityReader.java → MarketoInputStreamResponseEntityReader.java} +2 -2
- data/src/main/java/org/embulk/input/marketo/rest/MarketoResponseJetty92EntityReader.java +4 -1
- data/src/main/java/org/embulk/input/marketo/rest/MarketoRestClient.java +76 -42
- data/src/main/java/org/embulk/input/marketo/rest/RecordPagingIterable.java +35 -11
- data/src/test/java/org/embulk/input/marketo/MarketoServiceImplTest.java +159 -0
- data/src/test/java/org/embulk/input/marketo/MarketoUtilsTest.java +87 -0
- data/src/test/java/org/embulk/input/marketo/delegate/ActivityBulkExtractInputPluginTest.java +84 -0
- data/src/test/java/org/embulk/input/marketo/delegate/CampaignInputPluginTest.java +73 -0
- data/src/test/java/org/embulk/input/marketo/delegate/LeadBulkExtractInputPluginTest.java +94 -0
- data/src/test/java/org/embulk/input/marketo/delegate/LeadWithListInputPluginTest.java +99 -0
- data/src/test/java/org/embulk/input/marketo/delegate/LeadWithProgramInputPluginTest.java +101 -0
- data/src/test/java/org/embulk/input/marketo/delegate/MarketoBaseBulkExtractInputPluginTest.java +114 -0
- data/src/test/java/org/embulk/input/marketo/rest/MarketoBaseRestClientTest.java +3 -15
- data/src/test/java/org/embulk/input/marketo/rest/MarketoRestClientTest.java +450 -0
- data/src/test/resources/config/activity_bulk_extract_config.yaml +7 -0
- data/src/test/resources/config/lead_bulk_extract_config.yaml +8 -0
- data/src/test/resources/config/rest_config.yaml +3 -0
- data/src/test/resources/fixtures/activity_extract1.csv +35 -0
- data/src/test/resources/fixtures/activity_extract2.csv +22 -0
- data/src/test/resources/fixtures/all_program_full.json +53 -0
- data/src/test/resources/fixtures/campaign_response.json +38 -0
- data/src/test/resources/fixtures/campaign_response_full.json +102 -0
- data/src/test/resources/fixtures/lead_by_list.json +33 -0
- data/src/test/resources/fixtures/lead_by_program_response.json +47 -0
- data/src/test/resources/fixtures/lead_describe.json +221 -0
- data/src/test/resources/fixtures/lead_describe_expected.json +66 -0
- data/src/test/resources/fixtures/lead_describe_marketo_fields_full.json +518 -0
- data/src/test/resources/fixtures/lead_extract1.csv +11 -0
- data/src/test/resources/fixtures/lead_response_full.json +2402 -0
- data/src/test/resources/fixtures/lead_with_program_full.json +17 -0
- data/src/test/resources/fixtures/leads_extract2.csv +10 -0
- data/src/test/resources/fixtures/lists_response.json +31 -0
- data/src/test/resources/fixtures/program_response.json +71 -0
- metadata +39 -8
- data/src/main/java/org/embulk/input/marketo/model/filter/ListFilter.java +0 -10
@@ -12,7 +12,7 @@ import java.util.NoSuchElementException;
|
|
12
12
|
*/
|
13
13
|
public class RecordPagingIterable<T> implements Iterable<T>
|
14
14
|
{
|
15
|
-
private PagingFunction pagingFunction;
|
15
|
+
private PagingFunction<Page<T>> pagingFunction;
|
16
16
|
|
17
17
|
public RecordPagingIterable(PagingFunction pagingFunction)
|
18
18
|
{
|
@@ -27,7 +27,7 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
27
27
|
|
28
28
|
private class RecordIterator implements Iterator<T>
|
29
29
|
{
|
30
|
-
Page currentPage;
|
30
|
+
Page<T> currentPage;
|
31
31
|
private Iterator<T> currentIterator;
|
32
32
|
|
33
33
|
public RecordIterator()
|
@@ -39,9 +39,18 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
39
39
|
{
|
40
40
|
if (currentPage == null) {
|
41
41
|
currentPage = pagingFunction.getFirstPage();
|
42
|
-
this.currentIterator = currentPage.
|
42
|
+
this.currentIterator = currentPage.getRecordsIter();
|
43
43
|
}
|
44
|
-
|
44
|
+
if (currentIterator.hasNext()) {
|
45
|
+
return true;
|
46
|
+
}
|
47
|
+
if (!currentPage.hasNext) {
|
48
|
+
return false;
|
49
|
+
}
|
50
|
+
Page<T> nextPage = pagingFunction.getNextPage(currentPage);
|
51
|
+
currentIterator = nextPage.getRecordsIter();
|
52
|
+
currentPage = nextPage;
|
53
|
+
return currentIterator.hasNext();
|
45
54
|
}
|
46
55
|
|
47
56
|
@Override
|
@@ -50,10 +59,6 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
50
59
|
if (!hasNext()) {
|
51
60
|
throw new NoSuchElementException("Call next on an empty iterator");
|
52
61
|
}
|
53
|
-
if (!currentIterator.hasNext()) {
|
54
|
-
currentPage = pagingFunction.getNextPage(currentPage);
|
55
|
-
currentIterator = currentPage.getRecords().iterator();
|
56
|
-
}
|
57
62
|
return currentIterator.next();
|
58
63
|
}
|
59
64
|
|
@@ -90,7 +95,10 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
90
95
|
{
|
91
96
|
return records;
|
92
97
|
}
|
93
|
-
|
98
|
+
public Iterator<T> getRecordsIter()
|
99
|
+
{
|
100
|
+
return records.iterator();
|
101
|
+
}
|
94
102
|
public void setRecords(List<T> records)
|
95
103
|
{
|
96
104
|
this.records = records;
|
@@ -107,11 +115,11 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
107
115
|
}
|
108
116
|
}
|
109
117
|
|
110
|
-
public static class
|
118
|
+
public static class TokenPage<T> extends Page<T>
|
111
119
|
{
|
112
120
|
private String nextPageToken;
|
113
121
|
|
114
|
-
public
|
122
|
+
public TokenPage(Iterable<T> records, String nextPageToken, boolean moreResult)
|
115
123
|
{
|
116
124
|
super(records, moreResult);
|
117
125
|
this.nextPageToken = nextPageToken;
|
@@ -127,4 +135,20 @@ public class RecordPagingIterable<T> implements Iterable<T>
|
|
127
135
|
this.nextPageToken = nextPageToken;
|
128
136
|
}
|
129
137
|
}
|
138
|
+
|
139
|
+
public static class OffsetPage<T> extends Page<T>
|
140
|
+
{
|
141
|
+
private int nextOffSet;
|
142
|
+
|
143
|
+
public OffsetPage(Iterable<T> records, int nextOffSet, boolean moreResult)
|
144
|
+
{
|
145
|
+
super(records, moreResult);
|
146
|
+
this.nextOffSet = nextOffSet;
|
147
|
+
}
|
148
|
+
|
149
|
+
public int getNextOffSet()
|
150
|
+
{
|
151
|
+
return nextOffSet;
|
152
|
+
}
|
153
|
+
}
|
130
154
|
}
|
@@ -0,0 +1,159 @@
|
|
1
|
+
package org.embulk.input.marketo;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
5
|
+
import com.google.common.io.ByteStreams;
|
6
|
+
import org.embulk.EmbulkTestRuntime;
|
7
|
+
import org.embulk.input.marketo.model.MarketoField;
|
8
|
+
import org.embulk.input.marketo.rest.MarketoRestClient;
|
9
|
+
import org.embulk.input.marketo.rest.RecordPagingIterable;
|
10
|
+
import org.junit.Before;
|
11
|
+
import org.junit.Rule;
|
12
|
+
import org.junit.Test;
|
13
|
+
|
14
|
+
import java.io.ByteArrayInputStream;
|
15
|
+
import java.io.File;
|
16
|
+
import java.io.FileInputStream;
|
17
|
+
import java.util.*;
|
18
|
+
|
19
|
+
import static org.junit.Assert.*;
|
20
|
+
import static org.mockito.Mockito.*;
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Created by tai.khuu on 10/9/17.
|
24
|
+
*/
|
25
|
+
public class MarketoServiceImplTest
|
26
|
+
{
|
27
|
+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
28
|
+
@Rule
|
29
|
+
public EmbulkTestRuntime embulkTestRuntime = new EmbulkTestRuntime();
|
30
|
+
private MarketoService marketoService;
|
31
|
+
|
32
|
+
private MarketoRestClient mockMarketoRestClient;
|
33
|
+
@Before
|
34
|
+
public void prepare()
|
35
|
+
{
|
36
|
+
mockMarketoRestClient = mock(MarketoRestClient.class);
|
37
|
+
marketoService = new MarketoServiceImpl(mockMarketoRestClient);
|
38
|
+
}
|
39
|
+
|
40
|
+
@Test
|
41
|
+
public void extractLead() throws Exception
|
42
|
+
{
|
43
|
+
Date startDate = new Date(1507223374000L);
|
44
|
+
Date endDate = new Date(1507655374000L);
|
45
|
+
List<String> extractedFields = Arrays.asList("field1", "field2");
|
46
|
+
String filerField = "field1";
|
47
|
+
String exportId = "exportId";
|
48
|
+
when(mockMarketoRestClient.createLeadBulkExtract(eq(startDate), eq(endDate), eq(extractedFields), eq(filerField))).thenReturn(exportId);
|
49
|
+
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("Test File Content".getBytes());
|
50
|
+
when(mockMarketoRestClient.getLeadBulkExtractResult(eq(exportId))).thenReturn(byteArrayInputStream);
|
51
|
+
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));
|
55
|
+
}
|
56
|
+
|
57
|
+
@Test
|
58
|
+
public void extractAllActivity() throws Exception
|
59
|
+
{
|
60
|
+
Date startDate = new Date(1507223374000L);
|
61
|
+
Date endDate = new Date(1507655374000L);
|
62
|
+
String exportId = "exportId";
|
63
|
+
when(mockMarketoRestClient.createActivityExtract(eq(startDate), eq(endDate))).thenReturn(exportId);
|
64
|
+
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("Test File Content".getBytes());
|
65
|
+
when(mockMarketoRestClient.getActivitiesBulkExtractResult(eq(exportId))).thenReturn(byteArrayInputStream);
|
66
|
+
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));
|
70
|
+
}
|
71
|
+
|
72
|
+
@Test
|
73
|
+
public void getAllListLead() throws Exception
|
74
|
+
{
|
75
|
+
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);
|
81
|
+
List<ObjectNode> leadList1 = new ArrayList<>();
|
82
|
+
leadList1.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead1\"}"));
|
83
|
+
List<ObjectNode> leadList2 = new ArrayList<>();
|
84
|
+
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(extractFields))).thenReturn(leadIterable1);
|
91
|
+
when(mockMarketoRestClient.getLeadsByList(eq("2"), eq(extractFields))).thenReturn(leadsIterable2);
|
92
|
+
Iterable<ObjectNode> allListLead = marketoService.getAllListLead(extractFields);
|
93
|
+
assertEquals(leadList1.get(0), allListLead.iterator().next());
|
94
|
+
assertEquals(leadList2.get(0), allListLead.iterator().next());
|
95
|
+
}
|
96
|
+
|
97
|
+
@Test
|
98
|
+
public void getAllProgramLead() throws Exception
|
99
|
+
{
|
100
|
+
List<String> extractFields = Arrays.asList("field1", "field2");
|
101
|
+
RecordPagingIterable<ObjectNode> listObjectNodes = mock(RecordPagingIterable.class);
|
102
|
+
Iterator listIterator = mock(Iterator.class);
|
103
|
+
when(listIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
|
104
|
+
when(listIterator.next()).thenReturn(OBJECT_MAPPER.readTree("{\"id\":1}")).thenReturn(OBJECT_MAPPER.readTree("{\"id\":2}"));
|
105
|
+
when(listObjectNodes.iterator()).thenReturn(listIterator);
|
106
|
+
List<ObjectNode> leadList1 = new ArrayList<>();
|
107
|
+
leadList1.add((ObjectNode) OBJECT_MAPPER.readTree("{\"id\":\"lead1\"}"));
|
108
|
+
List<ObjectNode> leadList2 = new ArrayList<>();
|
109
|
+
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(extractFields))).thenReturn(leadIterable1);
|
116
|
+
when(mockMarketoRestClient.getLeadsByProgram(eq("2"), eq(extractFields))).thenReturn(leadsIterable2);
|
117
|
+
Iterable<ObjectNode> allListLead = marketoService.getAllProgramLead(extractFields);
|
118
|
+
assertEquals(leadList1.get(0), allListLead.iterator().next());
|
119
|
+
assertEquals(leadList2.get(0), allListLead.iterator().next());
|
120
|
+
}
|
121
|
+
|
122
|
+
@Test
|
123
|
+
public void getCampaign() throws Exception
|
124
|
+
{
|
125
|
+
marketoService.getCampaign();
|
126
|
+
verify(mockMarketoRestClient, times(1)).getCampaign();
|
127
|
+
}
|
128
|
+
|
129
|
+
@Test
|
130
|
+
public void describeLead() throws Exception
|
131
|
+
{
|
132
|
+
marketoService.describeLead();
|
133
|
+
verify(mockMarketoRestClient, times(1)).describeLead();
|
134
|
+
}
|
135
|
+
|
136
|
+
@Test
|
137
|
+
public void describeLeadByProgram() throws Exception
|
138
|
+
{
|
139
|
+
List<MarketoField> marketoFields = new ArrayList<>();
|
140
|
+
when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
141
|
+
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());
|
146
|
+
}
|
147
|
+
|
148
|
+
@Test
|
149
|
+
public void describeLeadByLists() throws Exception
|
150
|
+
{
|
151
|
+
List<MarketoField> marketoFields = new ArrayList<>();
|
152
|
+
when(mockMarketoRestClient.describeLead()).thenReturn(marketoFields);
|
153
|
+
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());
|
158
|
+
}
|
159
|
+
}
|
@@ -0,0 +1,87 @@
|
|
1
|
+
package org.embulk.input.marketo;
|
2
|
+
|
3
|
+
import org.embulk.base.restclient.ServiceResponseMapper;
|
4
|
+
import org.embulk.base.restclient.record.ValueLocator;
|
5
|
+
import org.embulk.input.marketo.model.MarketoField;
|
6
|
+
import org.embulk.spi.Column;
|
7
|
+
import org.embulk.spi.type.Types;
|
8
|
+
import org.joda.time.DateTime;
|
9
|
+
import org.junit.Test;
|
10
|
+
|
11
|
+
import java.util.ArrayList;
|
12
|
+
import java.util.List;
|
13
|
+
|
14
|
+
import static org.junit.Assert.assertEquals;
|
15
|
+
import static org.junit.Assert.assertFalse;
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Created by tai.khuu on 10/7/17.
|
19
|
+
*/
|
20
|
+
public class MarketoUtilsTest
|
21
|
+
{
|
22
|
+
@Test
|
23
|
+
public void buildDynamicResponseMapper() throws Exception
|
24
|
+
{
|
25
|
+
List<MarketoField> marketoFields = new ArrayList<>();
|
26
|
+
marketoFields.add(new MarketoField("marketoField1", "text"));
|
27
|
+
marketoFields.add(new MarketoField("marketoField2", "date"));
|
28
|
+
marketoFields.add(new MarketoField("marketoField3", "datetime"));
|
29
|
+
ServiceResponseMapper<? extends ValueLocator> mapper = MarketoUtils.buildDynamicResponseMapper("marketo", marketoFields);
|
30
|
+
List<Column> columns = mapper.getEmbulkSchema().getColumns();
|
31
|
+
Column column1 = columns.get(0);
|
32
|
+
assertEquals("marketo_marketoField1", column1.getName());
|
33
|
+
assertEquals(Types.STRING, column1.getType());
|
34
|
+
Column column2 = columns.get(1);
|
35
|
+
assertEquals(Types.TIMESTAMP, column2.getType());
|
36
|
+
assertEquals("marketo_marketoField2", column2.getName());
|
37
|
+
Column column3 = columns.get(2);
|
38
|
+
assertEquals("marketo_marketoField3", column3.getName());
|
39
|
+
assertEquals(Types.TIMESTAMP, column3.getType());
|
40
|
+
}
|
41
|
+
|
42
|
+
@Test
|
43
|
+
public void getFieldNameFromMarketoFields() throws Exception
|
44
|
+
{
|
45
|
+
List<MarketoField> marketoFields = new ArrayList<>();
|
46
|
+
marketoFields.add(new MarketoField("marketoField1", "text"));
|
47
|
+
marketoFields.add(new MarketoField("marketoField2", "date"));
|
48
|
+
marketoFields.add(new MarketoField("marketoField3", "datetime"));
|
49
|
+
List<String> marketoFieldList = MarketoUtils.getFieldNameFromMarketoFields(marketoFields, "marketoField2");
|
50
|
+
assertEquals(2, marketoFieldList.size());
|
51
|
+
assertFalse(marketoFieldList.contains("marketoField2"));
|
52
|
+
}
|
53
|
+
|
54
|
+
@Test
|
55
|
+
public void buildColumnName() throws Exception
|
56
|
+
{
|
57
|
+
String columnName = MarketoUtils.buildColumnName("prefix", "columnName");
|
58
|
+
assertEquals("prefix_columnName", columnName);
|
59
|
+
}
|
60
|
+
|
61
|
+
@Test
|
62
|
+
public void getIdentityEndPoint() throws Exception
|
63
|
+
{
|
64
|
+
String identityEndPoint = MarketoUtils.getIdentityEndPoint("accountId");
|
65
|
+
assertEquals("https://accountId.mktorest.com/identity", identityEndPoint);
|
66
|
+
}
|
67
|
+
|
68
|
+
@Test
|
69
|
+
public void getEndPoint() throws Exception
|
70
|
+
{
|
71
|
+
String endPoint = MarketoUtils.getEndPoint("accountId");
|
72
|
+
assertEquals("https://accountId.mktorest.com", endPoint);
|
73
|
+
}
|
74
|
+
|
75
|
+
@Test
|
76
|
+
public void sliceRange() throws Exception
|
77
|
+
{
|
78
|
+
DateTime startDate = new DateTime(1507369760000L);
|
79
|
+
List<MarketoUtils.DateRange> dateRanges1 = MarketoUtils.sliceRange(startDate, startDate.plusDays(7), 2);
|
80
|
+
assertEquals(4, dateRanges1.size());
|
81
|
+
assertEquals(startDate.plusDays(7), dateRanges1.get(3).toDate);
|
82
|
+
|
83
|
+
List<MarketoUtils.DateRange> dateRanges2 = MarketoUtils.sliceRange(startDate, startDate.plusDays(1), 2);
|
84
|
+
assertEquals(1, dateRanges2.size());
|
85
|
+
assertEquals(startDate.plusDays(1), dateRanges2.get(0).toDate);
|
86
|
+
}
|
87
|
+
}
|
@@ -0,0 +1,84 @@
|
|
1
|
+
package org.embulk.input.marketo.delegate;
|
2
|
+
|
3
|
+
import org.embulk.EmbulkTestRuntime;
|
4
|
+
import org.embulk.base.restclient.ServiceResponseMapper;
|
5
|
+
import org.embulk.base.restclient.record.ValueLocator;
|
6
|
+
import org.embulk.config.ConfigLoader;
|
7
|
+
import org.embulk.config.ConfigSource;
|
8
|
+
import org.embulk.config.TaskReport;
|
9
|
+
import org.embulk.input.marketo.rest.MarketoRestClient;
|
10
|
+
import org.embulk.spi.Column;
|
11
|
+
import org.embulk.spi.PageBuilder;
|
12
|
+
import org.joda.time.DateTime;
|
13
|
+
import org.junit.Before;
|
14
|
+
import org.junit.Rule;
|
15
|
+
import org.junit.Test;
|
16
|
+
import org.mockito.ArgumentCaptor;
|
17
|
+
|
18
|
+
import java.io.IOException;
|
19
|
+
import java.util.Arrays;
|
20
|
+
import java.util.Date;
|
21
|
+
import java.util.List;
|
22
|
+
import java.util.Set;
|
23
|
+
|
24
|
+
import static org.junit.Assert.assertEquals;
|
25
|
+
import static org.mockito.ArgumentMatchers.any;
|
26
|
+
import static org.mockito.Mockito.*;
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Created by khuutantaitai on 10/3/17.
|
30
|
+
*/
|
31
|
+
public class ActivityBulkExtractInputPluginTest
|
32
|
+
{
|
33
|
+
@Rule
|
34
|
+
public EmbulkTestRuntime embulkTestRuntime = new EmbulkTestRuntime();
|
35
|
+
|
36
|
+
private ActivityBulkExtractInputPlugin activityBulkExtractInputPlugin;
|
37
|
+
|
38
|
+
private ConfigSource configSource;
|
39
|
+
|
40
|
+
private MarketoRestClient mockMarketoRestclient;
|
41
|
+
|
42
|
+
@Before
|
43
|
+
public void prepare() throws IOException
|
44
|
+
{
|
45
|
+
activityBulkExtractInputPlugin = spy(new ActivityBulkExtractInputPlugin());
|
46
|
+
ConfigLoader configLoader = embulkTestRuntime.getInjector().getInstance(ConfigLoader.class);
|
47
|
+
configSource = configLoader.fromYaml(this.getClass().getResourceAsStream("/config/activity_bulk_extract_config.yaml"));
|
48
|
+
mockMarketoRestclient = mock(MarketoRestClient.class);
|
49
|
+
doReturn(mockMarketoRestclient).when(activityBulkExtractInputPlugin).createMarketoRestClient(any(ActivityBulkExtractInputPlugin.PluginTask.class));
|
50
|
+
}
|
51
|
+
|
52
|
+
@Test
|
53
|
+
public void testRun() throws InterruptedException
|
54
|
+
{
|
55
|
+
ActivityBulkExtractInputPlugin.PluginTask task = configSource.loadConfig(ActivityBulkExtractInputPlugin.PluginTask.class);
|
56
|
+
DateTime startDate = new DateTime(task.getFromDate());
|
57
|
+
PageBuilder pageBuilder = mock(PageBuilder.class);
|
58
|
+
String exportId1 = "exportId1";
|
59
|
+
String exportId2 = "exportId2";
|
60
|
+
when(mockMarketoRestclient.createActivityExtract(any(Date.class), any(Date.class))).thenReturn(exportId1).thenReturn(exportId2).thenReturn(null);
|
61
|
+
when(mockMarketoRestclient.getActivitiesBulkExtractResult(eq(exportId1))).thenReturn(this.getClass().getResourceAsStream("/fixtures/activity_extract1.csv"));
|
62
|
+
when(mockMarketoRestclient.getActivitiesBulkExtractResult(eq(exportId2))).thenReturn(this.getClass().getResourceAsStream("/fixtures/activity_extract2.csv"));
|
63
|
+
ServiceResponseMapper<? extends ValueLocator> mapper = activityBulkExtractInputPlugin.buildServiceResponseMapper(task);
|
64
|
+
activityBulkExtractInputPlugin.validateInputTask(task);
|
65
|
+
TaskReport taskReport = activityBulkExtractInputPlugin.ingestServiceData(task, mapper.createRecordImporter(), 1, pageBuilder);
|
66
|
+
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
|
67
|
+
Column marketoGUID = mapper.getEmbulkSchema().lookupColumn("marketoGUID");
|
68
|
+
verify(pageBuilder, times(55)).setString(eq(marketoGUID), argumentCaptor.capture());
|
69
|
+
verify(mockMarketoRestclient, times(1)).startActitvityBulkExtract(eq(exportId1));
|
70
|
+
verify(mockMarketoRestclient, times(1)).waitActitvityExportJobComplete(eq(exportId1), eq(task.getPollingIntervalSecond()), eq(task.getBulkJobTimeoutSecond()));
|
71
|
+
verify(mockMarketoRestclient, times(1)).startActitvityBulkExtract(eq(exportId2));
|
72
|
+
verify(mockMarketoRestclient, times(1)).waitActitvityExportJobComplete(eq(exportId2), eq(task.getPollingIntervalSecond()), eq(task.getBulkJobTimeoutSecond()));
|
73
|
+
verify(mockMarketoRestclient, times(1)).createActivityExtract(startDate.toDate(), startDate.plusDays(30).toDate());
|
74
|
+
DateTime startDate2 = startDate.plusDays(30).plusSeconds(1);
|
75
|
+
verify(mockMarketoRestclient, times(1)).createActivityExtract(startDate2.toDate(), startDate.plusDays(task.getFetchDays()).toDate());
|
76
|
+
List<String> marketoUids = argumentCaptor.getAllValues();
|
77
|
+
assertEquals(55, marketoUids.size());
|
78
|
+
long latestFetchTime = taskReport.get(Long.class, "latest_fetch_time");
|
79
|
+
Set latestUids = taskReport.get(Set.class, "latest_uids");
|
80
|
+
assertEquals(1504888754000L, latestFetchTime);
|
81
|
+
assertEquals(Arrays.asList("558681", "558682", "558683", "558684", "558685", "558686", "558687", "558688", "558689", "558690", "558691", "558692", "558693", "558694", "558695", "558696", "558697", "558698", "558699", "558700", "558701", "558702", "558703", "558704", "558705", "558706", "558707", "558708", "558709", "558710", "558711", "558712", "558713", "558714", "558716", "558717", "558718", "558719", "558720", "558721", "558722", "558723", "558724", "558725", "558726", "558727", "558728", "558729", "558730", "558731", "558732", "558733", "558734", "558735", "558736"), marketoUids);
|
82
|
+
assertEquals(36, latestUids.size());
|
83
|
+
}
|
84
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
package org.embulk.input.marketo.delegate;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.JavaType;
|
4
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
6
|
+
import org.embulk.EmbulkTestRuntime;
|
7
|
+
import org.embulk.base.restclient.ServiceResponseMapper;
|
8
|
+
import org.embulk.base.restclient.record.RecordImporter;
|
9
|
+
import org.embulk.base.restclient.record.ValueLocator;
|
10
|
+
import org.embulk.config.ConfigLoader;
|
11
|
+
import org.embulk.config.ConfigSource;
|
12
|
+
import org.embulk.input.marketo.rest.MarketoRestClient;
|
13
|
+
import org.embulk.input.marketo.rest.RecordPagingIterable;
|
14
|
+
import org.embulk.spi.PageBuilder;
|
15
|
+
import org.embulk.spi.Schema;
|
16
|
+
import org.junit.Before;
|
17
|
+
import org.junit.Rule;
|
18
|
+
import org.junit.Test;
|
19
|
+
import org.mockito.ArgumentCaptor;
|
20
|
+
|
21
|
+
import java.io.IOException;
|
22
|
+
import java.util.List;
|
23
|
+
|
24
|
+
import static org.junit.Assert.assertArrayEquals;
|
25
|
+
import static org.mockito.Mockito.*;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Created by tai.khuu on 10/10/17.
|
29
|
+
*/
|
30
|
+
public class CampaignInputPluginTest
|
31
|
+
{
|
32
|
+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
33
|
+
|
34
|
+
@Rule
|
35
|
+
public EmbulkTestRuntime embulkTestRuntime = new EmbulkTestRuntime();
|
36
|
+
|
37
|
+
private ConfigSource configSource;
|
38
|
+
|
39
|
+
private CampaignInputPlugin campaignInputPlugin;
|
40
|
+
|
41
|
+
private MarketoRestClient mockMarketoRestClient;
|
42
|
+
|
43
|
+
@Before
|
44
|
+
public void setUp() throws Exception
|
45
|
+
{
|
46
|
+
campaignInputPlugin = spy(new CampaignInputPlugin());
|
47
|
+
ConfigLoader configLoader = embulkTestRuntime.getInjector().getInstance(ConfigLoader.class);
|
48
|
+
configSource = configLoader.fromYaml(this.getClass().getResourceAsStream("/config/rest_config.yaml"));
|
49
|
+
mockMarketoRestClient = mock(MarketoRestClient.class);
|
50
|
+
doReturn(mockMarketoRestClient).when(campaignInputPlugin).createMarketoRestClient(any(CampaignInputPlugin.PluginTask.class));
|
51
|
+
}
|
52
|
+
|
53
|
+
@Test
|
54
|
+
public void testRun() throws IOException
|
55
|
+
{
|
56
|
+
RecordPagingIterable<ObjectNode> mockRecordPagingIterable = mock(RecordPagingIterable.class);
|
57
|
+
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametrizedType(List.class, List.class, ObjectNode.class);
|
58
|
+
List<ObjectNode> objectNodeList = OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/fixtures/campaign_response_full.json"), javaType);
|
59
|
+
when(mockRecordPagingIterable.iterator()).thenReturn(objectNodeList.iterator());
|
60
|
+
when(mockMarketoRestClient.getCampaign()).thenReturn(mockRecordPagingIterable);
|
61
|
+
CampaignInputPlugin.PluginTask task = configSource.loadConfig(CampaignInputPlugin.PluginTask.class);
|
62
|
+
ServiceResponseMapper<? extends ValueLocator> mapper = campaignInputPlugin.buildServiceResponseMapper(task);
|
63
|
+
RecordImporter recordImporter = mapper.createRecordImporter();
|
64
|
+
PageBuilder mockPageBuilder = mock(PageBuilder.class);
|
65
|
+
campaignInputPlugin.ingestServiceData(task, recordImporter, 1, mockPageBuilder);
|
66
|
+
verify(mockMarketoRestClient, times(1)).getCampaign();
|
67
|
+
Schema embulkSchema = mapper.getEmbulkSchema();
|
68
|
+
ArgumentCaptor<Long> longArgumentCaptor = ArgumentCaptor.forClass(Long.class);
|
69
|
+
verify(mockPageBuilder, times(10)).setLong(eq(embulkSchema.lookupColumn("id")), longArgumentCaptor.capture());
|
70
|
+
List<Long> allValues = longArgumentCaptor.getAllValues();
|
71
|
+
assertArrayEquals(new Long[]{1003L, 1004L, 1005L, 1006L, 1007L, 1008L, 1029L, 1048L, 1051L, 1065L}, allValues.toArray());
|
72
|
+
}
|
73
|
+
}
|