You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ex...@apache.org on 2022/05/31 15:04:26 UTC
[nifi] branch main updated: NIFI-9867 Adding new tests to increase Test Coverage of GetTwitter
This is an automated email from the ASF dual-hosted git repository.
exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 9878df9f5a NIFI-9867 Adding new tests to increase Test Coverage of GetTwitter
9878df9f5a is described below
commit 9878df9f5a3980e150351f61cf131df4f19ea880
Author: Paula Yamashita <py...@gmail.com>
AuthorDate: Tue Apr 19 11:15:30 2022 -0700
NIFI-9867 Adding new tests to increase Test Coverage of GetTwitter
This closes #5978
Signed-off-by: David Handermann <ex...@apache.org>
---
.../nifi/processors/twitter/TestGetTwitter.java | 198 ++++++++++++++++++++-
1 file changed, 191 insertions(+), 7 deletions(-)
diff --git a/nifi-nar-bundles/nifi-social-media-bundle/nifi-twitter-processors/src/test/java/org/apache/nifi/processors/twitter/TestGetTwitter.java b/nifi-nar-bundles/nifi-social-media-bundle/nifi-twitter-processors/src/test/java/org/apache/nifi/processors/twitter/TestGetTwitter.java
index c575bd210b..d7dcb31e95 100644
--- a/nifi-nar-bundles/nifi-social-media-bundle/nifi-twitter-processors/src/test/java/org/apache/nifi/processors/twitter/TestGetTwitter.java
+++ b/nifi-nar-bundles/nifi-social-media-bundle/nifi-twitter-processors/src/test/java/org/apache/nifi/processors/twitter/TestGetTwitter.java
@@ -15,16 +15,40 @@
* limitations under the License.
*/
package org.apache.nifi.processors.twitter;
-
+import com.twitter.hbc.core.Client;
+import com.twitter.hbc.core.endpoint.StreamingEndpoint;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+import java.util.concurrent.BlockingQueue;
+
+@ExtendWith(MockitoExtension.class)
public class TestGetTwitter {
+ private TestRunner runner;
+ @Mock
+ Client client = Mockito.mock(Client.class);
+ @Mock
+ BlockingQueue<String> messageQueue = Mockito.mock(BlockingQueue.class);
+ @InjectMocks
+ GetTwitter getTwitter = new GetTwitter();
+
+ @BeforeEach
+ public void init() {
+ runner = TestRunners.newTestRunner(getTwitter);
+ }
+
@Test
public void testLocationValidatorWithValidLocations() {
- final TestRunner runner = TestRunners.newTestRunner(GetTwitter.class);
runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
@@ -37,7 +61,6 @@ public class TestGetTwitter {
@Test
public void testLocationValidatorWithEqualLatitudes() {
- final TestRunner runner = TestRunners.newTestRunner(GetTwitter.class);
runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
@@ -50,7 +73,6 @@ public class TestGetTwitter {
@Test
public void testLocationValidatorWithEqualLongitudes() {
- final TestRunner runner = TestRunners.newTestRunner(GetTwitter.class);
runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
@@ -63,7 +85,6 @@ public class TestGetTwitter {
@Test
public void testLocationValidatorWithSWLatGreaterThanNELat() {
- final TestRunner runner = TestRunners.newTestRunner(GetTwitter.class);
runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
@@ -76,7 +97,6 @@ public class TestGetTwitter {
@Test
public void testLocationValidatorWithSWLonGreaterThanNELon() {
- final TestRunner runner = TestRunners.newTestRunner(GetTwitter.class);
runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
@@ -86,4 +106,168 @@ public class TestGetTwitter {
runner.setProperty(GetTwitter.LOCATIONS, "-122.75,36.8,-121.75,37.8,-74,40,-75,41");
runner.assertNotValid();
}
-}
+
+
+ // To test getSupportedDynamicPropertyDescriptor
+ @Test
+ public void testValidGetSupportedDynamicPropertyDescriptor() {
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ PropertyDescriptor dynamicProperty = new PropertyDescriptor.Builder()
+ .name("foo")
+ .description("Adds a query parameter with name '" + "foo" + "' to the Twitter query")
+ .required(false)
+ .dynamic(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+ .build();
+ runner.setProperty(dynamicProperty, "{\"a\": \"a\"}");
+ runner.assertValid();
+ }
+
+
+ // To test customValidate - lines 222 to 224
+ @Test
+ public void testCustomValidatorWithoutTermsFollowingLocation() {
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.assertNotValid();
+ }
+
+ // To test onScheduled using ENDPOINT_SAMPLE and language
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointSampleAndLanguage() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_SAMPLE);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.setProperty(GetTwitter.LANGUAGES, "en, pt, it");
+ runner.assertValid();
+ runner.run();
+ }
+
+ // To test onScheduled using ENDPOINT_SAMPLE
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointSample() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_SAMPLE);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.assertValid();
+ runner.run(1);
+ }
+
+
+ // To test onScheduled using ENDPOINT_FILTER with valid locations, and language list
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointFilterAndLanguage() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.setProperty(GetTwitter.LOCATIONS, "-122.75,36.8,-121.75,37.8,-74,40,-73,41");
+ runner.setProperty(GetTwitter.LANGUAGES, "en, pt, it");
+ runner.assertValid();
+ runner.run(1);
+ }
+
+ // To test onScheduled using ENDPOINT_FILTER with valid TERMS and no language, and no location
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointFilterAndTerms() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.setProperty(GetTwitter.TERMS, "any thing we want to filter");
+ runner.assertValid();
+ runner.run(1);
+ }
+
+ // To test onScheduled using ENDPOINT_FILTER with IDs to follow
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointFilterAndID() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ String followingIds = " 4265731,\n" +
+ " 27674040,\n" +
+ " 26123649,\n" +
+ " 9576402,\n" +
+ " 821958,\n" +
+ " 7852612,\n" +
+ " 819797\n";
+ runner.setProperty(GetTwitter.FOLLOWING, followingIds);
+ runner.assertValid();
+ runner.run(1);
+ }
+
+ // To test onScheduled using ENDPOINT_FIREHOUSE and languages list
+ // Mocking Client and messageQueue instead to avoid make calls to the Twitter service
+ @Test
+ public void testRunsOnSchedulerEndpointFirehouseAndLanguage() {
+ Mockito.when(messageQueue.poll()).thenReturn("Hello World!");
+ StreamingEndpoint streamep = Mockito.mock(StreamingEndpoint.class);
+ Mockito.when(client.getEndpoint()).thenReturn(streamep);
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FIREHOSE);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.setProperty(GetTwitter.LANGUAGES, "en, pt, it");
+ runner.assertValid();
+ runner.run(1);
+ }
+
+ // To test FollowingValidator for Invalid Following - not number
+ // and test catch invalid location values
+ @Test
+ public void testCustomValidatorInvalidFollowingLocation() {
+ runner.setProperty(GetTwitter.ENDPOINT, GetTwitter.ENDPOINT_FILTER);
+ runner.setProperty(GetTwitter.MAX_CLIENT_ERROR_RETRIES, "5");
+ runner.setProperty(GetTwitter.CONSUMER_KEY, "consumerKey");
+ runner.setProperty(GetTwitter.CONSUMER_SECRET, "consumerSecret");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN, "accessToken");
+ runner.setProperty(GetTwitter.ACCESS_TOKEN_SECRET, "accessTokenSecret");
+ runner.setProperty(GetTwitter.FOLLOWING, "invalid id value");
+ runner.setProperty(GetTwitter.LOCATIONS, "invalid location value");
+ runner.assertNotValid();
+ }
+}
\ No newline at end of file