forked from treasure-data/embulk-input-marketo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/java/org/embulk/input/marketo/delegate/OpportunityRoleInputPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.embulk.input.marketo.delegate; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.embulk.EmbulkTestRuntime; | ||
import org.embulk.base.restclient.ServiceResponseMapper; | ||
import org.embulk.base.restclient.record.RecordImporter; | ||
import org.embulk.base.restclient.record.ValueLocator; | ||
import org.embulk.config.ConfigException; | ||
import org.embulk.config.ConfigLoader; | ||
import org.embulk.config.ConfigSource; | ||
import org.embulk.input.marketo.rest.MarketoRestClient; | ||
import org.embulk.input.marketo.rest.RecordPagingIterable; | ||
import org.embulk.spi.PageBuilder; | ||
import org.embulk.spi.Schema; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.embulk.input.marketo.MarketoUtilsTest.CONFIG_MAPPER; | ||
import static org.embulk.input.marketo.delegate.OpportunityRoleInputPlugin.PluginTask; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.anyString; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class OpportunityRoleInputPluginTest | ||
{ | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
@Rule | ||
public EmbulkTestRuntime embulkTestRuntime = new EmbulkTestRuntime(); | ||
|
||
private OpportunityRoleInputPlugin opportunityRoleInputPlugin; | ||
|
||
private ConfigSource configSource; | ||
|
||
private MarketoRestClient mockMarketoRestClient; | ||
|
||
@Before | ||
public void setUp() throws Exception | ||
{ | ||
opportunityRoleInputPlugin = spy(new OpportunityRoleInputPlugin()); | ||
ConfigLoader configLoader = embulkTestRuntime.getInjector().getInstance(ConfigLoader.class); | ||
configSource = configLoader.fromYaml(this.getClass().getResourceAsStream("/config/opportunity_role.yaml")); | ||
mockMarketoRestClient = mock(MarketoRestClient.class); | ||
doReturn(mockMarketoRestClient).when(opportunityRoleInputPlugin).createMarketoRestClient(any(PluginTask.class)); | ||
} | ||
|
||
@Test | ||
public void testValidPluginTask() | ||
{ | ||
PluginTask pluginTask = mapTask(configSource); | ||
opportunityRoleInputPlugin.validateInputTask(pluginTask); | ||
} | ||
|
||
@Test | ||
public void testOpportunityRoleFilterTypeError() | ||
{ | ||
PluginTask pluginTask = mapTask(configSource.set("opportunity_role_filter_type", "")); | ||
Assert.assertThrows(ConfigException.class, () -> opportunityRoleInputPlugin.validateInputTask(pluginTask)); | ||
} | ||
|
||
@Test | ||
public void testEmptyStringFilterValues() | ||
{ | ||
PluginTask pluginTask = mapTask(configSource.set("opportunity_role_filter_values", "")); | ||
Assert.assertThrows(ConfigException.class, () -> opportunityRoleInputPlugin.validateInputTask(pluginTask)); | ||
} | ||
|
||
@Test | ||
public void testAllEmptyStringFilterValues() | ||
{ | ||
PluginTask pluginTask = mapTask(configSource.set("opportunity_role_filter_values", ",, , ")); | ||
Assert.assertThrows(ConfigException.class, () -> opportunityRoleInputPlugin.validateInputTask(pluginTask)); | ||
} | ||
|
||
@Test | ||
public void testRun() throws IOException | ||
{ | ||
RecordPagingIterable<ObjectNode> mockRecordPagingIterable = mock(RecordPagingIterable.class); | ||
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametrizedType(List.class, List.class, ObjectNode.class); | ||
List<ObjectNode> objectNodeList = OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/fixtures/opportunity_role_response.json"), javaType); | ||
when(mockRecordPagingIterable.iterator()).thenReturn(objectNodeList.iterator()); | ||
when(mockMarketoRestClient.getOpportunityRoles(anyString(), anyString())).thenReturn(mockRecordPagingIterable); | ||
|
||
PluginTask task = mapTask(configSource); | ||
ServiceResponseMapper<? extends ValueLocator> mapper = opportunityRoleInputPlugin.buildServiceResponseMapper(task); | ||
RecordImporter recordImporter = mapper.createRecordImporter(); | ||
PageBuilder mockPageBuilder = mock(PageBuilder.class); | ||
opportunityRoleInputPlugin.ingestServiceData(task, recordImporter, 1, mockPageBuilder); | ||
verify(mockMarketoRestClient, times(1)).getOpportunityRoles(anyString(), anyString()); | ||
|
||
Schema schema = mapper.getEmbulkSchema(); | ||
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); | ||
verify(mockPageBuilder, times(3)).setString(eq(schema.lookupColumn("externalOpportunityId")), stringArgumentCaptor.capture()); | ||
List<String> allValues = stringArgumentCaptor.getAllValues(); | ||
assertArrayEquals(new String[]{"externalOpportunityId_1", "externalOpportunityId_2", "externalOpportunityId_3"}, allValues.toArray()); | ||
} | ||
|
||
private PluginTask mapTask(ConfigSource config) | ||
{ | ||
return CONFIG_MAPPER.map(config, PluginTask.class); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
account_id: account_id | ||
client_id: client_id | ||
client_secret: client_secret | ||
opportunity_role_filter_type: opportunity_filter_type | ||
opportunity_role_filter_values: opportunity_filter_values | ||
|
30 changes: 30 additions & 0 deletions
30
src/test/resources/fixtures/opportunity_role_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[ | ||
{ | ||
"seq": 0, | ||
"marketoGUID": "marketoGUID_1", | ||
"role": null, | ||
"externalOpportunityId": "externalOpportunityId_1", | ||
"createdAt": "2022-12-06T04:11:14Z", | ||
"updatedAt": "2022-12-06T04:11:14Z", | ||
"leadId": 1 | ||
}, | ||
{ | ||
"seq": 1, | ||
"marketoGUID": "marketoGUID_2", | ||
"role": "Closing", | ||
"externalOpportunityId": "externalOpportunityId_2", | ||
"createdAt": "2022-12-06T04:11:14Z", | ||
"updatedAt": "2022-12-06T04:11:14Z", | ||
"leadId": 22 | ||
}, | ||
{ | ||
"seq": 2, | ||
"marketoGUID": "marketoGUID_3", | ||
"role": "Introducing", | ||
"externalOpportunityId": "externalOpportunityId_3", | ||
"createdAt": "2022-12-06T04:11:14Z", | ||
"updatedAt": "2022-12-06T04:11:14Z", | ||
"leadId": 33 | ||
} | ||
] | ||
|