NIFI-268: Updated unit tests

This commit is contained in:
Mark Payne 2015-01-20 13:02:59 -05:00
parent bd290018ad
commit 1b1f45faac
4 changed files with 83 additions and 124 deletions

View File

@ -55,7 +55,6 @@ public class TestDetectDuplicate {
@Test
public void testDuplicate() throws InitializationException {
TestRunner runner = TestRunners.newTestRunner(DetectDuplicate.class);
final DistributedMapCacheClientImpl client = createClient();
final Map<String, String> clientProperties = new HashMap<>();
@ -67,6 +66,8 @@ public class TestDetectDuplicate {
Map<String, String> props = new HashMap<>();
props.put("hash.value", "1000");
runner.enqueue(new byte[]{}, props);
runner.enableControllerService(client);
runner.run();
runner.assertAllFlowFilesTransferred(DetectDuplicate.REL_NON_DUPLICATE, 1);
runner.clearTransferState();
@ -89,9 +90,12 @@ public class TestDetectDuplicate {
runner.setProperty(DetectDuplicate.DISTRIBUTED_CACHE_SERVICE, "client");
runner.setProperty(DetectDuplicate.FLOWFILE_DESCRIPTION, "The original flow file");
runner.setProperty(DetectDuplicate.AGE_OFF_DURATION, "2 secs");
runner.enableControllerService(client);
Map<String, String> props = new HashMap<>();
props.put("hash.value", "1000");
runner.enqueue(new byte[]{}, props);
runner.run();
runner.assertAllFlowFilesTransferred(DetectDuplicate.REL_NON_DUPLICATE, 1);
runner.clearTransferState();

View File

@ -40,8 +40,6 @@ import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author unattributed
@ -49,7 +47,6 @@ import org.slf4j.LoggerFactory;
*/
public class TestGetHTTP {
private static Logger LOGGER;
private TestRunner controller;
@BeforeClass
@ -58,7 +55,6 @@ public class TestGetHTTP {
System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.GetHTTP", "debug");
System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.TestGetHTTP", "debug");
LOGGER = LoggerFactory.getLogger(TestGetHTTP.class);
File confDir = new File("conf");
if (!confDir.exists()) {
confDir.mkdir();
@ -77,16 +73,6 @@ public class TestGetHTTP {
assertTrue(confDir.delete());
}
private static Map<String, String> createSslProperties() {
Map<String, String> map = new HashMap<String, String>();
map.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
map.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
map.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
map.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
map.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
map.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
return map;
}
@Test
public final void testContentModified() throws Exception {
@ -308,6 +294,7 @@ public class TestGetHTTP {
final SSLContextService service = new StandardSSLContextService();
try {
controller.addControllerService("ssl-service", service, getSslProperties());
controller.enableControllerService(service);
} catch (InitializationException ex) {
ex.printStackTrace();
Assert.fail("Could not create SSL Context Service");

View File

@ -83,7 +83,9 @@ public class TestInvokeHTTP {
@Before
public void before() throws Exception {
runner = TestRunners.newTestRunner(InvokeHTTP.class);
runner.addControllerService("ssl-context", new StandardSSLContextService(), sslProperties);
final StandardSSLContextService sslService = new StandardSSLContextService();
runner.addControllerService("ssl-context", sslService, sslProperties);
runner.enableControllerService(sslService);
runner.setProperty(Config.PROP_SSL_CONTEXT_SERVICE, "ssl-context");
server.clearHandlers();

View File

@ -16,79 +16,56 @@
*/
package org.apache.nifi.ssl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.apache.nifi.controller.ControllerService;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.ssl.SSLContextService.ClientAuth;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Assert;
import org.junit.Test;
public class SSLContextServiceTest {
@Test
public void testBad1() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
public void testBad1() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
runner.addControllerService("test-bad1", service, properties);
Assert.fail("Should have thrown an Exception");
} catch (InitializationException e) {
assertEquals(
"org.apache.nifi.reporting.InitializationException: SSLContextService[id=test-bad1] does not have the KeyStore or the TrustStore populated",
e.getCause().getCause().toString());
}
runner.assertNotValid(service);
}
@Test
public void testBad2() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
public void testBad2() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
runner.addControllerService("test-bad2", service, properties);
Assert.fail("Should have thrown an Exception");
} catch (InitializationException e) {
assertEquals(
"org.apache.nifi.reporting.InitializationException: SSLContextService[id=test-bad2] is not valid due to:\n'Keystore Properties' is invalid because Must set either 0 or 3 properties for Keystore",
e.getCause().getCause().toString());
}
runner.assertNotValid(service);
}
@Test
public void testBad3() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
public void testBad3() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
properties.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
runner.addControllerService("test-bad3", service, properties);
Assert.fail("Should have thrown an Exception");
} catch (InitializationException e) {
assertEquals(
"org.apache.nifi.reporting.InitializationException: SSLContextService[id=test-bad3] is not valid due to:\n'Truststore Properties' is invalid because Must set either 0 or 3 properties for Truststore",
e.getCause().getCause().toString());
}
runner.assertNotValid(service);
}
@Test
public void testBad4() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
public void testBad4() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "wrongpassword");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "PKCS12");
@ -96,22 +73,15 @@ public class SSLContextServiceTest {
properties.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "wrongpassword");
properties.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-bad4", service, properties);
Assert.fail("Should have thrown an Exception");
} catch (InitializationException e) {
assertEquals(
"org.apache.nifi.reporting.InitializationException: SSLContextService[id=test-bad4] is not valid due to:\n"
+ "'Keystore Properties' is invalid because Invalid KeyStore Password or Type specified for file src/test/resources/localhost-ks.jks\n"
+ "'Truststore Properties' is invalid because Invalid KeyStore Password or Type specified for file src/test/resources/localhost-ts.jks",
e.getCause().getCause().toString());
}
runner.assertNotValid(service);
}
@Test
public void testBad5() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
public void testBad5() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final SSLContextService service = new StandardSSLContextService();
final Map<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/DOES-NOT-EXIST.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "PKCS12");
@ -119,38 +89,30 @@ public class SSLContextServiceTest {
properties.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-bad5", service, properties);
Assert.fail("Should have thrown an Exception");
} catch (InitializationException e) {
assertTrue(e.getCause().getCause().toString().startsWith("org.apache.nifi.reporting.InitializationException: "
+ "SSLContextService[id=test-bad5] is not valid due to:\n'Keystore Properties' is invalid "
+ "because Cannot access file"));
}
runner.assertNotValid(service);
}
@Test
public void testGood() {
try {
TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
ControllerService service = new StandardSSLContextService();
HashMap<String, String> properties = new HashMap<String, String>();
properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "PKCS12");
properties.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
properties.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-good1", service, properties);
public void testGood() throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
SSLContextService service = new StandardSSLContextService();
runner.addControllerService("test-good1", service);
runner.setProperty(service, StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
runner.setProperty(service, StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
runner.setProperty(service, StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
runner.setProperty(service, StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
runner.enableControllerService(service);
runner.setProperty("SSL Context Svc ID", "test-good1");
runner.assertValid();
service = runner.getProcessContext().getControllerServiceLookup().getControllerService("test-good1");
runner.assertValid(service);
service = (SSLContextService) runner.getProcessContext().getControllerServiceLookup().getControllerService("test-good1");
Assert.assertNotNull(service);
Assert.assertTrue(service instanceof StandardSSLContextService);
SSLContextService sslService = (SSLContextService) service;
sslService.createSSLContext(ClientAuth.REQUIRED);
sslService.createSSLContext(ClientAuth.WANT);
sslService.createSSLContext(ClientAuth.NONE);
} catch (InitializationException e) {
}
}
@Test
@ -163,6 +125,8 @@ public class SSLContextServiceTest {
properties.put(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-good2", service, properties);
runner.enableControllerService(service);
runner.setProperty("SSL Context Svc ID", "test-good2");
runner.assertValid();
Assert.assertNotNull(service);
@ -182,6 +146,8 @@ public class SSLContextServiceTest {
properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
runner.addControllerService("test-good3", service, properties);
runner.enableControllerService(service);
runner.setProperty("SSL Context Svc ID", "test-good3");
runner.assertValid();
Assert.assertNotNull(service);