NIFI-9080 Converted nifi-commons to use JUnit 5

This closes #5332

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Mike Thomsen 2021-08-13 07:11:25 -04:00 committed by exceptionfactory
parent 309ab8f4ab
commit 8d513c5ed3
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
110 changed files with 1257 additions and 1852 deletions

View File

@ -16,14 +16,14 @@
*/ */
package org.apache.nifi.bootstrap.util; package org.apache.nifi.bootstrap.util;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
public class OSUtilsTest { public class OSUtilsTest {
@ -34,7 +34,7 @@ public class OSUtilsTest {
final Logger logger = LoggerFactory.getLogger("testing"); final Logger logger = LoggerFactory.getLogger("testing");
final Long pid = OSUtils.getProcessId(process, logger); final Long pid = OSUtils.getProcessId(process, logger);
process.destroy(); process.destroy();
assertNotNull("Process ID not found", pid); assertNotNull(pid, "Process ID not found");
} }
@Test @Test

View File

@ -18,39 +18,36 @@ package org.apache.nifi.attribute.expression.language
import org.apache.nifi.attribute.expression.language.evaluation.QueryResult import org.apache.nifi.attribute.expression.language.evaluation.QueryResult
import org.apache.nifi.expression.AttributeExpression import org.apache.nifi.expression.AttributeExpression
import org.junit.After import org.junit.jupiter.api.AfterEach
import org.junit.Before import org.junit.jupiter.api.BeforeAll
import org.junit.BeforeClass import org.junit.jupiter.api.BeforeEach
import org.junit.Test import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@RunWith(JUnit4.class) class QueryGroovyTest extends GroovyTestCase {
public class QueryGroovyTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(QueryGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(QueryGroovyTest.class)
@BeforeClass @BeforeAll
public static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before @BeforeEach
public void setUp() { void setUp() {
} }
@After @AfterEach
public void tearDown() { void tearDown() {
Query.metaClass.static = null Query.metaClass.static = null
} }
@Test @Test
public void testReplaceShouldReplaceAllLiteralMatches() { void testReplaceShouldReplaceAllLiteralMatches() {
// Arrange // Arrange
int n = 3 int n = 3
final String ORIGINAL_VALUE = "Hello World" final String ORIGINAL_VALUE = "Hello World"
@ -90,7 +87,7 @@ public class QueryGroovyTest extends GroovyTestCase {
} }
@Test @Test
public void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() { void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() {
// Arrange // Arrange
int n = 3 int n = 3
final String ORIGINAL_VALUE = "Hello World" final String ORIGINAL_VALUE = "Hello World"
@ -130,7 +127,7 @@ public class QueryGroovyTest extends GroovyTestCase {
} }
@Test @Test
public void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() { void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() {
// Arrange // Arrange
int n = 3 int n = 3
final String ORIGINAL_VALUE = "Hello World" final String ORIGINAL_VALUE = "Hello World"
@ -170,7 +167,7 @@ public class QueryGroovyTest extends GroovyTestCase {
} }
@Test @Test
public void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() { void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() {
// Arrange // Arrange
int n = 3 int n = 3
final String ORIGINAL_VALUE = "Hello World" final String ORIGINAL_VALUE = "Hello World"

View File

@ -29,9 +29,8 @@ import org.apache.nifi.parameter.Parameter;
import org.apache.nifi.parameter.ParameterDescriptor; import org.apache.nifi.parameter.ParameterDescriptor;
import org.apache.nifi.parameter.ParameterLookup; import org.apache.nifi.parameter.ParameterLookup;
import org.apache.nifi.registry.VariableRegistry; import org.apache.nifi.registry.VariableRegistry;
import org.junit.Assert; import org.junit.jupiter.api.Disabled;
import org.junit.Ignore; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -55,10 +54,11 @@ import static java.lang.Double.NaN;
import static java.lang.Double.POSITIVE_INFINITY; import static java.lang.Double.POSITIVE_INFINITY;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestQuery { public class TestQuery {
@ -138,20 +138,11 @@ public class TestQuery {
} }
private void assertValid(final String query) { private void assertValid(final String query) {
try { assertDoesNotThrow(() -> Query.compile(query));
Query.compile(query);
} catch (final Exception e) {
e.printStackTrace();
Assert.fail("Expected query to be valid, but it failed to compile due to " + e);
}
} }
private void assertInvalid(final String query) { private void assertInvalid(final String query) {
try { assertThrows(Exception.class, () -> Query.compile(query));
Query.compile(query);
Assert.fail("Expected query to be invalid, but it did compile");
} catch (final Exception e) {
}
} }
@Test @Test
@ -159,11 +150,7 @@ public class TestQuery {
Query.validateExpression("${abc:substring(${xyz:length()})}", false); Query.validateExpression("${abc:substring(${xyz:length()})}", false);
Query.isValidExpression("${now():format('yyyy-MM-dd')}"); Query.isValidExpression("${now():format('yyyy-MM-dd')}");
try { assertThrows(AttributeExpressionLanguageParsingException.class, () -> Query.validateExpression("$${attr}", false));
Query.validateExpression("$${attr}", false);
Assert.fail("invalid query validated");
} catch (final AttributeExpressionLanguageParsingException e) {
}
Query.validateExpression("$${attr}", true); Query.validateExpression("$${attr}", true);
@ -311,7 +298,7 @@ public class TestQuery {
} }
@Test @Test
@Ignore("Requires specific locale") @Disabled("Requires specific locale")
public void implicitDateConversion() { public void implicitDateConversion() {
final Date date = new Date(); final Date date = new Date();
final Query query = Query.compile("${dateTime:format('yyyy/MM/dd HH:mm:ss.SSS')}"); final Query query = Query.compile("${dateTime:format('yyyy/MM/dd HH:mm:ss.SSS')}");
@ -376,22 +363,15 @@ public class TestQuery {
ADDRESS_BOOK_JSON_PATH_EMPTY, ADDRESS_BOOK_JSON_PATH_EMPTY,
"", "${json:jsonPathDelete('$.missingpath')}", ""); "", "${json:jsonPathDelete('$.missingpath')}", "");
verifyEquals("${json:jsonPath('$.missingpath')}", attributes, ""); verifyEquals("${json:jsonPath('$.missingpath')}", attributes, "");
try {
verifyEquals("${json:jsonPath('$..')}", attributes, ""); assertThrows(AttributeExpressionLanguageException.class,
Assert.fail("Did not detect bad JSON path expression"); () -> verifyEquals("${json:jsonPath('$..')}", attributes, ""));
} catch (final AttributeExpressionLanguageException e) { assertThrows(AttributeExpressionLanguageException.class,
} () -> verifyEquals("${missing:jsonPath('$.firstName')}", attributes, ""));
try {
verifyEquals("${missing:jsonPath('$.firstName')}", attributes, "");
Assert.fail("Did not detect empty JSON document");
} catch (AttributeExpressionLanguageException e) {
}
attributes.put("invalid", "[}"); attributes.put("invalid", "[}");
try { assertThrows(AttributeExpressionLanguageException.class,
verifyEquals("${invlaid:jsonPath('$.firstName')}", attributes, "John"); () -> verifyEquals("${invlaid:jsonPath('$.firstName')}", attributes, "John"));
Assert.fail("Did not detect invalid JSON document");
} catch (AttributeExpressionLanguageException e) {
}
} }
private void verifyAddressBookAttributes(String originalAddressBook, Map<String,String> attributes, String updatedAttribute, Object updatedValue) { private void verifyAddressBookAttributes(String originalAddressBook, Map<String,String> attributes, String updatedAttribute, Object updatedValue) {
@ -526,13 +506,12 @@ public class TestQuery {
verifyEquals("${json:jsonPath('$.missing-path')}", attributes, ""); verifyEquals("${json:jsonPath('$.missing-path')}", attributes, "");
} }
@Test(expected=IllegalArgumentException.class)
public void testJsonPathAddNicknameJimmyAtNonArray() throws IOException { public void testJsonPathAddNicknameJimmyAtNonArray() throws IOException {
Map<String,String> attributes = verifyJsonPathExpressions( assertThrows(IllegalArgumentException.class, () -> verifyJsonPathExpressions(
ADDRESS_BOOK_JSON_PATH_EMPTY, ADDRESS_BOOK_JSON_PATH_EMPTY,
"", "",
"${json:jsonPathAdd('$.firstName', 'Jimmy')}", "${json:jsonPathAdd('$.firstName', 'Jimmy')}",
""); ""));
} }
@Test @Test
@ -601,9 +580,9 @@ public class TestQuery {
verifyEquals("${allAttributes( 'x', 'y' ):join(',')}", attributes, ","); verifyEquals("${allAttributes( 'x', 'y' ):join(',')}", attributes, ",");
} }
@Test(expected = AttributeExpressionLanguageException.class) @Test
public void testCannotCombineWithNonReducingFunction() { public void testCannotCombineWithNonReducingFunction() {
Query.compile("${allAttributes( 'a.1' ):plus(1)}"); assertThrows(AttributeExpressionLanguageException.class, () -> Query.compile("${allAttributes( 'a.1' ):plus(1)}"));
} }
@Test @Test
@ -1128,32 +1107,25 @@ public class TestQuery {
attributes.put("negativeDecimal", "-64.1"); attributes.put("negativeDecimal", "-64.1");
// Test that errors relating to not finding methods are properly handled // Test that errors relating to not finding methods are properly handled
try {
verifyEquals("${math('rand'):toNumber()}", attributes, 0L); AttributeExpressionLanguageException expected = assertThrows(AttributeExpressionLanguageException.class,
fail(); () -> verifyEquals("${math('rand'):toNumber()}", attributes, 0L));
} catch (AttributeExpressionLanguageException expected) { assertEquals("Cannot evaluate 'math' function because no subjectless method was found with the name:'rand'", expected.getMessage());
assertEquals("Cannot evaluate 'math' function because no subjectless method was found with the name:'rand'", expected.getMessage());
} expected = assertThrows(AttributeExpressionLanguageException.class,
try { () -> verifyEquals("${negativeDecimal:math('absolute')}", attributes, 0L));
verifyEquals("${negativeDecimal:math('absolute')}", attributes, 0L); assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'absolute', one argument of type: 'double'", expected.getMessage());
fail();
} catch (AttributeExpressionLanguageException expected) { expected = assertThrows(AttributeExpressionLanguageException.class,
assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'absolute', one argument of type: 'double'", expected.getMessage()); () -> verifyEquals("${oneDecimal:math('power', ${two:toDecimal()})}", attributes, 0L));
} assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'power', " +
try { "first argument type: 'double', second argument type: 'double'", expected.getMessage());
verifyEquals("${oneDecimal:math('power', ${two:toDecimal()})}", attributes, 0L);
fail(); expected = assertThrows(AttributeExpressionLanguageException.class,
} catch (AttributeExpressionLanguageException expected) { () -> verifyEquals("${oneDecimal:math('power', ${two})}", attributes, 0L));
assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'power', " + assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'power', " +
"first argument type: 'double', second argument type: 'double'", expected.getMessage()); "first argument type: 'double', second argument type: 'long'", expected.getMessage());
}
try {
verifyEquals("${oneDecimal:math('power', ${two})}", attributes, 0L);
fail();
} catch (AttributeExpressionLanguageException expected) {
assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'power', " +
"first argument type: 'double', second argument type: 'long'", expected.getMessage());
}
// Can only verify that it runs. ToNumber() will verify that it produced a number greater than or equal to 0.0 and less than 1.0 // Can only verify that it runs. ToNumber() will verify that it produced a number greater than or equal to 0.0 and less than 1.0
verifyEquals("${math('random'):toNumber()}", attributes, 0L); verifyEquals("${math('random'):toNumber()}", attributes, 0L);
@ -1363,12 +1335,7 @@ public class TestQuery {
verifyEquals("${allAttributes('abc', 'xyz'):length():equals(4)}", attributes, true); verifyEquals("${allAttributes('abc', 'xyz'):length():equals(4)}", attributes, true);
verifyEquals("${allAttributes('abc', 'xyz', 'other'):isNull()}", attributes, false); verifyEquals("${allAttributes('abc', 'xyz', 'other'):isNull()}", attributes, false);
try { assertThrows(AttributeExpressionLanguageParsingException.class, () -> Query.compile("${allAttributes('#ah'):equals('hello')"));
Query.compile("${allAttributes('#ah'):equals('hello')");
Assert.fail("Was able to compile with allAttributes and an invalid attribute name");
} catch (final AttributeExpressionLanguageParsingException e) {
// expected behavior
}
} }
@Test @Test
@ -1925,7 +1892,7 @@ public class TestQuery {
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
long result = (Long) getResult("${random()}", attrs).getValue(); long result = (Long) getResult("${random()}", attrs).getValue();
assertThat("random", result, greaterThan(negOne)); assertThat("random", result, greaterThan(negOne));
assertEquals("duplicate random", true, results.add(result)); assertEquals(true, results.add(result), "duplicate random");
} }
} }
@ -2108,10 +2075,11 @@ public class TestQuery {
verifyEquals("${nbr_attr:hash('MD5')}", attributes, "d3d9446802a44259755d38e6d163e820"); verifyEquals("${nbr_attr:hash('MD5')}", attributes, "d3d9446802a44259755d38e6d163e820");
} }
@Test(expected = AttributeExpressionLanguageException.class) @Test
public void testHashFailure() { public void testHashFailure() {
final Map<String, String> attributes = new HashMap<>(); final Map<String, String> attributes = new HashMap<>();
verifyEquals("${literal('john'):hash('NOT_A_ALGO')}", attributes, "527bd5b5d689e2c32ae974c6229ff785"); assertThrows(AttributeExpressionLanguageException.class,
() -> verifyEquals("${literal('john'):hash('NOT_A_ALGO')}", attributes, "527bd5b5d689e2c32ae974c6229ff785"));
} }
@Test @Test
@ -2177,24 +2145,9 @@ public class TestQuery {
assertTrue(multipleResultExpectedResults.contains(actualResult)); assertTrue(multipleResultExpectedResults.contains(actualResult));
verifyEquals("${str:repeat(4)}", attributes, "abcabcabcabc"); verifyEquals("${str:repeat(4)}", attributes, "abcabcabcabc");
try { assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(-1)}", attributes, ""));
verifyEquals("${str:repeat(-1)}", attributes, ""); assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(0)}", attributes, ""));
fail("Should have failed on numRepeats < 0"); assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(2,1)}", attributes, ""));
} catch(AttributeExpressionLanguageException aele) {
// Do nothing, it is expected
}
try {
verifyEquals("${str:repeat(0)}", attributes, "");
fail("Should have failed on numRepeats = 0");
} catch(AttributeExpressionLanguageException aele) {
// Do nothing, it is expected
}
try {
verifyEquals("${str:repeat(2,1)}", attributes, "");
fail("Should have failed on minRepeats > maxRepeats");
} catch(AttributeExpressionLanguageException aele) {
// Do nothing, it is expected
}
} }
@Test @Test

View File

@ -19,8 +19,8 @@ package org.apache.nifi.attribute.expression.language;
import org.apache.nifi.parameter.Parameter; import org.apache.nifi.parameter.Parameter;
import org.apache.nifi.parameter.ParameterDescriptor; import org.apache.nifi.parameter.ParameterDescriptor;
import org.apache.nifi.parameter.ParameterLookup; import org.apache.nifi.parameter.ParameterLookup;
import org.junit.Ignore; import org.junit.jupiter.api.Disabled;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -55,7 +55,7 @@ public class TestStandardPreparedQuery {
} }
@Test @Test
@Ignore("Intended for manual performance testing; should not be run in an automated environment") @Disabled("Intended for manual performance testing; should not be run in an automated environment")
public void test10MIterations() { public void test10MIterations() {
final Map<String, String> attrs = new HashMap<>(); final Map<String, String> attrs = new HashMap<>();
attrs.put("xx", "world"); attrs.put("xx", "world");
@ -70,7 +70,7 @@ public class TestStandardPreparedQuery {
} }
@Test @Test
@Ignore("Takes too long") @Disabled("Takes too long")
public void test10MIterationsWithQuery() { public void test10MIterationsWithQuery() {
final Map<String, String> attrs = new HashMap<>(); final Map<String, String> attrs = new HashMap<>();
attrs.put("xx", "world"); attrs.put("xx", "world");

View File

@ -18,7 +18,7 @@ package org.apache.nifi.attribute.expression.language;
import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.registry.VariableRegistry; import org.apache.nifi.registry.VariableRegistry;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -19,8 +19,8 @@ package org.apache.nifi.flow.encryptor;
import org.apache.nifi.encrypt.PropertyEncryptor; import org.apache.nifi.encrypt.PropertyEncryptor;
import org.apache.nifi.encrypt.PropertyEncryptorBuilder; import org.apache.nifi.encrypt.PropertyEncryptorBuilder;
import org.apache.nifi.security.util.EncryptionMethod; import org.apache.nifi.security.util.EncryptionMethod;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -49,7 +49,7 @@ public class StandardFlowEncryptorTest {
private StandardFlowEncryptor flowEncryptor; private StandardFlowEncryptor flowEncryptor;
@Before @BeforeEach
public void setEncryptors() { public void setEncryptors() {
inputEncryptor = getPropertyEncryptor(INPUT_KEY, EncryptionMethod.MD5_256AES.getAlgorithm()); inputEncryptor = getPropertyEncryptor(INPUT_KEY, EncryptionMethod.MD5_256AES.getAlgorithm());
outputEncryptor = getPropertyEncryptor(OUTPUT_KEY, EncryptionMethod.SHA256_256AES.getAlgorithm()); outputEncryptor = getPropertyEncryptor(OUTPUT_KEY, EncryptionMethod.SHA256_256AES.getAlgorithm());

View File

@ -17,8 +17,8 @@
package org.apache.nifi.flow.encryptor.command; package org.apache.nifi.flow.encryptor.command;
import org.apache.nifi.stream.io.GZIPOutputStream; import org.apache.nifi.stream.io.GZIPOutputStream;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -39,7 +39,7 @@ import static org.junit.Assert.assertTrue;
public class SetSensitivePropertiesKeyTest { public class SetSensitivePropertiesKeyTest {
private static final String FLOW_CONTENTS = "<property><value>PROPERTY</value></property>"; private static final String FLOW_CONTENTS = "<property><value>PROPERTY</value></property>";
@After @AfterEach
public void clearProperties() { public void clearProperties() {
System.clearProperty(SetSensitivePropertiesKey.PROPERTIES_FILE_PATH); System.clearProperty(SetSensitivePropertiesKey.PROPERTIES_FILE_PATH);
} }

View File

@ -16,8 +16,7 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -26,7 +25,8 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TestPackageUnpackageV3 { public class TestPackageUnpackageV3 {

View File

@ -16,9 +16,17 @@
*/ */
package org.apache.nifi.hl7.query; package org.apache.nifi.hl7.query;
import static org.junit.Assert.assertEquals; import ca.uhn.hl7v2.DefaultHapiContext;
import static org.junit.Assert.assertFalse; import ca.uhn.hl7v2.HL7Exception;
import static org.junit.Assert.assertTrue; import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
import org.apache.nifi.hl7.hapi.HapiMessage;
import org.apache.nifi.hl7.model.HL7Field;
import org.apache.nifi.hl7.model.HL7Message;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -26,18 +34,9 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.nifi.hl7.hapi.HapiMessage; import static org.junit.Assert.assertEquals;
import org.apache.nifi.hl7.model.HL7Field; import static org.junit.Assert.assertFalse;
import org.apache.nifi.hl7.model.HL7Message; import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
@SuppressWarnings("resource") @SuppressWarnings("resource")
public class TestHL7Query { public class TestHL7Query {
@ -60,7 +59,7 @@ public class TestHL7Query {
private HL7Message hypoglycemia; private HL7Message hypoglycemia;
@Before @BeforeEach
public void init() throws IOException, HL7Exception { public void init() throws IOException, HL7Exception {
this.hyperglycemia = createMessage(HYPERGLYCEMIA); this.hyperglycemia = createMessage(HYPERGLYCEMIA);
this.hypoglycemia = createMessage(HYPOGLYCEMIA); this.hypoglycemia = createMessage(HYPOGLYCEMIA);

View File

@ -20,21 +20,21 @@ import org.apache.nifi.components.ValidationContext
import org.apache.nifi.components.ValidationResult import org.apache.nifi.components.ValidationResult
import org.apache.nifi.components.Validator import org.apache.nifi.components.Validator
import org.apache.nifi.processor.util.JsonValidator import org.apache.nifi.processor.util.JsonValidator
import org.junit.Before import org.junit.jupiter.api.BeforeEach
import org.junit.Test import org.junit.jupiter.api.Test
import static groovy.json.JsonOutput.prettyPrint
import static groovy.json.JsonOutput.toJson
import static org.junit.Assert.assertFalse import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertTrue import static org.junit.Assert.assertTrue
import static org.mockito.Mockito.mock import static org.mockito.Mockito.mock
import static groovy.json.JsonOutput.*
class TestStandardValidators { class TestStandardValidators {
final String DUMMY_JSON_PROPERTY = "JSONProperty" final String DUMMY_JSON_PROPERTY = "JSONProperty"
Validator validator Validator validator
ValidationContext context ValidationContext context
@Before @BeforeEach
void setup() { void setup() {
validator = JsonValidator.INSTANCE validator = JsonValidator.INSTANCE
context = mock(ValidationContext.class) context = mock(ValidationContext.class)

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.nifi.parameter; package org.apache.nifi.parameter;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import static junit.framework.TestCase.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestExpressionLanguageAgnosticParameterParser { public class TestExpressionLanguageAgnosticParameterParser {

View File

@ -16,13 +16,14 @@
*/ */
package org.apache.nifi.parameter; package org.apache.nifi.parameter;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import static junit.framework.TestCase.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestExpressionLanguageAwareParameterParser { public class TestExpressionLanguageAwareParameterParser {

View File

@ -16,15 +16,15 @@
*/ */
package org.apache.nifi.parameter; package org.apache.nifi.parameter;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
public class TestStandardParameterTokenList { public class TestStandardParameterTokenList {

View File

@ -16,8 +16,7 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -29,10 +28,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -54,9 +55,9 @@ public class NiFiPropertiesTest {
directories.add(narLibDir.toString()); directories.add(narLibDir.toString());
} }
Assert.assertEquals("Did not have the anticipated number of directories", expectedDirectories.size(), directories.size()); assertEquals(expectedDirectories.size(), directories.size(), "Did not have the anticipated number of directories");
for (File expectedDirectory : expectedDirectories) { for (File expectedDirectory : expectedDirectories) {
Assert.assertTrue("Listed directories did not contain expected directory", directories.contains(expectedDirectory.getPath())); assertTrue(directories.contains(expectedDirectory.getPath()), "Listed directories did not contain expected directory");
} }
} }
@ -95,43 +96,31 @@ public class NiFiPropertiesTest {
additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "localhost"); additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "localhost");
NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
try { assertGoodProperties(properties);
properties.validate();
} catch (Throwable t) {
Assert.fail("unexpected exception: " + t.getMessage());
}
// expect no error to be thrown // expect no error to be thrown
additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, ""); additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "");
properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
try { assertGoodProperties(properties);
properties.validate();
} catch (Throwable t) {
Assert.fail("unexpected exception: " + t.getMessage());
}
// expect no error to be thrown // expect no error to be thrown
additionalProperties.remove(NiFiProperties.REMOTE_INPUT_HOST); additionalProperties.remove(NiFiProperties.REMOTE_INPUT_HOST);
properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
try { assertGoodProperties(properties);
properties.validate();
} catch (Throwable t) {
Assert.fail("unexpected exception: " + t.getMessage());
}
// expected error // expected error
additionalProperties = new HashMap<>(); additionalProperties = new HashMap<>();
additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "http://localhost"); additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "http://localhost");
properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
try { final NiFiProperties test = properties;
properties.validate(); assertThrows(Throwable.class, () -> test.validate());
Assert.fail("Validation should throw an exception"); }
} catch (Throwable t) {
// nothing to do private void assertGoodProperties(final NiFiProperties properties) {
} assertDoesNotThrow(() -> properties.validate());
} }
@Test @Test
@ -187,7 +176,7 @@ public class NiFiPropertiesTest {
assertEquals(Integer.parseInt(portValue), clusterProtocolPort.intValue()); assertEquals(Integer.parseInt(portValue), clusterProtocolPort.intValue());
} }
@Test(expected = NumberFormatException.class) @Test
public void testShouldVerifyExceptionThrownWhenInValidFormatPortValue() { public void testShouldVerifyExceptionThrownWhenInValidFormatPortValue() {
// Testing with CLUSTER_NODE_PROTOCOL_PORT // Testing with CLUSTER_NODE_PROTOCOL_PORT
@ -200,10 +189,7 @@ public class NiFiPropertiesTest {
// Act // Act
Integer clusterProtocolPort = properties.getClusterNodeProtocolPort(); Integer clusterProtocolPort = properties.getClusterNodeProtocolPort();
assertThrows(NumberFormatException.class, () -> Integer.parseInt(portValue));
// Assert
// Expect NumberFormatException thrown
assertEquals(Integer.parseInt(portValue), clusterProtocolPort.intValue());
} }
@Test @Test
@ -225,7 +211,7 @@ public class NiFiPropertiesTest {
assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort()); assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort());
} }
@Test(expected = RuntimeException.class) @Test
public void testShouldVerifyExceptionThrownWhenInvalidPortValue() { public void testShouldVerifyExceptionThrownWhenInvalidPortValue() {
// Testing with CLUSTER_NODE_ADDRESS // Testing with CLUSTER_NODE_ADDRESS
@ -238,15 +224,10 @@ public class NiFiPropertiesTest {
additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue); additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue);
NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
// Act assertThrows(RuntimeException.class, () -> properties.getClusterNodeProtocolAddress());
InetSocketAddress clusterProtocolAddress = properties.getClusterNodeProtocolAddress();
// Assert
// Expect RuntimeException thrown
assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort());
} }
@Test(expected = RuntimeException.class) @Test
public void testShouldVerifyExceptionThrownWhenPortValueIsZero() { public void testShouldVerifyExceptionThrownWhenPortValueIsZero() {
// Arrange // Arrange
String portValue = "0"; String portValue = "0";
@ -256,12 +237,7 @@ public class NiFiPropertiesTest {
additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue); additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue);
NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties);
// Act assertThrows(RuntimeException.class, () -> properties.getClusterNodeProtocolAddress());
InetSocketAddress clusterProtocolAddress = properties.getClusterNodeProtocolAddress();
// Assert
// Expect RuntimeException thrown
assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort());
} }
@Test @Test
@ -384,9 +360,9 @@ public class NiFiPropertiesTest {
final Map<String, String> result = testSubject.getPropertiesWithPrefix("nifi.web.http"); final Map<String, String> result = testSubject.getPropertiesWithPrefix("nifi.web.http");
// then // then
Assert.assertEquals(4, result.size()); assertEquals(4, result.size());
Assert.assertTrue(result.containsKey("nifi.web.http.host")); assertTrue(result.containsKey("nifi.web.http.host"));
Assert.assertTrue(result.containsKey("nifi.web.https.host")); assertTrue(result.containsKey("nifi.web.https.host"));
} }
@Test @Test
@ -398,9 +374,9 @@ public class NiFiPropertiesTest {
final Map<String, String> result = testSubject.getPropertiesWithPrefix("nifi.web.http."); final Map<String, String> result = testSubject.getPropertiesWithPrefix("nifi.web.http.");
// then // then
Assert.assertEquals(2, result.size()); assertEquals(2, result.size());
Assert.assertTrue(result.containsKey("nifi.web.http.host")); assertTrue(result.containsKey("nifi.web.http.host"));
Assert.assertFalse(result.containsKey("nifi.web.https.host")); assertFalse(result.containsKey("nifi.web.https.host"));
} }
@Test @Test
@ -412,7 +388,7 @@ public class NiFiPropertiesTest {
final Map<String, String> result = testSubject.getPropertiesWithPrefix("invalid.property"); final Map<String, String> result = testSubject.getPropertiesWithPrefix("invalid.property");
// then // then
Assert.assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
@Test @Test
@ -424,9 +400,9 @@ public class NiFiPropertiesTest {
final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web.http"); final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web.http");
// then // then
Assert.assertEquals(2, result.size()); assertEquals(2, result.size());
Assert.assertTrue(result.contains("host")); assertTrue(result.contains("host"));
Assert.assertTrue(result.contains("port")); assertTrue(result.contains("port"));
} }
@Test @Test
@ -438,9 +414,9 @@ public class NiFiPropertiesTest {
final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web.http."); final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web.http.");
// then // then
Assert.assertEquals(2, result.size()); assertEquals(2, result.size());
Assert.assertTrue(result.contains("host")); assertTrue(result.contains("host"));
Assert.assertTrue(result.contains("port")); assertTrue(result.contains("port"));
} }
@Test @Test
@ -452,7 +428,7 @@ public class NiFiPropertiesTest {
final Set<String> result = testSubject.getDirectSubsequentTokens("lorem.ipsum"); final Set<String> result = testSubject.getDirectSubsequentTokens("lorem.ipsum");
// then // then
Assert.assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
@Test @Test
@ -464,10 +440,10 @@ public class NiFiPropertiesTest {
final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web"); final Set<String> result = testSubject.getDirectSubsequentTokens("nifi.web");
// then // then
Assert.assertEquals(4, result.size()); assertEquals(4, result.size());
Assert.assertTrue(result.contains("http")); assertTrue(result.contains("http"));
Assert.assertTrue(result.contains("https")); assertTrue(result.contains("https"));
Assert.assertTrue(result.contains("war")); assertTrue(result.contains("war"));
Assert.assertTrue(result.contains("jetty")); assertTrue(result.contains("jetty"));
} }
} }

