@Test(expected = ConnectionException.class) public void testCreateBulkWithTimeOutException() throws Exception { SalesforceConnector connector = new SalesforceConnector(); SaveResult saveResult = Mockito.mock(SaveResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); when(partnerConnection.create(Mockito.argThat(new SObjectArrayMatcher()))) .thenReturn(new SaveResult[] {saveResult}); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); JobInfo jobInfo = Mockito.mock(JobInfo.class); BatchRequest batchRequest = Mockito.mock(BatchRequest.class); AsyncApiException exception = Mockito.mock(AsyncApiException.class); doReturn(AsyncExceptionCode.InvalidSessionId).when(exception).getExceptionCode(); doReturn(jobInfo).when(bulkConnection).createJob(any(JobInfo.class)); doReturn(batchRequest).when(bulkConnection).createBatch(any(JobInfo.class)); doThrow(exception).when(batchRequest).completeRequest(); Map<String, Object> sObject = new HashMap<String, Object>(); sObject.put(FIRST_NAME_FIELD, FIRST_NAME); sObject.put(LAST_NAME_FIELD, LAST_NAME); List<Map<String, Object>> sObjectList = new ArrayList<Map<String, Object>>(); sObjectList.add(sObject); connector.createBulk(MOCK_OBJET_TYPE, sObjectList); }
@Test public void testGetUpdatedObjects() throws Exception { SalesforceConnector connector = Mockito.spy(new SalesforceConnector()); connector.setConnection(connection); when(connection.getConfig()).thenReturn(createConnectorConfig("userX")); connector.setObjectStoreHelper(objectStoreHelper); Calendar lastUpdateTime = createCalendar(4, 15); when(objectStoreHelper.getTimestamp("Account")).thenReturn(lastUpdateTime); setServerTime(connection, 5, 15); when(connection.getUpdated(anyString(), any(Calendar.class), any(Calendar.class))) .thenReturn(getUpdatedResult); Calendar latestDateCovered = createCalendar(5, 10); when(getUpdatedResult.getLatestDateCovered()).thenReturn(latestDateCovered); when(getUpdatedResult.getIds()).thenReturn(new String[] {"1", "3"}); List<Map<String, Object>> updatedObjects = new ArrayList<Map<String, Object>>(); doReturn(updatedObjects) .when(connector) .retrieve("Account", Arrays.asList("1", "3"), Arrays.asList("Id", "Name")); assertSame( updatedObjects, connector.getUpdatedObjects("Account", 60, Arrays.asList("Id", "Name"))); verify(connection) .getUpdated(eq("Account"), startTimeCaptor.capture(), endTimeCaptor.capture()); assertStartTime(4, 15); assertEndTime(5, 15); verify(objectStoreHelper).updateTimestamp(getUpdatedResult, "Account"); }
@Test public void testCreateJob() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class); Mockito.when(bulkConnection.createJob(Mockito.isA(JobInfo.class))) .thenAnswer( new Answer<JobInfo>() { @Override public JobInfo answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); return (JobInfo) args[0]; } }); JobInfo actualJobInfo = connector.createJob(OperationEnum.upsert, "Account", "NewField", ContentType.CSV, null); Mockito.verify(bulkConnection).createJob(expectedJobInfo.capture()); assertEquals(expectedJobInfo.getValue(), actualJobInfo); assertEquals(OperationEnum.upsert, expectedJobInfo.getValue().getOperation()); assertEquals(actualJobInfo.getObject(), expectedJobInfo.getValue().getObject()); assertEquals( actualJobInfo.getExternalIdFieldName(), expectedJobInfo.getValue().getExternalIdFieldName()); assertEquals(actualJobInfo.getContentType(), expectedJobInfo.getValue().getContentType()); }
@Test public void testIsNotConnectedWhenLoginResultIsNull() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); assertFalse(connector.isConnected()); }
@Test public void testQuery() throws Exception { SalesforceConnector connector = new SalesforceConnector(); final SObject sObject1 = Mockito.mock(SObject.class); final SObject sObject2 = Mockito.mock(SObject.class); final SObject sObject3 = Mockito.mock(SObject.class); when(sObject1.getId()).thenReturn("1"); when(sObject2.getId()).thenReturn("2"); when(sObject3.getId()).thenReturn("3"); QueryResult queryResult = Mockito.mock(QueryResult.class); when(queryResult.getRecords()) .thenAnswer( new Answer<Object>() { int counter = 0; @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SObject[] result = null; switch (counter) { case 0: result = new SObject[] {sObject1}; break; case 1: result = new SObject[] {sObject2, sObject3}; break; case 2: result = new SObject[] {}; break; } counter++; return result; } }); when(queryResult.isDone()).thenReturn(false).thenReturn(true); when(queryResult.getQueryLocator()).thenReturn("001").thenReturn(null); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); connector.setConnection(partnerConnection); when(partnerConnection.query(eq(MOCK_QUERY))).thenReturn(queryResult); when(partnerConnection.queryMore("001")).thenReturn(queryResult); PagingDelegate<Map<String, Object>> delegate = connector.query(MOCK_QUERY, new PagingConfiguration(1)); List<Map<String, Object>> result = delegate.getPage(); assertEquals(1, result.size()); result = delegate.getPage(); assertEquals(2, result.size()); assertNull(delegate.getPage()); }
@Test public void testBatchResultsStream() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BatchInfo batchInfo = setupBulkConnection(connector); BulkConnection bulkConnection = connector.getBulkConnection(); InputStream stream = Mockito.mock(InputStream.class); when(bulkConnection.getBatchResultStream(batchInfo.getJobId(), batchInfo.getId())) .thenReturn(stream); InputStream actualIs = connector.batchResultStream(batchInfo); assertEquals(stream, actualIs); }
@Test public void testGetUserInfo() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); GetUserInfoResult getUserInfoResult = Mockito.mock(GetUserInfoResult.class); when(partnerConnection.getUserInfo()).thenReturn(getUserInfoResult); assertEquals(getUserInfoResult, connector.getUserInfo()); }
@Test public void testAbortJob() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); JobInfo expectedJobInfo = new JobInfo(); String jobId = "uVsd234k23neasd"; Mockito.when(bulkConnection.abortJob(jobId)).thenReturn(expectedJobInfo); JobInfo actualJobInfo = connector.abortJob(jobId); assertEquals(expectedJobInfo, actualJobInfo); }
private SalesforceBayeuxClient mockBayeuxClient() throws MalformedURLException { PartnerConnection connection = Mockito.mock(PartnerConnection.class); LoginResult loginResult = Mockito.mock(LoginResult.class); ConnectorConfig connectorConfig = Mockito.mock(ConnectorConfig.class); SalesforceConnector connector = Mockito.mock(SalesforceConnector.class); when(connector.getConnection()).thenReturn(connection); when(connector.getLoginResult()).thenReturn(loginResult); when(connector.getSessionId()).thenReturn("001"); when(connection.getConfig()).thenReturn(connectorConfig); when(connectorConfig.getServiceEndpoint()).thenReturn("http://xxx.salesforce.com"); when(connectorConfig.getUsername()).thenReturn("mulesoft"); return new SalesforceBayeuxClient(connector); }
@Test public void testDescribeSObject() throws Exception { SalesforceConnector connector = new SalesforceConnector(); DescribeSObjectResult describeSObjectResult = Mockito.mock(DescribeSObjectResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); when(partnerConnection.describeSObject(eq(MOCK_OBJET_TYPE))).thenReturn(describeSObjectResult); connector.describeSObject(MOCK_OBJET_TYPE); }
@Test public void testGetSessionId() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); LoginResult loginResult = Mockito.mock(LoginResult.class); connector.setConnection(partnerConnection); connector.setLoginResult(loginResult); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); when(loginResult.getSessionId()).thenReturn(MOCKED_ID); assertEquals(connector.getSessionId(), MOCKED_ID); }
@Test public void testHardDeleteBulk() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BatchInfo batchInfo = setupBulkConnection(connector); Map<String, Object> sObject = new HashMap<String, Object>(); sObject.put(ID_FIELD, ACCOUNT_ID); List<Map<String, Object>> sObjectList = new ArrayList<Map<String, Object>>(); sObjectList.add(sObject); BatchInfo returnedBatchInfo = connector.hardDeleteBulk(MOCK_OBJET_TYPE, sObjectList); assertEquals(batchInfo, returnedBatchInfo); }
@Test public void testQueryResultStreamNoResults() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BatchInfo batchInfo = setupBulkConnection(connector); QueryResultList queryResultList = Mockito.mock(QueryResultList.class); BulkConnection bulkConnection = connector.getBulkConnection(); String[] queryResults = new String[0]; when(bulkConnection.getQueryResultList(batchInfo.getJobId(), batchInfo.getId())) .thenReturn(queryResultList); when(queryResultList.getResult()).thenReturn(queryResults); InputStream actualIs = connector.queryResultStream(batchInfo); assertEquals(null, actualIs); }
@Test(expected = ConnectionException.class) public void testCreateBatchWithConnectionException() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); AsyncApiException exception = Mockito.mock(AsyncApiException.class); connector.setBulkConnection(bulkConnection); JobInfo jobInfo = new JobInfo(); List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>(); Mockito.when(exception.getExceptionCode()).thenReturn(AsyncExceptionCode.InvalidSessionId); Mockito.when(bulkConnection.createBatch(jobInfo)).thenThrow(exception); connector.createBatch(jobInfo, objects); }
@Test public void testQuerySingle() throws Exception { SalesforceConnector connector = new SalesforceConnector(); QueryResult queryResult = Mockito.mock(QueryResult.class); when(queryResult.getRecords()).thenReturn(new SObject[] {}); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); connector.setConnection(partnerConnection); when(partnerConnection.query(eq(MOCK_QUERY))).thenReturn(queryResult); connector.querySingle(MOCK_QUERY); }
@Test public void testDescribeGlobal() throws Exception { SalesforceConnector connector = new SalesforceConnector(); DescribeGlobalResult describeGlobalResult = Mockito.mock(DescribeGlobalResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); connector.setConnection(partnerConnection); when(partnerConnection.describeGlobal()).thenReturn(describeGlobalResult); connector.describeGlobal(); verify(partnerConnection).describeGlobal(); }
@Test public void testUpsertBulk() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BatchInfo batchInfo = setupBulkConnection(connector); Map<String, Object> sObject = new HashMap<String, Object>(); sObject.put(FIRST_NAME_FIELD, FIRST_NAME); sObject.put(LAST_NAME_FIELD, LAST_NAME); List<Map<String, Object>> sObjectList = new ArrayList<Map<String, Object>>(); sObjectList.add(sObject); BatchInfo returnedBatchInfo = connector.upsertBulk(MOCK_OBJET_TYPE, "X_c", sObjectList); assertEquals(batchInfo, returnedBatchInfo); }
@Test public void testCreateConnectorConfig() throws Exception { SalesforceConnector connector = new SalesforceConnector(); ConnectorConfig config = connector.createConnectorConfig( "http://www.salesforce.com", "username", "password", "", 0, "", ""); assertEquals(config.getUsername(), "username"); assertEquals(config.getPassword(), "password"); assertEquals(config.getAuthEndpoint(), "http://www.salesforce.com"); assertEquals(config.getServiceEndpoint(), "http://www.salesforce.com"); assertTrue(config.isManualLogin()); assertFalse(config.isCompression()); }
private BatchInfo setupBulkConnection(SalesforceConnector salesforceConnector) throws AsyncApiException { PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); salesforceConnector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); salesforceConnector.setBulkConnection(bulkConnection); JobInfo jobInfo = Mockito.mock(JobInfo.class); BatchRequest batchRequest = Mockito.mock(BatchRequest.class); BatchInfo batchInfo = Mockito.mock(BatchInfo.class); doReturn(jobInfo).when(bulkConnection).createJob(any(JobInfo.class)); doReturn(batchRequest).when(bulkConnection).createBatch(any(JobInfo.class)); doReturn(batchInfo).when(batchRequest).completeRequest(); return batchInfo; }
@Test public void testGetDeletedRange() throws Exception { SalesforceConnector connector = spy(new SalesforceConnector()); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); GetServerTimestampResult getServerTimestampResult = Mockito.mock(GetServerTimestampResult.class); when(partnerConnection.getServerTimestamp()).thenReturn(getServerTimestampResult); connector.getDeletedRange("Account", Calendar.getInstance(), Calendar.getInstance()); verify(partnerConnection, atLeastOnce()) .getDeleted(eq("Account"), any(Calendar.class), any(Calendar.class)); }
@Test public void testEmptyRecycleBin() throws Exception { SalesforceConnector connector = new SalesforceConnector(); EmptyRecycleBinResult emptyRecycleBinResult = Mockito.mock(EmptyRecycleBinResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); when(partnerConnection.emptyRecycleBin(argThat(new StringArrayMatcher()))) .thenReturn(new EmptyRecycleBinResult[] {emptyRecycleBinResult}); List<String> ids = new ArrayList<String>(); ids.add(MOCKED_ID); connector.emptyRecycleBin(ids); }
@Test public void testDelete() throws Exception { SalesforceConnector connector = new SalesforceConnector(); DeleteResult deleteResult = Mockito.mock(DeleteResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); when(partnerConnection.delete(argThat(new StringArrayMatcher()))) .thenReturn(new DeleteResult[] {deleteResult}); List<String> ids = new ArrayList<String>(); ids.add(MOCKED_ID); connector.delete(ids); }
@Test public void testCreateSingleWithNoSaveResults() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); when(partnerConnection.create(Mockito.argThat(new SObjectArrayMatcher()))) .thenReturn(new SaveResult[] {}); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); Map<String, Object> sObject = new HashMap<String, Object>(); sObject.put(FIRST_NAME_FIELD, FIRST_NAME); sObject.put(LAST_NAME_FIELD, LAST_NAME); SaveResult returnedSaveResult = connector.createSingle(MOCK_OBJET_TYPE, sObject); assertNull(returnedSaveResult); }
@Test public void testCreateBatch() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); BatchRequest batchRequest = Mockito.mock(BatchRequest.class); connector.setBulkConnection(bulkConnection); JobInfo jobInfo = new JobInfo(); List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>(); BatchInfo expectedBatchInfo = new BatchInfo(); Mockito.when(bulkConnection.createBatch(jobInfo)).thenReturn(batchRequest); Mockito.when(batchRequest.completeRequest()).thenReturn(expectedBatchInfo); BatchInfo actualBatchInfo = connector.createBatch(jobInfo, objects); assertEquals(expectedBatchInfo, actualBatchInfo); Mockito.verify(batchRequest).addSObjects(connector.toAsyncSObjectList(objects)); }
@Test public void testRetrieve() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); connector.setConnection(partnerConnection); SObject sObject1 = Mockito.mock(SObject.class); SObject sObject2 = Mockito.mock(SObject.class); when(partnerConnection.retrieve(eq("Id,Name"), eq("Account"), eq(new String[] {"id1", "id2"}))) .thenReturn(new SObject[] {sObject1, sObject2}); List<Map<String, Object>> result = connector.retrieve("Account", Arrays.asList("id1", "id2"), Arrays.asList("Id", "Name")); assertEquals(2, result.size()); }
@Test(expected = ConnectionException.class) public void testCreateBatchForQueryWithConnectionException() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class); AsyncApiException exception = Mockito.mock(AsyncApiException.class); connector.setBulkConnection(bulkConnection); JobInfo actualJobInfo = new JobInfo(); String query = "SELECT Id FROM Contact"; Mockito.when(exception.getExceptionCode()).thenReturn(AsyncExceptionCode.InvalidSessionId); Mockito.when( bulkConnection.createBatchFromStream( expectedJobInfo.capture(), Mockito.isA(InputStream.class))) .thenThrow(exception); connector.createBatchForQuery(actualJobInfo, query); assertEquals(expectedJobInfo.getValue(), actualJobInfo); }
@Test public void testPublishTopic() throws Exception { SalesforceConnector connector = new SalesforceConnector(); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); connector.setConnection(partnerConnection); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); SaveResult saveResult = Mockito.mock(SaveResult.class); when(saveResult.isSuccess()).thenReturn(true); when(partnerConnection.create(Mockito.argThat(new SObjectArrayMatcher()))) .thenReturn(new SaveResult[] {saveResult}); QueryResult queryResult = Mockito.mock(QueryResult.class); when(partnerConnection.query(eq("SELECT Id FROM PushTopic WHERE Name = 'TopicName'"))) .thenReturn(queryResult); when(queryResult.getSize()).thenReturn(0); connector.publishTopic("TopicName", "SELECT * FROM Account", "Description"); verify(partnerConnection, atLeastOnce()).create(Mockito.argThat(new SObjectArrayMatcher())); }
@Test public void testCreateBatchStream() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class); connector.setBulkConnection(bulkConnection); JobInfo actualJobInfo = new JobInfo(); InputStream stream = Mockito.mock(InputStream.class); BatchInfo expectedBatchInfo = new BatchInfo(); Mockito.when( bulkConnection.createBatchFromStream( expectedJobInfo.capture(), Mockito.isA(InputStream.class))) .thenReturn(expectedBatchInfo); BatchInfo actualBatchInfo = connector.createBatchStream(actualJobInfo, stream); assertEquals(expectedBatchInfo, actualBatchInfo); assertEquals(expectedJobInfo.getValue(), actualJobInfo); }
@Test public void testUpdate() throws Exception { SalesforceConnector connector = new SalesforceConnector(); SaveResult saveResult = Mockito.mock(SaveResult.class); PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class); when(partnerConnection.update(Mockito.argThat(new SObjectArrayMatcher()))) .thenReturn(new SaveResult[] {saveResult}); BulkConnection bulkConnection = Mockito.mock(BulkConnection.class); connector.setBulkConnection(bulkConnection); connector.setConnection(partnerConnection); Map<String, Object> sObject = new HashMap<String, Object>(); sObject.put(FIRST_NAME_FIELD, FIRST_NAME); sObject.put(LAST_NAME_FIELD, LAST_NAME); List<Map<String, Object>> sObjectList = new ArrayList<Map<String, Object>>(); sObjectList.add(sObject); List<SaveResult> saveResults = connector.update(MOCK_OBJET_TYPE, sObjectList); assertEquals(saveResults.get(0), saveResult); }
@Test public void testQueryResultStream() throws Exception { SalesforceConnector connector = new SalesforceConnector(); BatchInfo batchInfo = setupBulkConnection(connector); QueryResultList queryResultList = Mockito.mock(QueryResultList.class); BulkConnection bulkConnection = connector.getBulkConnection(); String[] queryResults = {"ID1", "ID2"}; byte[] a = {'a'}; byte[] b = {'b'}; InputStream[] sourceIs = {new ByteArrayInputStream(a), new ByteArrayInputStream(b)}; when(bulkConnection.getQueryResultList(batchInfo.getJobId(), batchInfo.getId())) .thenReturn(queryResultList); when(queryResultList.getResult()).thenReturn(queryResults); when(bulkConnection.getQueryResultStream(batchInfo.getJobId(), batchInfo.getId(), "ID1")) .thenReturn(sourceIs[0]); when(bulkConnection.getQueryResultStream(batchInfo.getJobId(), batchInfo.getId(), "ID2")) .thenReturn(sourceIs[1]); InputStream actualIs = connector.queryResultStream(batchInfo); assertTrue(actualIs instanceof SequenceInputStream); assertEquals(actualIs.read(), a[0]); assertEquals(actualIs.read(), b[0]); }