View File

@ -16,16 +16,17 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StringUtilsTest { public class StringUtilsTest {

View File

@ -22,8 +22,8 @@ import org.apache.nifi.security.util.EncryptionMethod;
import org.apache.nifi.security.util.crypto.AESKeyedCipherProvider; import org.apache.nifi.security.util.crypto.AESKeyedCipherProvider;
import org.apache.nifi.security.util.crypto.KeyedCipherProvider; import org.apache.nifi.security.util.crypto.KeyedCipherProvider;
import org.apache.nifi.util.StringUtils; import org.apache.nifi.util.StringUtils;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
@ -55,7 +55,7 @@ public class KeyedCipherPropertyEncryptorTest {
private KeyedCipherPropertyEncryptor encryptor; private KeyedCipherPropertyEncryptor encryptor;
@Before @BeforeEach
public void setUp() { public void setUp() {
encryptor = new KeyedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, SECRET_KEY); encryptor = new KeyedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, SECRET_KEY);
} }

View File

@ -20,8 +20,8 @@ import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.apache.nifi.security.util.EncryptionMethod; import org.apache.nifi.security.util.EncryptionMethod;
import org.apache.nifi.security.util.crypto.PBECipherProvider; import org.apache.nifi.security.util.crypto.PBECipherProvider;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -46,7 +46,7 @@ public class PasswordBasedCipherPropertyEncryptorTest {
private PasswordBasedCipherPropertyEncryptor encryptor; private PasswordBasedCipherPropertyEncryptor encryptor;
@Before @BeforeEach
public void setUp() { public void setUp() {
encryptor = new PasswordBasedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, PASSWORD); encryptor = new PasswordBasedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, PASSWORD);
} }

View File

@ -19,7 +19,7 @@ package org.apache.nifi.encrypt;
import org.apache.nifi.security.util.EncryptionMethod; import org.apache.nifi.security.util.EncryptionMethod;
import org.apache.nifi.util.NiFiProperties; import org.apache.nifi.util.NiFiProperties;
import org.apache.nifi.util.StringUtils; import org.apache.nifi.util.StringUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Properties; import java.util.Properties;

View File

@ -16,8 +16,8 @@
*/ */
package org.apache.nifi.encrypt; package org.apache.nifi.encrypt;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
@ -31,7 +31,7 @@ public class StandardPropertySecretKeyProviderTest {
private StandardPropertySecretKeyProvider provider; private StandardPropertySecretKeyProvider provider;
@Before @BeforeEach
public void setUp() { public void setUp() {
provider = new StandardPropertySecretKeyProvider(); provider = new StandardPropertySecretKeyProvider();
} }

View File

@ -28,9 +28,9 @@ import org.apache.nifi.serialization.record.RecordSchema;
import org.apache.nifi.serialization.record.type.ArrayDataType; import org.apache.nifi.serialization.record.type.ArrayDataType;
import org.apache.nifi.serialization.record.util.DataTypeUtils; import org.apache.nifi.serialization.record.util.DataTypeUtils;
import org.apache.nifi.uuid5.Uuid5Util; import org.apache.nifi.uuid5.Uuid5Util;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -57,8 +57,8 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class TestRecordPath { public class TestRecordPath {
@ -72,12 +72,12 @@ public class TestRecordPath {
private static final String TEST_TIMEZONE_OFFSET = String.format("GMT+0%d:00", TEST_OFFSET_HOURS); private static final String TEST_TIMEZONE_OFFSET = String.format("GMT+0%d:00", TEST_OFFSET_HOURS);
@BeforeClass @BeforeAll
public static void setTestTimezone() { public static void setTestTimezone() {
System.setProperty(USER_TIMEZONE_PROPERTY, TEST_TIMEZONE); System.setProperty(USER_TIMEZONE_PROPERTY, TEST_TIMEZONE);
} }
@AfterClass @AfterAll
public static void setSystemTimezone() { public static void setSystemTimezone() {
System.setProperty(USER_TIMEZONE_PROPERTY, SYSTEM_TIMEZONE); System.setProperty(USER_TIMEZONE_PROPERTY, SYSTEM_TIMEZONE);
} }
@ -93,12 +93,7 @@ public class TestRecordPath {
RecordPath.compile("/name[contains(., 'hello')]"); RecordPath.compile("/name[contains(., 'hello')]");
// substring is not a filter function so cannot be used as a predicate // substring is not a filter function so cannot be used as a predicate
try { assertThrows(RecordPathException.class, () -> RecordPath.compile("/name[substring(., 1, 2)]"));
RecordPath.compile("/name[substring(., 1, 2)]");
fail("Expected RecordPathException");
} catch (final RecordPathException e) {
// expected
}
// substring is not a filter function so can be used as *part* of a predicate but not as the entire predicate // substring is not a filter function so can be used as *part* of a predicate but not as the entire predicate
RecordPath.compile("/name[substring(., 1, 2) = 'e']"); RecordPath.compile("/name[substring(., 1, 2) = 'e']");
@ -1568,7 +1563,7 @@ public class TestRecordPath {
assertEquals("Hello World!", RecordPath.compile("toString(/bytes, \"UTF-16\")").evaluate(record).getSelectedFields().findFirst().get().getValue()); assertEquals("Hello World!", RecordPath.compile("toString(/bytes, \"UTF-16\")").evaluate(record).getSelectedFields().findFirst().get().getValue());
} }
@Test(expected = IllegalCharsetNameException.class) @Test
public void testToStringBadCharset() { public void testToStringBadCharset() {
final List<RecordField> fields = new ArrayList<>(); final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType())); fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
@ -1581,7 +1576,9 @@ public class TestRecordPath {
values.put("bytes", "Hello World!".getBytes(StandardCharsets.UTF_16)); values.put("bytes", "Hello World!".getBytes(StandardCharsets.UTF_16));
final Record record = new MapRecord(schema, values); final Record record = new MapRecord(schema, values);
RecordPath.compile("toString(/bytes, \"NOT A REAL CHARSET\")").evaluate(record).getSelectedFields().findFirst().get().getValue(); assertThrows(IllegalCharsetNameException.class, () ->
RecordPath.compile("toString(/bytes, \"NOT A REAL CHARSET\")").evaluate(record)
.getSelectedFields().findFirst().get().getValue());
} }
@Test @Test
@ -1601,7 +1598,7 @@ public class TestRecordPath {
(byte[]) RecordPath.compile("toBytes(/s, \"UTF-16LE\")").evaluate(record).getSelectedFields().findFirst().get().getValue()); (byte[]) RecordPath.compile("toBytes(/s, \"UTF-16LE\")").evaluate(record).getSelectedFields().findFirst().get().getValue());
} }
@Test(expected = IllegalCharsetNameException.class) @Test
public void testToBytesBadCharset() { public void testToBytesBadCharset() {
final List<RecordField> fields = new ArrayList<>(); final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType())); fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
@ -1614,7 +1611,8 @@ public class TestRecordPath {
values.put("s", "Hello World!"); values.put("s", "Hello World!");
final Record record = new MapRecord(schema, values); final Record record = new MapRecord(schema, values);
RecordPath.compile("toBytes(/s, \"NOT A REAL CHARSET\")").evaluate(record).getSelectedFields().findFirst().get().getValue(); assertThrows(IllegalCharsetNameException.class, () -> RecordPath.compile("toBytes(/s, \"NOT A REAL CHARSET\")").evaluate(record)
.getSelectedFields().findFirst().get().getValue());
} }
@Test @Test
@ -1796,21 +1794,20 @@ public class TestRecordPath {
// test invalid JSON // test invalid JSON
final Record recordInvalidJson = new MapRecord(schema, Collections.singletonMap("json_str", "{\"invalid\": \"json")); final Record recordInvalidJson = new MapRecord(schema, Collections.singletonMap("json_str", "{\"invalid\": \"json"));
try {
RecordPath.compile("unescapeJson(/json_str)").evaluate(recordInvalidJson).getSelectedFields().findFirst().orElseThrow(IllegalStateException::new).getValue(); RecordPathException rpe = assertThrows(RecordPathException.class,
fail("Expected a RecordPathException for invalid JSON"); () -> RecordPath.compile("unescapeJson(/json_str)")
} catch (RecordPathException rpe) { .evaluate(recordInvalidJson).getSelectedFields()
assertEquals("Unable to deserialise JSON String into Record Path value", rpe.getMessage()); .findFirst().orElseThrow(IllegalStateException::new).getValue());
} assertEquals("Unable to deserialise JSON String into Record Path value", rpe.getMessage());
// test not String // test not String
final Record recordNotString = new MapRecord(schema, Collections.singletonMap("person", new MapRecord(person, Collections.singletonMap("age", 30)))); final Record recordNotString = new MapRecord(schema, Collections.singletonMap("person", new MapRecord(person, Collections.singletonMap("age", 30))));
try { IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
RecordPath.compile("unescapeJson(/person/age)").evaluate(recordNotString).getSelectedFields().findFirst().orElseThrow(IllegalStateException::new).getValue(); () -> RecordPath.compile("unescapeJson(/person/age)")
fail("Expected IllegalArgumentException for non-String input"); .evaluate(recordNotString).getSelectedFields()
} catch (IllegalArgumentException iae) { .findFirst().orElseThrow(IllegalStateException::new).getValue());
assertEquals("Argument supplied to unescapeJson must be a String", iae.getMessage()); assertEquals("Argument supplied to unescapeJson must be a String", iae.getMessage());
}
} }
@Test @Test
@ -1820,10 +1817,11 @@ public class TestRecordPath {
assertEquals("5753a498f025464d72e088a9d5d6e872592d5f91", RecordPath.compile("hash(/firstName, 'SHA-1')").evaluate(record).getSelectedFields().findFirst().get().getValue()); assertEquals("5753a498f025464d72e088a9d5d6e872592d5f91", RecordPath.compile("hash(/firstName, 'SHA-1')").evaluate(record).getSelectedFields().findFirst().get().getValue());
} }
@Test(expected = RecordPathException.class) @Test
public void testHashFailure() { public void testHashFailure() {
final Record record = getCaseTestRecord(); final Record record = getCaseTestRecord();
assertEquals("61409aa1fd47d4a5332de23cbf59a36f", RecordPath.compile("hash(/firstName, 'NOT_A_ALGO')").evaluate(record).getSelectedFields().findFirst().get().getValue()); assertThrows(RecordPathException.class, () -> RecordPath.compile("hash(/firstName, 'NOT_A_ALGO')").evaluate(record)
.getSelectedFields().findFirst().get().getValue());
} }
@Test @Test

View File

@ -16,13 +16,6 @@
*/ */
package org.apache.nifi.record.path.util; package org.apache.nifi.record.path.util;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.nifi.record.path.FieldValue; import org.apache.nifi.record.path.FieldValue;
import org.apache.nifi.serialization.SimpleRecordSchema; import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.record.DataType; import org.apache.nifi.serialization.record.DataType;
@ -32,6 +25,13 @@ import org.apache.nifi.serialization.record.RecordField;
import org.apache.nifi.serialization.record.RecordFieldType; import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.serialization.record.RecordSchema; import org.apache.nifi.serialization.record.RecordSchema;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
/** /**
* Abstract base class for tests that walk FieldValue hierarchies. * Abstract base class for tests that walk FieldValue hierarchies.
*/ */

View File

@ -16,15 +16,16 @@
*/ */
package org.apache.nifi.record.path.util; package org.apache.nifi.record.path.util;
import static org.junit.Assert.assertEquals; import org.apache.nifi.record.path.FieldValue;
import org.apache.nifi.record.path.RecordPath;
import org.apache.nifi.serialization.record.Record;
import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.nifi.record.path.FieldValue; import static org.junit.Assert.assertEquals;
import org.apache.nifi.record.path.RecordPath; import static org.junit.Assert.assertThrows;
import org.apache.nifi.serialization.record.Record;
import org.junit.Test;
public class TestFieldValueLogicalPathBuilder extends AbstractWalkerTest { public class TestFieldValueLogicalPathBuilder extends AbstractWalkerTest {
@ -86,9 +87,9 @@ public class TestFieldValueLogicalPathBuilder extends AbstractWalkerTest {
} }
} }
@Test(expected = NullPointerException.class) @Test
public void buildLogicalPathBuilderFailsNull() { public void buildLogicalPathBuilderFailsNull() {
final FieldValueLogicalPathBuilder builder = getDefaultLogicalPathBuilder(); final FieldValueLogicalPathBuilder builder = getDefaultLogicalPathBuilder();
builder.buildLogicalPath(null); assertThrows(NullPointerException.class, () -> builder.buildLogicalPath(null));
} }
} }

View File

@ -16,15 +16,15 @@
*/ */
package org.apache.nifi.record.path.util; package org.apache.nifi.record.path.util;
import static org.junit.Assert.assertEquals; import org.apache.nifi.record.path.FieldValue;
import org.apache.nifi.record.path.RecordPath;
import org.apache.nifi.serialization.record.Record;
import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.nifi.record.path.FieldValue; import static org.junit.Assert.assertEquals;
import org.apache.nifi.record.path.RecordPath;
import org.apache.nifi.serialization.record.Record;
import org.junit.Test;
public class TestFieldValueWalker extends AbstractWalkerTest { public class TestFieldValueWalker extends AbstractWalkerTest {

View File

@ -27,5 +27,4 @@
several interfaces for interacting with Records. This module should not depend several interfaces for interacting with Records. This module should not depend
on any external libraries. on any external libraries.
</description> </description>
</project> </project>

View File

@ -20,8 +20,7 @@ package org.apache.nifi.serialization;
import org.apache.nifi.serialization.record.RecordField; import org.apache.nifi.serialization.record.RecordField;
import org.apache.nifi.serialization.record.RecordFieldType; import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.serialization.record.SchemaIdentifier; import org.apache.nifi.serialization.record.SchemaIdentifier;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,6 +30,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestSimpleRecordSchema { public class TestSimpleRecordSchema {
@ -40,11 +40,7 @@ public class TestSimpleRecordSchema {
fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar"))); fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar")));
fields.add(new RecordField("goodbye", RecordFieldType.STRING.getDataType(), null, set("baz", "bar"))); fields.add(new RecordField("goodbye", RecordFieldType.STRING.getDataType(), null, set("baz", "bar")));
try { assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields));
new SimpleRecordSchema(fields);
Assert.fail("Was able to create two fields with same alias");
} catch (final IllegalArgumentException expected) {
}
} }
@Test @Test
@ -53,11 +49,7 @@ public class TestSimpleRecordSchema {
fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar"))); fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar")));
fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType())); fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType()));
try { assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields));
new SimpleRecordSchema(fields);
Assert.fail("Was able to create two fields with same name");
} catch (final IllegalArgumentException expected) {
}
} }
@Test @Test
@ -66,11 +58,7 @@ public class TestSimpleRecordSchema {
fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar"))); fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar")));
fields.add(new RecordField("bar", RecordFieldType.STRING.getDataType())); fields.add(new RecordField("bar", RecordFieldType.STRING.getDataType()));
try { assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields));
new SimpleRecordSchema(fields);
Assert.fail("Was able to create two fields with conflicting names/aliases");
} catch (final IllegalArgumentException expected) {
}
} }
@Test @Test

View File

@ -19,13 +19,15 @@ package org.apache.nifi.serialization.record;
import org.apache.nifi.serialization.SimpleRecordSchema; import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.record.type.ArrayDataType; import org.apache.nifi.serialization.record.type.ArrayDataType;
import org.apache.nifi.serialization.record.type.DecimalDataType; import org.apache.nifi.serialization.record.type.DecimalDataType;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers; import org.mockito.ArgumentMatchers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Array; import java.sql.Array;
@ -54,7 +56,8 @@ import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class ResultSetRecordSetTest { public class ResultSetRecordSetTest {
private static final String COLUMN_NAME_VARCHAR = "varchar"; private static final String COLUMN_NAME_VARCHAR = "varchar";
@ -105,7 +108,7 @@ public class ResultSetRecordSetTest {
@Mock @Mock
private ResultSetMetaData resultSetMetaData; private ResultSetMetaData resultSetMetaData;
@Before @BeforeEach
public void setUp() throws SQLException { public void setUp() throws SQLException {
setUpMocks(COLUMNS, resultSetMetaData, resultSet); setUpMocks(COLUMNS, resultSetMetaData, resultSet);
} }

View File

@ -22,7 +22,7 @@ import org.apache.nifi.serialization.record.type.ChoiceDataType;
import org.apache.nifi.serialization.record.type.RecordDataType; import org.apache.nifi.serialization.record.type.RecordDataType;
import org.apache.nifi.serialization.record.util.DataTypeUtils; import org.apache.nifi.serialization.record.util.DataTypeUtils;
import org.apache.nifi.serialization.record.util.IllegalTypeConversionException; import org.apache.nifi.serialization.record.util.IllegalTypeConversionException;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -55,11 +55,12 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestDataTypeUtils { public class TestDataTypeUtils {
private static final ZoneId SYSTEM_DEFAULT_ZONE_ID = ZoneOffset.systemDefault(); private static final ZoneId SYSTEM_DEFAULT_ZONE_ID = ZoneOffset.systemDefault();
@ -82,12 +83,12 @@ public class TestDataTypeUtils {
Timestamp ts = DataTypeUtils.toTimestamp(date, null, null); Timestamp ts = DataTypeUtils.toTimestamp(date, null, null);
assertNotNull(ts); assertNotNull(ts);
assertEquals("Times didn't match", ts.getTime(), date.getTime()); assertEquals(ts.getTime(), date.getTime(), "Times didn't match");
java.sql.Date sDate = new java.sql.Date(date.getTime()); java.sql.Date sDate = new java.sql.Date(date.getTime());
ts = DataTypeUtils.toTimestamp(date, null, null); ts = DataTypeUtils.toTimestamp(date, null, null);
assertNotNull(ts); assertNotNull(ts);
assertEquals("Times didn't match", ts.getTime(), sDate.getTime()); assertEquals(ts.getTime(), sDate.getTime(), "Times didn't match");
} }
/* /*
@ -102,7 +103,7 @@ public class TestDataTypeUtils {
java.sql.Date output = DataTypeUtils.toDate(ts, null, null); java.sql.Date output = DataTypeUtils.toDate(ts, null, null);
assertNotNull(output); assertNotNull(output);
assertEquals("Timestamps didn't match", output.getTime(), ts.getTime()); assertEquals(output.getTime(), ts.getTime(), "Timestamps didn't match");
} }
@Test @Test
@ -266,11 +267,11 @@ public class TestDataTypeUtils {
assertTrue(bytes instanceof Byte[]); assertTrue(bytes instanceof Byte[]);
assertNotNull(bytes); assertNotNull(bytes);
Byte[] b = (Byte[]) bytes; Byte[] b = (Byte[]) bytes;
assertEquals("Conversion from String to byte[] failed", (long) 72, (long) b[0] ); // H assertEquals((long) 72, (long) b[0], "Conversion from String to byte[] failed"); // H
assertEquals("Conversion from String to byte[] failed", (long) 101, (long) b[1] ); // e assertEquals((long) 101, (long) b[1], "Conversion from String to byte[] failed" ); // e
assertEquals("Conversion from String to byte[] failed", (long) 108, (long) b[2] ); // l assertEquals((long) 108, (long) b[2], "Conversion from String to byte[] failed" ); // l
assertEquals("Conversion from String to byte[] failed", (long) 108, (long) b[3] ); // l assertEquals((long) 108, (long) b[3], "Conversion from String to byte[] failed" ); // l
assertEquals("Conversion from String to byte[] failed", (long) 111, (long) b[4] ); // o assertEquals((long) 111, (long) b[4], "Conversion from String to byte[] failed" ); // o
} }
@Test @Test
@ -278,7 +279,7 @@ public class TestDataTypeUtils {
Object s = DataTypeUtils.convertType("Hello".getBytes(StandardCharsets.UTF_16), RecordFieldType.STRING.getDataType(),null, StandardCharsets.UTF_16); Object s = DataTypeUtils.convertType("Hello".getBytes(StandardCharsets.UTF_16), RecordFieldType.STRING.getDataType(),null, StandardCharsets.UTF_16);
assertNotNull(s); assertNotNull(s);
assertTrue(s instanceof String); assertTrue(s instanceof String);
assertEquals("Conversion from byte[] to String failed", "Hello", s); assertEquals("Hello", s, "Conversion from byte[] to String failed");
} }
@Test @Test
@ -286,7 +287,7 @@ public class TestDataTypeUtils {
Object b = DataTypeUtils.convertType("Hello".getBytes(StandardCharsets.UTF_16), RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),null, StandardCharsets.UTF_16); Object b = DataTypeUtils.convertType("Hello".getBytes(StandardCharsets.UTF_16), RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),null, StandardCharsets.UTF_16);
assertNotNull(b); assertNotNull(b);
assertTrue(b instanceof Byte[]); assertTrue(b instanceof Byte[]);
assertEquals("Conversion from byte[] to String failed at char 0", (Object) "Hello".getBytes(StandardCharsets.UTF_16)[0], ((Byte[]) b)[0]); assertEquals((Object) "Hello".getBytes(StandardCharsets.UTF_16)[0], ((Byte[]) b)[0], "Conversion from byte[] to String failed at char 0");
} }
@Test @Test
@ -314,19 +315,22 @@ public class TestDataTypeUtils {
assertNull(DataTypeUtils.convertType(null, RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8)); assertNull(DataTypeUtils.convertType(null, RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8));
} }
@Test(expected = IllegalTypeConversionException.class) @Test
public void testConvertToBigDecimalWhenInputStringIsInvalid() { public void testConvertToBigDecimalWhenInputStringIsInvalid() {
DataTypeUtils.convertType("test", RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8); assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType("test", RecordFieldType.DECIMAL.getDecimalDataType(30, 10),
null, StandardCharsets.UTF_8));
} }
@Test(expected = IllegalTypeConversionException.class) @Test
public void testConvertToBigDecimalWhenUnsupportedType() { public void testConvertToBigDecimalWhenUnsupportedType() {
DataTypeUtils.convertType(new ArrayList<Double>(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8); assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType(new ArrayList<Double>(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10),
null, StandardCharsets.UTF_8));
} }
@Test(expected = IllegalTypeConversionException.class) @Test
public void testConvertToBigDecimalWhenUnsupportedNumberType() { public void testConvertToBigDecimalWhenUnsupportedNumberType() {
DataTypeUtils.convertType(new DoubleAdder(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8); assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType(new DoubleAdder(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10),
null, StandardCharsets.UTF_8));
} }
@Test @Test
@ -413,16 +417,16 @@ public class TestDataTypeUtils {
for (final String decimal : decimals) { for (final String decimal : decimals) {
for (final String exp : exponents) { for (final String exp : exponents) {
String toTest = prefix + "100" + decimal + exp; String toTest = prefix + "100" + decimal + exp;
assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest)); assertTrue(DataTypeUtils.isFloatTypeCompatible(toTest), toTest + " not valid float");
assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest)); assertTrue(DataTypeUtils.isDoubleTypeCompatible(toTest), toTest + " not valid double");
Double.parseDouble(toTest); // ensure we can actually parse it Double.parseDouble(toTest); // ensure we can actually parse it
Float.parseFloat(toTest); Float.parseFloat(toTest);
if (decimal.length() > 1) { if (decimal.length() > 1) {
toTest = prefix + decimal + exp; toTest = prefix + decimal + exp;
assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest)); assertTrue(DataTypeUtils.isFloatTypeCompatible(toTest), toTest + " not valid float");
assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest)); assertTrue(DataTypeUtils.isDoubleTypeCompatible(toTest), toTest + " not valid double");
Double.parseDouble(toTest); // ensure we can actually parse it Double.parseDouble(toTest); // ensure we can actually parse it
Float.parseFloat(toTest); Float.parseFloat(toTest);
} }
@ -532,7 +536,7 @@ public class TestDataTypeUtils {
}; };
Optional<String> actual = DataTypeUtils.findMostSuitableTypeByStringValue(valueAsString, types, dataTypeMapper); Optional<String> actual = DataTypeUtils.findMostSuitableTypeByStringValue(valueAsString, types, dataTypeMapper);
assertTrue("Exception not thrown during test as intended.", exceptionThrown.get()); assertTrue(exceptionThrown.get(), "Exception not thrown during test as intended.");
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ -753,11 +757,11 @@ public class TestDataTypeUtils {
final BigDecimal indirectResult = whenExpectingValidConversion(expectedValue, incomingValue, RecordFieldType.DECIMAL.getDecimalDataType(30, 10)); final BigDecimal indirectResult = whenExpectingValidConversion(expectedValue, incomingValue, RecordFieldType.DECIMAL.getDecimalDataType(30, 10));
// In some cases, direct equality check comes with false negative as the changing representation brings in // In some cases, direct equality check comes with false negative as the changing representation brings in
// insignificant changes what might break the comparison. For example 12F will be represented as "12.0" // insignificant changes what might break the comparison. For example 12F will be represented as "12.0"
assertEquals(failureMessage + "indirect", 0, expectedValue.compareTo(indirectResult)); assertEquals(0, expectedValue.compareTo(indirectResult), failureMessage + "indirect");
// Checking direct conversion // Checking direct conversion
final BigDecimal directResult = DataTypeUtils.toBigDecimal(incomingValue, "field"); final BigDecimal directResult = DataTypeUtils.toBigDecimal(incomingValue, "field");
assertEquals(failureMessage + "direct", 0, expectedValue.compareTo(directResult)); assertEquals(0, expectedValue.compareTo(directResult), failureMessage + "direct");
} }
@ -947,7 +951,7 @@ public class TestDataTypeUtils {
@Test @Test
public void testConvertTypeStringToDateDefaultTimeZoneFormat() { public void testConvertTypeStringToDateDefaultTimeZoneFormat() {
final Object converted = DataTypeUtils.convertType(ISO_8601_YEAR_MONTH_DAY, RecordFieldType.DATE.getDataType(), DATE_FIELD); final Object converted = DataTypeUtils.convertType(ISO_8601_YEAR_MONTH_DAY, RecordFieldType.DATE.getDataType(), DATE_FIELD);
assertTrue("Converted value is not java.sql.Date", converted instanceof java.sql.Date); assertTrue(converted instanceof java.sql.Date, "Converted value is not java.sql.Date");
assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString()); assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString());
} }
@ -958,7 +962,7 @@ public class TestDataTypeUtils {
public void testConvertTypeStringToDateConfiguredTimeZoneFormat() { public void testConvertTypeStringToDateConfiguredTimeZoneFormat() {
final DateFormat dateFormat = DataTypeUtils.getDateFormat(CUSTOM_MONTH_DAY_YEAR_PATTERN, "GMT"); final DateFormat dateFormat = DataTypeUtils.getDateFormat(CUSTOM_MONTH_DAY_YEAR_PATTERN, "GMT");
final Object converted = DataTypeUtils.convertType(CUSTOM_MONTH_DAY_YEAR, RecordFieldType.DATE.getDataType(), () -> dateFormat, null, null,"date"); final Object converted = DataTypeUtils.convertType(CUSTOM_MONTH_DAY_YEAR, RecordFieldType.DATE.getDataType(), () -> dateFormat, null, null,"date");
assertTrue("Converted value is not java.sql.Date", converted instanceof java.sql.Date); assertTrue(converted instanceof java.sql.Date, "Converted value is not java.sql.Date");
assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString()); assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString());
} }
@ -969,7 +973,7 @@ public class TestDataTypeUtils {
public void testConvertTypeStringToDateConfiguredSystemDefaultTimeZoneFormat() { public void testConvertTypeStringToDateConfiguredSystemDefaultTimeZoneFormat() {
final DateFormat dateFormat = DataTypeUtils.getDateFormat(CUSTOM_MONTH_DAY_YEAR_PATTERN, TimeZone.getDefault().getID()); final DateFormat dateFormat = DataTypeUtils.getDateFormat(CUSTOM_MONTH_DAY_YEAR_PATTERN, TimeZone.getDefault().getID());
final Object converted = DataTypeUtils.convertType(CUSTOM_MONTH_DAY_YEAR, RecordFieldType.DATE.getDataType(), () -> dateFormat, null, null,"date"); final Object converted = DataTypeUtils.convertType(CUSTOM_MONTH_DAY_YEAR, RecordFieldType.DATE.getDataType(), () -> dateFormat, null, null,"date");
assertTrue("Converted value is not java.sql.Date", converted instanceof java.sql.Date); assertTrue(converted instanceof java.sql.Date, "Converted value is not java.sql.Date");
assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString()); assertEquals(ISO_8601_YEAR_MONTH_DAY, converted.toString());
} }
@ -1006,6 +1010,6 @@ public class TestDataTypeUtils {
private void assertToLocalDateEquals(final String expected, final Object value) { private void assertToLocalDateEquals(final String expected, final Object value) {
final DateTimeFormatter systemDefaultZoneFormatter = DataTypeUtils.getDateTimeFormatter(RecordFieldType.DATE.getDefaultFormat(), SYSTEM_DEFAULT_ZONE_ID); final DateTimeFormatter systemDefaultZoneFormatter = DataTypeUtils.getDateTimeFormatter(RecordFieldType.DATE.getDefaultFormat(), SYSTEM_DEFAULT_ZONE_ID);
final LocalDate localDate = DataTypeUtils.toLocalDate(value, () -> systemDefaultZoneFormatter, DATE_FIELD); final LocalDate localDate = DataTypeUtils.toLocalDate(value, () -> systemDefaultZoneFormatter, DATE_FIELD);
assertEquals(String.format("Value Class [%s] to LocalDate not matched", value.getClass()), expected, localDate.toString()); assertEquals(expected, localDate.toString(), String.format("Value Class [%s] to LocalDate not matched", value.getClass()));
} }
} }

View File

@ -17,8 +17,8 @@
package org.apache.nifi.serialization.record; package org.apache.nifi.serialization.record;
import static org.junit.Assert.assertEquals; import org.apache.nifi.serialization.SimpleRecordSchema;
import static org.junit.Assert.assertNull; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -27,9 +27,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.nifi.serialization.SimpleRecordSchema; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Assert; import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestMapRecord { public class TestMapRecord {
@ -71,12 +71,7 @@ public class TestMapRecord {
new RecordField("hello", RecordFieldType.INT.getDataType(), 84); new RecordField("hello", RecordFieldType.INT.getDataType(), 84);
new RecordField("hello", RecordFieldType.INT.getDataType(), (Object) null); new RecordField("hello", RecordFieldType.INT.getDataType(), (Object) null);
try { assertThrows(IllegalArgumentException.class, () -> new RecordField("hello", RecordFieldType.INT.getDataType(), "foo"));
new RecordField("hello", RecordFieldType.INT.getDataType(), "foo");
Assert.fail("Was able to set a default value of \"foo\" for INT type");
} catch (final IllegalArgumentException expected) {
// expected
}
} }
private Set<String> set(final String... values) { private Set<String> set(final String... values) {

View File

@ -16,18 +16,19 @@
*/ */
package org.apache.nifi.rocksdb; package org.apache.nifi.rocksdb;
import org.apache.commons.lang3.SystemUtils; import org.junit.jupiter.api.AfterEach;
import org.junit.After; import org.junit.jupiter.api.BeforeEach;
import org.junit.Assume; import org.junit.jupiter.api.Test;
import org.junit.Before; import org.junit.jupiter.api.Timeout;
import org.junit.BeforeClass; import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.Rule; import org.junit.jupiter.api.condition.OS;
import org.junit.Test; import org.junit.jupiter.api.io.TempDir;
import org.junit.rules.TemporaryFolder;
import org.rocksdb.ColumnFamilyHandle; import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.RocksIterator; import org.rocksdb.RocksIterator;
import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -42,13 +43,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertThrows;
@DisabledOnOs(OS.WINDOWS)
public class TestRocksDBMetronome { public class TestRocksDBMetronome {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private static final byte[] KEY = "key".getBytes(StandardCharsets.UTF_8); private static final byte[] KEY = "key".getBytes(StandardCharsets.UTF_8);
private static final byte[] VALUE = "value".getBytes(StandardCharsets.UTF_8); private static final byte[] VALUE = "value".getBytes(StandardCharsets.UTF_8);
private static final byte[] KEY_2 = "key 2".getBytes(StandardCharsets.UTF_8); private static final byte[] KEY_2 = "key 2".getBytes(StandardCharsets.UTF_8);
@ -56,17 +55,12 @@ public class TestRocksDBMetronome {
private ExecutorService executor; private ExecutorService executor;
@BeforeClass @BeforeEach
public static void setupClass() {
Assume.assumeTrue("Test only runs on *nix", !SystemUtils.IS_OS_WINDOWS);
}
@Before
public void before() { public void before() {
executor = Executors.newSingleThreadExecutor(); executor = Executors.newSingleThreadExecutor();
} }
@After @AfterEach
public void after() { public void after() {
executor.shutdownNow(); executor.shutdownNow();
} }
@ -98,10 +92,16 @@ public class TestRocksDBMetronome {
} }
} }
private Path newFolder(Path parent) {
File newFolder = parent.resolve("temp-" + System.currentTimeMillis()).toFile();
newFolder.mkdirs();
return newFolder.toPath();
}
@Test @Test
public void testPutGetDelete() throws Exception { public void testPutGetDelete(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.build()) { .build()) {
db.initialize(); db.initialize();
@ -122,9 +122,9 @@ public class TestRocksDBMetronome {
} }
@Test @Test
public void testPutGetConfiguration() throws Exception { public void testPutGetConfiguration(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.build()) { .build()) {
db.initialize(); db.initialize();
@ -136,32 +136,36 @@ public class TestRocksDBMetronome {
} }
} }
@Test(expected = IllegalStateException.class) @Test
public void testPutBeforeInit() throws Exception { public void testPutBeforeInit(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() assertThrows(IllegalStateException.class, () -> {
.setStoragePath(temporaryFolder.newFolder().toPath()) try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(newFolder(temporaryFolder))
.build()) { .build()) {
db.put(KEY, VALUE); db.put(KEY, VALUE);
} }
} });
@Test(expected = IllegalStateException.class)
public void testPutClosed() throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath())
.build()) {
db.initialize();
db.close();
db.put(KEY_2, VALUE_2);
}
} }
@Test @Test
public void testColumnFamilies() throws Exception { public void testPutClosed(@TempDir Path temporaryFolder) {
assertThrows(IllegalStateException.class, () -> {
try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(newFolder(temporaryFolder))
.build()) {
db.initialize();
db.close();
db.put(KEY_2, VALUE_2);
}
});
}
@Test
public void testColumnFamilies(@TempDir Path temporaryFolder) throws Exception {
String secondFamilyName = "second family"; String secondFamilyName = "second family";
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.addColumnFamily(secondFamilyName) .addColumnFamily(secondFamilyName)
.build()) { .build()) {
db.initialize(); db.initialize();
@ -210,9 +214,9 @@ public class TestRocksDBMetronome {
} }
@Test @Test
public void testIterator() throws Exception { public void testIterator(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.build()) { .build()) {
db.initialize(); db.initialize();
@ -236,9 +240,9 @@ public class TestRocksDBMetronome {
} }
@Test @Test
public void testCounterIncrement() throws Exception { public void testCounterIncrement(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync
.build()) { .build()) {
db.initialize(); db.initialize();
@ -254,10 +258,11 @@ public class TestRocksDBMetronome {
} }
} }
@Test(timeout = 10_000) @Test
public void testWaitForSync() throws Exception { @Timeout(unit = TimeUnit.MILLISECONDS, value = 10_000)
public void testWaitForSync(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync
.build()) { .build()) {
db.initialize(); db.initialize();
@ -285,10 +290,11 @@ public class TestRocksDBMetronome {
} }
} }
@Test(timeout = 10_000) @Test
public void testWaitForSyncWithValue() throws Exception { @Timeout(unit = TimeUnit.MILLISECONDS, value = 10_000)
public void testWaitForSyncWithValue(@TempDir Path temporaryFolder) throws Exception {
try (RocksDBMetronome db = new RocksDBMetronome.Builder() try (RocksDBMetronome db = new RocksDBMetronome.Builder()
.setStoragePath(temporaryFolder.newFolder().toPath()) .setStoragePath(newFolder(temporaryFolder))
.setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync
.build()) { .build()) {
db.initialize(); db.initialize();
@ -312,15 +318,11 @@ public class TestRocksDBMetronome {
} }
} }
private void assertBlocks(RocksDBMetronome db, int counterValue) throws InterruptedException, java.util.concurrent.ExecutionException { private void assertBlocks(RocksDBMetronome db, int counterValue) {
Future<Boolean> future = getWaitForSyncFuture(db, counterValue); Future<Boolean> future = getWaitForSyncFuture(db, counterValue);
try { assertThrows(TimeoutException.class, () -> future.get(1, TimeUnit.SECONDS));
future.get(1, TimeUnit.SECONDS); assertFalse(future.isDone());
fail();
} catch (TimeoutException expected) {
assertFalse(future.isDone());
}
future.cancel(true); future.cancel(true);
} }

View File

@ -17,11 +17,7 @@
package org.apache.nifi.repository.schema; package org.apache.nifi.repository.schema;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -34,7 +30,11 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestSchemaRecordReader { public class TestSchemaRecordReader {

View File

@ -17,11 +17,8 @@
package org.apache.nifi.repository.schema; package org.apache.nifi.repository.schema;
import static org.apache.nifi.repository.schema.SchemaRecordWriter.MAX_ALLOWED_UTF_LENGTH; import org.apache.commons.lang3.StringUtils;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -35,8 +32,11 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang3.StringUtils; import static org.apache.nifi.repository.schema.SchemaRecordWriter.MAX_ALLOWED_UTF_LENGTH;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestSchemaRecordReaderWriter { public class TestSchemaRecordReaderWriter {
@ -237,69 +237,69 @@ public class TestSchemaRecordReaderWriter {
@Test @Test
public void testSingleCharUTF8Lengths() { public void testSingleCharUTF8Lengths() {
// verify handling of single characters mapping to utf8 byte strings // verify handling of single characters mapping to utf8 byte strings
assertEquals("test 1 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 0)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 0), "test 1 char string truncated to 0 utf bytes should be 0");
assertEquals("test 2 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 0)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 0), "test 2 char string truncated to 0 utf bytes should be 0");
assertEquals("test 3 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 0)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 0), "test 3 char string truncated to 0 utf bytes should be 0");
assertEquals("test 1 char string truncated to 1 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 1)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 1), "test 1 char string truncated to 1 utf bytes should be 1");
assertEquals("test 2 char string truncated to 1 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 1)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 1), "test 2 char string truncated to 1 utf bytes should be 0");
assertEquals("test 3 char string truncated to 1 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 1)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 1), "test 3 char string truncated to 1 utf bytes should be 0");
assertEquals("test 1 char string truncated to 2 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 2)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 2), "test 1 char string truncated to 2 utf bytes should be 1");
assertEquals("test 2 char string truncated to 2 utf bytes should be 2", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 2)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 2), "test 2 char string truncated to 2 utf bytes should be 2");
assertEquals("test 3 char string truncated to 2 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 2)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 2), "test 3 char string truncated to 2 utf bytes should be 0");
assertEquals("test 1 char string truncated to 3 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 3)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 3), "test 1 char string truncated to 3 utf bytes should be 1");
assertEquals("test 2 char string truncated to 3 utf bytes should be 2", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 3)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 3), "test 2 char string truncated to 3 utf bytes should be 2");
assertEquals("test 3 char string truncated to 3 utf bytes should be 3", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 3)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 3), "test 3 char string truncated to 3 utf bytes should be 3");
} }
@Test @Test
public void testMultiCharUTFLengths() { public void testMultiCharUTFLengths() {
// test boundary conditions as 1, 2, and 3 UTF byte chars are included into utf limit positions used by strings // test boundary conditions as 1, 2, and 3 UTF byte chars are included into utf limit positions used by strings
final String testString1 = utfStringOneByte + utfStringTwoByte + utfStringThreeByte; // char 'abc' utf 'abbccc' final String testString1 = utfStringOneByte + utfStringTwoByte + utfStringThreeByte; // char 'abc' utf 'abbccc'
assertEquals("test 6 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 0)); // utf '' assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 0), "test 6 char string truncated to 0 utf bytes should be 0"); // utf ''
assertEquals("test 6 char string truncated to 1 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 1)); // utf 'a' assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 1), "test 6 char string truncated to 1 utf bytes should be 1"); // utf 'a'
assertEquals("test 6 char string truncated to 2 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 2)); // utf 'a' assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 2), "test 6 char string truncated to 2 utf bytes should be 1"); // utf 'a'
assertEquals("test 6 char string truncated to 3 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 3)); // utf 'abb' assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 3), "test 6 char string truncated to 3 utf bytes should be 2"); // utf 'abb'
assertEquals("test 6 char string truncated to 4 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 4)); // utf 'abb' assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 4), "test 6 char string truncated to 4 utf bytes should be 2"); // utf 'abb'
assertEquals("test 6 char string truncated to 5 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 5)); // utf 'abb' assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 5), "test 6 char string truncated to 5 utf bytes should be 2"); // utf 'abb'
assertEquals("test 6 char string truncated to 6 utf bytes should be 3", 3, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 6)); // utf 'abbccc' assertEquals(3, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 6), "test 6 char string truncated to 6 utf bytes should be 3"); // utf 'abbccc'
} }
@Test @Test
public void testSmallCharUTFLengths() throws UnsupportedEncodingException { public void testSmallCharUTFLengths() throws UnsupportedEncodingException {
final String string12b = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 2); final String string12b = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 2);
assertEquals("test multi-char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 0)); assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 0), "test multi-char string truncated to 0 utf bytes should be 0");
assertEquals("test multi-char string truncated to 1 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 1)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 1), "test multi-char string truncated to 1 utf bytes should be 0");
assertEquals("test multi-char string truncated to 2 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 2)); assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 2), "test multi-char string truncated to 2 utf bytes should be 0");
assertEquals("test multi-char string truncated to 3 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 3)); assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 3), "test multi-char string truncated to 3 utf bytes should be 0");
assertEquals("test multi-char string truncated to 4 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 4)); assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 4), "test multi-char string truncated to 4 utf bytes should be 0");
assertEquals("test multi-char string truncated to 5 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 5)); assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 5), "test multi-char string truncated to 5 utf bytes should be 0");
assertEquals("test multi-char string truncated to 6 utf bytes should be 0", 3, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 6)); assertEquals(3, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 6), "test multi-char string truncated to 6 utf bytes should be 0");
assertEquals("test multi-char string truncated to 7 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 7)); assertEquals(4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 7), "test multi-char string truncated to 7 utf bytes should be 0");
assertEquals("test multi-char string truncated to 8 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 8)); assertEquals(4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 8), "test multi-char string truncated to 8 utf bytes should be 0");
assertEquals("test multi-char string truncated to 9 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 9)); assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 9), "test multi-char string truncated to 9 utf bytes should be 0");
assertEquals("test multi-char string truncated to 10 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 10)); assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 10), "test multi-char string truncated to 10 utf bytes should be 0");
assertEquals("test multi-char string truncated to 11 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 11)); assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 11), "test multi-char string truncated to 11 utf bytes should be 0");
assertEquals("test multi-char string truncated to 12 utf bytes should be 0", 6, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 12)); assertEquals(6, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 12), "test multi-char string truncated to 12 utf bytes should be 0");
} }
@Test @Test
public void testLargeCharUTFLengths() { public void testLargeCharUTFLengths() {
final String string64k = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 21845); final String string64k = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 21845);
assertEquals("test 64k char string should be 64k chars long", 65535, string64k.length()); assertEquals(65535, string64k.length(), "test 64k char string should be 64k chars long");
// drop half the chars going to utf of 64k bytes -- (1+1+1) * 21845 = 65535 chars which converts to (1+2+3) * 21845 = 131070 utf bytes so 1/2 is truncated // drop half the chars going to utf of 64k bytes -- (1+1+1) * 21845 = 65535 chars which converts to (1+2+3) * 21845 = 131070 utf bytes so 1/2 is truncated
assertEquals("test 64k char string truncated to 65,535 utf bytes should be 32768", 32768, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65535)); assertEquals(32768, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65535), "test 64k char string truncated to 65,535 utf bytes should be 32768");
// dropping bytes off the end of utf length // dropping bytes off the end of utf length
assertEquals("test 64k char string truncated to 65,534 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65534)); // lost 2 byte char assertEquals(32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65534), "test 64k char string truncated to 65,534 utf bytes should be 32767"); // lost 2 byte char
assertEquals("test 64k char string truncated to 65,533 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65533)); assertEquals(32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65533), "test 64k char string truncated to 65,533 utf bytes should be 32767");
assertEquals("test 64k char string truncated to 65,532 utf bytes should be 32766", 32766, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65532)); // lost 1 byte char assertEquals(32766, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65532), "test 64k char string truncated to 65,532 utf bytes should be 32766"); // lost 1 byte char
assertEquals("test 64k char string truncated to 65,531 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65531)); // lost 3 byte char assertEquals(32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65531), "test 64k char string truncated to 65,531 utf bytes should be 32765"); // lost 3 byte char
assertEquals("test 64k char string truncated to 65,530 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65530)); assertEquals(32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65530), "test 64k char string truncated to 65,530 utf bytes should be 32765");
assertEquals("test 64k char string truncated to 65,529 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65529)); assertEquals(32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65529), "test 64k char string truncated to 65,529 utf bytes should be 32765");
assertEquals("test 64k char string truncated to 65,528 utf bytes should be 32764", 32764, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65528)); // lost 2 byte char (again) assertEquals(32764, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65528), "test 64k char string truncated to 65,528 utf bytes should be 32764"); // lost 2 byte char (again)
} }
private SimpleRecordField createField(final String fieldName, final FieldType type) { private SimpleRecordField createField(final String fieldName, final FieldType type) {

View File

@ -18,10 +18,9 @@ package org.apache.nifi.security.krb;
import org.apache.nifi.logging.ComponentLog; import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.ProcessContext; import org.apache.nifi.processor.ProcessContext;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.ClassRule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.io.TempDir;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito; import org.mockito.Mockito;
import javax.security.auth.Subject; import javax.security.auth.Subject;
@ -29,6 +28,7 @@ import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.kerberos.KerberosTicket; import javax.security.auth.kerberos.KerberosTicket;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.security.AccessControlContext; import java.security.AccessControlContext;
import java.security.AccessController; import java.security.AccessController;
import java.security.Principal; import java.security.Principal;
@ -41,13 +41,9 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertThrows;
public class KerberosUserIT { public class KerberosUserIT {
@ClassRule
public static TemporaryFolder tmpDir = new TemporaryFolder();
private static KDCServer kdc; private static KDCServer kdc;
private static KerberosPrincipal principal1; private static KerberosPrincipal principal1;
@ -59,18 +55,21 @@ public class KerberosUserIT {
private static KerberosPrincipal principal3; private static KerberosPrincipal principal3;
private static final String principal3Password = "changeme"; private static final String principal3Password = "changeme";
@BeforeClass @BeforeAll
public static void setupClass() throws Exception { public static void setupClass(@TempDir Path tmpDir) throws Exception {
kdc = new KDCServer(tmpDir.newFolder("mini-kdc_")); File kdcFolder = tmpDir.resolve("mini-kdc_").toFile();
kdcFolder.mkdirs();
kdc = new KDCServer(kdcFolder);
kdc.setMaxTicketLifetime("15"); // set ticket lifetime to 15 seconds so we can test relogin kdc.setMaxTicketLifetime("15"); // set ticket lifetime to 15 seconds so we can test relogin
kdc.start(); kdc.start();
principal1 = new KerberosPrincipal("user1@" + kdc.getRealm()); principal1 = new KerberosPrincipal("user1@" + kdc.getRealm());
principal1KeytabFile = tmpDir.newFile("user1.keytab"); principal1KeytabFile = tmpDir.resolve("user1.keytab").toFile();
kdc.createKeytabPrincipal(principal1KeytabFile, "user1"); kdc.createKeytabPrincipal(principal1KeytabFile, "user1");
principal2 = new KerberosPrincipal("user2@" + kdc.getRealm()); principal2 = new KerberosPrincipal("user2@" + kdc.getRealm());
principal2KeytabFile = tmpDir.newFile("user2.keytab"); principal2KeytabFile = tmpDir.resolve("user2.keytab").toFile();
kdc.createKeytabPrincipal(principal2KeytabFile, "user2"); kdc.createKeytabPrincipal(principal2KeytabFile, "user2");
principal3 = new KerberosPrincipal("user3@" + kdc.getRealm()); principal3 = new KerberosPrincipal("user3@" + kdc.getRealm());
@ -113,16 +112,10 @@ public class KerberosUserIT {
} }
@Test @Test
public void testKeytabLoginWithUnknownPrincipal() throws LoginException { public void testKeytabLoginWithUnknownPrincipal() {
final String unknownPrincipal = "doesnotexist@" + kdc.getRealm(); final String unknownPrincipal = "doesnotexist@" + kdc.getRealm();
final KerberosUser user1 = new KerberosKeytabUser(unknownPrincipal, principal1KeytabFile.getAbsolutePath()); final KerberosUser user1 = new KerberosKeytabUser(unknownPrincipal, principal1KeytabFile.getAbsolutePath());
try { assertThrows(Exception.class, () -> user1.login());
user1.login();
fail("Login should have failed");
} catch (Exception e) {
// exception is expected here
//e.printStackTrace();
}
} }
@Test @Test
@ -147,11 +140,11 @@ public class KerberosUserIT {
assertEquals(0, userSubject.getPrincipals().size()); assertEquals(0, userSubject.getPrincipals().size());
} }
@Test(expected = LoginException.class) @Test
public void testPasswordUserLoginWithInvalidPassword() throws LoginException { public void testPasswordUserLoginWithInvalidPassword() {
// perform login for user // perform login for user
final KerberosUser user = new KerberosPasswordUser("user3", "NOT THE PASSWORD"); final KerberosUser user = new KerberosPasswordUser("user3", "NOT THE PASSWORD");
user.login(); assertThrows(LoginException.class, () -> user.login());
} }
@Test @Test

View File

@ -17,7 +17,7 @@
package org.apache.nifi.security.kms; package org.apache.nifi.security.kms;
import org.apache.nifi.security.kms.util.SecretKeyUtils; import org.apache.nifi.security.kms.util.SecretKeyUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -31,9 +31,9 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class FileBasedKeyProviderTest { public class FileBasedKeyProviderTest {
private static final String KEYS_EXTENSION = ".keys"; private static final String KEYS_EXTENSION = ".keys";

View File

@ -22,7 +22,7 @@ import org.apache.nifi.security.kms.configuration.KeyProviderConfiguration;
import org.apache.nifi.security.kms.configuration.KeyStoreKeyProviderConfiguration; import org.apache.nifi.security.kms.configuration.KeyStoreKeyProviderConfiguration;
import org.apache.nifi.security.kms.configuration.StaticKeyProviderConfiguration; import org.apache.nifi.security.kms.configuration.StaticKeyProviderConfiguration;
import org.apache.nifi.security.kms.util.SecretKeyUtils; import org.apache.nifi.security.kms.util.SecretKeyUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.io.File; import java.io.File;
@ -32,8 +32,8 @@ import java.security.KeyStore;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
public class KeyProviderFactoryTest { public class KeyProviderFactoryTest {

View File

@ -17,8 +17,8 @@
package org.apache.nifi.security.kms; package org.apache.nifi.security.kms;
import org.apache.nifi.security.kms.util.SecretKeyUtils; import org.apache.nifi.security.kms.util.SecretKeyUtils;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.io.IOException; import java.io.IOException;
@ -28,9 +28,9 @@ import java.security.KeyStore;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class KeyStoreKeyProviderTest { public class KeyStoreKeyProviderTest {
private static final String KEY_ID = UUID.randomUUID().toString(); private static final String KEY_ID = UUID.randomUUID().toString();
@ -43,7 +43,7 @@ public class KeyStoreKeyProviderTest {
private static KeyStore keyStore; private static KeyStore keyStore;
@BeforeClass @BeforeAll
public static void setKeyStore() throws GeneralSecurityException, IOException { public static void setKeyStore() throws GeneralSecurityException, IOException {
keyStore = getKeyStore(); keyStore = getKeyStore();
} }

View File

@ -17,7 +17,7 @@
package org.apache.nifi.security.kms; package org.apache.nifi.security.kms;
import org.apache.nifi.security.kms.util.SecretKeyUtils; import org.apache.nifi.security.kms.util.SecretKeyUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.security.KeyManagementException; import java.security.KeyManagementException;
@ -25,9 +25,9 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class StaticKeyProviderTest { public class StaticKeyProviderTest {
private static final String KEY_ID = UUID.randomUUID().toString(); private static final String KEY_ID = UUID.randomUUID().toString();

View File

@ -17,7 +17,7 @@
package org.apache.nifi.security.kms.reader; package org.apache.nifi.security.kms.reader;
import org.apache.nifi.security.kms.util.SecretKeyUtils; import org.apache.nifi.security.kms.util.SecretKeyUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -28,7 +28,7 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class StandardFileBasedKeyReaderTest { public class StandardFileBasedKeyReaderTest {
private static final String KEYS_EXTENSION = ".keys"; private static final String KEYS_EXTENSION = ".keys";
@ -45,7 +45,7 @@ public class StandardFileBasedKeyReaderTest {
final Path path = getSecretKeysPath(ROOT_KEY, Collections.singletonMap(keyId, secretKey)); final Path path = getSecretKeysPath(ROOT_KEY, Collections.singletonMap(keyId, secretKey));
final Map<String, SecretKey> secretKeys = reader.readSecretKeys(path, ROOT_KEY); final Map<String, SecretKey> secretKeys = reader.readSecretKeys(path, ROOT_KEY);
final SecretKey readSecretKey = secretKeys.get(keyId); final SecretKey readSecretKey = secretKeys.get(keyId);
assertEquals("Secret Key not matched", secretKey, readSecretKey); assertEquals(secretKey, readSecretKey, "Secret Key not matched");
} }
private Path getSecretKeysPath(final SecretKey rootKey, final Map<String, SecretKey> secretKeys) throws Exception { private Path getSecretKeysPath(final SecretKey rootKey, final Map<String, SecretKey> secretKeys) throws Exception {

View File

@ -170,12 +170,14 @@ public class SSLSocketChannelTest {
} }
@Test @Test
@Timeout(value = CHANNEL_TIMEOUT, unit = TimeUnit.MILLISECONDS)
public void testServerReadWriteTls12() throws Exception { public void testServerReadWriteTls12() throws Exception {
assertServerChannelConnectedReadClosed(TLS_1_2); assertServerChannelConnectedReadClosed(TLS_1_2);
} }
@EnabledIf(TLS_1_3_SUPPORTED) @EnabledIf(TLS_1_3_SUPPORTED)
@Test @Test
@Timeout(value = CHANNEL_TIMEOUT, unit = TimeUnit.MILLISECONDS)
public void testServerReadWriteTls13() throws Exception { public void testServerReadWriteTls13() throws Exception {
assertServerChannelConnectedReadClosed(TLS_1_3); assertServerChannelConnectedReadClosed(TLS_1_3);
} }

View File

@ -16,34 +16,30 @@
*/ */
package org.apache.nifi.security.util package org.apache.nifi.security.util
import org.junit.jupiter.api.AfterEach
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.BeforeEach
import org.junit.BeforeClass import org.junit.jupiter.api.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@RunWith(JUnit4.class)
class TlsConfigurationTest extends GroovyTestCase { class TlsConfigurationTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(TlsConfigurationTest.class) private static final Logger logger = LoggerFactory.getLogger(TlsConfigurationTest.class)
@BeforeClass @BeforeAll
static void setUpOnce() { static void setUpOnce() {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before @BeforeEach
void setUp() { void setUp() {
super.setUp() super.setUp()
} }
@After @AfterEach
void tearDown() { void tearDown() {
} }

View File

@ -16,16 +16,16 @@
*/ */
package org.apache.nifi.security.util; package org.apache.nifi.security.util;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
public class TlsPlatformTest { public class TlsPlatformTest {
private static final int ZERO_LENGTH = 0; private static final int ZERO_LENGTH = 0;

View File

@ -20,22 +20,14 @@ import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers
import org.bouncycastle.asn1.x500.X500Name import org.bouncycastle.asn1.x500.X500Name
import org.bouncycastle.asn1.x500.style.BCStyle import org.bouncycastle.asn1.x500.style.BCStyle
import org.bouncycastle.asn1.x500.style.IETFUtils import org.bouncycastle.asn1.x500.style.IETFUtils
import org.bouncycastle.asn1.x509.Extension import org.bouncycastle.asn1.x509.*
import org.bouncycastle.asn1.x509.Extensions
import org.bouncycastle.asn1.x509.ExtensionsGenerator
import org.bouncycastle.asn1.x509.GeneralName
import org.bouncycastle.asn1.x509.GeneralNames
import org.bouncycastle.operator.OperatorCreationException import org.bouncycastle.operator.OperatorCreationException
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder
import org.bouncycastle.util.IPAddress import org.bouncycastle.util.IPAddress
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -43,27 +35,15 @@ import javax.net.ssl.SSLException
import javax.net.ssl.SSLPeerUnverifiedException import javax.net.ssl.SSLPeerUnverifiedException
import javax.net.ssl.SSLSession import javax.net.ssl.SSLSession
import javax.net.ssl.SSLSocket import javax.net.ssl.SSLSocket
import java.security.InvalidKeyException import java.security.*
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.NoSuchAlgorithmException
import java.security.NoSuchProviderException
import java.security.SignatureException
import java.security.cert.Certificate import java.security.cert.Certificate
import java.security.cert.CertificateException import java.security.cert.CertificateException
import java.security.cert.X509Certificate import java.security.cert.X509Certificate
import java.util.concurrent.Callable import java.util.concurrent.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue import static org.junit.Assert.assertTrue
@RunWith(JUnit4.class)
class CertificateUtilsTest extends GroovyTestCase { class CertificateUtilsTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(CertificateUtilsTest.class) private static final Logger logger = LoggerFactory.getLogger(CertificateUtilsTest.class)
@ -80,24 +60,13 @@ class CertificateUtilsTest extends GroovyTestCase {
private static final String ISSUER_DN = "CN=NiFi Test CA,OU=Security,O=Apache,ST=CA,C=US" private static final String ISSUER_DN = "CN=NiFi Test CA,OU=Security,O=Apache,ST=CA,C=US"
private static final List<String> SUBJECT_ALT_NAMES = ["127.0.0.1", "nifi.nifi.apache.org"] private static final List<String> SUBJECT_ALT_NAMES = ["127.0.0.1", "nifi.nifi.apache.org"]
@BeforeClass @BeforeAll
static void setUpOnce() { static void setUpOnce() {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before
void setUp() {
super.setUp()
}
@After
void tearDown() {
}
/** /**
* Generates a public/private RSA keypair using the default key size. * Generates a public/private RSA keypair using the default key size.
* *

View File

@ -19,13 +19,8 @@ package org.apache.nifi.security.util.crypto
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.Test
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -36,8 +31,8 @@ import java.security.SecureRandom
import java.security.Security import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class AESKeyedCipherProviderGroovyTest { class AESKeyedCipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(AESKeyedCipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(AESKeyedCipherProviderGroovyTest.class)
@ -49,7 +44,7 @@ class AESKeyedCipherProviderGroovyTest {
private static final SecretKey key = new SecretKeySpec(Hex.decodeHex(KEY_HEX as char[]), "AES") private static final SecretKey key = new SecretKeySpec(Hex.decodeHex(KEY_HEX as char[]), "AES")
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -58,14 +53,6 @@ class AESKeyedCipherProviderGroovyTest {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
private static boolean isUnlimitedStrengthCryptoAvailable() { private static boolean isUnlimitedStrengthCryptoAvailable() {
Cipher.getMaxAllowedKeyLength("AES") > 128 Cipher.getMaxAllowedKeyLength("AES") > 128
} }
@ -127,7 +114,7 @@ class AESKeyedCipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", isUnlimitedStrengthCryptoAvailable()) assumeTrue(isUnlimitedStrengthCryptoAvailable(), "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.")
KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider() KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider()
final List<Integer> LONG_KEY_LENGTHS = [192, 256] final List<Integer> LONG_KEY_LENGTHS = [192, 256]

View File

@ -20,13 +20,10 @@ import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.Assumptions
import org.junit.Assume import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.BeforeEach
import org.junit.BeforeClass import org.junit.jupiter.api.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -38,7 +35,6 @@ import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
@RunWith(JUnit4.class)
class Argon2CipherProviderGroovyTest extends GroovyTestCase { class Argon2CipherProviderGroovyTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(Argon2CipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(Argon2CipherProviderGroovyTest.class)
@ -53,7 +49,7 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase {
RandomIVPBECipherProvider cipherProvider RandomIVPBECipherProvider cipherProvider
private final IntRange FULL_SALT_LENGTH_RANGE= (49..53) private final IntRange FULL_SALT_LENGTH_RANGE= (49..53)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -70,17 +66,12 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase {
} }
} }
@Before @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
// Very fast parameters to test for correctness rather than production values // Very fast parameters to test for correctness rather than production values
cipherProvider = new Argon2CipherProvider(1024, 1, 3) cipherProvider = new Argon2CipherProvider(1024, 1, 3)
} }
@After
void tearDown() throws Exception {
}
@Test @Test
void testGetCipherShouldBeInternallyConsistent() throws Exception { void testGetCipherShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
@ -234,8 +225,8 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", Assumptions.assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(),
CipherUtility.isUnlimitedStrengthCryptoSupported()) "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.")
final String PASSWORD = "shortPassword" final String PASSWORD = "shortPassword"
final byte[] SALT = cipherProvider.generateSalt() final byte[] SALT = cipherProvider.generateSalt()

View File

@ -18,22 +18,20 @@ package org.apache.nifi.security.util.crypto
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.BeforeClass import org.junit.jupiter.api.BeforeAll
import org.junit.Ignore import org.junit.jupiter.api.Disabled
import org.junit.Test import org.junit.jupiter.api.Test
import org.junit.runner.RunWith import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class Argon2SecureHasherTest extends GroovyTestCase { class Argon2SecureHasherTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(Argon2SecureHasherTest.class) private static final Logger logger = LoggerFactory.getLogger(Argon2SecureHasherTest.class)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -46,32 +44,6 @@ class Argon2SecureHasherTest extends GroovyTestCase {
Hex.decode(hex?.replaceAll("[^0-9a-fA-F]", "")) Hex.decode(hex?.replaceAll("[^0-9a-fA-F]", ""))
} }
@Ignore("Cannot override static salt")
@Test
void testShouldMatchReferenceVectors() {
// Arrange
int hashLength = 32
int memory = 32
int parallelism = 4
int iterations = 3
logger.info("Generating Argon2 hash for hash length: ${hashLength} B, mem: ${memory} KiB, parallelism: ${parallelism}, iterations: ${iterations}")
Argon2SecureHasher a2sh = new Argon2SecureHasher(hashLength, memory, parallelism, iterations)
// Override the static salt for the published test vector
// a2sh.staticSalt = [0x02] * 16
// Act
byte[] hash = a2sh.hashRaw([0x01] * 32 as byte[])
logger.info("Generated hash: ${Hex.encode(hash)}")
// Assert
assert hash == decodeHex("0d 64 0d f5 8d 78 76 6c 08 c0 37 a3 4a 8b 53 c9 d0 " +
"1e f0 45 2d 75 b6 5e b5 25 20 e9 6b 01 e6 59")
// Clean up
// Argon2SecureHasher.staticSalt = "NiFi Static Salt".bytes
}
@Test @Test
void testShouldBeDeterministicWithStaticSalt() { void testShouldBeDeterministicWithStaticSalt() {
// Arrange // Arrange
@ -297,7 +269,7 @@ class Argon2SecureHasherTest extends GroovyTestCase {
* This test can have the minimum time threshold updated to determine if the performance * This test can have the minimum time threshold updated to determine if the performance
* is still sufficient compared to the existing threat model. * is still sufficient compared to the existing threat model.
*/ */
@Ignore("Long running test") @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true")
@Test @Test
void testDefaultCostParamsShouldBeSufficient() { void testDefaultCostParamsShouldBeSufficient() {
// Arrange // Arrange

View File

@ -21,14 +21,9 @@ import at.favre.lib.crypto.bcrypt.Radix64Encoder
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.Disabled
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -41,8 +36,8 @@ import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.Assert.assertTrue import static org.junit.Assert.assertTrue
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class BcryptCipherProviderGroovyTest { class BcryptCipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(BcryptCipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(BcryptCipherProviderGroovyTest.class)
@ -54,7 +49,7 @@ class BcryptCipherProviderGroovyTest {
public static final String MICROBENCHMARK = "microbenchmark" public static final String MICROBENCHMARK = "microbenchmark"
private static ArrayList<Integer> AES_KEY_LENGTHS private static ArrayList<Integer> AES_KEY_LENGTHS
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -71,15 +66,6 @@ class BcryptCipherProviderGroovyTest {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testGetCipherShouldBeInternallyConsistent() throws Exception { void testGetCipherShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
@ -143,8 +129,8 @@ class BcryptCipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(),
CipherUtility.isUnlimitedStrengthCryptoSupported()) "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.")
RandomIVPBECipherProvider cipherProvider = new BcryptCipherProvider(4) RandomIVPBECipherProvider cipherProvider = new BcryptCipherProvider(4)
@ -610,7 +596,7 @@ class BcryptCipherProviderGroovyTest {
assert decryptMsg =~ "The salt must be of the format" assert decryptMsg =~ "The salt must be of the format"
} }
@Ignore("This test can be run on a specific machine to evaluate if the default work factor is sufficient") @Disabled("This test can be run on a specific machine to evaluate if the default work factor is sufficient")
@Test @Test
void testDefaultConstructorShouldProvideStrongWorkFactor() { void testDefaultConstructorShouldProvideStrongWorkFactor() {
// Arrange // Arrange

View File

@ -18,37 +18,25 @@ package org.apache.nifi.security.util.crypto
import at.favre.lib.crypto.bcrypt.Radix64Encoder import at.favre.lib.crypto.bcrypt.Radix64Encoder
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Disabled
import org.junit.BeforeClass import org.junit.jupiter.api.Test
import org.junit.Ignore import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@RunWith(JUnit4.class)
class BcryptSecureHasherTest extends GroovyTestCase { class BcryptSecureHasherTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(BcryptSecureHasher) private static final Logger logger = LoggerFactory.getLogger(BcryptSecureHasher)
@BeforeClass @BeforeAll
static void setupOnce() throws Exception { static void setupOnce() throws Exception {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testShouldBeDeterministicWithStaticSalt() { void testShouldBeDeterministicWithStaticSalt() {
// Arrange // Arrange
@ -263,7 +251,7 @@ class BcryptSecureHasherTest extends GroovyTestCase {
* This test can have the minimum time threshold updated to determine if the performance * This test can have the minimum time threshold updated to determine if the performance
* is still sufficient compared to the existing threat model. * is still sufficient compared to the existing threat model.
*/ */
@Ignore("Long running test") @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true")
@Test @Test
void testDefaultCostParamsShouldBeSufficient() { void testDefaultCostParamsShouldBeSufficient() {
// Arrange // Arrange

View File

@ -18,19 +18,13 @@ package org.apache.nifi.security.util.crypto
import org.apache.nifi.security.util.KeyDerivationFunction import org.apache.nifi.security.util.KeyDerivationFunction
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class CipherProviderFactoryGroovyTest extends GroovyTestCase { class CipherProviderFactoryGroovyTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(CipherProviderFactoryGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(CipherProviderFactoryGroovyTest.class)
@ -44,7 +38,7 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase {
(KeyDerivationFunction.ARGON2) : Argon2CipherProvider.class (KeyDerivationFunction.ARGON2) : Argon2CipherProvider.class
] ]
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -53,14 +47,6 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testGetCipherProviderShouldResolveRegisteredKDFs() { void testGetCipherProviderShouldResolveRegisteredKDFs() {
// Arrange // Arrange
@ -75,24 +61,4 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase {
assert cp.class == (EXPECTED_CIPHER_PROVIDERS.get(kdf)) assert cp.class == (EXPECTED_CIPHER_PROVIDERS.get(kdf))
} }
} }
@Ignore("Cannot mock enum using Groovy map coercion")
@Test
void testGetCipherProviderShouldHandleUnregisteredKDFs() {
// Arrange
// Can't mock this; see http://stackoverflow.com/questions/5323505/mocking-java-enum-to-add-a-value-to-test-fail-case
KeyDerivationFunction invalidKDF = [name: "Unregistered", description: "Not a registered KDF"] as KeyDerivationFunction
logger.info("Expected: ${invalidKDF.kdfName} -> error")
// Act
def msg = shouldFail(IllegalArgumentException) {
CipherProvider cp = CipherProviderFactory.getCipherProvider(invalidKDF)
logger.info("Resolved: ${invalidKDF.kdfName} -> ${cp.class.simpleName}")
}
logger.expected(msg)
// Assert
assert msg =~ "No cipher provider registered for ${invalidKDF.kdfName}"
}
} }

View File

@ -20,18 +20,13 @@ import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.apache.nifi.security.util.KeyDerivationFunction import org.apache.nifi.security.util.KeyDerivationFunction
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class CipherUtilityGroovyTest extends GroovyTestCase { class CipherUtilityGroovyTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(CipherUtilityGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(CipherUtilityGroovyTest.class)
@ -76,7 +71,7 @@ class CipherUtilityGroovyTest extends GroovyTestCase {
"AES/GCM/NoPadding"] "AES/GCM/NoPadding"]
] ]
@BeforeClass @BeforeAll
static void setUpOnce() { static void setUpOnce() {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -87,16 +82,6 @@ class CipherUtilityGroovyTest extends GroovyTestCase {
logger.info("Mapped algorithms: ${ALGORITHMS_MAPPED_BY_CIPHER}") logger.info("Mapped algorithms: ${ALGORITHMS_MAPPED_BY_CIPHER}")
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testShouldParseCipherFromAlgorithm() { void testShouldParseCipherFromAlgorithm() {
// Arrange // Arrange

View File

@ -18,23 +18,18 @@ package org.apache.nifi.security.util.crypto
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class HashAlgorithmTest extends GroovyTestCase { class HashAlgorithmTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(HashAlgorithmTest.class) private static final Logger logger = LoggerFactory.getLogger(HashAlgorithmTest.class)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -43,14 +38,6 @@ class HashAlgorithmTest extends GroovyTestCase {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testDetermineBrokenAlgorithms() throws Exception { void testDetermineBrokenAlgorithms() throws Exception {
// Arrange // Arrange

View File

@ -16,18 +16,12 @@
*/ */
package org.apache.nifi.security.util.crypto package org.apache.nifi.security.util.crypto
import org.apache.nifi.components.AllowableValue import org.apache.nifi.components.AllowableValue
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.AfterClass import org.junit.jupiter.api.Test
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -35,14 +29,10 @@ import java.nio.charset.Charset
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class HashServiceTest extends GroovyTestCase { class HashServiceTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(HashServiceTest.class) private static final Logger logger = LoggerFactory.getLogger(HashServiceTest.class)
@Rule @BeforeAll
public TemporaryFolder temporaryFolder = new TemporaryFolder()
@BeforeClass
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -51,18 +41,6 @@ class HashServiceTest extends GroovyTestCase {
} }
} }
@AfterClass
static void tearDownOnce() throws Exception {
}
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testShouldHashValue() { void testShouldHashValue() {
// Arrange // Arrange
@ -393,16 +371,12 @@ class HashServiceTest extends GroovyTestCase {
// No command-line md2sum tool available // No command-line md2sum tool available
def algorithms = HashAlgorithm.values() - HashAlgorithm.MD2 def algorithms = HashAlgorithm.values() - HashAlgorithm.MD2
File inputFile = temporaryFolder.newFile() StringBuilder sb = new StringBuilder()
10_000.times { int i ->
// Generates a file with "apachenifi" 10 times per line for 10_000 lines (11 bytes * 10 * 10_000 ~= 1 MiB) sb.append("${i.toString().padLeft(5)}: ${"apachenifi " * 10}\n")
if (!inputFile.exists() || inputFile.length() == 0) {
inputFile.createNewFile()
10_000.times { int i ->
inputFile << "${i.toString().padLeft(5)}: ${"apachenifi " * 10}\n"
}
} }
/* These values were generated using command-line tools (openssl dgst -md5, shasum [-a 1 224 256 384 512 512224 512256], rhash --sha3-224, b2sum -l 160) /* These values were generated using command-line tools (openssl dgst -md5, shasum [-a 1 224 256 384 512 512224 512256], rhash --sha3-224, b2sum -l 160)
* Ex: {@code $ openssl dgst -md5 src/test/resources/HashServiceTest/largefile.txt} * Ex: {@code $ openssl dgst -md5 src/test/resources/HashServiceTest/largefile.txt}
*/ */
@ -428,9 +402,8 @@ class HashServiceTest extends GroovyTestCase {
// Act // Act
def generatedHashes = algorithms.collectEntries { HashAlgorithm algorithm -> def generatedHashes = algorithms.collectEntries { HashAlgorithm algorithm ->
// Get a new InputStream for each iteration, or it will calculate the hash of an empty input on iterations 1 - n // Get a new InputStream for each iteration, or it will calculate the hash of an empty input on iterations 1 - n
InputStream input = inputFile.newInputStream() InputStream input = new ByteArrayInputStream(sb.toString().bytes)
String hash = HashService.hashValueStreaming(algorithm, input) String hash = HashService.hashValueStreaming(algorithm, input)
logger.info("${algorithm.getName().padLeft(11)}(${inputFile.path}) [${hash.length() / 2}] = ${hash}")
[(algorithm.name), hash] [(algorithm.name), hash]
} }

View File

@ -19,14 +19,9 @@ package org.apache.nifi.security.util.crypto
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.Test
import org.junit.Before import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -38,8 +33,8 @@ import javax.crypto.spec.PBEParameterSpec
import java.security.Security import java.security.Security
import static org.junit.Assert.fail import static org.junit.Assert.fail
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class NiFiLegacyCipherProviderGroovyTest { class NiFiLegacyCipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(NiFiLegacyCipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(NiFiLegacyCipherProviderGroovyTest.class)
@ -51,7 +46,7 @@ class NiFiLegacyCipherProviderGroovyTest {
private static final byte[] SALT_16_BYTES = Hex.decodeHex("aabbccddeeff00112233445566778899".toCharArray()) private static final byte[] SALT_16_BYTES = Hex.decodeHex("aabbccddeeff00112233445566778899".toCharArray())
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -59,15 +54,6 @@ class NiFiLegacyCipherProviderGroovyTest {
limitedStrengthPbeEncryptionMethods = pbeEncryptionMethods.findAll { !it.isUnlimitedStrength() } limitedStrengthPbeEncryptionMethods = pbeEncryptionMethods.findAll { !it.isUnlimitedStrength() }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
private static Cipher getLegacyCipher(String password, byte[] salt, String algorithm) { private static Cipher getLegacyCipher(String password, byte[] salt, String algorithm) {
try { try {
final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()) final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray())
@ -123,9 +109,6 @@ class NiFiLegacyCipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.",
CipherUtility.isUnlimitedStrengthCryptoSupported())
NiFiLegacyCipherProvider cipherProvider = new NiFiLegacyCipherProvider() NiFiLegacyCipherProvider cipherProvider = new NiFiLegacyCipherProvider()
@ -260,7 +243,7 @@ class NiFiLegacyCipherProviderGroovyTest {
* from the password using a long digest result at the time of key length checking. * from the password using a long digest result at the time of key length checking.
* @throws IOException * @throws IOException
*/ */
@Ignore("Only needed once to determine max supported password lengths") @EnabledIfSystemProperty(named = "legacyCipherTest", matches = "true", disabledReason = "Only needed once to determine max supported password lengths")
@Test @Test
void testShouldDetermineDependenceOnUnlimitedStrengthCrypto() throws IOException { void testShouldDetermineDependenceOnUnlimitedStrengthCrypto() throws IOException {
def encryptionMethods = EncryptionMethod.values().findAll { it.algorithm.startsWith("PBE") } def encryptionMethods = EncryptionMethod.values().findAll { it.algorithm.startsWith("PBE") }

View File

@ -19,13 +19,8 @@ package org.apache.nifi.security.util.crypto
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.Test
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -38,8 +33,8 @@ import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.Assert.fail import static org.junit.Assert.fail
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class OpenSSLPKCS5CipherProviderGroovyTest { class OpenSSLPKCS5CipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(OpenSSLPKCS5CipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(OpenSSLPKCS5CipherProviderGroovyTest.class)
@ -49,7 +44,7 @@ class OpenSSLPKCS5CipherProviderGroovyTest {
private static final String PROVIDER_NAME = "BC" private static final String PROVIDER_NAME = "BC"
private static final int ITERATION_COUNT = 0 private static final int ITERATION_COUNT = 0
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -57,15 +52,6 @@ class OpenSSLPKCS5CipherProviderGroovyTest {
limitedStrengthPbeEncryptionMethods = pbeEncryptionMethods.findAll { !it.isUnlimitedStrength() } limitedStrengthPbeEncryptionMethods = pbeEncryptionMethods.findAll { !it.isUnlimitedStrength() }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
private static Cipher getLegacyCipher(String password, byte[] salt, String algorithm) { private static Cipher getLegacyCipher(String password, byte[] salt, String algorithm) {
try { try {
final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()) final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray())
@ -121,8 +107,8 @@ class OpenSSLPKCS5CipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(),
CipherUtility.isUnlimitedStrengthCryptoSupported()) "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.")
OpenSSLPKCS5CipherProvider cipherProvider = new OpenSSLPKCS5CipherProvider() OpenSSLPKCS5CipherProvider cipherProvider = new OpenSSLPKCS5CipherProvider()

View File

@ -19,9 +19,10 @@ package org.apache.nifi.security.util.crypto
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.* import org.junit.jupiter.api.BeforeAll
import org.junit.runner.RunWith import org.junit.jupiter.api.Disabled
import org.junit.runners.JUnit4 import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -30,8 +31,8 @@ import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.Assert.assertTrue import static org.junit.Assert.assertTrue
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class PBKDF2CipherProviderGroovyTest { class PBKDF2CipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(PBKDF2CipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(PBKDF2CipherProviderGroovyTest.class)
@ -47,7 +48,7 @@ class PBKDF2CipherProviderGroovyTest {
private final String IV_HEX = "01" * 16 private final String IV_HEX = "01" * 16
private static ArrayList<Integer> AES_KEY_LENGTHS private static ArrayList<Integer> AES_KEY_LENGTHS
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -64,15 +65,6 @@ class PBKDF2CipherProviderGroovyTest {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testGetCipherShouldBeInternallyConsistent() throws Exception { void testGetCipherShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
@ -163,10 +155,6 @@ class PBKDF2CipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.",
CipherUtility.isUnlimitedStrengthCryptoSupported())
RandomIVPBECipherProvider cipherProvider = new PBKDF2CipherProvider(DEFAULT_PRF, TEST_ITERATION_COUNT) RandomIVPBECipherProvider cipherProvider = new PBKDF2CipherProvider(DEFAULT_PRF, TEST_ITERATION_COUNT)
final String PASSWORD = "shortPassword" final String PASSWORD = "shortPassword"
@ -468,7 +456,7 @@ class PBKDF2CipherProviderGroovyTest {
} }
} }
@Ignore("This test can be run on a specific machine to evaluate if the default iteration count is sufficient") @EnabledIfSystemProperty(named = "nifi.test.unstable", matches = "true")
@Test @Test
void testDefaultConstructorShouldProvideStrongIterationCount() { void testDefaultConstructorShouldProvideStrongIterationCount() {
// Arrange // Arrange

View File

@ -18,18 +18,26 @@ package org.apache.nifi.security.util.crypto
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.* import org.junit.jupiter.api.BeforeAll
import org.junit.runner.RunWith import org.junit.jupiter.api.Disabled
import org.junit.runners.JUnit4 import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class PBKDF2SecureHasherTest extends GroovyTestCase { class PBKDF2SecureHasherTest extends GroovyTestCase {
@BeforeClass private static final Logger logger = LoggerFactory.getLogger(PBKDF2SecureHasherTest)
@BeforeAll
static void setupOnce() throws Exception { static void setupOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
}
} }
@Test @Test
@ -242,7 +250,7 @@ class PBKDF2SecureHasherTest extends GroovyTestCase {
* This test can have the minimum time threshold updated to determine if the performance * This test can have the minimum time threshold updated to determine if the performance
* is still sufficient compared to the existing threat model. * is still sufficient compared to the existing threat model.
*/ */
@Ignore("Long running test") @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true")
@Test @Test
void testDefaultCostParamsShouldBeSufficient() { void testDefaultCostParamsShouldBeSufficient() {
// Arrange // Arrange

View File

@ -21,14 +21,11 @@ import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.EncryptionMethod
import org.apache.nifi.security.util.crypto.scrypt.Scrypt import org.apache.nifi.security.util.crypto.scrypt.Scrypt
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.BeforeEach
import org.junit.Before import org.junit.jupiter.api.Disabled
import org.junit.BeforeClass import org.junit.jupiter.api.Test
import org.junit.Ignore import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -41,8 +38,8 @@ import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.Assert.assertTrue import static org.junit.Assert.assertTrue
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class ScryptCipherProviderGroovyTest { class ScryptCipherProviderGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(ScryptCipherProviderGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(ScryptCipherProviderGroovyTest.class)
@ -56,7 +53,7 @@ class ScryptCipherProviderGroovyTest {
RandomIVPBECipherProvider cipherProvider RandomIVPBECipherProvider cipherProvider
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -73,17 +70,12 @@ class ScryptCipherProviderGroovyTest {
} }
} }
@Before @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
// Very fast parameters to test for correctness rather than production values // Very fast parameters to test for correctness rather than production values
cipherProvider = new ScryptCipherProvider(4, 1, 1) cipherProvider = new ScryptCipherProvider(4, 1, 1)
} }
@After
void tearDown() throws Exception {
}
@Test @Test
void testGetCipherShouldBeInternallyConsistent() throws Exception { void testGetCipherShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
@ -143,8 +135,8 @@ class ScryptCipherProviderGroovyTest {
@Test @Test
void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(),
CipherUtility.isUnlimitedStrengthCryptoSupported()) "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.")
final String PASSWORD = "shortPassword" final String PASSWORD = "shortPassword"
final byte[] SALT = cipherProvider.generateSalt() final byte[] SALT = cipherProvider.generateSalt()
@ -621,7 +613,8 @@ class ScryptCipherProviderGroovyTest {
assert isScryptSalt assert isScryptSalt
} }
@Ignore("This test can be run on a specific machine to evaluate if the default parameters are sufficient") @EnabledIfSystemProperty(named = "nifi.test.unstable", matches = "true",
disabledReason = "This test can be run on a specific machine to evaluate if the default parameters are sufficient")
@Test @Test
void testDefaultConstructorShouldProvideStrongParameters() { void testDefaultConstructorShouldProvideStrongParameters() {
// Arrange // Arrange

View File

@ -16,15 +16,29 @@
*/ */
package org.apache.nifi.security.util.crypto package org.apache.nifi.security.util.crypto
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.* import org.junit.jupiter.api.BeforeAll
import org.junit.runner.RunWith import org.junit.jupiter.api.Disabled
import org.junit.runners.JUnit4 import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.Security
@RunWith(JUnit4.class)
class ScryptSecureHasherTest extends GroovyTestCase { class ScryptSecureHasherTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(ScryptSecureHasherTest)
@BeforeAll
static void setupOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider())
logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
}
}
@Test @Test
void testShouldBeDeterministicWithStaticSalt() { void testShouldBeDeterministicWithStaticSalt() {
@ -239,7 +253,7 @@ class ScryptSecureHasherTest extends GroovyTestCase {
* This test can have the minimum time threshold updated to determine if the performance * This test can have the minimum time threshold updated to determine if the performance
* is still sufficient compared to the existing threat model. * is still sufficient compared to the existing threat model.
*/ */
@Ignore("Long running test") @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true")
@Test @Test
void testDefaultCostParamsShouldBeSufficient() { void testDefaultCostParamsShouldBeSufficient() {
// Arrange // Arrange

View File

@ -19,14 +19,10 @@ package org.apache.nifi.security.util.scrypt
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.apache.nifi.security.util.crypto.scrypt.Scrypt import org.apache.nifi.security.util.crypto.scrypt.Scrypt
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Assume import org.junit.jupiter.api.Disabled
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -34,8 +30,8 @@ import java.security.SecureRandom
import java.security.Security import java.security.Security
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
import static org.junit.jupiter.api.Assumptions.assumeTrue
@RunWith(JUnit4.class)
class ScryptGroovyTest { class ScryptGroovyTest {
private static final Logger logger = LoggerFactory.getLogger(ScryptGroovyTest.class) private static final Logger logger = LoggerFactory.getLogger(ScryptGroovyTest.class)
@ -50,7 +46,7 @@ class ScryptGroovyTest {
private static final int DK_LEN = 128 private static final int DK_LEN = 128
private static final long TWO_GIGABYTES = 2048L * 1024 * 1024 private static final long TWO_GIGABYTES = 2048L * 1024 * 1024
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -59,15 +55,6 @@ class ScryptGroovyTest {
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testDeriveScryptKeyShouldBeInternallyConsistent() throws Exception { void testDeriveScryptKeyShouldBeInternallyConsistent() throws Exception {
// Arrange // Arrange
@ -150,8 +137,7 @@ class ScryptGroovyTest {
long totalMemory = Runtime.getRuntime().totalMemory() long totalMemory = Runtime.getRuntime().totalMemory()
logger.info("Required memory: ${TWO_GIGABYTES} bytes") logger.info("Required memory: ${TWO_GIGABYTES} bytes")
logger.info("Max heap memory: ${totalMemory} bytes") logger.info("Max heap memory: ${totalMemory} bytes")
Assume.assumeTrue("Test is being skipped due to JVM heap size. Please run with -Xmx3072m to set sufficient heap size", assumeTrue(totalMemory >= TWO_GIGABYTES, "Test is being skipped due to JVM heap size. Please run with -Xmx3072m to set sufficient heap size")
totalMemory >= TWO_GIGABYTES)
// These values are taken from Colin Percival's scrypt paper: https://www.tarsnap.com/scrypt/scrypt.pdf // These values are taken from Colin Percival's scrypt paper: https://www.tarsnap.com/scrypt/scrypt.pdf
final byte[] HASH = Hex.decodeHex("2101cb9b6a511aaeaddbbe09cf70f881" + final byte[] HASH = Hex.decodeHex("2101cb9b6a511aaeaddbbe09cf70f881" +
@ -180,7 +166,7 @@ class ScryptGroovyTest {
assert calculatedHash == HASH assert calculatedHash == HASH
} }
@Ignore("This test was just to exercise the heap and debug OOME issues") @EnabledIfSystemProperty(named = "nifi.test.unstable", matches = "true")
@Test @Test
void testShouldCauseOutOfMemoryError() { void testShouldCauseOutOfMemoryError() {
SecureRandom secureRandom = new SecureRandom() SecureRandom secureRandom = new SecureRandom()

View File

@ -16,12 +16,8 @@
*/ */
package org.apache.nifi.security.xml package org.apache.nifi.security.xml
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.xml.sax.SAXParseException import org.xml.sax.SAXParseException
@ -38,27 +34,16 @@ import javax.xml.stream.XMLStreamReader
import static groovy.test.GroovyAssert.shouldFail import static groovy.test.GroovyAssert.shouldFail
@RunWith(JUnit4.class)
class XmlUtilsTest { class XmlUtilsTest {
private static final Logger logger = LoggerFactory.getLogger(XmlUtilsTest.class) private static final Logger logger = LoggerFactory.getLogger(XmlUtilsTest.class)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
@Test @Test
void testShouldHandleXXEInUnmarshal() { void testShouldHandleXXEInUnmarshal() {
// Arrange // Arrange

View File

@ -16,22 +16,17 @@
*/ */
package org.apache.nifi.security; package org.apache.nifi.security;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.security.xml.XXEValidator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.security.xml.XXEValidator;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class XXEValidatorTest { public class XXEValidatorTest {
private String simpleXMLFile = "src/test/resources/no_xxe.xml"; private String simpleXMLFile = "src/test/resources/no_xxe.xml";
private String remoteXXEFile = "src/test/resources/remote_xxe_file.xml"; private String remoteXXEFile = "src/test/resources/remote_xxe_file.xml";
@ -41,21 +36,11 @@ public class XXEValidatorTest {
private String configurationKey = "Configuration Name"; private String configurationKey = "Configuration Name";
private ValidationContext validationContext; private ValidationContext validationContext;
@BeforeClass @BeforeEach
public static void setUpOnce() throws Exception {
}
@Before
public void setUp() throws Exception { public void setUp() throws Exception {
validationContext = mock(ValidationContext.class); validationContext = mock(ValidationContext.class);
} }
@After
public void tearDown() throws Exception {
}
@Test @Test
public void testXmlFileWithNoXXEIsValid() { public void testXmlFileWithNoXXEIsValid() {
// Arrange // Arrange

View File

@ -17,6 +17,12 @@
package org.apache.nifi.security.util; package org.apache.nifi.security.util;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -32,13 +38,6 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -60,7 +59,7 @@ public class KeyStoreUtilsTest {
private static X509Certificate certificate; private static X509Certificate certificate;
private static SecretKey secretKey; private static SecretKey secretKey;
@BeforeClass @BeforeAll
public static void generateKeysAndCertificates() throws NoSuchAlgorithmException, CertificateException { public static void generateKeysAndCertificates() throws NoSuchAlgorithmException, CertificateException {
keyPair = KeyPairGenerator.getInstance(KEY_ALGORITHM).generateKeyPair(); keyPair = KeyPairGenerator.getInstance(KEY_ALGORITHM).generateKeyPair();
certificate = CertificateUtils.generateSelfSignedX509Certificate(keyPair, SUBJECT_DN, SIGNING_ALGORITHM, DURATION_DAYS); certificate = CertificateUtils.generateSelfSignedX509Certificate(keyPair, SUBJECT_DN, SIGNING_ALGORITHM, DURATION_DAYS);

View File

@ -16,9 +16,7 @@
*/ */
package org.apache.nifi.security.util.crypto; package org.apache.nifi.security.util.crypto;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -26,9 +24,6 @@ public class SecureHasherFactoryTest {
private static final Argon2SecureHasher DEFAULT_HASHER = new Argon2SecureHasher(); private static final Argon2SecureHasher DEFAULT_HASHER = new Argon2SecureHasher();
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test @Test
public void testSecureHasherFactoryArgon2() { public void testSecureHasherFactoryArgon2() {
SecureHasher hasher = SecureHasherFactory.getSecureHasher("NIFI_ARGON2_AES_GCM_128"); SecureHasher hasher = SecureHasherFactory.getSecureHasher("NIFI_ARGON2_AES_GCM_128");

View File

@ -18,13 +18,10 @@ package org.apache.nifi.properties
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.encoders.Hex import org.bouncycastle.util.encoders.Hex
import org.junit.After import org.junit.jupiter.api.AfterEach
import org.junit.Assume import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.BeforeEach
import org.junit.BeforeClass import org.junit.jupiter.api.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -35,7 +32,8 @@ import java.nio.charset.StandardCharsets
import java.security.SecureRandom import java.security.SecureRandom
import java.security.Security import java.security.Security
@RunWith(JUnit4.class) import static org.junit.Assume.assumeTrue
class AESSensitivePropertyProviderTest extends GroovyTestCase { class AESSensitivePropertyProviderTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(AESSensitivePropertyProviderTest.class) private static final Logger logger = LoggerFactory.getLogger(AESSensitivePropertyProviderTest.class)
@ -52,7 +50,7 @@ class AESSensitivePropertyProviderTest extends GroovyTestCase {
private static final Base64.Encoder encoder = Base64.encoder private static final Base64.Encoder encoder = Base64.encoder
private static final Base64.Decoder decoder = Base64.decoder private static final Base64.Decoder decoder = Base64.decoder
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -61,12 +59,12 @@ class AESSensitivePropertyProviderTest extends GroovyTestCase {
} }
} }
@Before @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
} }
@After @AfterEach
void tearDown() throws Exception { void tearDown() throws Exception {
} }
@ -475,7 +473,7 @@ class AESSensitivePropertyProviderTest extends GroovyTestCase {
@Test @Test
void testShouldDecryptPaddedValue() { void testShouldDecryptPaddedValue() {
// Arrange // Arrange
Assume.assumeTrue("JCE unlimited strength crypto policy must be installed for this test", Cipher.getMaxAllowedKeyLength("AES") > 128) assumeTrue("JCE unlimited strength crypto policy must be installed for this test", Cipher.getMaxAllowedKeyLength("AES") > 128)
final String EXPECTED_VALUE = getKeyOfSize(256) // "thisIsABadKeyPassword" final String EXPECTED_VALUE = getKeyOfSize(256) // "thisIsABadKeyPassword"
String cipherText = "aYDkDKys1ENr3gp+||sTBPpMlIvHcOLTGZlfWct8r9RY8BuDlDkoaYmGJ/9m9af9tZIVzcnDwvYQAaIKxRGF7vI2yrY7Xd6x9GTDnWGiGiRXlaP458BBMMgfzH2O8" String cipherText = "aYDkDKys1ENr3gp+||sTBPpMlIvHcOLTGZlfWct8r9RY8BuDlDkoaYmGJ/9m9af9tZIVzcnDwvYQAaIKxRGF7vI2yrY7Xd6x9GTDnWGiGiRXlaP458BBMMgfzH2O8"

View File

@ -17,10 +17,9 @@
package org.apache.nifi.properties; package org.apache.nifi.properties;
import org.apache.nifi.properties.configuration.AwsKmsClientProvider; import org.apache.nifi.properties.configuration.AwsKmsClientProvider;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.Assert; import org.junit.jupiter.api.BeforeAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.mockito.internal.util.io.IOUtil; import org.mockito.internal.util.io.IOUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -32,6 +31,10 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Properties; import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* To run this test, make sure to first configure sensitive credential information as in the following link * To run this test, make sure to first configure sensitive credential information as in the following link
* https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html * https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
@ -99,18 +102,18 @@ public class AwsKmsSensitivePropertyProviderIT {
IOUtil.writeText(bootstrapConfText.toString(), mockAWSBootstrapConf.toFile()); IOUtil.writeText(bootstrapConfText.toString(), mockAWSBootstrapConf.toFile());
} }
@BeforeClass @BeforeAll
public static void initOnce() throws IOException { public static void initOnce() throws IOException {
initializeBootstrapProperties(); initializeBootstrapProperties();
Assert.assertNotNull(props); assertNotNull(props);
final AwsKmsClientProvider provider = new AwsKmsClientProvider(); final AwsKmsClientProvider provider = new AwsKmsClientProvider();
final Properties properties = provider.getClientProperties(props).orElse(null); final Properties properties = provider.getClientProperties(props).orElse(null);
final KmsClient kmsClient = provider.getClient(properties).orElse(null); final KmsClient kmsClient = provider.getClient(properties).orElse(null);
spp = new AwsKmsSensitivePropertyProvider(kmsClient, properties); spp = new AwsKmsSensitivePropertyProvider(kmsClient, properties);
Assert.assertNotNull(spp); assertNotNull(spp);
} }
@AfterClass @AfterAll
public static void tearDownOnce() throws IOException { public static void tearDownOnce() throws IOException {
Files.deleteIfExists(mockBootstrapConf); Files.deleteIfExists(mockBootstrapConf);
Files.deleteIfExists(mockAWSBootstrapConf); Files.deleteIfExists(mockAWSBootstrapConf);
@ -132,8 +135,8 @@ public class AwsKmsSensitivePropertyProviderIT {
String unprotectedValue = spp.unprotect(protectedValue, ProtectedPropertyContext.defaultContext("property")); String unprotectedValue = spp.unprotect(protectedValue, ProtectedPropertyContext.defaultContext("property"));
logger.info("Unprotected Value: " + unprotectedValue); logger.info("Unprotected Value: " + unprotectedValue);
Assert.assertEquals(SAMPLE_PLAINTEXT, unprotectedValue); assertEquals(SAMPLE_PLAINTEXT, unprotectedValue);
Assert.assertNotEquals(SAMPLE_PLAINTEXT, protectedValue); assertNotEquals(SAMPLE_PLAINTEXT, protectedValue);
Assert.assertNotEquals(protectedValue, unprotectedValue); assertNotEquals(protectedValue, unprotectedValue);
} }
} }

View File

@ -19,9 +19,9 @@ package org.apache.nifi.properties;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.nifi.util.NiFiProperties; import org.apache.nifi.util.NiFiProperties;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.internal.util.io.IOUtil; import org.mockito.internal.util.io.IOUtil;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -35,12 +35,12 @@ import java.security.Security;
import java.util.Properties; import java.util.Properties;
import java.util.function.Supplier; import java.util.function.Supplier;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class StandardSensitivePropertyProviderFactoryTest { public class StandardSensitivePropertyProviderFactoryTest {
@ -56,7 +56,9 @@ public class StandardSensitivePropertyProviderFactoryTest {
private static Path nifiProperties; private static Path nifiProperties;
private static String defaultBootstrapContents; private static String defaultBootstrapContents;
@BeforeClass private static NiFiProperties niFiProperties;
@BeforeAll
public static void initOnce() throws IOException { public static void initOnce() throws IOException {
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());
tempConfDir = Files.createTempDirectory("conf"); tempConfDir = Files.createTempDirectory("conf");
@ -86,7 +88,7 @@ public class StandardSensitivePropertyProviderFactoryTest {
return bootstrapConf; return bootstrapConf;
} }
@AfterClass @AfterAll
public static void tearDownOnce() throws IOException { public static void tearDownOnce() throws IOException {
Files.deleteIfExists(bootstrapConf); Files.deleteIfExists(bootstrapConf);
Files.deleteIfExists(hashicorpVaultBootstrapConf); Files.deleteIfExists(hashicorpVaultBootstrapConf);

View File

@ -17,8 +17,8 @@
package org.apache.nifi.authentication.single.user.command; package org.apache.nifi.authentication.single.user.command;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -34,14 +34,14 @@ import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SetSingleUserCredentialsTest { public class SetSingleUserCredentialsTest {
private static final Pattern BCRYPT_PATTERN = Pattern.compile("\\$2b\\$12\\$.+"); private static final Pattern BCRYPT_PATTERN = Pattern.compile("\\$2b\\$12\\$.+");
private static final String PROPERTIES_PATH = "/conf/login.nifi.properties"; private static final String PROPERTIES_PATH = "/conf/login.nifi.properties";
@After @AfterEach
public void clearProperties() { public void clearProperties() {
System.clearProperty(SetSingleUserCredentials.PROPERTIES_FILE_PATH); System.clearProperty(SetSingleUserCredentials.PROPERTIES_FILE_PATH);
} }
@ -66,8 +66,8 @@ public class SetSingleUserCredentialsTest {
private void assertProvidersUpdated(final Path providersConfigurationPath, final String username) throws IOException { private void assertProvidersUpdated(final Path providersConfigurationPath, final String username) throws IOException {
final String providersConfiguration = new String(Files.readAllBytes(providersConfigurationPath), StandardCharsets.UTF_8); final String providersConfiguration = new String(Files.readAllBytes(providersConfigurationPath), StandardCharsets.UTF_8);
assertTrue("Username not found", providersConfiguration.contains(username)); assertTrue(providersConfiguration.contains(username), "Username not found");
assertTrue("Encoded Password not found", BCRYPT_PATTERN.matcher(providersConfiguration).find()); assertTrue(BCRYPT_PATTERN.matcher(providersConfiguration).find(), "Encoded Password not found");
} }
private Path getNiFiProperties(final Path providersPath) throws IOException, URISyntaxException { private Path getNiFiProperties(final Path providersPath) throws IOException, URISyntaxException {

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.nifi.authentication.single.user.encoder; package org.apache.nifi.authentication.single.user.encoder;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class BCryptPasswordEncoderTest { public class BCryptPasswordEncoderTest {
@ -31,8 +31,8 @@ public class BCryptPasswordEncoderTest {
public void testEncode() { public void testEncode() {
final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
final String encoded = encoder.encode(String.class.getSimpleName().toCharArray()); final String encoded = encoder.encode(String.class.getSimpleName().toCharArray());
assertNotNull("Encoded Password not found", encoded); assertNotNull(encoded, "Encoded Password not found");
assertTrue("Encoded Password bcrypt hash not found", BCRYPT_PATTERN.matcher(encoded).matches()); assertTrue(BCRYPT_PATTERN.matcher(encoded).matches(), "Encoded Password bcrypt hash not found");
} }
@Test @Test
@ -40,6 +40,6 @@ public class BCryptPasswordEncoderTest {
final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
final char[] password = String.class.getSimpleName().toCharArray(); final char[] password = String.class.getSimpleName().toCharArray();
final String encoded = encoder.encode(password); final String encoded = encoder.encode(password);
assertTrue("Encoded Password not matched", encoder.matches(password, encoded)); assertTrue(encoder.matches(password, encoded), "Encoded Password not matched");
} }
} }

View File

@ -17,7 +17,7 @@
package org.apache.nifi.authentication.single.user.writer; package org.apache.nifi.authentication.single.user.writer;
import org.apache.nifi.authentication.single.user.SingleUserCredentials; import org.apache.nifi.authentication.single.user.SingleUserCredentials;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -27,7 +27,7 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class StandardLoginCredentialsWriterTest { public class StandardLoginCredentialsWriterTest {
private static final String BLANK_PROVIDERS = "/conf/login-identity-providers.xml"; private static final String BLANK_PROVIDERS = "/conf/login-identity-providers.xml";
@ -64,7 +64,7 @@ public class StandardLoginCredentialsWriterTest {
writer.writeLoginCredentials(credentials); writer.writeLoginCredentials(credentials);
final String configuration = new String(Files.readAllBytes(configuredProvidersPath)); final String configuration = new String(Files.readAllBytes(configuredProvidersPath));
assertTrue("Username not found", configuration.contains(username)); assertTrue(configuration.contains(username), "Username not found");
assertTrue("Password not found", configuration.contains(password)); assertTrue(configuration.contains(password), "Password not found");
} }
} }

View File

@ -23,19 +23,16 @@ import org.apache.nifi.remote.TransferDirection
import org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol import org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol
import org.apache.nifi.remote.util.PeerStatusCache import org.apache.nifi.remote.util.PeerStatusCache
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.AfterEach
import org.junit.Before import org.junit.jupiter.api.BeforeAll
import org.junit.BeforeClass import org.junit.jupiter.api.BeforeEach
import org.junit.Test import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.security.Security import java.security.Security
import java.util.concurrent.ArrayBlockingQueue import java.util.concurrent.ArrayBlockingQueue
@RunWith(JUnit4.class)
class PeerSelectorTest extends GroovyTestCase { class PeerSelectorTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(PeerSelectorTest.class) private static final Logger logger = LoggerFactory.getLogger(PeerSelectorTest.class)
@ -51,7 +48,7 @@ class PeerSelectorTest extends GroovyTestCase {
private static mockPP private static mockPP
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -60,14 +57,14 @@ class PeerSelectorTest extends GroovyTestCase {
} }
} }
@Before @BeforeEach
void setUp() { void setUp() {
// Mock collaborators // Mock collaborators
mockPSP = mockPeerStatusProvider() mockPSP = mockPeerStatusProvider()
mockPP = mockPeerPersistence() mockPP = mockPeerPersistence()
} }
@After @AfterEach
void tearDown() { void tearDown() {
} }

View File

@ -21,7 +21,7 @@ import org.apache.nifi.remote.protocol.http.HttpProxy;
import org.apache.nifi.remote.util.SiteToSiteRestApiClient; import org.apache.nifi.remote.util.SiteToSiteRestApiClient;
import org.apache.nifi.web.api.dto.ControllerDTO; import org.apache.nifi.web.api.dto.ControllerDTO;
import org.apache.nifi.web.api.dto.PortDTO; import org.apache.nifi.web.api.dto.PortDTO;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import java.io.IOException; import java.io.IOException;
@ -31,11 +31,11 @@ import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
@ -190,18 +190,7 @@ public class TestSiteInfoProvider {
return apiClient; return apiClient;
}).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any()); }).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
try { assertThrows(IOException.class, () -> siteInfoProvider.getSiteToSitePort());
siteInfoProvider.getSiteToSitePort(); assertThrows(IOException.class, () -> siteInfoProvider.getActiveClusterUrl());
fail();
} catch (IOException e) {
}
try {
siteInfoProvider.getActiveClusterUrl();
fail();
} catch (IOException e) {
}
} }
} }

View File

@ -16,40 +16,7 @@
*/ */
package org.apache.nifi.remote.client.http; package org.apache.nifi.remote.client.http;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_HEADER_NAME;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_URI_INTENT_NAME;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_URI_INTENT_VALUE;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.PROTOCOL_VERSION;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.SERVER_SIDE_TRANSACTION_TTL;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.nifi.controller.ScheduledState; import org.apache.nifi.controller.ScheduledState;
import org.apache.nifi.remote.Peer; import org.apache.nifi.remote.Peer;
import org.apache.nifi.remote.Transaction; import org.apache.nifi.remote.Transaction;
@ -88,11 +55,13 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.After; import org.junit.jupiter.api.AfterAll;
import org.junit.AfterClass; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeAll;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.littleshoot.proxy.HttpProxyServer; import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.ProxyAuthenticator; import org.littleshoot.proxy.ProxyAuthenticator;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer; import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
@ -100,6 +69,40 @@ import org.littleshoot.proxy.impl.ThreadPoolConfiguration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_HEADER_NAME;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_URI_INTENT_NAME;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.LOCATION_URI_INTENT_VALUE;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.PROTOCOL_VERSION;
import static org.apache.nifi.remote.protocol.http.HttpHeaders.SERVER_SIDE_TRANSACTION_TTL;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class TestHttpClient { public class TestHttpClient {
private static final Logger logger = LoggerFactory.getLogger(TestHttpClient.class); private static final Logger logger = LoggerFactory.getLogger(TestHttpClient.class);
@ -139,11 +142,11 @@ public class TestHttpClient {
controller.setInstanceId("remote-instance-id"); controller.setInstanceId("remote-instance-id");
controller.setName("Remote NiFi Flow"); controller.setName("Remote NiFi Flow");
assertNotNull("Test case should set <inputPorts> depending on the test scenario.", inputPorts); assertNotNull(inputPorts, "Test case should set <inputPorts> depending on the test scenario.");
controller.setInputPorts(inputPorts); controller.setInputPorts(inputPorts);
controller.setInputPortCount(inputPorts.size()); controller.setInputPortCount(inputPorts.size());
assertNotNull("Test case should set <outputPorts> depending on the test scenario.", outputPorts); assertNotNull(outputPorts, "Test case should set <outputPorts> depending on the test scenario.");
controller.setOutputPorts(outputPorts); controller.setOutputPorts(outputPorts);
controller.setOutputPortCount(outputPorts.size()); controller.setOutputPortCount(outputPorts.size());
@ -171,10 +174,10 @@ public class TestHttpClient {
final PeersEntity peersEntity = new PeersEntity(); final PeersEntity peersEntity = new PeersEntity();
if (req.getLocalPort() == httpConnector.getLocalPort()) { if (req.getLocalPort() == httpConnector.getLocalPort()) {
assertNotNull("Test case should set <peers> depending on the test scenario.", peers); assertNotNull(peers, "Test case should set <peers> depending on the test scenario.");
peersEntity.setPeers(peers); peersEntity.setPeers(peers);
} else { } else {
assertNotNull("Test case should set <peersSecure> depending on the test scenario.", peersSecure); assertNotNull(peersSecure, "Test case should set <peersSecure> depending on the test scenario.");
peersEntity.setPeers(peersSecure); peersEntity.setPeers(peersSecure);
} }
@ -431,7 +434,7 @@ public class TestHttpClient {
out.flush(); out.flush();
} }
@BeforeClass @BeforeAll
public static void setup() throws Exception { public static void setup() throws Exception {
// Create embedded Jetty server // Create embedded Jetty server
// Use less threads to mitigate Gateway Timeout (504) with proxy test // Use less threads to mitigate Gateway Timeout (504) with proxy test
@ -559,7 +562,7 @@ public class TestHttpClient {
.start(); .start();
} }
@AfterClass @AfterAll
public static void teardown() throws Exception { public static void teardown() throws Exception {
logger.info("Stopping servers."); logger.info("Stopping servers.");
try { try {
@ -600,7 +603,7 @@ public class TestHttpClient {
} }
@Before @BeforeEach
public void before() throws Exception { public void before() throws Exception {
testCaseFinished = new CountDownLatch(1); testCaseFinished = new CountDownLatch(1);
@ -678,7 +681,7 @@ public class TestHttpClient {
} }
@After @AfterEach
public void after() throws Exception { public void after() throws Exception {
testCaseFinished.countDown(); testCaseFinished.countDown();
} }
@ -774,13 +777,8 @@ public class TestHttpClient {
.portName("input-unknown") .portName("input-unknown")
.build() .build()
) { ) {
try { IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.SEND));
client.createTransaction(TransferDirection.SEND); assertTrue(e.getMessage().contains("Failed to determine the identifier of port"));
fail();
} catch (IOException e) {
logger.info("Exception message: {}", e.getMessage());
assertTrue(e.getMessage().contains("Failed to determine the identifier of port"));
}
} }
} }
@ -858,7 +856,7 @@ public class TestHttpClient {
.build() .build()
) { ) {
final Transaction transaction = client.createTransaction(TransferDirection.SEND); final Transaction transaction = client.createTransaction(TransferDirection.SEND);
assertNull("createTransaction should fail at peer selection and return null.", transaction); assertNull(transaction, "createTransaction should fail at peer selection and return null.");
} }
} }
@ -885,13 +883,8 @@ public class TestHttpClient {
.portName("input-access-denied") .portName("input-access-denied")
.build() .build()
) { ) {
try { assertThrows(HandshakeException.class, () -> client.createTransaction(TransferDirection.SEND));
client.createTransaction(TransferDirection.SEND);
fail("Handshake exception should be thrown.");
} catch (HandshakeException e) {
}
} }
} }
@Test @Test
@ -1112,13 +1105,8 @@ public class TestHttpClient {
} }
private void completeShouldFail(Transaction transaction) throws IOException { private void completeShouldFail(Transaction transaction) {
try { assertThrows(IllegalStateException.class, () -> transaction.complete());
transaction.complete();
fail("Complete operation should fail since transaction has already failed.");
} catch (IllegalStateException e) {
logger.info("An exception was thrown as expected.", e);
}
} }
private void confirmShouldFail(Transaction transaction) throws IOException { private void confirmShouldFail(Transaction transaction) throws IOException {
@ -1131,8 +1119,8 @@ public class TestHttpClient {
} }
@Test @Test
@DisabledOnOs(OS.WINDOWS)
public void testSendTimeout() throws Exception { public void testSendTimeout() throws Exception {
assumeFalse(isWindowsEnvironment());//skip on windows
try ( try (
SiteToSiteClient client = getDefaultBuilder() SiteToSiteClient client = getDefaultBuilder()
.timeout(1, TimeUnit.SECONDS) .timeout(1, TimeUnit.SECONDS)
@ -1151,26 +1139,17 @@ public class TestHttpClient {
serverChecksum = "1345413116"; serverChecksum = "1345413116";
transaction.send(packet); transaction.send(packet);
try { IOException e = assertThrows(IOException.class, () -> transaction.confirm());
transaction.confirm(); assertTrue(e.getMessage().contains("TimeoutException"));
fail();
} catch (IOException e) {
logger.info("An exception was thrown as expected.", e);
assertTrue(e.getMessage().contains("TimeoutException"));
}
completeShouldFail(transaction); completeShouldFail(transaction);
} }
} }
private boolean isWindowsEnvironment() {
return System.getProperty("os.name").toLowerCase().startsWith("windows");
}
@Test @Test
@DisabledOnOs(OS.WINDOWS)
public void testSendTimeoutAfterDataExchange() throws Exception { public void testSendTimeoutAfterDataExchange() throws Exception {
assumeFalse(isWindowsEnvironment());//skip on windows
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote.protocol.http.HttpClientTransaction", "INFO"); System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote.protocol.http.HttpClientTransaction", "INFO");
try ( try (
@ -1197,17 +1176,11 @@ public class TestHttpClient {
} }
} }
try { IOException e = assertThrows(IOException.class, () -> confirmShouldFail(transaction));
confirmShouldFail(transaction); assertTrue(e.getMessage().contains("TimeoutException"));
fail("Should be timeout.");
} catch (IOException e) {
logger.info("Exception message: {}", e.getMessage());
assertTrue(e.getMessage().contains("TimeoutException"));
}
completeShouldFail(transaction); completeShouldFail(transaction);
} }
} }
@Test @Test
@ -1218,13 +1191,8 @@ public class TestHttpClient {
.portName("output-unknown") .portName("output-unknown")
.build() .build()
) { ) {
try { IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.RECEIVE));
client.createTransaction(TransferDirection.RECEIVE); assertTrue(e.getMessage().contains("Failed to determine the identifier of port"));
fail();
} catch (IOException e) {
logger.info("Exception message: {}", e.getMessage());
assertTrue(e.getMessage().contains("Failed to determine the identifier of port"));
}
} }
} }
@ -1361,13 +1329,8 @@ public class TestHttpClient {
.portName("output-timeout") .portName("output-timeout")
.build() .build()
) { ) {
try { IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.RECEIVE));
client.createTransaction(TransferDirection.RECEIVE); assertTrue(e instanceof SocketTimeoutException);
fail();
} catch (IOException e) {
logger.info("An exception was thrown as expected.", e);
assertTrue(e instanceof SocketTimeoutException);
}
} }
} }
@ -1387,13 +1350,8 @@ public class TestHttpClient {
assertNotNull(packet); assertNotNull(packet);
consumeDataPacket(packet); consumeDataPacket(packet);
try { IOException e = assertThrows(IOException.class, () -> transaction.receive());
transaction.receive(); assertTrue(e.getCause() instanceof SocketTimeoutException);
fail();
} catch (IOException e) {
logger.info("An exception was thrown as expected.", e);
assertTrue(e.getCause() instanceof SocketTimeoutException);
}
confirmShouldFail(transaction); confirmShouldFail(transaction);
completeShouldFail(transaction); completeShouldFail(transaction);

View File

@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.remote.client.socket;
import org.apache.nifi.remote.Transaction;
import org.apache.nifi.remote.TransferDirection;
import org.apache.nifi.remote.client.SiteToSiteClient;
import org.apache.nifi.remote.protocol.DataPacket;
import org.apache.nifi.remote.util.StandardDataPacket;
import org.apache.nifi.stream.io.StreamUtils;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class SiteToSiteClientIT {
@Test
public void testReceive() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder()
.url("http://localhost:8080/nifi")
.portName("cba")
.requestBatchCount(10)
.build();
try {
for (int i = 0; i < 1000; i++) {
final Transaction transaction = client.createTransaction(TransferDirection.RECEIVE);
assertNotNull(transaction);
DataPacket packet;
while (true) {
packet = transaction.receive();
if (packet == null) {
break;
}
final InputStream in = packet.getData();
final long size = packet.getSize();
final byte[] buff = new byte[(int) size];
StreamUtils.fillBuffer(in, buff);
}
transaction.confirm();
transaction.complete();
}
} finally {
client.close();
}
}
@Test
public void testSend() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder()
.url("http://localhost:8080/nifi")
.portName("input")
.build();
try {
final Transaction transaction = client.createTransaction(TransferDirection.SEND);
assertNotNull(transaction);
final Map<String, String> attrs = new HashMap<>();
attrs.put("site-to-site", "yes, please!");
final byte[] bytes = "Hello".getBytes();
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
final DataPacket packet = new StandardDataPacket(attrs, bais, bytes.length);
transaction.send(packet);
transaction.confirm();
transaction.complete();
} finally {
client.close();
}
}
}

View File

@ -20,95 +20,22 @@ import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
import org.apache.nifi.components.state.StateManager; import org.apache.nifi.components.state.StateManager;
import org.apache.nifi.remote.Transaction;
import org.apache.nifi.remote.TransferDirection;
import org.apache.nifi.remote.client.SiteToSiteClient; import org.apache.nifi.remote.client.SiteToSiteClient;
import org.apache.nifi.remote.client.SiteToSiteClientConfig; import org.apache.nifi.remote.client.SiteToSiteClientConfig;
import org.apache.nifi.remote.protocol.DataPacket; import org.junit.jupiter.api.Test;
import org.apache.nifi.remote.util.StandardDataPacket;
import org.apache.nifi.stream.io.StreamUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class TestSiteToSiteClient { public class TestSiteToSiteClient {
@Test
@Ignore("For local testing only; not really a unit test but a manual test")
public void testReceive() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder()
.url("http://localhost:8080/nifi")
.portName("cba")
.requestBatchCount(10)
.build();
try {
for (int i = 0; i < 1000; i++) {
final Transaction transaction = client.createTransaction(TransferDirection.RECEIVE);
Assert.assertNotNull(transaction);
DataPacket packet;
while (true) {
packet = transaction.receive();
if (packet == null) {
break;
}
final InputStream in = packet.getData();
final long size = packet.getSize();
final byte[] buff = new byte[(int) size];
StreamUtils.fillBuffer(in, buff);
}
transaction.confirm();
transaction.complete();
}
} finally {
client.close();
}
}
@Test
@Ignore("For local testing only; not really a unit test but a manual test")
public void testSend() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder()
.url("http://localhost:8080/nifi")
.portName("input")
.build();
try {
final Transaction transaction = client.createTransaction(TransferDirection.SEND);
Assert.assertNotNull(transaction);
final Map<String, String> attrs = new HashMap<>();
attrs.put("site-to-site", "yes, please!");
final byte[] bytes = "Hello".getBytes();
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
final DataPacket packet = new StandardDataPacket(attrs, bais, bytes.length);
transaction.send(packet);
transaction.confirm();
transaction.complete();
} finally {
client.close();
}
}
@Test @Test
public void testSerialization() { public void testSerialization() {
final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder() final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder()
@ -132,7 +59,7 @@ public class TestSiteToSiteClient {
try { try {
SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class); SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class);
Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls()); assertEquals(clientConfig.getUrls(), clientConfig2.getUrls());
} finally { } finally {
input.close(); input.close();
} }
@ -163,10 +90,10 @@ public class TestSiteToSiteClient {
try { try {
SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class); SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class);
Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls()); assertEquals(clientConfig.getUrls(), clientConfig2.getUrls());
// Serialization works, but the state manager is not serialized. // Serialization works, but the state manager is not serialized.
Assert.assertNotNull(clientConfig.getStateManager()); assertNotNull(clientConfig.getStateManager());
Assert.assertNull(clientConfig2.getStateManager()); assertNull(clientConfig2.getStateManager());
} finally { } finally {
input.close(); input.close();
} }
@ -181,8 +108,8 @@ public class TestSiteToSiteClient {
.urls(urls) .urls(urls)
.buildConfig(); .buildConfig();
Assert.assertEquals("http://node1:8080/nifi", config.getUrl()); assertEquals("http://node1:8080/nifi", config.getUrl());
Assert.assertEquals(urls, config.getUrls()); assertEquals(urls, config.getUrls());
} }
} }

View File

@ -16,22 +16,23 @@
*/ */
package org.apache.nifi.remote.protocol; package org.apache.nifi.remote.protocol;
import static org.junit.Assert.assertEquals; import org.apache.nifi.remote.Transaction;
import static org.junit.Assert.assertFalse; import org.apache.nifi.remote.TransactionCompletion;
import static org.junit.Assert.assertNotNull; import org.apache.nifi.remote.util.StandardDataPacket;
import static org.junit.Assert.assertNull; import org.apache.nifi.stream.io.StreamUtils;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import org.apache.nifi.remote.Transaction;
import org.apache.nifi.remote.TransactionCompletion; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.nifi.remote.util.StandardDataPacket; import static org.junit.jupiter.api.Assertions.assertFalse;
import org.apache.nifi.stream.io.StreamUtils; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SiteToSiteTestUtils { public class SiteToSiteTestUtils {
public static DataPacket createDataPacket(String contents) { public static DataPacket createDataPacket(String contents) {
@ -61,7 +62,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertFalse("Should NOT be backoff", completion.isBackoff()); assertFalse(completion.isBackoff(), "Should NOT be backoff");
assertEquals(0, completion.getDataPacketsTransferred()); assertEquals(0, completion.getDataPacketsTransferred());
} }
@ -81,7 +82,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertFalse("Should NOT be backoff", completion.isBackoff()); assertFalse(completion.isBackoff(), "Should NOT be backoff");
assertEquals(1, completion.getDataPacketsTransferred()); assertEquals(1, completion.getDataPacketsTransferred());
} }
@ -104,7 +105,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertFalse("Should NOT be backoff", completion.isBackoff()); assertFalse(completion.isBackoff(), "Should NOT be backoff");
assertEquals(2, completion.getDataPacketsTransferred()); assertEquals(2, completion.getDataPacketsTransferred());
} }
@ -124,36 +125,18 @@ public class SiteToSiteTestUtils {
packet = transaction.receive(); packet = transaction.receive();
assertNull(packet); assertNull(packet);
try { IOException io = assertThrows(IOException.class, () -> transaction.confirm());
transaction.confirm(); assertTrue(io.getMessage().contains("Received a BadChecksum response"));
fail(); assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
} catch (IOException e){
assertTrue(e.getMessage().contains("Received a BadChecksum response"));
assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
}
try { assertThrows(IllegalStateException.class, () -> transaction.complete());
transaction.complete(); assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
fail("It's not confirmed.");
} catch (IllegalStateException e){
assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
}
} }
public static void execSendZeroFlowFile(Transaction transaction) throws IOException { public static void execSendZeroFlowFile(Transaction transaction) throws IOException {
assertEquals(Transaction.TransactionState.TRANSACTION_STARTED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_STARTED, transaction.getState());
assertThrows(IllegalStateException.class, () -> transaction.confirm());
try { assertThrows(IllegalStateException.class, () -> transaction.complete());
transaction.confirm();
fail("Nothing has been sent.");
} catch (IllegalStateException e){
}
try {
transaction.complete();
fail("Nothing has been sent.");
} catch (IllegalStateException e){
}
} }
public static void execSendOneFlowFile(Transaction transaction) throws IOException { public static void execSendOneFlowFile(Transaction transaction) throws IOException {
@ -167,7 +150,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertFalse("Should NOT be backoff", completion.isBackoff()); assertFalse(completion.isBackoff(), "Should NOT be backoff");
assertEquals(1, completion.getDataPacketsTransferred()); assertEquals(1, completion.getDataPacketsTransferred());
} }
@ -185,7 +168,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertFalse("Should NOT be backoff", completion.isBackoff()); assertFalse(completion.isBackoff(), "Should NOT be backoff");
assertEquals(2, completion.getDataPacketsTransferred()); assertEquals(2, completion.getDataPacketsTransferred());
} }
@ -198,21 +181,12 @@ public class SiteToSiteTestUtils {
packet = createDataPacket("contents on client 2"); packet = createDataPacket("contents on client 2");
transaction.send(packet); transaction.send(packet);
IOException e = assertThrows(IOException.class, () -> transaction.confirm());
assertTrue(e.getMessage().contains("peer calculated CRC32 Checksum as Different checksum"));
assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
try { assertThrows(IllegalStateException.class, () -> transaction.complete());
transaction.confirm(); assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
fail();
} catch (IOException e){
assertTrue(e.getMessage().contains("peer calculated CRC32 Checksum as Different checksum"));
assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
}
try {
transaction.complete();
fail("It's not confirmed.");
} catch (IllegalStateException e){
assertEquals(Transaction.TransactionState.ERROR, transaction.getState());
}
} }
public static void execSendButDestinationFull(Transaction transaction) throws IOException { public static void execSendButDestinationFull(Transaction transaction) throws IOException {
@ -229,7 +203,7 @@ public class SiteToSiteTestUtils {
TransactionCompletion completion = transaction.complete(); TransactionCompletion completion = transaction.complete();
assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState());
assertTrue("Should be backoff", completion.isBackoff()); assertTrue(completion.isBackoff(), "Should be backoff");
assertEquals(2, completion.getDataPacketsTransferred()); assertEquals(2, completion.getDataPacketsTransferred());
} }

View File

@ -16,32 +16,6 @@
*/ */
package org.apache.nifi.remote.protocol.http; package org.apache.nifi.remote.protocol.http;
import static org.apache.nifi.remote.protocol.ResponseCode.CONFIRM_TRANSACTION;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveOneFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveTwoFlowFiles;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveWithInvalidChecksum;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveZeroFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendButDestinationFull;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendOneFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendTwoFlowFiles;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendWithInvalidChecksum;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendZeroFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.readContents;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.nifi.events.EventReporter; import org.apache.nifi.events.EventReporter;
import org.apache.nifi.remote.Peer; import org.apache.nifi.remote.Peer;
import org.apache.nifi.remote.PeerDescription; import org.apache.nifi.remote.PeerDescription;
@ -57,12 +31,37 @@ import org.apache.nifi.remote.protocol.ResponseCode;
import org.apache.nifi.remote.util.SiteToSiteRestApiClient; import org.apache.nifi.remote.util.SiteToSiteRestApiClient;
import org.apache.nifi.reporting.Severity; import org.apache.nifi.reporting.Severity;
import org.apache.nifi.web.api.entity.TransactionResultEntity; import org.apache.nifi.web.api.entity.TransactionResultEntity;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.nifi.remote.protocol.ResponseCode.CONFIRM_TRANSACTION;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveOneFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveTwoFlowFiles;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveWithInvalidChecksum;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execReceiveZeroFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendButDestinationFull;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendOneFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendTwoFlowFiles;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendWithInvalidChecksum;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendZeroFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.readContents;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class TestHttpClientTransaction { public class TestHttpClientTransaction {
private Logger logger = LoggerFactory.getLogger(TestHttpClientTransaction.class); private Logger logger = LoggerFactory.getLogger(TestHttpClientTransaction.class);
@ -108,7 +107,7 @@ public class TestHttpClientTransaction {
execReceiveZeroFlowFile(transaction); execReceiveZeroFlowFile(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length); assertEquals(0, clientRequest.toByteArray().length, "Client sends nothing as payload to receive flow files.");
} }
@Test @Test
@ -129,7 +128,7 @@ public class TestHttpClientTransaction {
execReceiveOneFlowFile(transaction); execReceiveOneFlowFile(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length); assertEquals(0, clientRequest.toByteArray().length, "Client sends nothing as payload to receive flow files.");
verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "3680976076"); verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "3680976076");
} }
@ -152,7 +151,7 @@ public class TestHttpClientTransaction {
execReceiveTwoFlowFiles(transaction); execReceiveTwoFlowFiles(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length); assertEquals(0, clientRequest.toByteArray().length, "Client sends nothing as payload to receive flow files.");
verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230"); verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230");
} }
@ -176,7 +175,7 @@ public class TestHttpClientTransaction {
execReceiveWithInvalidChecksum(transaction); execReceiveWithInvalidChecksum(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length); assertEquals(0, clientRequest.toByteArray().length, "Client sends nothing as payload to receive flow files.");
verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230"); verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230");
} }
@ -194,7 +193,7 @@ public class TestHttpClientTransaction {
execSendZeroFlowFile(transaction); execSendZeroFlowFile(transaction);
assertEquals("Client didn't send anything", 0, clientRequest.toByteArray().length); assertEquals(0, clientRequest.toByteArray().length, "Client didn't send anything");
} }
@Test @Test
@ -204,13 +203,10 @@ public class TestHttpClientTransaction {
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId"; final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class)); doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class));
// Emulate that server returns correct checksum. // Emulate that server returns correct checksum.
doAnswer(new Answer() { doAnswer(invocation -> {
@Override HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0];
public Object answer(InvocationOnMock invocation) throws Throwable { commSession.setChecksum("2946083981");
HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; return null;
commSession.setChecksum("2946083981");
return null;
}
}).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
TransactionResultEntity resultEntity = new TransactionResultEntity(); TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.getCode()); resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.getCode());
@ -238,13 +234,10 @@ public class TestHttpClientTransaction {
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId"; final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class)); doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class));
// Emulate that server returns correct checksum. // Emulate that server returns correct checksum.
doAnswer(new Answer() { doAnswer(invocation -> {
@Override HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0];
public Object answer(InvocationOnMock invocation) throws Throwable { commSession.setChecksum("3359812065");
HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; return null;
commSession.setChecksum("3359812065");
return null;
}
}).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
TransactionResultEntity resultEntity = new TransactionResultEntity(); TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.getCode()); resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.getCode());
@ -273,21 +266,15 @@ public class TestHttpClientTransaction {
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId"; final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class)); doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class));
// Emulate that server returns incorrect checksum. // Emulate that server returns incorrect checksum.
doAnswer(new Answer() { doAnswer(invocation -> {
@Override HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0];
public Object answer(InvocationOnMock invocation) throws Throwable { commSession.setChecksum("Different checksum");
HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; return null;
commSession.setChecksum("Different checksum");
return null;
}
}).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
doAnswer(new Answer() { doAnswer(invocation -> {
@Override TransactionResultEntity serverResult = new TransactionResultEntity();
public Object answer(InvocationOnMock invocation) throws Throwable { serverResult.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode());
TransactionResultEntity serverResult = new TransactionResultEntity(); return serverResult;
serverResult.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode());
return serverResult;
}
}).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(ResponseCode.BAD_CHECKSUM)); }).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(ResponseCode.BAD_CHECKSUM));
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream(); ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
@ -314,13 +301,10 @@ public class TestHttpClientTransaction {
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId"; final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class)); doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class));
// Emulate that server returns correct checksum. // Emulate that server returns correct checksum.
doAnswer(new Answer() { doAnswer(invocation -> {
@Override HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0];
public Object answer(InvocationOnMock invocation) throws Throwable { commSession.setChecksum("3359812065");
HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; return null;
commSession.setChecksum("3359812065");
return null;
}
}).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
TransactionResultEntity resultEntity = new TransactionResultEntity(); TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.getCode()); resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.getCode());

View File

@ -30,7 +30,7 @@ import org.apache.nifi.remote.protocol.DataPacket;
import org.apache.nifi.remote.protocol.RequestType; import org.apache.nifi.remote.protocol.RequestType;
import org.apache.nifi.remote.protocol.Response; import org.apache.nifi.remote.protocol.Response;
import org.apache.nifi.remote.protocol.ResponseCode; import org.apache.nifi.remote.protocol.ResponseCode;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -51,7 +51,7 @@ import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendTwoFlo
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendWithInvalidChecksum; import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendWithInvalidChecksum;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendZeroFlowFile; import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.execSendZeroFlowFile;
import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.readContents; import static org.apache.nifi.remote.protocol.SiteToSiteTestUtils.readContents;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -124,7 +124,7 @@ public class TestSocketClientTransaction {
assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
Response confirmResponse = Response.read(sentByClient); Response confirmResponse = Response.read(sentByClient);
assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode()); assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
assertEquals("Checksum should be calculated at client", "3680976076", confirmResponse.getMessage()); assertEquals( "3680976076", confirmResponse.getMessage(), "Checksum should be calculated at client");
Response completeResponse = Response.read(sentByClient); Response completeResponse = Response.read(sentByClient);
assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode()); assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode());
assertEquals(-1, sentByClient.read()); assertEquals(-1, sentByClient.read());
@ -157,7 +157,7 @@ public class TestSocketClientTransaction {
assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
Response confirmResponse = Response.read(sentByClient); Response confirmResponse = Response.read(sentByClient);
assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode()); assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
assertEquals("Checksum should be calculated at client", "2969091230", confirmResponse.getMessage()); assertEquals("2969091230", confirmResponse.getMessage(), "Checksum should be calculated at client");
Response completeResponse = Response.read(sentByClient); Response completeResponse = Response.read(sentByClient);
assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode()); assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode());
assertEquals(-1, sentByClient.read()); assertEquals(-1, sentByClient.read());
@ -188,7 +188,7 @@ public class TestSocketClientTransaction {
assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
Response confirmResponse = Response.read(sentByClient); Response confirmResponse = Response.read(sentByClient);
assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode()); assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
assertEquals("Checksum should be calculated at client", "2969091230", confirmResponse.getMessage()); assertEquals( "2969091230", confirmResponse.getMessage(), "Checksum should be calculated at client");
assertEquals(-1, sentByClient.read()); assertEquals(-1, sentByClient.read());
} }

View File

@ -16,21 +16,21 @@
*/ */
package org.apache.nifi.remote.util; package org.apache.nifi.remote.util;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import static org.apache.nifi.remote.util.SiteToSiteRestApiClient.parseClusterUrls; import static org.apache.nifi.remote.util.SiteToSiteRestApiClient.parseClusterUrls;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestSiteToSiteRestApiClient { public class TestSiteToSiteRestApiClient {
private static void assertSingleUri(final String expected, final Set<String> urls) { private static void assertSingleUri(final String expected, final Set<String> urls) {
Assert.assertEquals(1, urls.size()); assertEquals(1, urls.size());
Assert.assertEquals(expected, urls.iterator().next().toString()); assertEquals(expected, urls.iterator().next().toString());
} }
@Test @Test
@ -122,57 +122,42 @@ public class TestSiteToSiteRestApiClient {
@Test @Test
public void testGetUrlsEmpty() throws Exception { public void testGetUrlsEmpty() throws Exception {
try { assertThrows(IllegalArgumentException.class, () -> parseClusterUrls(null));
parseClusterUrls(null);
fail("Should fail if cluster URL was not specified.");
} catch (IllegalArgumentException e) {
}
try { assertThrows(IllegalArgumentException.class, () -> parseClusterUrls(""));
parseClusterUrls("");
fail("Should fail if cluster URL was not specified.");
} catch (IllegalArgumentException e) {
}
} }
@Test @Test
public void testGetUrlsOne() throws Exception { public void testGetUrlsOne() throws Exception {
final Set<String> urls = parseClusterUrls("http://localhost:8080/nifi"); final Set<String> urls = parseClusterUrls("http://localhost:8080/nifi");
Assert.assertEquals(1, urls.size()); assertEquals(1, urls.size());
Assert.assertEquals("http://localhost:8080/nifi-api", urls.iterator().next()); assertEquals("http://localhost:8080/nifi-api", urls.iterator().next());
} }
@Test @Test
public void testGetUrlsThree() throws Exception { public void testGetUrlsThree() throws Exception {
final Set<String> urls = parseClusterUrls("http://host1:8080/nifi,http://host2:8080/nifi,http://host3:8080/nifi"); final Set<String> urls = parseClusterUrls("http://host1:8080/nifi,http://host2:8080/nifi,http://host3:8080/nifi");
Assert.assertEquals(3, urls.size()); assertEquals(3, urls.size());
final Iterator<String> iterator = urls.iterator(); final Iterator<String> iterator = urls.iterator();
Assert.assertEquals("http://host1:8080/nifi-api", iterator.next()); assertEquals("http://host1:8080/nifi-api", iterator.next());
Assert.assertEquals("http://host2:8080/nifi-api", iterator.next()); assertEquals("http://host2:8080/nifi-api", iterator.next());
Assert.assertEquals("http://host3:8080/nifi-api", iterator.next()); assertEquals("http://host3:8080/nifi-api", iterator.next());
} }
@Test @Test
public void testGetUrlsDifferentProtocols() throws Exception { public void testGetUrlsDifferentProtocols() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> parseClusterUrls("http://host1:8080/nifi,https://host2:8080/nifi,http://host3:8080/nifi"));
try { assertTrue(exception.getMessage().contains("Different protocols"));
parseClusterUrls("http://host1:8080/nifi,https://host2:8080/nifi,http://host3:8080/nifi");
fail("Should fail if cluster URLs contain different protocols.");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Different protocols"));
}
} }
@Test @Test
public void testGetUrlsMalformed() throws Exception { public void testGetUrlsMalformed() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
try { () -> parseClusterUrls("http://host1:8080/nifi,host&2:8080,http://host3:8080/nifi"));
parseClusterUrls("http://host1:8080/nifi,host&2:8080,http://host3:8080/nifi"); assertTrue(exception.getMessage().contains("malformed"));
fail("Should fail if cluster URLs contain illegal URL.");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("malformed"));
}
} }
} }

View File

@ -16,25 +16,19 @@
*/ */
package org.apache.nifi.io.socket package org.apache.nifi.io.socket
import org.apache.nifi.security.util.KeystoreType import org.apache.nifi.security.util.KeystoreType
import org.apache.nifi.security.util.StandardTlsConfiguration import org.apache.nifi.security.util.StandardTlsConfiguration
import org.apache.nifi.security.util.TlsConfiguration import org.apache.nifi.security.util.TlsConfiguration
import org.apache.nifi.util.NiFiProperties import org.apache.nifi.util.NiFiProperties
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.junit.After import org.junit.jupiter.api.BeforeAll
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import javax.net.ssl.SSLServerSocket import javax.net.ssl.SSLServerSocket
import java.security.Security import java.security.Security
@RunWith(JUnit4.class)
class SocketUtilsTest extends GroovyTestCase { class SocketUtilsTest extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(SocketUtilsTest.class) private static final Logger logger = LoggerFactory.getLogger(SocketUtilsTest.class)
@ -65,7 +59,7 @@ class SocketUtilsTest extends GroovyTestCase {
// private static final TlsConfiguration TLS_CONFIGURATION = new StandardTlsConfiguration(KEYSTORE_PATH, KEYSTORE_PASSWORD, KEY_PASSWORD, KEYSTORE_TYPE, TRUSTSTORE_PATH, TRUSTSTORE_PASSWORD, TRUSTSTORE_TYPE, PROTOCOL) // private static final TlsConfiguration TLS_CONFIGURATION = new StandardTlsConfiguration(KEYSTORE_PATH, KEYSTORE_PASSWORD, KEY_PASSWORD, KEYSTORE_TYPE, TRUSTSTORE_PATH, TRUSTSTORE_PASSWORD, TRUSTSTORE_TYPE, PROTOCOL)
// private static final SSLContext sslContext = SslContextFactory.createSslContext(TLS_CONFIGURATION, ClientAuth.NONE) // private static final SSLContext sslContext = SslContextFactory.createSslContext(TLS_CONFIGURATION, ClientAuth.NONE)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
Security.addProvider(new BouncyCastleProvider()) Security.addProvider(new BouncyCastleProvider())
@ -74,16 +68,6 @@ class SocketUtilsTest extends GroovyTestCase {
} }
} }
@Before
void setUp() {
}
@After
void tearDown() {
}
@Test @Test
void testCreateSSLServerSocketShouldRestrictTlsProtocols() { void testCreateSSLServerSocketShouldRestrictTlsProtocols() {
// Arrange // Arrange

View File

@ -16,6 +16,13 @@
*/ */
package org.apache.nifi.io.nio.example; package org.apache.nifi.io.nio.example;
import org.apache.nifi.io.nio.BufferPool;
import org.apache.nifi.io.nio.ChannelListener;
import org.apache.nifi.io.nio.consumer.StreamConsumer;
import org.apache.nifi.io.nio.consumer.StreamConsumerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashSet; import java.util.HashSet;
@ -27,14 +34,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.nifi.io.nio.BufferPool;
import org.apache.nifi.io.nio.ChannelListener;
import org.apache.nifi.io.nio.consumer.StreamConsumer;
import org.apache.nifi.io.nio.consumer.StreamConsumerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
*/ */

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.nifi.io.nio.example; package org.apache.nifi.io.nio.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
*/ */
public class TCPClient { public class TCPClient {

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.nifi.io.nio.example; package org.apache.nifi.io.nio.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
*/ */
public class UDPClient { public class UDPClient {

View File

@ -16,11 +16,11 @@
*/ */
package org.apache.nifi.io.nio.example; package org.apache.nifi.io.nio.example;
import org.apache.nifi.io.nio.consumer.AbstractStreamConsumer;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.apache.nifi.io.nio.consumer.AbstractStreamConsumer;
/** /**
* *
*/ */

View File

@ -16,39 +16,23 @@
*/ */
package org.apache.nifi.util package org.apache.nifi.util
import org.junit.jupiter.api.BeforeAll
import org.junit.After import org.junit.jupiter.api.Test
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@RunWith(JUnit4.class)
class TestFormatUtilsGroovy extends GroovyTestCase { class TestFormatUtilsGroovy extends GroovyTestCase {
private static final Logger logger = LoggerFactory.getLogger(TestFormatUtilsGroovy.class) private static final Logger logger = LoggerFactory.getLogger(TestFormatUtilsGroovy.class)
@BeforeClass @BeforeAll
static void setUpOnce() throws Exception { static void setUpOnce() throws Exception {
logger.metaClass.methodMissing = { String name, args -> logger.metaClass.methodMissing = { String name, args ->
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
} }
} }
@Before
void setUp() throws Exception {
}
@After
void tearDown() throws Exception {
}
/** /**
* New feature test * New feature test
*/ */

View File

@ -17,7 +17,7 @@
package org.apache.nifi.processor; package org.apache.nifi.processor;
import org.apache.nifi.util.FormatUtils; import org.apache.nifi.util.FormatUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;

View File

@ -16,7 +16,7 @@
*/ */
package org.apache.nifi.remote.io; package org.apache.nifi.remote.io;
import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -24,10 +24,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import org.apache.nifi.remote.io.CompressionInputStream; import static org.junit.Assert.assertTrue;
import org.apache.nifi.remote.io.CompressionOutputStream;
import org.junit.Test;
public class TestCompressionInputOutputStreams { public class TestCompressionInputOutputStreams {

View File

@ -16,12 +16,15 @@
*/ */
package org.apache.nifi.stream.io; package org.apache.nifi.stream.io;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;
public class ByteCountingInputStreamTest extends TestCase { import static org.junit.jupiter.api.Assertions.assertEquals;
public class ByteCountingInputStreamTest {
@Test
public void testReset() throws Exception { public void testReset() throws Exception {
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.UTF_8)); final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.UTF_8));
@ -53,6 +56,7 @@ public class ByteCountingInputStreamTest extends TestCase {
assertEquals(bytesAtMark, bcis.getBytesRead()); assertEquals(bytesAtMark, bcis.getBytesRead());
} }
@Test
public void testAvailableShouldReturnCorrectCount() throws Exception { public void testAvailableShouldReturnCorrectCount() throws Exception {
// Arrange // Arrange
final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

View File

@ -16,15 +16,21 @@
*/ */
package org.apache.nifi.stream.io; package org.apache.nifi.stream.io;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase;
public class LimitingInputStreamTest extends TestCase { import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LimitingInputStreamTest {
private final static byte[] TEST_BUFFER = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; private final static byte[] TEST_BUFFER = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
@Test
public void testReadLimitNotReached() throws IOException { public void testReadLimitNotReached() throws IOException {
final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 50); final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 50);
long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream());
@ -32,6 +38,7 @@ public class LimitingInputStreamTest extends TestCase {
assertFalse(is.hasReachedLimit()); assertFalse(is.hasReachedLimit());
} }
@Test
public void testReadLimitMatched() throws IOException { public void testReadLimitMatched() throws IOException {
final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 10); final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 10);
long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream());
@ -39,11 +46,11 @@ public class LimitingInputStreamTest extends TestCase {
assertTrue(is.hasReachedLimit()); assertTrue(is.hasReachedLimit());
} }
@Test
public void testReadLimitExceeded() throws IOException { public void testReadLimitExceeded() throws IOException {
final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 9); final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 9);
final long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); final long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream());
assertEquals(bytesRead, 9); assertEquals(bytesRead, 9);
assertTrue(is.hasReachedLimit()); assertTrue(is.hasReachedLimit());
} }
} }

View File

@ -16,8 +16,9 @@
*/ */
package org.apache.nifi.stream.io; package org.apache.nifi.stream.io;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Disabled;
import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -26,13 +27,16 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Ignore; import java.util.concurrent.TimeUnit;
import org.junit.Test;
@Ignore("Tests are time-based") import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Disabled("Tests are time-based")
public class TestLeakyBucketThrottler { public class TestLeakyBucketThrottler {
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testOutputStreamInterface() throws IOException { public void testOutputStreamInterface() throws IOException {
// throttle rate at 1 MB/sec // throttle rate at 1 MB/sec
final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024);
@ -51,7 +55,8 @@ public class TestLeakyBucketThrottler {
} }
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testInputStreamInterface() throws IOException { public void testInputStreamInterface() throws IOException {
final byte[] data = new byte[1024 * 1024 * 4]; final byte[] data = new byte[1024 * 1024 * 4];
@ -75,7 +80,8 @@ public class TestLeakyBucketThrottler {
} }
} }
@Test(timeout = 10000) @Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testDirectInterface() throws IOException, InterruptedException { public void testDirectInterface() throws IOException, InterruptedException {
// throttle rate at 1 MB/sec // throttle rate at 1 MB/sec
try (final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); try (final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024);

View File

@ -16,13 +16,7 @@
*/ */
package org.apache.nifi.stream.io.util; package org.apache.nifi.stream.io.util;
import static org.junit.Assert.assertArrayEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@ -30,39 +24,26 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@SuppressWarnings("resource") @SuppressWarnings("resource")
public class StreamDemarcatorTest { public class StreamDemarcatorTest {
@Test @Test
public void validateInitializationFailure() { public void validateInitializationFailure() {
try { assertThrows(IllegalArgumentException.class, () ->new StreamDemarcator(null, null, -1));
new StreamDemarcator(null, null, -1);
fail();
} catch (IllegalArgumentException e) {
// success
}
try { assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), null, -1));
new StreamDemarcator(mock(InputStream.class), null, -1);
fail();
} catch (IllegalArgumentException e) {
// success
}
try { assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), null, 10, -1));
new StreamDemarcator(mock(InputStream.class), null, 10, -1);
fail();
} catch (IllegalArgumentException e) {
// success
}
try { assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), new byte[0], 10, 1));
new StreamDemarcator(mock(InputStream.class), new byte[0], 10, 1);
fail();
} catch (IllegalArgumentException e) {
// success
}
} }
@Test @Test
@ -229,13 +210,13 @@ public class StreamDemarcatorTest {
assertNull(scanner.nextToken()); assertNull(scanner.nextToken());
} }
@Test(expected = IOException.class) @Test
public void validateMaxBufferSize() throws IOException { public void validateMaxBufferSize() throws IOException {
String data = "THIS IS MY TEXT<MY DELIMITER>THIS IS MY NEW TEXT THEN<MY DELIMITER>THIS IS MY NEWEST TEXT"; String data = "THIS IS MY TEXT<MY DELIMITER>THIS IS MY NEW TEXT THEN<MY DELIMITER>THIS IS MY NEWEST TEXT";
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes()); ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
StreamDemarcator scanner = new StreamDemarcator(is, "<MY DELIMITER>".getBytes(StandardCharsets.UTF_8), 20); StreamDemarcator scanner = new StreamDemarcator(is, "<MY DELIMITER>".getBytes(StandardCharsets.UTF_8), 20);
scanner.nextToken(); scanner.nextToken();
scanner.nextToken(); assertThrows(IOException.class, () -> scanner.nextToken());
} }
@Test @Test

View File

@ -17,8 +17,8 @@
package org.apache.nifi.stream.io.util; package org.apache.nifi.stream.io.util;
import org.apache.nifi.stream.io.RepeatingInputStream; import org.apache.nifi.stream.io.RepeatingInputStream;
import org.junit.Ignore; import org.junit.jupiter.api.Disabled;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@ -100,8 +100,9 @@ public class TestLineDemarcator {
} }
@Test @Test
@Ignore("Intended only for manual testing. While this can take a while to run, it can be very helpful for manual testing before and after a change to the class. However, we don't want this to " + @Disabled("Intended only for manual testing. While this can take a while to run, it can be very helpful for manual testing before " +
"run in automated tests because we have no way to compare from one run to another, so it will only slow down automated tests.") "and after a change to the class. However, we don't want this to run in automated tests because we have no way to compare " +
"from one run to another, so it will only slow down automated tests.")
public void testPerformance() throws IOException { public void testPerformance() throws IOException {
final String lines = "The\nquick\nbrown\nfox\njumped\nover\nthe\nlazy\ndog.\r\n\n"; final String lines = "The\nquick\nbrown\nfox\njumped\nover\nthe\nlazy\ndog.\r\n\n";
final byte[] bytes = lines.getBytes(StandardCharsets.UTF_8); final byte[] bytes = lines.getBytes(StandardCharsets.UTF_8);

View File

@ -16,32 +16,33 @@
*/ */
package org.apache.nifi.stream.io.util; package org.apache.nifi.stream.io.util;
import static org.junit.Assert.assertEquals; import org.apache.nifi.stream.io.util.TextLineDemarcator.OffsetInfo;
import static org.junit.Assert.assertFalse; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.nifi.stream.io.util.TextLineDemarcator.OffsetInfo; import static org.junit.Assert.assertEquals;
import org.junit.Test; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
@SuppressWarnings("resource") @SuppressWarnings("resource")
public class TextLineDemarcatorTest { public class TextLineDemarcatorTest {
@Test(expected = IllegalArgumentException.class) @Test
public void nullStream() { public void nullStream() {
new TextLineDemarcator(null); assertThrows(IllegalArgumentException.class, () -> new TextLineDemarcator(null));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void illegalBufferSize() { public void illegalBufferSize() {
new TextLineDemarcator(mock(InputStream.class), -234); assertThrows(IllegalArgumentException.class, () -> new TextLineDemarcator(mock(InputStream.class), -234));
} }
@Test @Test

View File

@ -16,7 +16,7 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

View File

@ -16,10 +16,10 @@
*/ */
package org.apache.nifi.util; package org.apache.nifi.util;
import static org.junit.Assert.assertFalse; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestNaiveSearchRingBuffer { public class TestNaiveSearchRingBuffer {

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.nifi.util.file.classloader; package org.apache.nifi.util.file.classloader;
import org.junit.jupiter.api.Test;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -24,12 +26,10 @@ import java.nio.file.Paths;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class TestClassLoaderUtils { public class TestClassLoaderUtils {
@ -46,25 +46,16 @@ public class TestClassLoaderUtils {
final ClassLoader originalClassLoader = this.getClass().getClassLoader(); final ClassLoader originalClassLoader = this.getClass().getClassLoader();
ClassLoader customClassLoader = ClassLoaderUtils.getCustomClassLoader(null,originalClassLoader, getJarFilenameFilter()); ClassLoader customClassLoader = ClassLoaderUtils.getCustomClassLoader(null,originalClassLoader, getJarFilenameFilter());
assertTrue(customClassLoader != null); assertTrue(customClassLoader != null);
try{ ClassNotFoundException cex = assertThrows(ClassNotFoundException.class, () -> customClassLoader.loadClass("TestSuccess"));
customClassLoader.loadClass("TestSuccess"); assertTrue(cex.getLocalizedMessage().equals("TestSuccess"));
}catch (ClassNotFoundException cex){
assertTrue(cex.getLocalizedMessage().equals("TestSuccess"));
return;
}
fail("exception did not occur, class should not be found");
} }
@Test @Test
public void testGetCustomClassLoaderWithInvalidPath() { public void testGetCustomClassLoaderWithInvalidPath() {
final String jarFilePath = "src/test/resources/FakeTestClassLoaderUtils/TestSuccess.jar"; final String jarFilePath = "src/test/resources/FakeTestClassLoaderUtils/TestSuccess.jar";
try { MalformedURLException mex = assertThrows(MalformedURLException.class,
ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter()); () -> ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter()));
}catch(MalformedURLException mex){ assertTrue(mex.getLocalizedMessage().equals("Path specified does not exist"));
assertTrue(mex.getLocalizedMessage().equals("Path specified does not exist"));
return;
}
fail("exception did not occur, path should not exist");
} }
@Test @Test
@ -99,10 +90,10 @@ public class TestClassLoaderUtils {
assertEquals(1, urls.length); assertEquals(1, urls.length);
} }
@Test(expected = MalformedURLException.class) @Test
public void testGetURLsForClasspathWithSomeNonExistentAndNoSuppression() throws MalformedURLException { public void testGetURLsForClasspathWithSomeNonExistentAndNoSuppression() throws MalformedURLException {
final String jarFilePath = "src/test/resources/TestClassLoaderUtils/TestSuccess.jar,src/test/resources/TestClassLoaderUtils/FakeTest.jar"; final String jarFilePath = "src/test/resources/TestClassLoaderUtils/TestSuccess.jar,src/test/resources/TestClassLoaderUtils/FakeTest.jar";
ClassLoaderUtils.getURLsForClasspath(jarFilePath, null, false); assertThrows(MalformedURLException.class, () -> ClassLoaderUtils.getURLsForClasspath(jarFilePath, null, false));
} }
@Test @Test

View File

@ -16,9 +16,7 @@
*/ */
package org.apache.nifi.util.file.monitor; package org.apache.nifi.util.file.monitor;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -27,7 +25,9 @@ import java.io.OutputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestCompoundUpdateMonitor { public class TestCompoundUpdateMonitor {

View File

@ -16,8 +16,7 @@
*/ */
package org.apache.nifi.util.file.monitor; package org.apache.nifi.util.file.monitor;
import static org.junit.Assert.assertFalse; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -28,7 +27,8 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestSynchronousFileWatcher { public class TestSynchronousFileWatcher {

View File

@ -16,15 +16,16 @@
*/ */
package org.apache.nifi.util.locale; package org.apache.nifi.util.locale;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Assume;
import org.junit.Test;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/** /**
* Testing of the test suite environment {@link java.util.Locale}. The locales specified * Testing of the test suite environment {@link java.util.Locale}. The locales specified
* in ".github/workflows/ci-workflow.yml" are exercised. This test is inert when run in alternate locales. * in ".github/workflows/ci-workflow.yml" are exercised. This test is inert when run in alternate locales.
@ -37,14 +38,14 @@ public class TestLocaleOfTestSuite {
@Test @Test
public void testLocaleCI_EN_AU() { public void testLocaleCI_EN_AU() {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
Assume.assumeTrue(locale.toLanguageTag().equals("en-AU")); assumeTrue(locale.toLanguageTag().equals("en-AU"));
final String runtimeJavaVersion = System.getProperty("java.version"); final String runtimeJavaVersion = System.getProperty("java.version");
final boolean isJava8 = Pattern.compile("1\\.8.+").matcher(runtimeJavaVersion).matches(); final boolean isJava8 = Pattern.compile("1\\.8.+").matcher(runtimeJavaVersion).matches();
final String expected = (isJava8 ? "E" : "e"); // tested in Java 8 and Java 11 final String expected = (isJava8 ? "E" : "e"); // tested in Java 8 and Java 11
Assert.assertEquals(expected, DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); assertEquals(expected, DecimalFormatSymbols.getInstance(locale).getExponentSeparator());
Assert.assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); assertEquals("1,000", NumberFormat.getInstance(locale).format(1000));
} }
/** /**
@ -53,10 +54,10 @@ public class TestLocaleOfTestSuite {
@Test @Test
public void testLocaleCI_JA_JP() { public void testLocaleCI_JA_JP() {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
Assume.assumeTrue(locale.toLanguageTag().equals("ja-JP")); assumeTrue(locale.toLanguageTag().equals("ja-JP"));
Assert.assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator());
Assert.assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); assertEquals("1,000", NumberFormat.getInstance(locale).format(1000));
} }
/** /**
@ -65,9 +66,9 @@ public class TestLocaleOfTestSuite {
@Test @Test
public void testLocaleCI_FR_FR() { public void testLocaleCI_FR_FR() {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
Assume.assumeTrue(locale.toLanguageTag().equals("fr-FR")); assumeTrue(locale.toLanguageTag().equals("fr-FR"));
Assert.assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator());
Assert.assertEquals("1\u00a0000", NumberFormat.getInstance(locale).format(1000)); assertEquals("1\u00a0000", NumberFormat.getInstance(locale).format(1000));
} }
} }

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.nifi.util.text; package org.apache.nifi.util.text;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestRegexDateTimeMatcher { public class TestRegexDateTimeMatcher {
@ -63,7 +63,7 @@ public class TestRegexDateTimeMatcher {
final RegexDateTimeMatcher matcher = new RegexDateTimeMatcher.Compiler().compile(entry.getValue()); final RegexDateTimeMatcher matcher = new RegexDateTimeMatcher.Compiler().compile(entry.getValue());
final boolean matches = matcher.matches(entry.getKey()); final boolean matches = matcher.matches(entry.getKey());
assertTrue("Pattern <" + entry.getValue() + "> did not match <" + entry.getKey() + ">", matches); assertTrue(matches, "Pattern <" + entry.getValue() + "> did not match <" + entry.getKey() + ">");
} }
} }
} }

View File

@ -16,17 +16,17 @@
*/ */
package org.apache.nifi.util.timebuffer; package org.apache.nifi.util.timebuffer;
import static org.junit.Assert.assertEquals; import org.apache.nifi.util.RingBuffer;
import static org.junit.Assert.assertTrue; import org.apache.nifi.util.RingBuffer.ForEachEvaluator;
import org.apache.nifi.util.RingBuffer.IterationDirection;
import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.apache.nifi.util.RingBuffer; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.nifi.util.RingBuffer.ForEachEvaluator; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.nifi.util.RingBuffer.IterationDirection;
import org.junit.Test;
/** /**
* *

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.nifi.util.timebuffer; package org.apache.nifi.util.timebuffer;
import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNull;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class TestTimedBuffer { public class TestTimedBuffer {

View File

@ -16,26 +16,26 @@
*/ */
package org.apache.nifi.util.validator; package org.apache.nifi.util.validator;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.nifi.components.PropertyValue; import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.components.ValidationContext; import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult; import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.components.Validator; import org.apache.nifi.components.Validator;
import org.apache.nifi.processor.util.StandardValidators; import org.apache.nifi.processor.util.StandardValidators;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestStandardValidators { public class TestStandardValidators {
@Test @Test

Some files were not shown because too many files have changed in this diff Show More