From 8d513c5ed37b95cc84416fe4f3f6b609e7117dcb Mon Sep 17 00:00:00 2001 From: Mike Thomsen Date: Fri, 13 Aug 2021 07:11:25 -0400 Subject: [PATCH] NIFI-9080 Converted nifi-commons to use JUnit 5 This closes #5332 Signed-off-by: David Handermann --- .../nifi/bootstrap/util/OSUtilsTest.java | 8 +- .../language/QueryGroovyTest.groovy | 33 ++-- .../expression/language/TestQuery.java | 147 +++++---------- .../language/TestStandardPreparedQuery.java | 8 +- .../expression/language/TestValueLookup.java | 2 +- .../encryptor/StandardFlowEncryptorTest.java | 6 +- .../SetSensitivePropertiesKeyTest.java | 6 +- .../nifi/util/TestPackageUnpackageV3.java | 6 +- .../apache/nifi/hl7/query/TestHL7Query.java | 31 ++- .../processor/TestStandardValidators.groovy | 10 +- ...essionLanguageAgnosticParameterParser.java | 8 +- ...xpressionLanguageAwareParameterParser.java | 9 +- .../TestStandardParameterTokenList.java | 6 +- .../apache/nifi/util/NiFiPropertiesTest.java | 110 +++++------ .../org/apache/nifi/util/StringUtilsTest.java | 11 +- .../KeyedCipherPropertyEncryptorTest.java | 6 +- ...swordBasedCipherPropertyEncryptorTest.java | 6 +- .../encrypt/PropertyEncryptorFactoryTest.java | 2 +- ...StandardPropertySecretKeyProviderTest.java | 6 +- .../nifi/record/path/TestRecordPath.java | 58 +++--- .../record/path/util/AbstractWalkerTest.java | 14 +- .../TestFieldValueLogicalPathBuilder.java | 15 +- .../path/util/TestFieldValueWalker.java | 10 +- nifi-commons/nifi-record/pom.xml | 1 - .../serialization/TestSimpleRecordSchema.java | 22 +-- .../record/ResultSetRecordSetTest.java | 15 +- .../record/TestDataTypeUtils.java | 70 +++---- .../serialization/record/TestMapRecord.java | 17 +- .../nifi/rocksdb/TestRocksDBMetronome.java | 122 ++++++------ .../schema/TestSchemaRecordReader.java | 12 +- .../schema/TestSchemaRecordReaderWriter.java | 96 +++++----- .../nifi/security/krb/KerberosUserIT.java | 43 ++--- .../kms/FileBasedKeyProviderTest.java | 8 +- .../security/kms/KeyProviderFactoryTest.java | 6 +- .../security/kms/KeyStoreKeyProviderTest.java | 12 +- .../security/kms/StaticKeyProviderTest.java | 8 +- .../StandardFileBasedKeyReaderTest.java | 6 +- .../io/socket/ssl/SSLSocketChannelTest.java | 2 + .../security/util/TlsConfigurationTest.groovy | 18 +- .../nifi/security/util/TlsPlatformTest.java | 14 +- .../security/util/CertificateUtilsTest.groovy | 43 +---- .../AESKeyedCipherProviderGroovyTest.groovy | 23 +-- .../Argon2CipherProviderGroovyTest.groovy | 25 +-- .../util/crypto/Argon2SecureHasherTest.groovy | 40 +--- .../BcryptCipherProviderGroovyTest.groovy | 30 +-- .../util/crypto/BcryptSecureHasherTest.groovy | 24 +-- .../CipherProviderFactoryGroovyTest.groovy | 40 +--- .../crypto/CipherUtilityGroovyTest.groovy | 21 +-- .../util/crypto/HashAlgorithmTest.groovy | 19 +- .../util/crypto/HashServiceTest.groovy | 45 +---- .../NiFiLegacyCipherProviderGroovyTest.groovy | 29 +-- ...penSSLPKCS5CipherProviderGroovyTest.groovy | 26 +-- .../PBKDF2CipherProviderGroovyTest.groovy | 26 +-- .../util/crypto/PBKDF2SecureHasherTest.groovy | 20 +- .../ScryptCipherProviderGroovyTest.groovy | 31 ++- .../util/crypto/ScryptSecureHasherTest.groovy | 24 ++- .../util/scrypt/ScryptGroovyTest.groovy | 30 +-- .../nifi/security/xml/XmlUtilsTest.groovy | 21 +-- .../nifi/security/XXEValidatorTest.java | 29 +-- .../nifi/security/util/KeyStoreUtilsTest.java | 15 +- .../util/crypto/SecureHasherFactoryTest.java | 7 +- .../AESSensitivePropertyProviderTest.groovy | 22 +-- .../AwsKmsSensitivePropertyProviderIT.java | 25 +-- ...dSensitivePropertyProviderFactoryTest.java | 24 +-- .../command/SetSingleUserCredentialsTest.java | 12 +- .../encoder/BCryptPasswordEncoderTest.java | 12 +- .../StandardLoginCredentialsWriterTest.java | 8 +- .../remote/client/PeerSelectorTest.groovy | 17 +- .../remote/client/TestSiteInfoProvider.java | 27 +-- .../remote/client/http/TestHttpClient.java | 176 +++++++----------- .../client/socket/SiteToSiteClientIT.java | 99 ++++++++++ .../client/socket/TestSiteToSiteClient.java | 95 ++-------- .../remote/protocol/SiteToSiteTestUtils.java | 84 +++------ .../http/TestHttpClientTransaction.java | 122 ++++++------ .../socket/TestSocketClientTransaction.java | 10 +- .../util/TestSiteToSiteRestApiClient.java | 59 +++--- .../nifi/io/socket/SocketUtilsTest.groovy | 22 +-- .../nifi/io/nio/example/ServerMain.java | 15 +- .../apache/nifi/io/nio/example/TCPClient.java | 6 +- .../apache/nifi/io/nio/example/UDPClient.java | 6 +- .../io/nio/example/UselessStreamConsumer.java | 4 +- .../nifi/util/TestFormatUtilsGroovy.groovy | 22 +-- .../nifi/processor/TestFormatUtils.java | 2 +- .../io/TestCompressionInputOutputStreams.java | 7 +- .../io/ByteCountingInputStreamTest.java | 8 +- .../stream/io/LimitingInputStreamTest.java | 13 +- .../stream/io/TestLeakyBucketThrottler.java | 22 ++- .../stream/io/util/StreamDemarcatorTest.java | 49 ++--- .../stream/io/util/TestLineDemarcator.java | 9 +- .../io/util/TextLineDemarcatorTest.java | 25 +-- .../apache/nifi/util/StringSelectorTest.java | 2 +- .../nifi/util/TestNaiveSearchRingBuffer.java | 6 +- .../classloader/TestClassLoaderUtils.java | 35 ++-- .../monitor/TestCompoundUpdateMonitor.java | 8 +- .../monitor/TestSynchronousFileWatcher.java | 6 +- .../util/locale/TestLocaleOfTestSuite.java | 25 +-- .../util/text/TestRegexDateTimeMatcher.java | 6 +- .../nifi/util/timebuffer/TestRingBuffer.java | 12 +- .../nifi/util/timebuffer/TestTimedBuffer.java | 6 +- .../validator/TestStandardValidators.java | 26 +-- ...dHashiCorpVaultCommunicationServiceIT.java | 8 +- .../TestHashiCorpVaultConfiguration.java | 49 ++--- ...ardHashiCorpVaultCommunicationService.java | 10 +- .../apache/nifi/web/util/WebUtilsTest.java | 2 +- .../nifi/wali/TestBlockingQueuePool.java | 13 +- .../apache/nifi/wali/TestHashMapSnapshot.java | 38 ++-- .../nifi/wali/TestLengthDelimitedJournal.java | 58 ++---- .../TestSequentialAccessWriteAheadLog.java | 78 ++++---- .../wali/TestMinimalLockingWriteAheadLog.java | 159 +++++----------- pom.xml | 2 +- 110 files changed, 1257 insertions(+), 1852 deletions(-) create mode 100644 nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/SiteToSiteClientIT.java diff --git a/nifi-commons/nifi-bootstrap-utils/src/test/java/org/apache/nifi/bootstrap/util/OSUtilsTest.java b/nifi-commons/nifi-bootstrap-utils/src/test/java/org/apache/nifi/bootstrap/util/OSUtilsTest.java index 5a8abe957e..bfe85c3b87 100644 --- a/nifi-commons/nifi-bootstrap-utils/src/test/java/org/apache/nifi/bootstrap/util/OSUtilsTest.java +++ b/nifi-commons/nifi-bootstrap-utils/src/test/java/org/apache/nifi/bootstrap/util/OSUtilsTest.java @@ -16,14 +16,14 @@ */ package org.apache.nifi.bootstrap.util; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class OSUtilsTest { @@ -34,7 +34,7 @@ public class OSUtilsTest { final Logger logger = LoggerFactory.getLogger("testing"); final Long pid = OSUtils.getProcessId(process, logger); process.destroy(); - assertNotNull("Process ID not found", pid); + assertNotNull(pid, "Process ID not found"); } @Test diff --git a/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy b/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy index 78de467c8c..de80f782f0 100644 --- a/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy +++ b/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy @@ -18,39 +18,36 @@ package org.apache.nifi.attribute.expression.language import org.apache.nifi.attribute.expression.language.evaluation.QueryResult import org.apache.nifi.expression.AttributeExpression -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 +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory -@RunWith(JUnit4.class) -public class QueryGroovyTest extends GroovyTestCase { +class QueryGroovyTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(QueryGroovyTest.class) - @BeforeClass - public static void setUpOnce() throws Exception { + @BeforeAll + static void setUpOnce() throws Exception { logger.metaClass.methodMissing = { String name, args -> logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") } } - @Before - public void setUp() { + @BeforeEach + void setUp() { } - @After - public void tearDown() { + @AfterEach + void tearDown() { Query.metaClass.static = null } @Test - public void testReplaceShouldReplaceAllLiteralMatches() { + void testReplaceShouldReplaceAllLiteralMatches() { // Arrange int n = 3 final String ORIGINAL_VALUE = "Hello World" @@ -90,7 +87,7 @@ public class QueryGroovyTest extends GroovyTestCase { } @Test - public void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() { + void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() { // Arrange int n = 3 final String ORIGINAL_VALUE = "Hello World" @@ -130,7 +127,7 @@ public class QueryGroovyTest extends GroovyTestCase { } @Test - public void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() { + void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() { // Arrange int n = 3 final String ORIGINAL_VALUE = "Hello World" @@ -170,7 +167,7 @@ public class QueryGroovyTest extends GroovyTestCase { } @Test - public void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() { + void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() { // Arrange int n = 3 final String ORIGINAL_VALUE = "Hello World" diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java index 1c38a9bc66..5ff24b71e7 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java @@ -29,9 +29,8 @@ import org.apache.nifi.parameter.Parameter; import org.apache.nifi.parameter.ParameterDescriptor; import org.apache.nifi.parameter.ParameterLookup; import org.apache.nifi.registry.VariableRegistry; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.io.BufferedInputStream; @@ -55,10 +54,11 @@ import static java.lang.Double.NaN; import static java.lang.Double.POSITIVE_INFINITY; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestQuery { @@ -138,20 +138,11 @@ public class TestQuery { } private void assertValid(final String query) { - try { - Query.compile(query); - } catch (final Exception e) { - e.printStackTrace(); - Assert.fail("Expected query to be valid, but it failed to compile due to " + e); - } + assertDoesNotThrow(() -> Query.compile(query)); } private void assertInvalid(final String query) { - try { - Query.compile(query); - Assert.fail("Expected query to be invalid, but it did compile"); - } catch (final Exception e) { - } + assertThrows(Exception.class, () -> Query.compile(query)); } @Test @@ -159,11 +150,7 @@ public class TestQuery { Query.validateExpression("${abc:substring(${xyz:length()})}", false); Query.isValidExpression("${now():format('yyyy-MM-dd')}"); - try { - Query.validateExpression("$${attr}", false); - Assert.fail("invalid query validated"); - } catch (final AttributeExpressionLanguageParsingException e) { - } + assertThrows(AttributeExpressionLanguageParsingException.class, () -> Query.validateExpression("$${attr}", false)); Query.validateExpression("$${attr}", true); @@ -311,7 +298,7 @@ public class TestQuery { } @Test - @Ignore("Requires specific locale") + @Disabled("Requires specific locale") public void implicitDateConversion() { final Date date = new Date(); 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, "", "${json:jsonPathDelete('$.missingpath')}", ""); verifyEquals("${json:jsonPath('$.missingpath')}", attributes, ""); - try { - verifyEquals("${json:jsonPath('$..')}", attributes, ""); - Assert.fail("Did not detect bad JSON path expression"); - } catch (final AttributeExpressionLanguageException e) { - } - try { - verifyEquals("${missing:jsonPath('$.firstName')}", attributes, ""); - Assert.fail("Did not detect empty JSON document"); - } catch (AttributeExpressionLanguageException e) { - } + + assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${json:jsonPath('$..')}", attributes, "")); + assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${missing:jsonPath('$.firstName')}", attributes, "")); + attributes.put("invalid", "[}"); - try { - verifyEquals("${invlaid:jsonPath('$.firstName')}", attributes, "John"); - Assert.fail("Did not detect invalid JSON document"); - } catch (AttributeExpressionLanguageException e) { - } + assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${invlaid:jsonPath('$.firstName')}", attributes, "John")); } private void verifyAddressBookAttributes(String originalAddressBook, Map attributes, String updatedAttribute, Object updatedValue) { @@ -526,13 +506,12 @@ public class TestQuery { verifyEquals("${json:jsonPath('$.missing-path')}", attributes, ""); } - @Test(expected=IllegalArgumentException.class) public void testJsonPathAddNicknameJimmyAtNonArray() throws IOException { - Map attributes = verifyJsonPathExpressions( + assertThrows(IllegalArgumentException.class, () -> verifyJsonPathExpressions( ADDRESS_BOOK_JSON_PATH_EMPTY, "", "${json:jsonPathAdd('$.firstName', 'Jimmy')}", - ""); + "")); } @Test @@ -601,9 +580,9 @@ public class TestQuery { verifyEquals("${allAttributes( 'x', 'y' ):join(',')}", attributes, ","); } - @Test(expected = AttributeExpressionLanguageException.class) + @Test public void testCannotCombineWithNonReducingFunction() { - Query.compile("${allAttributes( 'a.1' ):plus(1)}"); + assertThrows(AttributeExpressionLanguageException.class, () -> Query.compile("${allAttributes( 'a.1' ):plus(1)}")); } @Test @@ -1128,32 +1107,25 @@ public class TestQuery { attributes.put("negativeDecimal", "-64.1"); // Test that errors relating to not finding methods are properly handled - try { - verifyEquals("${math('rand'):toNumber()}", attributes, 0L); - fail(); - } catch (AttributeExpressionLanguageException expected) { - assertEquals("Cannot evaluate 'math' function because no subjectless method was found with the name:'rand'", expected.getMessage()); - } - try { - verifyEquals("${negativeDecimal:math('absolute')}", attributes, 0L); - fail(); - } catch (AttributeExpressionLanguageException expected) { - assertEquals("Cannot evaluate 'math' function because no method was found matching the passed parameters: name:'absolute', one argument of type: 'double'", expected.getMessage()); - } - try { - verifyEquals("${oneDecimal:math('power', ${two:toDecimal()})}", 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: 'double'", 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()); - } + + AttributeExpressionLanguageException expected = assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${math('rand'):toNumber()}", attributes, 0L)); + assertEquals("Cannot evaluate 'math' function because no subjectless method was found with the name:'rand'", expected.getMessage()); + + expected = assertThrows(AttributeExpressionLanguageException.class, + () -> 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()); + + expected = assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${oneDecimal:math('power', ${two:toDecimal()})}", attributes, 0L)); + 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()); + + expected = assertThrows(AttributeExpressionLanguageException.class, + () -> verifyEquals("${oneDecimal:math('power', ${two})}", attributes, 0L)); + 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 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', 'other'):isNull()}", attributes, false); - try { - 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 - } + assertThrows(AttributeExpressionLanguageParsingException.class, () -> Query.compile("${allAttributes('#ah'):equals('hello')")); } @Test @@ -1925,7 +1892,7 @@ public class TestQuery { for (int i = 0; i < results.size(); i++) { long result = (Long) getResult("${random()}", attrs).getValue(); 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"); } - @Test(expected = AttributeExpressionLanguageException.class) + @Test public void testHashFailure() { final Map 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 @@ -2177,24 +2145,9 @@ public class TestQuery { assertTrue(multipleResultExpectedResults.contains(actualResult)); verifyEquals("${str:repeat(4)}", attributes, "abcabcabcabc"); - try { - verifyEquals("${str:repeat(-1)}", attributes, ""); - fail("Should have failed on numRepeats < 0"); - } 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 - } + assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(-1)}", attributes, "")); + assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(0)}", attributes, "")); + assertThrows(AttributeExpressionLanguageException.class, () -> verifyEquals("${str:repeat(2,1)}", attributes, "")); } @Test diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestStandardPreparedQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestStandardPreparedQuery.java index 34eef46e96..937f6f6c86 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestStandardPreparedQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestStandardPreparedQuery.java @@ -19,8 +19,8 @@ package org.apache.nifi.attribute.expression.language; import org.apache.nifi.parameter.Parameter; import org.apache.nifi.parameter.ParameterDescriptor; import org.apache.nifi.parameter.ParameterLookup; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.HashMap; @@ -55,7 +55,7 @@ public class TestStandardPreparedQuery { } @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() { final Map attrs = new HashMap<>(); attrs.put("xx", "world"); @@ -70,7 +70,7 @@ public class TestStandardPreparedQuery { } @Test - @Ignore("Takes too long") + @Disabled("Takes too long") public void test10MIterationsWithQuery() { final Map attrs = new HashMap<>(); attrs.put("xx", "world"); diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestValueLookup.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestValueLookup.java index 97935fdfb8..fc4b0b7f6b 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestValueLookup.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestValueLookup.java @@ -18,7 +18,7 @@ package org.apache.nifi.attribute.expression.language; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.registry.VariableRegistry; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; diff --git a/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/StandardFlowEncryptorTest.java b/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/StandardFlowEncryptorTest.java index d9cfd6a411..2494a953cc 100644 --- a/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/StandardFlowEncryptorTest.java +++ b/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/StandardFlowEncryptorTest.java @@ -19,8 +19,8 @@ package org.apache.nifi.flow.encryptor; import org.apache.nifi.encrypt.PropertyEncryptor; import org.apache.nifi.encrypt.PropertyEncryptorBuilder; import org.apache.nifi.security.util.EncryptionMethod; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -49,7 +49,7 @@ public class StandardFlowEncryptorTest { private StandardFlowEncryptor flowEncryptor; - @Before + @BeforeEach public void setEncryptors() { inputEncryptor = getPropertyEncryptor(INPUT_KEY, EncryptionMethod.MD5_256AES.getAlgorithm()); outputEncryptor = getPropertyEncryptor(OUTPUT_KEY, EncryptionMethod.SHA256_256AES.getAlgorithm()); diff --git a/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/command/SetSensitivePropertiesKeyTest.java b/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/command/SetSensitivePropertiesKeyTest.java index 13f2837dd2..8a2a4d8b6f 100644 --- a/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/command/SetSensitivePropertiesKeyTest.java +++ b/nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/command/SetSensitivePropertiesKeyTest.java @@ -17,8 +17,8 @@ package org.apache.nifi.flow.encryptor.command; import org.apache.nifi.stream.io.GZIPOutputStream; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileOutputStream; @@ -39,7 +39,7 @@ import static org.junit.Assert.assertTrue; public class SetSensitivePropertiesKeyTest { private static final String FLOW_CONTENTS = "PROPERTY"; - @After + @AfterEach public void clearProperties() { System.clearProperty(SetSensitivePropertiesKey.PROPERTIES_FILE_PATH); } diff --git a/nifi-commons/nifi-flowfile-packager/src/test/java/org/apache/nifi/util/TestPackageUnpackageV3.java b/nifi-commons/nifi-flowfile-packager/src/test/java/org/apache/nifi/util/TestPackageUnpackageV3.java index 24cd374645..6a4d1e441d 100644 --- a/nifi-commons/nifi-flowfile-packager/src/test/java/org/apache/nifi/util/TestPackageUnpackageV3.java +++ b/nifi-commons/nifi-flowfile-packager/src/test/java/org/apache/nifi/util/TestPackageUnpackageV3.java @@ -16,8 +16,7 @@ */ package org.apache.nifi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -26,7 +25,8 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class TestPackageUnpackageV3 { diff --git a/nifi-commons/nifi-hl7-query-language/src/test/java/org/apache/nifi/hl7/query/TestHL7Query.java b/nifi-commons/nifi-hl7-query-language/src/test/java/org/apache/nifi/hl7/query/TestHL7Query.java index 0e875c0844..cc71d7c877 100644 --- a/nifi-commons/nifi-hl7-query-language/src/test/java/org/apache/nifi/hl7/query/TestHL7Query.java +++ b/nifi-commons/nifi-hl7-query-language/src/test/java/org/apache/nifi/hl7/query/TestHL7Query.java @@ -16,9 +16,17 @@ */ package org.apache.nifi.hl7.query; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +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; +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.util.ArrayList; @@ -26,18 +34,9 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.apache.nifi.hl7.hapi.HapiMessage; -import org.apache.nifi.hl7.model.HL7Field; -import org.apache.nifi.hl7.model.HL7Message; -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; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; @SuppressWarnings("resource") public class TestHL7Query { @@ -60,7 +59,7 @@ public class TestHL7Query { private HL7Message hypoglycemia; - @Before + @BeforeEach public void init() throws IOException, HL7Exception { this.hyperglycemia = createMessage(HYPERGLYCEMIA); this.hypoglycemia = createMessage(HYPOGLYCEMIA); diff --git a/nifi-commons/nifi-json-utils/src/test/groovy/org/apache/nifi/processor/TestStandardValidators.groovy b/nifi-commons/nifi-json-utils/src/test/groovy/org/apache/nifi/processor/TestStandardValidators.groovy index c820c03dfc..8a5494f3e1 100644 --- a/nifi-commons/nifi-json-utils/src/test/groovy/org/apache/nifi/processor/TestStandardValidators.groovy +++ b/nifi-commons/nifi-json-utils/src/test/groovy/org/apache/nifi/processor/TestStandardValidators.groovy @@ -20,21 +20,21 @@ import org.apache.nifi.components.ValidationContext import org.apache.nifi.components.ValidationResult import org.apache.nifi.components.Validator import org.apache.nifi.processor.util.JsonValidator -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.BeforeEach +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.assertTrue import static org.mockito.Mockito.mock -import static groovy.json.JsonOutput.* - class TestStandardValidators { final String DUMMY_JSON_PROPERTY = "JSONProperty" Validator validator ValidationContext context - @Before + @BeforeEach void setup() { validator = JsonValidator.INSTANCE context = mock(ValidationContext.class) diff --git a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAgnosticParameterParser.java b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAgnosticParameterParser.java index c0c7213455..dd87de6988 100644 --- a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAgnosticParameterParser.java +++ b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAgnosticParameterParser.java @@ -16,13 +16,13 @@ */ package org.apache.nifi.parameter; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.List; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +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 TestExpressionLanguageAgnosticParameterParser { diff --git a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAwareParameterParser.java b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAwareParameterParser.java index c91f38a3f2..dbba4f1e2e 100644 --- a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAwareParameterParser.java +++ b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestExpressionLanguageAwareParameterParser.java @@ -16,13 +16,14 @@ */ package org.apache.nifi.parameter; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import java.util.List; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +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 TestExpressionLanguageAwareParameterParser { diff --git a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestStandardParameterTokenList.java b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestStandardParameterTokenList.java index 7f5afe76bf..342510ced9 100644 --- a/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestStandardParameterTokenList.java +++ b/nifi-commons/nifi-parameter/src/test/java/org/apache/nifi/parameter/TestStandardParameterTokenList.java @@ -16,15 +16,15 @@ */ package org.apache.nifi.parameter; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class TestStandardParameterTokenList { diff --git a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java index 08a87bf244..353d87727c 100644 --- a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java +++ b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/NiFiPropertiesTest.java @@ -16,8 +16,7 @@ */ package org.apache.nifi.util; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.File; import java.net.InetSocketAddress; @@ -29,10 +28,12 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -54,9 +55,9 @@ public class NiFiPropertiesTest { 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) { - 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"); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - try { - properties.validate(); - } catch (Throwable t) { - Assert.fail("unexpected exception: " + t.getMessage()); - } + assertGoodProperties(properties); // expect no error to be thrown additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, ""); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - try { - properties.validate(); - } catch (Throwable t) { - Assert.fail("unexpected exception: " + t.getMessage()); - } + assertGoodProperties(properties); // expect no error to be thrown additionalProperties.remove(NiFiProperties.REMOTE_INPUT_HOST); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - try { - properties.validate(); - } catch (Throwable t) { - Assert.fail("unexpected exception: " + t.getMessage()); - } + assertGoodProperties(properties); // expected error additionalProperties = new HashMap<>(); additionalProperties.put(NiFiProperties.REMOTE_INPUT_HOST, "http://localhost"); properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - try { - properties.validate(); - Assert.fail("Validation should throw an exception"); - } catch (Throwable t) { - // nothing to do - } + final NiFiProperties test = properties; + assertThrows(Throwable.class, () -> test.validate()); + } + + private void assertGoodProperties(final NiFiProperties properties) { + assertDoesNotThrow(() -> properties.validate()); } @Test @@ -187,7 +176,7 @@ public class NiFiPropertiesTest { assertEquals(Integer.parseInt(portValue), clusterProtocolPort.intValue()); } - @Test(expected = NumberFormatException.class) + @Test public void testShouldVerifyExceptionThrownWhenInValidFormatPortValue() { // Testing with CLUSTER_NODE_PROTOCOL_PORT @@ -200,10 +189,7 @@ public class NiFiPropertiesTest { // Act Integer clusterProtocolPort = properties.getClusterNodeProtocolPort(); - - // Assert - // Expect NumberFormatException thrown - assertEquals(Integer.parseInt(portValue), clusterProtocolPort.intValue()); + assertThrows(NumberFormatException.class, () -> Integer.parseInt(portValue)); } @Test @@ -225,7 +211,7 @@ public class NiFiPropertiesTest { assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort()); } - @Test(expected = RuntimeException.class) + @Test public void testShouldVerifyExceptionThrownWhenInvalidPortValue() { // Testing with CLUSTER_NODE_ADDRESS @@ -238,15 +224,10 @@ public class NiFiPropertiesTest { additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - // Act - InetSocketAddress clusterProtocolAddress = properties.getClusterNodeProtocolAddress(); - - // Assert - // Expect RuntimeException thrown - assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort()); + assertThrows(RuntimeException.class, () -> properties.getClusterNodeProtocolAddress()); } - @Test(expected = RuntimeException.class) + @Test public void testShouldVerifyExceptionThrownWhenPortValueIsZero() { // Arrange String portValue = "0"; @@ -256,12 +237,7 @@ public class NiFiPropertiesTest { additionalProperties.put(NiFiProperties.CLUSTER_NODE_ADDRESS, addressValue); NiFiProperties properties = loadNiFiProperties("/NiFiProperties/conf/nifi.blank.properties", additionalProperties); - // Act - InetSocketAddress clusterProtocolAddress = properties.getClusterNodeProtocolAddress(); - - // Assert - // Expect RuntimeException thrown - assertEquals(Integer.parseInt(portValue), clusterProtocolAddress.getPort()); + assertThrows(RuntimeException.class, () -> properties.getClusterNodeProtocolAddress()); } @Test @@ -384,9 +360,9 @@ public class NiFiPropertiesTest { final Map result = testSubject.getPropertiesWithPrefix("nifi.web.http"); // then - Assert.assertEquals(4, result.size()); - Assert.assertTrue(result.containsKey("nifi.web.http.host")); - Assert.assertTrue(result.containsKey("nifi.web.https.host")); + assertEquals(4, result.size()); + assertTrue(result.containsKey("nifi.web.http.host")); + assertTrue(result.containsKey("nifi.web.https.host")); } @Test @@ -398,9 +374,9 @@ public class NiFiPropertiesTest { final Map result = testSubject.getPropertiesWithPrefix("nifi.web.http."); // then - Assert.assertEquals(2, result.size()); - Assert.assertTrue(result.containsKey("nifi.web.http.host")); - Assert.assertFalse(result.containsKey("nifi.web.https.host")); + assertEquals(2, result.size()); + assertTrue(result.containsKey("nifi.web.http.host")); + assertFalse(result.containsKey("nifi.web.https.host")); } @Test @@ -412,7 +388,7 @@ public class NiFiPropertiesTest { final Map result = testSubject.getPropertiesWithPrefix("invalid.property"); // then - Assert.assertTrue(result.isEmpty()); + assertTrue(result.isEmpty()); } @Test @@ -424,9 +400,9 @@ public class NiFiPropertiesTest { final Set result = testSubject.getDirectSubsequentTokens("nifi.web.http"); // then - Assert.assertEquals(2, result.size()); - Assert.assertTrue(result.contains("host")); - Assert.assertTrue(result.contains("port")); + assertEquals(2, result.size()); + assertTrue(result.contains("host")); + assertTrue(result.contains("port")); } @Test @@ -438,9 +414,9 @@ public class NiFiPropertiesTest { final Set result = testSubject.getDirectSubsequentTokens("nifi.web.http."); // then - Assert.assertEquals(2, result.size()); - Assert.assertTrue(result.contains("host")); - Assert.assertTrue(result.contains("port")); + assertEquals(2, result.size()); + assertTrue(result.contains("host")); + assertTrue(result.contains("port")); } @Test @@ -452,7 +428,7 @@ public class NiFiPropertiesTest { final Set result = testSubject.getDirectSubsequentTokens("lorem.ipsum"); // then - Assert.assertTrue(result.isEmpty()); + assertTrue(result.isEmpty()); } @Test @@ -464,10 +440,10 @@ public class NiFiPropertiesTest { final Set result = testSubject.getDirectSubsequentTokens("nifi.web"); // then - Assert.assertEquals(4, result.size()); - Assert.assertTrue(result.contains("http")); - Assert.assertTrue(result.contains("https")); - Assert.assertTrue(result.contains("war")); - Assert.assertTrue(result.contains("jetty")); + assertEquals(4, result.size()); + assertTrue(result.contains("http")); + assertTrue(result.contains("https")); + assertTrue(result.contains("war")); + assertTrue(result.contains("jetty")); } } diff --git a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java index 059857c347..0de041accb 100644 --- a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java +++ b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java @@ -16,16 +16,17 @@ */ package org.apache.nifi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; 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 { diff --git a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java index 176fee8026..9385daa4bb 100644 --- a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java +++ b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java @@ -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.KeyedCipherProvider; import org.apache.nifi.util.StringUtils; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; @@ -55,7 +55,7 @@ public class KeyedCipherPropertyEncryptorTest { private KeyedCipherPropertyEncryptor encryptor; - @Before + @BeforeEach public void setUp() { encryptor = new KeyedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, SECRET_KEY); } diff --git a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PasswordBasedCipherPropertyEncryptorTest.java b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PasswordBasedCipherPropertyEncryptorTest.java index f9839c8ef6..9f0bb148f8 100644 --- a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PasswordBasedCipherPropertyEncryptorTest.java +++ b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PasswordBasedCipherPropertyEncryptorTest.java @@ -20,8 +20,8 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.apache.nifi.security.util.EncryptionMethod; import org.apache.nifi.security.util.crypto.PBECipherProvider; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -46,7 +46,7 @@ public class PasswordBasedCipherPropertyEncryptorTest { private PasswordBasedCipherPropertyEncryptor encryptor; - @Before + @BeforeEach public void setUp() { encryptor = new PasswordBasedCipherPropertyEncryptor(CIPHER_PROVIDER, ENCRYPTION_METHOD, PASSWORD); } diff --git a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PropertyEncryptorFactoryTest.java b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PropertyEncryptorFactoryTest.java index e044c81645..cf5889c2ca 100644 --- a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PropertyEncryptorFactoryTest.java +++ b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/PropertyEncryptorFactoryTest.java @@ -19,7 +19,7 @@ package org.apache.nifi.encrypt; import org.apache.nifi.security.util.EncryptionMethod; import org.apache.nifi.util.NiFiProperties; import org.apache.nifi.util.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Properties; diff --git a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/StandardPropertySecretKeyProviderTest.java b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/StandardPropertySecretKeyProviderTest.java index 9e2a924f28..c160ce491c 100644 --- a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/StandardPropertySecretKeyProviderTest.java +++ b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/StandardPropertySecretKeyProviderTest.java @@ -16,8 +16,8 @@ */ package org.apache.nifi.encrypt; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; @@ -31,7 +31,7 @@ public class StandardPropertySecretKeyProviderTest { private StandardPropertySecretKeyProvider provider; - @Before + @BeforeEach public void setUp() { provider = new StandardPropertySecretKeyProvider(); } diff --git a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java index 83c585346d..100861fd2b 100644 --- a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java +++ b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/TestRecordPath.java @@ -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.util.DataTypeUtils; import org.apache.nifi.uuid5.Uuid5Util; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.nio.charset.IllegalCharsetNameException; 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.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; 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); - @BeforeClass + @BeforeAll public static void setTestTimezone() { System.setProperty(USER_TIMEZONE_PROPERTY, TEST_TIMEZONE); } - @AfterClass + @AfterAll public static void setSystemTimezone() { System.setProperty(USER_TIMEZONE_PROPERTY, SYSTEM_TIMEZONE); } @@ -93,12 +93,7 @@ public class TestRecordPath { RecordPath.compile("/name[contains(., 'hello')]"); // substring is not a filter function so cannot be used as a predicate - try { - RecordPath.compile("/name[substring(., 1, 2)]"); - fail("Expected RecordPathException"); - } catch (final RecordPathException e) { - // expected - } + assertThrows(RecordPathException.class, () -> RecordPath.compile("/name[substring(., 1, 2)]")); // 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']"); @@ -1568,7 +1563,7 @@ public class TestRecordPath { assertEquals("Hello World!", RecordPath.compile("toString(/bytes, \"UTF-16\")").evaluate(record).getSelectedFields().findFirst().get().getValue()); } - @Test(expected = IllegalCharsetNameException.class) + @Test public void testToStringBadCharset() { final List fields = new ArrayList<>(); fields.add(new RecordField("id", RecordFieldType.INT.getDataType())); @@ -1581,7 +1576,9 @@ public class TestRecordPath { values.put("bytes", "Hello World!".getBytes(StandardCharsets.UTF_16)); 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 @@ -1601,7 +1598,7 @@ public class TestRecordPath { (byte[]) RecordPath.compile("toBytes(/s, \"UTF-16LE\")").evaluate(record).getSelectedFields().findFirst().get().getValue()); } - @Test(expected = IllegalCharsetNameException.class) + @Test public void testToBytesBadCharset() { final List fields = new ArrayList<>(); fields.add(new RecordField("id", RecordFieldType.INT.getDataType())); @@ -1614,7 +1611,8 @@ public class TestRecordPath { values.put("s", "Hello World!"); 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 @@ -1796,21 +1794,20 @@ public class TestRecordPath { // test 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(); - fail("Expected a RecordPathException for invalid JSON"); - } catch (RecordPathException rpe) { - assertEquals("Unable to deserialise JSON String into Record Path value", rpe.getMessage()); - } + + RecordPathException rpe = assertThrows(RecordPathException.class, + () -> RecordPath.compile("unescapeJson(/json_str)") + .evaluate(recordInvalidJson).getSelectedFields() + .findFirst().orElseThrow(IllegalStateException::new).getValue()); + assertEquals("Unable to deserialise JSON String into Record Path value", rpe.getMessage()); // test not String final Record recordNotString = new MapRecord(schema, Collections.singletonMap("person", new MapRecord(person, Collections.singletonMap("age", 30)))); - try { - RecordPath.compile("unescapeJson(/person/age)").evaluate(recordNotString).getSelectedFields().findFirst().orElseThrow(IllegalStateException::new).getValue(); - fail("Expected IllegalArgumentException for non-String input"); - } catch (IllegalArgumentException iae) { - assertEquals("Argument supplied to unescapeJson must be a String", iae.getMessage()); - } + IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, + () -> RecordPath.compile("unescapeJson(/person/age)") + .evaluate(recordNotString).getSelectedFields() + .findFirst().orElseThrow(IllegalStateException::new).getValue()); + assertEquals("Argument supplied to unescapeJson must be a String", iae.getMessage()); } @Test @@ -1820,10 +1817,11 @@ public class TestRecordPath { assertEquals("5753a498f025464d72e088a9d5d6e872592d5f91", RecordPath.compile("hash(/firstName, 'SHA-1')").evaluate(record).getSelectedFields().findFirst().get().getValue()); } - @Test(expected = RecordPathException.class) + @Test public void testHashFailure() { 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 diff --git a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/AbstractWalkerTest.java b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/AbstractWalkerTest.java index 6b1472e56f..d821525b18 100644 --- a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/AbstractWalkerTest.java +++ b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/AbstractWalkerTest.java @@ -16,13 +16,6 @@ */ 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.serialization.SimpleRecordSchema; 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.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. */ diff --git a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueLogicalPathBuilder.java b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueLogicalPathBuilder.java index 6860419f3d..5ad433abe2 100644 --- a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueLogicalPathBuilder.java +++ b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueLogicalPathBuilder.java @@ -16,15 +16,16 @@ */ 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.stream.Collectors; -import org.apache.nifi.record.path.FieldValue; -import org.apache.nifi.record.path.RecordPath; -import org.apache.nifi.serialization.record.Record; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; public class TestFieldValueLogicalPathBuilder extends AbstractWalkerTest { @@ -86,9 +87,9 @@ public class TestFieldValueLogicalPathBuilder extends AbstractWalkerTest { } } - @Test(expected = NullPointerException.class) + @Test public void buildLogicalPathBuilderFailsNull() { final FieldValueLogicalPathBuilder builder = getDefaultLogicalPathBuilder(); - builder.buildLogicalPath(null); + assertThrows(NullPointerException.class, () -> builder.buildLogicalPath(null)); } } \ No newline at end of file diff --git a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueWalker.java b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueWalker.java index 8fde8463b9..948ab8da9d 100644 --- a/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueWalker.java +++ b/nifi-commons/nifi-record-path/src/test/java/org/apache/nifi/record/path/util/TestFieldValueWalker.java @@ -16,15 +16,15 @@ */ 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.stream.Collectors; -import org.apache.nifi.record.path.FieldValue; -import org.apache.nifi.record.path.RecordPath; -import org.apache.nifi.serialization.record.Record; -import org.junit.Test; +import static org.junit.Assert.assertEquals; public class TestFieldValueWalker extends AbstractWalkerTest { diff --git a/nifi-commons/nifi-record/pom.xml b/nifi-commons/nifi-record/pom.xml index 866684d9c6..b675b69e9a 100644 --- a/nifi-commons/nifi-record/pom.xml +++ b/nifi-commons/nifi-record/pom.xml @@ -27,5 +27,4 @@ several interfaces for interacting with Records. This module should not depend on any external libraries. - diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java index c2b2d434b5..18fd711622 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java @@ -20,8 +20,7 @@ package org.apache.nifi.serialization; import org.apache.nifi.serialization.record.RecordField; import org.apache.nifi.serialization.record.RecordFieldType; import org.apache.nifi.serialization.record.SchemaIdentifier; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.util.ArrayList; @@ -31,6 +30,7 @@ import java.util.List; import java.util.Set; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; 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("goodbye", RecordFieldType.STRING.getDataType(), null, set("baz", "bar"))); - try { - new SimpleRecordSchema(fields); - Assert.fail("Was able to create two fields with same alias"); - } catch (final IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields)); } @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())); - try { - new SimpleRecordSchema(fields); - Assert.fail("Was able to create two fields with same name"); - } catch (final IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields)); } @Test @@ -66,11 +58,7 @@ public class TestSimpleRecordSchema { fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar"))); fields.add(new RecordField("bar", RecordFieldType.STRING.getDataType())); - try { - new SimpleRecordSchema(fields); - Assert.fail("Was able to create two fields with conflicting names/aliases"); - } catch (final IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> new SimpleRecordSchema(fields)); } @Test diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/ResultSetRecordSetTest.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/ResultSetRecordSetTest.java index 10fd23f6c2..8617fd5fef 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/ResultSetRecordSetTest.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/ResultSetRecordSetTest.java @@ -19,13 +19,15 @@ package org.apache.nifi.serialization.record; import org.apache.nifi.serialization.SimpleRecordSchema; import org.apache.nifi.serialization.record.type.ArrayDataType; import org.apache.nifi.serialization.record.type.DecimalDataType; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; import org.mockito.Mock; 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.sql.Array; @@ -54,7 +56,8 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) public class ResultSetRecordSetTest { private static final String COLUMN_NAME_VARCHAR = "varchar"; @@ -105,7 +108,7 @@ public class ResultSetRecordSetTest { @Mock private ResultSetMetaData resultSetMetaData; - @Before + @BeforeEach public void setUp() throws SQLException { setUpMocks(COLUMNS, resultSetMetaData, resultSet); } diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java index 2115e67523..21ec9199ef 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java @@ -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.util.DataTypeUtils; 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.BigInteger; @@ -55,11 +55,12 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; -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.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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestDataTypeUtils { private static final ZoneId SYSTEM_DEFAULT_ZONE_ID = ZoneOffset.systemDefault(); @@ -82,12 +83,12 @@ public class TestDataTypeUtils { Timestamp ts = DataTypeUtils.toTimestamp(date, null, null); 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()); ts = DataTypeUtils.toTimestamp(date, null, null); 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); assertNotNull(output); - assertEquals("Timestamps didn't match", output.getTime(), ts.getTime()); + assertEquals(output.getTime(), ts.getTime(), "Timestamps didn't match"); } @Test @@ -266,11 +267,11 @@ public class TestDataTypeUtils { assertTrue(bytes instanceof Byte[]); assertNotNull(bytes); Byte[] b = (Byte[]) bytes; - assertEquals("Conversion from String to byte[] failed", (long) 72, (long) b[0] ); // H - assertEquals("Conversion from String to byte[] failed", (long) 101, (long) b[1] ); // e - assertEquals("Conversion from String to byte[] failed", (long) 108, (long) b[2] ); // l - assertEquals("Conversion from String to byte[] failed", (long) 108, (long) b[3] ); // l - assertEquals("Conversion from String to byte[] failed", (long) 111, (long) b[4] ); // o + assertEquals((long) 72, (long) b[0], "Conversion from String to byte[] failed"); // H + assertEquals((long) 101, (long) b[1], "Conversion from String to byte[] failed" ); // e + assertEquals((long) 108, (long) b[2], "Conversion from String to byte[] failed" ); // l + assertEquals((long) 108, (long) b[3], "Conversion from String to byte[] failed" ); // l + assertEquals((long) 111, (long) b[4], "Conversion from String to byte[] failed" ); // o } @Test @@ -278,7 +279,7 @@ public class TestDataTypeUtils { Object s = DataTypeUtils.convertType("Hello".getBytes(StandardCharsets.UTF_16), RecordFieldType.STRING.getDataType(),null, StandardCharsets.UTF_16); assertNotNull(s); assertTrue(s instanceof String); - assertEquals("Conversion from byte[] to String failed", "Hello", s); + assertEquals("Hello", s, "Conversion from byte[] to String failed"); } @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); assertNotNull(b); 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 @@ -314,19 +315,22 @@ public class TestDataTypeUtils { assertNull(DataTypeUtils.convertType(null, RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8)); } - @Test(expected = IllegalTypeConversionException.class) + @Test 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() { - DataTypeUtils.convertType(new ArrayList(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10), null, StandardCharsets.UTF_8); + assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType(new ArrayList(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10), + null, StandardCharsets.UTF_8)); } - @Test(expected = IllegalTypeConversionException.class) + @Test 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 @@ -413,16 +417,16 @@ public class TestDataTypeUtils { for (final String decimal : decimals) { for (final String exp : exponents) { String toTest = prefix + "100" + decimal + exp; - assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest)); - assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest)); + assertTrue(DataTypeUtils.isFloatTypeCompatible(toTest), toTest + " not valid float"); + assertTrue(DataTypeUtils.isDoubleTypeCompatible(toTest), toTest + " not valid double"); Double.parseDouble(toTest); // ensure we can actually parse it Float.parseFloat(toTest); if (decimal.length() > 1) { toTest = prefix + decimal + exp; - assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest)); - assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest)); + assertTrue(DataTypeUtils.isFloatTypeCompatible(toTest), toTest + " not valid float"); + assertTrue(DataTypeUtils.isDoubleTypeCompatible(toTest), toTest + " not valid double"); Double.parseDouble(toTest); // ensure we can actually parse it Float.parseFloat(toTest); } @@ -532,7 +536,7 @@ public class TestDataTypeUtils { }; Optional 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); } @@ -753,11 +757,11 @@ public class TestDataTypeUtils { 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 // 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 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 public void testConvertTypeStringToDateDefaultTimeZoneFormat() { 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()); } @@ -958,7 +962,7 @@ public class TestDataTypeUtils { public void testConvertTypeStringToDateConfiguredTimeZoneFormat() { 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"); - 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()); } @@ -969,7 +973,7 @@ public class TestDataTypeUtils { public void testConvertTypeStringToDateConfiguredSystemDefaultTimeZoneFormat() { 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"); - 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()); } @@ -1006,6 +1010,6 @@ public class TestDataTypeUtils { private void assertToLocalDateEquals(final String expected, final Object value) { final DateTimeFormatter systemDefaultZoneFormatter = DataTypeUtils.getDateTimeFormatter(RecordFieldType.DATE.getDefaultFormat(), SYSTEM_DEFAULT_ZONE_ID); 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())); } } diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestMapRecord.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestMapRecord.java index 82e20a6143..a4a6ab8060 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestMapRecord.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestMapRecord.java @@ -17,8 +17,8 @@ package org.apache.nifi.serialization.record; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import org.apache.nifi.serialization.SimpleRecordSchema; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.HashMap; @@ -27,9 +27,9 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.apache.nifi.serialization.SimpleRecordSchema; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class TestMapRecord { @@ -71,12 +71,7 @@ public class TestMapRecord { new RecordField("hello", RecordFieldType.INT.getDataType(), 84); new RecordField("hello", RecordFieldType.INT.getDataType(), (Object) null); - try { - 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 - } + assertThrows(IllegalArgumentException.class, () -> new RecordField("hello", RecordFieldType.INT.getDataType(), "foo")); } private Set set(final String... values) { diff --git a/nifi-commons/nifi-rocksdb-utils/src/test/java/org/apache/nifi/rocksdb/TestRocksDBMetronome.java b/nifi-commons/nifi-rocksdb-utils/src/test/java/org/apache/nifi/rocksdb/TestRocksDBMetronome.java index 402f099887..b9405ce5b0 100644 --- a/nifi-commons/nifi-rocksdb-utils/src/test/java/org/apache/nifi/rocksdb/TestRocksDBMetronome.java +++ b/nifi-commons/nifi-rocksdb-utils/src/test/java/org/apache/nifi/rocksdb/TestRocksDBMetronome.java @@ -16,18 +16,19 @@ */ package org.apache.nifi.rocksdb; -import org.apache.commons.lang3.SystemUtils; -import org.junit.After; -import org.junit.Assume; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; import org.rocksdb.ColumnFamilyHandle; import org.rocksdb.RocksIterator; +import java.io.File; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; 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.assertNull; 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 { - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static final byte[] KEY = "key".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); @@ -56,17 +55,12 @@ public class TestRocksDBMetronome { private ExecutorService executor; - @BeforeClass - public static void setupClass() { - Assume.assumeTrue("Test only runs on *nix", !SystemUtils.IS_OS_WINDOWS); - } - - @Before + @BeforeEach public void before() { executor = Executors.newSingleThreadExecutor(); } - @After + @AfterEach public void after() { 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 - public void testPutGetDelete() throws Exception { + public void testPutGetDelete(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .build()) { db.initialize(); @@ -122,9 +122,9 @@ public class TestRocksDBMetronome { } @Test - public void testPutGetConfiguration() throws Exception { + public void testPutGetConfiguration(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .build()) { db.initialize(); @@ -136,32 +136,36 @@ public class TestRocksDBMetronome { } } - @Test(expected = IllegalStateException.class) - public void testPutBeforeInit() throws Exception { - try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + @Test + public void testPutBeforeInit(@TempDir Path temporaryFolder) throws Exception { + assertThrows(IllegalStateException.class, () -> { + try (RocksDBMetronome db = new RocksDBMetronome.Builder() + .setStoragePath(newFolder(temporaryFolder)) .build()) { - 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); - } + db.put(KEY, VALUE); + } + }); } @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"; try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .addColumnFamily(secondFamilyName) .build()) { db.initialize(); @@ -210,9 +214,9 @@ public class TestRocksDBMetronome { } @Test - public void testIterator() throws Exception { + public void testIterator(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .build()) { db.initialize(); @@ -236,9 +240,9 @@ public class TestRocksDBMetronome { } @Test - public void testCounterIncrement() throws Exception { + public void testCounterIncrement(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .build()) { db.initialize(); @@ -254,10 +258,11 @@ public class TestRocksDBMetronome { } } - @Test(timeout = 10_000) - public void testWaitForSync() throws Exception { + @Test + @Timeout(unit = TimeUnit.MILLISECONDS, value = 10_000) + public void testWaitForSync(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .build()) { db.initialize(); @@ -285,10 +290,11 @@ public class TestRocksDBMetronome { } } - @Test(timeout = 10_000) - public void testWaitForSyncWithValue() throws Exception { + @Test + @Timeout(unit = TimeUnit.MILLISECONDS, value = 10_000) + public void testWaitForSyncWithValue(@TempDir Path temporaryFolder) throws Exception { try (RocksDBMetronome db = new RocksDBMetronome.Builder() - .setStoragePath(temporaryFolder.newFolder().toPath()) + .setStoragePath(newFolder(temporaryFolder)) .setSyncMillis(Long.MAX_VALUE) // effectively disable the auto-sync .build()) { 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 future = getWaitForSyncFuture(db, counterValue); - try { - future.get(1, TimeUnit.SECONDS); - fail(); - } catch (TimeoutException expected) { - assertFalse(future.isDone()); - } + assertThrows(TimeoutException.class, () -> future.get(1, TimeUnit.SECONDS)); + assertFalse(future.isDone()); future.cancel(true); } diff --git a/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReader.java b/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReader.java index d5a4cfdf86..65b2736246 100644 --- a/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReader.java +++ b/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReader.java @@ -17,11 +17,7 @@ package org.apache.nifi.repository.schema; -import static org.junit.Assert.assertEquals; -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 org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -34,7 +30,11 @@ import java.util.HashMap; import java.util.List; 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 { diff --git a/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReaderWriter.java b/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReaderWriter.java index 5dc5cd832a..c177b61c30 100644 --- a/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReaderWriter.java +++ b/nifi-commons/nifi-schema-utils/src/test/java/org/apache/nifi/repository/schema/TestSchemaRecordReaderWriter.java @@ -17,11 +17,8 @@ package org.apache.nifi.repository.schema; -import static org.apache.nifi.repository.schema.SchemaRecordWriter.MAX_ALLOWED_UTF_LENGTH; -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 org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -35,8 +32,11 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import static org.apache.nifi.repository.schema.SchemaRecordWriter.MAX_ALLOWED_UTF_LENGTH; +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 { @@ -237,69 +237,69 @@ public class TestSchemaRecordReaderWriter { @Test public void testSingleCharUTF8Lengths() { // 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("test 2 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 0)); - assertEquals("test 3 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 0)); - assertEquals("test 1 char string truncated to 1 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 1)); - assertEquals("test 2 char string truncated to 1 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 1)); - assertEquals("test 3 char string truncated to 1 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 1)); - assertEquals("test 1 char string truncated to 2 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 2)); - assertEquals("test 2 char string truncated to 2 utf bytes should be 2", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 2)); - assertEquals("test 3 char string truncated to 2 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 2)); - assertEquals("test 1 char string truncated to 3 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 3)); - assertEquals("test 2 char string truncated to 3 utf bytes should be 2", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 3)); - assertEquals("test 3 char string truncated to 3 utf bytes should be 3", 1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 3)); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 0), "test 1 char string truncated to 0 utf bytes should be 0"); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 0), "test 2 char string truncated to 0 utf bytes should be 0"); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 0), "test 3 char string truncated to 0 utf bytes should be 0"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 1), "test 1 char string truncated to 1 utf bytes should be 1"); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 1), "test 2 char string truncated to 1 utf bytes should be 0"); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 1), "test 3 char string truncated to 1 utf bytes should be 0"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 2), "test 1 char string truncated to 2 utf bytes should be 1"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 2), "test 2 char string truncated to 2 utf bytes should be 2"); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 2), "test 3 char string truncated to 2 utf bytes should be 0"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringOneByte, 3), "test 1 char string truncated to 3 utf bytes should be 1"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringTwoByte, 3), "test 2 char string truncated to 3 utf bytes should be 2"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(utfStringThreeByte, 3), "test 3 char string truncated to 3 utf bytes should be 3"); } @Test public void testMultiCharUTFLengths() { // 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' - assertEquals("test 6 char string truncated to 0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 0)); // utf '' - assertEquals("test 6 char string truncated to 1 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 1)); // utf 'a' - assertEquals("test 6 char string truncated to 2 utf bytes should be 1", 1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 2)); // utf 'a' - assertEquals("test 6 char string truncated to 3 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 3)); // utf 'abb' - assertEquals("test 6 char string truncated to 4 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 4)); // utf 'abb' - assertEquals("test 6 char string truncated to 5 utf bytes should be 2", 2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 5)); // utf 'abb' - assertEquals("test 6 char string truncated to 6 utf bytes should be 3", 3, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 6)); // utf 'abbccc' + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 0), "test 6 char string truncated to 0 utf bytes should be 0"); // utf '' + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 1), "test 6 char string truncated to 1 utf bytes should be 1"); // utf 'a' + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 2), "test 6 char string truncated to 2 utf bytes should be 1"); // utf 'a' + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 3), "test 6 char string truncated to 3 utf bytes should be 2"); // utf 'abb' + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 4), "test 6 char string truncated to 4 utf bytes should be 2"); // utf 'abb' + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 5), "test 6 char string truncated to 5 utf bytes should be 2"); // utf 'abb' + assertEquals(3, SchemaRecordWriter.getCharsInUTF8Limit(testString1, 6), "test 6 char string truncated to 6 utf bytes should be 3"); // utf 'abbccc' } @Test public void testSmallCharUTFLengths() throws UnsupportedEncodingException { 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("test multi-char string truncated to 1 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 1)); - assertEquals("test multi-char string truncated to 2 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 2)); - assertEquals("test multi-char string truncated to 3 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 3)); - assertEquals("test multi-char string truncated to 4 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 4)); - assertEquals("test multi-char string truncated to 5 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 5)); - assertEquals("test multi-char string truncated to 6 utf bytes should be 0", 3, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 6)); - assertEquals("test multi-char string truncated to 7 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 7)); - assertEquals("test multi-char string truncated to 8 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 8)); - assertEquals("test multi-char string truncated to 9 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 9)); - assertEquals("test multi-char string truncated to 10 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 10)); - assertEquals("test multi-char string truncated to 11 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 11)); - assertEquals("test multi-char string truncated to 12 utf bytes should be 0", 6, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 12)); + assertEquals(0, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 0), "test multi-char string truncated to 0 utf bytes should be 0"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 1), "test multi-char string truncated to 1 utf bytes should be 0"); + assertEquals(1, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 2), "test multi-char string truncated to 2 utf bytes should be 0"); + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 3), "test multi-char string truncated to 3 utf bytes should be 0"); + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 4), "test multi-char string truncated to 4 utf bytes should be 0"); + assertEquals(2, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 5), "test multi-char string truncated to 5 utf bytes should be 0"); + assertEquals(3, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 6), "test multi-char string truncated to 6 utf bytes should be 0"); + assertEquals(4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 7), "test multi-char string truncated to 7 utf bytes should be 0"); + assertEquals(4, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 8), "test multi-char string truncated to 8 utf bytes should be 0"); + assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 9), "test multi-char string truncated to 9 utf bytes should be 0"); + assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 10), "test multi-char string truncated to 10 utf bytes should be 0"); + assertEquals(5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 11), "test multi-char string truncated to 11 utf bytes should be 0"); + assertEquals(6, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 12), "test multi-char string truncated to 12 utf bytes should be 0"); } @Test public void testLargeCharUTFLengths() { 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 - 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 - assertEquals("test 64k char string truncated to 65,534 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65534)); // lost 2 byte char - assertEquals("test 64k char string truncated to 65,533 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65533)); - assertEquals("test 64k char string truncated to 65,532 utf bytes should be 32766", 32766, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65532)); // 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("test 64k char string truncated to 65,530 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65530)); - assertEquals("test 64k char string truncated to 65,529 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65529)); - 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(32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65534), "test 64k char string truncated to 65,534 utf bytes should be 32767"); // lost 2 byte char + assertEquals(32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65533), "test 64k char string truncated to 65,533 utf bytes should be 32767"); + assertEquals(32766, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65532), "test 64k char string truncated to 65,532 utf bytes should be 32766"); // lost 1 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(32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65530), "test 64k char string truncated to 65,530 utf bytes should be 32765"); + assertEquals(32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65529), "test 64k char string truncated to 65,529 utf bytes should be 32765"); + 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) { diff --git a/nifi-commons/nifi-security-kerberos/src/test/java/org/apache/nifi/security/krb/KerberosUserIT.java b/nifi-commons/nifi-security-kerberos/src/test/java/org/apache/nifi/security/krb/KerberosUserIT.java index 5f8c457631..a7a3cb1032 100644 --- a/nifi-commons/nifi-security-kerberos/src/test/java/org/apache/nifi/security/krb/KerberosUserIT.java +++ b/nifi-commons/nifi-security-kerberos/src/test/java/org/apache/nifi/security/krb/KerberosUserIT.java @@ -18,10 +18,9 @@ package org.apache.nifi.security.krb; import org.apache.nifi.logging.ComponentLog; import org.apache.nifi.processor.ProcessContext; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; 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.login.LoginException; import java.io.File; +import java.nio.file.Path; import java.security.AccessControlContext; import java.security.AccessController; 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.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class KerberosUserIT { - - @ClassRule - public static TemporaryFolder tmpDir = new TemporaryFolder(); - private static KDCServer kdc; private static KerberosPrincipal principal1; @@ -59,18 +55,21 @@ public class KerberosUserIT { private static KerberosPrincipal principal3; private static final String principal3Password = "changeme"; - @BeforeClass - public static void setupClass() throws Exception { - kdc = new KDCServer(tmpDir.newFolder("mini-kdc_")); + @BeforeAll + public static void setupClass(@TempDir Path tmpDir) throws Exception { + 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.start(); principal1 = new KerberosPrincipal("user1@" + kdc.getRealm()); - principal1KeytabFile = tmpDir.newFile("user1.keytab"); + principal1KeytabFile = tmpDir.resolve("user1.keytab").toFile(); kdc.createKeytabPrincipal(principal1KeytabFile, "user1"); principal2 = new KerberosPrincipal("user2@" + kdc.getRealm()); - principal2KeytabFile = tmpDir.newFile("user2.keytab"); + principal2KeytabFile = tmpDir.resolve("user2.keytab").toFile(); kdc.createKeytabPrincipal(principal2KeytabFile, "user2"); principal3 = new KerberosPrincipal("user3@" + kdc.getRealm()); @@ -113,16 +112,10 @@ public class KerberosUserIT { } @Test - public void testKeytabLoginWithUnknownPrincipal() throws LoginException { + public void testKeytabLoginWithUnknownPrincipal() { final String unknownPrincipal = "doesnotexist@" + kdc.getRealm(); final KerberosUser user1 = new KerberosKeytabUser(unknownPrincipal, principal1KeytabFile.getAbsolutePath()); - try { - user1.login(); - fail("Login should have failed"); - } catch (Exception e) { - // exception is expected here - //e.printStackTrace(); - } + assertThrows(Exception.class, () -> user1.login()); } @Test @@ -147,11 +140,11 @@ public class KerberosUserIT { assertEquals(0, userSubject.getPrincipals().size()); } - @Test(expected = LoginException.class) - public void testPasswordUserLoginWithInvalidPassword() throws LoginException { + @Test + public void testPasswordUserLoginWithInvalidPassword() { // perform login for user final KerberosUser user = new KerberosPasswordUser("user3", "NOT THE PASSWORD"); - user.login(); + assertThrows(LoginException.class, () -> user.login()); } @Test diff --git a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/FileBasedKeyProviderTest.java b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/FileBasedKeyProviderTest.java index 09daf8afe6..56f920c383 100644 --- a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/FileBasedKeyProviderTest.java +++ b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/FileBasedKeyProviderTest.java @@ -17,7 +17,7 @@ package org.apache.nifi.security.kms; import org.apache.nifi.security.kms.util.SecretKeyUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import java.io.FileOutputStream; @@ -31,9 +31,9 @@ import java.util.Map; import java.util.Properties; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +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 FileBasedKeyProviderTest { private static final String KEYS_EXTENSION = ".keys"; diff --git a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyProviderFactoryTest.java b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyProviderFactoryTest.java index bf3ea04a15..72d7fab13c 100644 --- a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyProviderFactoryTest.java +++ b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyProviderFactoryTest.java @@ -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.StaticKeyProviderConfiguration; import org.apache.nifi.security.kms.util.SecretKeyUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import java.io.File; @@ -32,8 +32,8 @@ import java.security.KeyStore; import java.util.Collections; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class KeyProviderFactoryTest { diff --git a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyStoreKeyProviderTest.java b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyStoreKeyProviderTest.java index 5210ad5a2c..83516f1ddc 100644 --- a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyStoreKeyProviderTest.java +++ b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/KeyStoreKeyProviderTest.java @@ -17,8 +17,8 @@ package org.apache.nifi.security.kms; import org.apache.nifi.security.kms.util.SecretKeyUtils; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import java.io.IOException; @@ -28,9 +28,9 @@ import java.security.KeyStore; import java.util.List; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class KeyStoreKeyProviderTest { private static final String KEY_ID = UUID.randomUUID().toString(); @@ -43,7 +43,7 @@ public class KeyStoreKeyProviderTest { private static KeyStore keyStore; - @BeforeClass + @BeforeAll public static void setKeyStore() throws GeneralSecurityException, IOException { keyStore = getKeyStore(); } diff --git a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/StaticKeyProviderTest.java b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/StaticKeyProviderTest.java index 3f1aaad355..278e6b373d 100644 --- a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/StaticKeyProviderTest.java +++ b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/StaticKeyProviderTest.java @@ -17,7 +17,7 @@ package org.apache.nifi.security.kms; import org.apache.nifi.security.kms.util.SecretKeyUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import java.security.KeyManagementException; @@ -25,9 +25,9 @@ import java.util.Collections; import java.util.List; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class StaticKeyProviderTest { private static final String KEY_ID = UUID.randomUUID().toString(); diff --git a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/reader/StandardFileBasedKeyReaderTest.java b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/reader/StandardFileBasedKeyReaderTest.java index cd0f9e720a..4a6eeccbcf 100644 --- a/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/reader/StandardFileBasedKeyReaderTest.java +++ b/nifi-commons/nifi-security-kms/src/test/java/org/apache/nifi/security/kms/reader/StandardFileBasedKeyReaderTest.java @@ -17,7 +17,7 @@ package org.apache.nifi.security.kms.reader; import org.apache.nifi.security.kms.util.SecretKeyUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.crypto.SecretKey; import java.io.FileOutputStream; @@ -28,7 +28,7 @@ import java.util.Collections; import java.util.Map; import java.util.Properties; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class StandardFileBasedKeyReaderTest { 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 Map secretKeys = reader.readSecretKeys(path, ROOT_KEY); 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 secretKeys) throws Exception { diff --git a/nifi-commons/nifi-security-socket-ssl/src/test/java/org/apache/nifi/remote/io/socket/ssl/SSLSocketChannelTest.java b/nifi-commons/nifi-security-socket-ssl/src/test/java/org/apache/nifi/remote/io/socket/ssl/SSLSocketChannelTest.java index cb159597bd..491e282865 100644 --- a/nifi-commons/nifi-security-socket-ssl/src/test/java/org/apache/nifi/remote/io/socket/ssl/SSLSocketChannelTest.java +++ b/nifi-commons/nifi-security-socket-ssl/src/test/java/org/apache/nifi/remote/io/socket/ssl/SSLSocketChannelTest.java @@ -170,12 +170,14 @@ public class SSLSocketChannelTest { } @Test + @Timeout(value = CHANNEL_TIMEOUT, unit = TimeUnit.MILLISECONDS) public void testServerReadWriteTls12() throws Exception { assertServerChannelConnectedReadClosed(TLS_1_2); } @EnabledIf(TLS_1_3_SUPPORTED) @Test + @Timeout(value = CHANNEL_TIMEOUT, unit = TimeUnit.MILLISECONDS) public void testServerReadWriteTls13() throws Exception { assertServerChannelConnectedReadClosed(TLS_1_3); } diff --git a/nifi-commons/nifi-security-utils-api/src/test/groovy/org/apache/nifi/security/util/TlsConfigurationTest.groovy b/nifi-commons/nifi-security-utils-api/src/test/groovy/org/apache/nifi/security/util/TlsConfigurationTest.groovy index 88e95241f9..0881251573 100644 --- a/nifi-commons/nifi-security-utils-api/src/test/groovy/org/apache/nifi/security/util/TlsConfigurationTest.groovy +++ b/nifi-commons/nifi-security-utils-api/src/test/groovy/org/apache/nifi/security/util/TlsConfigurationTest.groovy @@ -16,34 +16,30 @@ */ package org.apache.nifi.security.util - -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 +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory -@RunWith(JUnit4.class) class TlsConfigurationTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(TlsConfigurationTest.class) - @BeforeClass + @BeforeAll static void setUpOnce() { logger.metaClass.methodMissing = { String name, args -> logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") } } - @Before + @BeforeEach void setUp() { super.setUp() } - @After + @AfterEach void tearDown() { } diff --git a/nifi-commons/nifi-security-utils-api/src/test/java/org/apache/nifi/security/util/TlsPlatformTest.java b/nifi-commons/nifi-security-utils-api/src/test/java/org/apache/nifi/security/util/TlsPlatformTest.java index 576c199946..ff413c097e 100644 --- a/nifi-commons/nifi-security-utils-api/src/test/java/org/apache/nifi/security/util/TlsPlatformTest.java +++ b/nifi-commons/nifi-security-utils-api/src/test/java/org/apache/nifi/security/util/TlsPlatformTest.java @@ -16,16 +16,16 @@ */ package org.apache.nifi.security.util; -import static org.junit.Assert.assertEquals; -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 org.junit.jupiter.api.Test; 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 { private static final int ZERO_LENGTH = 0; diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/CertificateUtilsTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/CertificateUtilsTest.groovy index 6155a9ad4d..786e140507 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/CertificateUtilsTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/CertificateUtilsTest.groovy @@ -20,22 +20,14 @@ import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers import org.bouncycastle.asn1.x500.X500Name import org.bouncycastle.asn1.x500.style.BCStyle import org.bouncycastle.asn1.x500.style.IETFUtils -import org.bouncycastle.asn1.x509.Extension -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.asn1.x509.* import org.bouncycastle.operator.OperatorCreationException import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder import org.bouncycastle.util.IPAddress -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -43,27 +35,15 @@ import javax.net.ssl.SSLException import javax.net.ssl.SSLPeerUnverifiedException import javax.net.ssl.SSLSession import javax.net.ssl.SSLSocket -import java.security.InvalidKeyException -import java.security.KeyPair -import java.security.KeyPairGenerator -import java.security.NoSuchAlgorithmException -import java.security.NoSuchProviderException -import java.security.SignatureException +import java.security.* import java.security.cert.Certificate import java.security.cert.CertificateException import java.security.cert.X509Certificate -import java.util.concurrent.Callable -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.* import java.util.concurrent.atomic.AtomicBoolean -import static org.junit.Assert.assertEquals import static org.junit.Assert.assertTrue -@RunWith(JUnit4.class) class CertificateUtilsTest extends GroovyTestCase { 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 List SUBJECT_ALT_NAMES = ["127.0.0.1", "nifi.nifi.apache.org"] - @BeforeClass + @BeforeAll static void setUpOnce() { logger.metaClass.methodMissing = { String name, args -> 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. * diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderGroovyTest.groovy index 35d1464a8e..1ebc1f5813 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderGroovyTest.groovy @@ -19,13 +19,8 @@ package org.apache.nifi.security.util.crypto import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -36,8 +31,8 @@ import java.security.SecureRandom import java.security.Security import static groovy.test.GroovyAssert.shouldFail +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class AESKeyedCipherProviderGroovyTest { 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") - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -58,14 +53,6 @@ class AESKeyedCipherProviderGroovyTest { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - } - private static boolean isUnlimitedStrengthCryptoAvailable() { Cipher.getMaxAllowedKeyLength("AES") > 128 } @@ -127,7 +114,7 @@ class AESKeyedCipherProviderGroovyTest { @Test void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { // 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() final List LONG_KEY_LENGTHS = [192, 256] diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2CipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2CipherProviderGroovyTest.groovy index 79c92d42ca..1bfb0313dc 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2CipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2CipherProviderGroovyTest.groovy @@ -20,13 +20,10 @@ import org.apache.commons.codec.binary.Base64 import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.Assumptions +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -38,7 +35,6 @@ import java.security.Security import static groovy.test.GroovyAssert.shouldFail -@RunWith(JUnit4.class) class Argon2CipherProviderGroovyTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(Argon2CipherProviderGroovyTest.class) @@ -53,7 +49,7 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase { RandomIVPBECipherProvider cipherProvider private final IntRange FULL_SALT_LENGTH_RANGE= (49..53) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -70,17 +66,12 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase { } } - @Before + @BeforeEach void setUp() throws Exception { // Very fast parameters to test for correctness rather than production values cipherProvider = new Argon2CipherProvider(1024, 1, 3) } - @After - void tearDown() throws Exception { - - } - @Test void testGetCipherShouldBeInternallyConsistent() throws Exception { // Arrange @@ -234,8 +225,8 @@ class Argon2CipherProviderGroovyTest extends GroovyTestCase { @Test void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { // Arrange - Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", - CipherUtility.isUnlimitedStrengthCryptoSupported()) + Assumptions.assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(), + "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.") final String PASSWORD = "shortPassword" final byte[] SALT = cipherProvider.generateSalt() diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2SecureHasherTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2SecureHasherTest.groovy index 7c802815ef..05ee9fb016 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2SecureHasherTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/Argon2SecureHasherTest.groovy @@ -18,22 +18,20 @@ package org.apache.nifi.security.util.crypto import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.util.encoders.Hex -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +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.security.Security -@RunWith(JUnit4.class) class Argon2SecureHasherTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(Argon2SecureHasherTest.class) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -46,32 +44,6 @@ class Argon2SecureHasherTest extends GroovyTestCase { 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 void testShouldBeDeterministicWithStaticSalt() { // Arrange @@ -297,7 +269,7 @@ class Argon2SecureHasherTest extends GroovyTestCase { * This test can have the minimum time threshold updated to determine if the performance * is still sufficient compared to the existing threat model. */ - @Ignore("Long running test") + @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true") @Test void testDefaultCostParamsShouldBeSufficient() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptCipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptCipherProviderGroovyTest.groovy index 4968d3bafe..8d28e7709d 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptCipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptCipherProviderGroovyTest.groovy @@ -21,14 +21,9 @@ import at.favre.lib.crypto.bcrypt.Radix64Encoder import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -41,8 +36,8 @@ import java.security.Security import static groovy.test.GroovyAssert.shouldFail import static org.junit.Assert.assertTrue +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class BcryptCipherProviderGroovyTest { private static final Logger logger = LoggerFactory.getLogger(BcryptCipherProviderGroovyTest.class) @@ -54,7 +49,7 @@ class BcryptCipherProviderGroovyTest { public static final String MICROBENCHMARK = "microbenchmark" private static ArrayList AES_KEY_LENGTHS - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -71,15 +66,6 @@ class BcryptCipherProviderGroovyTest { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - - } - @Test void testGetCipherShouldBeInternallyConsistent() throws Exception { // Arrange @@ -143,8 +129,8 @@ class BcryptCipherProviderGroovyTest { @Test void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { // Arrange - Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", - CipherUtility.isUnlimitedStrengthCryptoSupported()) + assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(), + "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.") RandomIVPBECipherProvider cipherProvider = new BcryptCipherProvider(4) @@ -610,7 +596,7 @@ class BcryptCipherProviderGroovyTest { 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 void testDefaultConstructorShouldProvideStrongWorkFactor() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptSecureHasherTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptSecureHasherTest.groovy index b01a17be2e..30dbb7257c 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptSecureHasherTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/BcryptSecureHasherTest.groovy @@ -18,37 +18,25 @@ package org.apache.nifi.security.util.crypto import at.favre.lib.crypto.bcrypt.Radix64Encoder import org.bouncycastle.util.encoders.Hex -import org.junit.After -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +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 -@RunWith(JUnit4.class) class BcryptSecureHasherTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(BcryptSecureHasher) - @BeforeClass + @BeforeAll static void setupOnce() throws Exception { logger.metaClass.methodMissing = { String name, args -> logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - } - @Test void testShouldBeDeterministicWithStaticSalt() { // Arrange @@ -263,7 +251,7 @@ class BcryptSecureHasherTest extends GroovyTestCase { * This test can have the minimum time threshold updated to determine if the performance * is still sufficient compared to the existing threat model. */ - @Ignore("Long running test") + @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true") @Test void testDefaultCostParamsShouldBeSufficient() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherProviderFactoryGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherProviderFactoryGroovyTest.groovy index d8e2f1dc0d..d4b0eb35ee 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherProviderFactoryGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherProviderFactoryGroovyTest.groovy @@ -18,19 +18,13 @@ package org.apache.nifi.security.util.crypto import org.apache.nifi.security.util.KeyDerivationFunction import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import java.security.Security -@RunWith(JUnit4.class) class CipherProviderFactoryGroovyTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(CipherProviderFactoryGroovyTest.class) @@ -44,7 +38,7 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase { (KeyDerivationFunction.ARGON2) : Argon2CipherProvider.class ] - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -53,14 +47,6 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - } - @Test void testGetCipherProviderShouldResolveRegisteredKDFs() { // Arrange @@ -75,24 +61,4 @@ class CipherProviderFactoryGroovyTest extends GroovyTestCase { 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}" - } } diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherUtilityGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherUtilityGroovyTest.groovy index bb42a90932..22a5ab55f3 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherUtilityGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/CipherUtilityGroovyTest.groovy @@ -20,18 +20,13 @@ import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.KeyDerivationFunction import org.bouncycastle.jce.provider.BouncyCastleProvider -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import java.security.Security -@RunWith(JUnit4.class) class CipherUtilityGroovyTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(CipherUtilityGroovyTest.class) @@ -76,7 +71,7 @@ class CipherUtilityGroovyTest extends GroovyTestCase { "AES/GCM/NoPadding"] ] - @BeforeClass + @BeforeAll static void setUpOnce() { Security.addProvider(new BouncyCastleProvider()) @@ -87,16 +82,6 @@ class CipherUtilityGroovyTest extends GroovyTestCase { logger.info("Mapped algorithms: ${ALGORITHMS_MAPPED_BY_CIPHER}") } - @Before - void setUp() throws Exception { - - } - - @After - void tearDown() throws Exception { - - } - @Test void testShouldParseCipherFromAlgorithm() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashAlgorithmTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashAlgorithmTest.groovy index 35f76666c7..73591bc48b 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashAlgorithmTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashAlgorithmTest.groovy @@ -18,23 +18,18 @@ package org.apache.nifi.security.util.crypto import org.bouncycastle.jce.provider.BouncyCastleProvider -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import java.security.Security -@RunWith(JUnit4.class) class HashAlgorithmTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(HashAlgorithmTest.class) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -43,14 +38,6 @@ class HashAlgorithmTest extends GroovyTestCase { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - } - @Test void testDetermineBrokenAlgorithms() throws Exception { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashServiceTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashServiceTest.groovy index 147215cc3d..ec1dbd43fc 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashServiceTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/HashServiceTest.groovy @@ -16,18 +16,12 @@ */ package org.apache.nifi.security.util.crypto + import org.apache.nifi.components.AllowableValue import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.util.encoders.Hex -import org.junit.After -import org.junit.AfterClass -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.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -35,14 +29,10 @@ import java.nio.charset.Charset import java.nio.charset.StandardCharsets import java.security.Security -@RunWith(JUnit4.class) class HashServiceTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(HashServiceTest.class) - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder() - - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { 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 void testShouldHashValue() { // Arrange @@ -393,16 +371,12 @@ class HashServiceTest extends GroovyTestCase { // No command-line md2sum tool available def algorithms = HashAlgorithm.values() - HashAlgorithm.MD2 - File inputFile = temporaryFolder.newFile() - - // Generates a file with "apachenifi" 10 times per line for 10_000 lines (11 bytes * 10 * 10_000 ~= 1 MiB) - if (!inputFile.exists() || inputFile.length() == 0) { - inputFile.createNewFile() - 10_000.times { int i -> - inputFile << "${i.toString().padLeft(5)}: ${"apachenifi " * 10}\n" - } + StringBuilder sb = new StringBuilder() + 10_000.times { int i -> + sb.append("${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) * Ex: {@code $ openssl dgst -md5 src/test/resources/HashServiceTest/largefile.txt} */ @@ -428,9 +402,8 @@ class HashServiceTest extends GroovyTestCase { // Act 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 - InputStream input = inputFile.newInputStream() + InputStream input = new ByteArrayInputStream(sb.toString().bytes) String hash = HashService.hashValueStreaming(algorithm, input) - logger.info("${algorithm.getName().padLeft(11)}(${inputFile.path}) [${hash.length() / 2}] = ${hash}") [(algorithm.name), hash] } diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/NiFiLegacyCipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/NiFiLegacyCipherProviderGroovyTest.groovy index 70ecda0632..47d6fa2287 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/NiFiLegacyCipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/NiFiLegacyCipherProviderGroovyTest.groovy @@ -19,14 +19,9 @@ package org.apache.nifi.security.util.crypto import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -38,8 +33,8 @@ import javax.crypto.spec.PBEParameterSpec import java.security.Security import static org.junit.Assert.fail +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class NiFiLegacyCipherProviderGroovyTest { 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()) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -59,15 +54,6 @@ class NiFiLegacyCipherProviderGroovyTest { 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) { try { final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()) @@ -123,9 +109,6 @@ class NiFiLegacyCipherProviderGroovyTest { @Test 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() @@ -260,7 +243,7 @@ class NiFiLegacyCipherProviderGroovyTest { * from the password using a long digest result at the time of key length checking. * @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 void testShouldDetermineDependenceOnUnlimitedStrengthCrypto() throws IOException { def encryptionMethods = EncryptionMethod.values().findAll { it.algorithm.startsWith("PBE") } diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/OpenSSLPKCS5CipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/OpenSSLPKCS5CipherProviderGroovyTest.groovy index 5d25ba2bb3..b3c337b9b3 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/OpenSSLPKCS5CipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/OpenSSLPKCS5CipherProviderGroovyTest.groovy @@ -19,13 +19,8 @@ package org.apache.nifi.security.util.crypto import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -38,8 +33,8 @@ import java.security.Security import static groovy.test.GroovyAssert.shouldFail import static org.junit.Assert.fail +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class OpenSSLPKCS5CipherProviderGroovyTest { 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 int ITERATION_COUNT = 0 - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -57,15 +52,6 @@ class OpenSSLPKCS5CipherProviderGroovyTest { 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) { try { final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()) @@ -121,8 +107,8 @@ class OpenSSLPKCS5CipherProviderGroovyTest { @Test void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { // Arrange - Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", - CipherUtility.isUnlimitedStrengthCryptoSupported()) + assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(), + "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.") OpenSSLPKCS5CipherProvider cipherProvider = new OpenSSLPKCS5CipherProvider() diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2CipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2CipherProviderGroovyTest.groovy index c03795b673..28aff8e859 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2CipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2CipherProviderGroovyTest.groovy @@ -19,9 +19,10 @@ package org.apache.nifi.security.util.crypto import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.* -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -30,8 +31,8 @@ import java.security.Security import static groovy.test.GroovyAssert.shouldFail import static org.junit.Assert.assertTrue +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class PBKDF2CipherProviderGroovyTest { private static final Logger logger = LoggerFactory.getLogger(PBKDF2CipherProviderGroovyTest.class) @@ -47,7 +48,7 @@ class PBKDF2CipherProviderGroovyTest { private final String IV_HEX = "01" * 16 private static ArrayList AES_KEY_LENGTHS - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -64,15 +65,6 @@ class PBKDF2CipherProviderGroovyTest { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - - } - @Test void testGetCipherShouldBeInternallyConsistent() throws Exception { // Arrange @@ -163,10 +155,6 @@ class PBKDF2CipherProviderGroovyTest { @Test 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) 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 void testDefaultConstructorShouldProvideStrongIterationCount() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2SecureHasherTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2SecureHasherTest.groovy index 27799aca20..8613d8b4e5 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2SecureHasherTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/PBKDF2SecureHasherTest.groovy @@ -18,18 +18,26 @@ package org.apache.nifi.security.util.crypto import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.util.encoders.Hex -import org.junit.* -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +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.security.Security -@RunWith(JUnit4.class) class PBKDF2SecureHasherTest extends GroovyTestCase { - @BeforeClass + private static final Logger logger = LoggerFactory.getLogger(PBKDF2SecureHasherTest) + + @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 @@ -242,7 +250,7 @@ class PBKDF2SecureHasherTest extends GroovyTestCase { * This test can have the minimum time threshold updated to determine if the performance * is still sufficient compared to the existing threat model. */ - @Ignore("Long running test") + @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true") @Test void testDefaultCostParamsShouldBeSufficient() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptCipherProviderGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptCipherProviderGroovyTest.groovy index 2a92b1f65a..bac83486e2 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptCipherProviderGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptCipherProviderGroovyTest.groovy @@ -21,14 +21,11 @@ import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.EncryptionMethod import org.apache.nifi.security.util.crypto.scrypt.Scrypt import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -41,8 +38,8 @@ import java.security.Security import static groovy.test.GroovyAssert.shouldFail import static org.junit.Assert.assertTrue +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class ScryptCipherProviderGroovyTest { private static final Logger logger = LoggerFactory.getLogger(ScryptCipherProviderGroovyTest.class) @@ -56,7 +53,7 @@ class ScryptCipherProviderGroovyTest { RandomIVPBECipherProvider cipherProvider - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -73,17 +70,12 @@ class ScryptCipherProviderGroovyTest { } } - @Before + @BeforeEach void setUp() throws Exception { // Very fast parameters to test for correctness rather than production values cipherProvider = new ScryptCipherProvider(4, 1, 1) } - @After - void tearDown() throws Exception { - - } - @Test void testGetCipherShouldBeInternallyConsistent() throws Exception { // Arrange @@ -143,8 +135,8 @@ class ScryptCipherProviderGroovyTest { @Test void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() throws Exception { // Arrange - Assume.assumeTrue("Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.", - CipherUtility.isUnlimitedStrengthCryptoSupported()) + assumeTrue(CipherUtility.isUnlimitedStrengthCryptoSupported(), + "Test is being skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy file.") final String PASSWORD = "shortPassword" final byte[] SALT = cipherProvider.generateSalt() @@ -621,7 +613,8 @@ class ScryptCipherProviderGroovyTest { 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 void testDefaultConstructorShouldProvideStrongParameters() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptSecureHasherTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptSecureHasherTest.groovy index 6dda22d06b..963fcae660 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptSecureHasherTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/crypto/ScryptSecureHasherTest.groovy @@ -16,15 +16,29 @@ */ package org.apache.nifi.security.util.crypto +import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.util.encoders.Hex -import org.junit.* -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +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.security.Security -@RunWith(JUnit4.class) 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 void testShouldBeDeterministicWithStaticSalt() { @@ -239,7 +253,7 @@ class ScryptSecureHasherTest extends GroovyTestCase { * This test can have the minimum time threshold updated to determine if the performance * is still sufficient compared to the existing threat model. */ - @Ignore("Long running test") + @EnabledIfSystemProperty(named = "nifi.test.performance", matches = "true") @Test void testDefaultCostParamsShouldBeSufficient() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/scrypt/ScryptGroovyTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/scrypt/ScryptGroovyTest.groovy index ad222a0d15..dcc8b5f5fa 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/scrypt/ScryptGroovyTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/util/scrypt/ScryptGroovyTest.groovy @@ -19,14 +19,10 @@ package org.apache.nifi.security.util.scrypt import org.apache.commons.codec.binary.Hex import org.apache.nifi.security.util.crypto.scrypt.Scrypt import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Ignore -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -34,8 +30,8 @@ import java.security.SecureRandom import java.security.Security import static groovy.test.GroovyAssert.shouldFail +import static org.junit.jupiter.api.Assumptions.assumeTrue -@RunWith(JUnit4.class) class ScryptGroovyTest { 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 long TWO_GIGABYTES = 2048L * 1024 * 1024 - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -59,15 +55,6 @@ class ScryptGroovyTest { } } - @Before - void setUp() throws Exception { - } - - @After - void tearDown() throws Exception { - - } - @Test void testDeriveScryptKeyShouldBeInternallyConsistent() throws Exception { // Arrange @@ -150,8 +137,7 @@ class ScryptGroovyTest { long totalMemory = Runtime.getRuntime().totalMemory() logger.info("Required memory: ${TWO_GIGABYTES} 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", - totalMemory >= TWO_GIGABYTES) + assumeTrue(totalMemory >= TWO_GIGABYTES, "Test is being skipped due to JVM heap size. Please run with -Xmx3072m to set sufficient heap size") // These values are taken from Colin Percival's scrypt paper: https://www.tarsnap.com/scrypt/scrypt.pdf final byte[] HASH = Hex.decodeHex("2101cb9b6a511aaeaddbbe09cf70f881" + @@ -180,7 +166,7 @@ class ScryptGroovyTest { assert calculatedHash == HASH } - @Ignore("This test was just to exercise the heap and debug OOME issues") + @EnabledIfSystemProperty(named = "nifi.test.unstable", matches = "true") @Test void testShouldCauseOutOfMemoryError() { SecureRandom secureRandom = new SecureRandom() diff --git a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/xml/XmlUtilsTest.groovy b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/xml/XmlUtilsTest.groovy index 1fda85b687..fb12bfec00 100644 --- a/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/xml/XmlUtilsTest.groovy +++ b/nifi-commons/nifi-security-utils/src/test/groovy/org/apache/nifi/security/xml/XmlUtilsTest.groovy @@ -16,12 +16,8 @@ */ package org.apache.nifi.security.xml -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import org.xml.sax.SAXParseException @@ -38,27 +34,16 @@ import javax.xml.stream.XMLStreamReader import static groovy.test.GroovyAssert.shouldFail -@RunWith(JUnit4.class) class XmlUtilsTest { private static final Logger logger = LoggerFactory.getLogger(XmlUtilsTest.class) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { logger.metaClass.methodMissing = { String name, args -> logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") } } - @Before - void setUp() throws Exception { - - } - - @After - void tearDown() throws Exception { - - } - @Test void testShouldHandleXXEInUnmarshal() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/XXEValidatorTest.java b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/XXEValidatorTest.java index 9c39e2888b..fd2dceac38 100644 --- a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/XXEValidatorTest.java +++ b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/XXEValidatorTest.java @@ -16,22 +16,17 @@ */ 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.assertFalse; import static org.junit.Assert.assertTrue; 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 { private String simpleXMLFile = "src/test/resources/no_xxe.xml"; private String remoteXXEFile = "src/test/resources/remote_xxe_file.xml"; @@ -41,21 +36,11 @@ public class XXEValidatorTest { private String configurationKey = "Configuration Name"; private ValidationContext validationContext; - @BeforeClass - public static void setUpOnce() throws Exception { - - } - - @Before + @BeforeEach public void setUp() throws Exception { validationContext = mock(ValidationContext.class); } - @After - public void tearDown() throws Exception { - - } - @Test public void testXmlFileWithNoXXEIsValid() { // Arrange diff --git a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/KeyStoreUtilsTest.java b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/KeyStoreUtilsTest.java index f41d5970d6..5ffed46267 100644 --- a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/KeyStoreUtilsTest.java +++ b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/KeyStoreUtilsTest.java @@ -17,6 +17,12 @@ 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.ByteArrayOutputStream; import java.io.File; @@ -32,13 +38,6 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; 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.assertEquals; import static org.junit.Assert.assertTrue; @@ -60,7 +59,7 @@ public class KeyStoreUtilsTest { private static X509Certificate certificate; private static SecretKey secretKey; - @BeforeClass + @BeforeAll public static void generateKeysAndCertificates() throws NoSuchAlgorithmException, CertificateException { keyPair = KeyPairGenerator.getInstance(KEY_ALGORITHM).generateKeyPair(); certificate = CertificateUtils.generateSelfSignedX509Certificate(keyPair, SUBJECT_DN, SIGNING_ALGORITHM, DURATION_DAYS); diff --git a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/SecureHasherFactoryTest.java b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/SecureHasherFactoryTest.java index 0f4a6cbf13..d566a2ee98 100644 --- a/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/SecureHasherFactoryTest.java +++ b/nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/SecureHasherFactoryTest.java @@ -16,9 +16,7 @@ */ package org.apache.nifi.security.util.crypto; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import static org.junit.Assert.assertEquals; @@ -26,9 +24,6 @@ public class SecureHasherFactoryTest { private static final Argon2SecureHasher DEFAULT_HASHER = new Argon2SecureHasher(); - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - @Test public void testSecureHasherFactoryArgon2() { SecureHasher hasher = SecureHasherFactory.getSecureHasher("NIFI_ARGON2_AES_GCM_128"); diff --git a/nifi-commons/nifi-sensitive-property-provider/src/test/groovy/org/apache/nifi/properties/AESSensitivePropertyProviderTest.groovy b/nifi-commons/nifi-sensitive-property-provider/src/test/groovy/org/apache/nifi/properties/AESSensitivePropertyProviderTest.groovy index cef68b170d..fe838c351a 100644 --- a/nifi-commons/nifi-sensitive-property-provider/src/test/groovy/org/apache/nifi/properties/AESSensitivePropertyProviderTest.groovy +++ b/nifi-commons/nifi-sensitive-property-provider/src/test/groovy/org/apache/nifi/properties/AESSensitivePropertyProviderTest.groovy @@ -18,13 +18,10 @@ package org.apache.nifi.properties import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.util.encoders.Hex -import org.junit.After -import org.junit.Assume -import org.junit.Before -import org.junit.BeforeClass -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -35,7 +32,8 @@ import java.nio.charset.StandardCharsets import java.security.SecureRandom import java.security.Security -@RunWith(JUnit4.class) +import static org.junit.Assume.assumeTrue + class AESSensitivePropertyProviderTest extends GroovyTestCase { 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.Decoder decoder = Base64.decoder - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -61,12 +59,12 @@ class AESSensitivePropertyProviderTest extends GroovyTestCase { } } - @Before + @BeforeEach void setUp() throws Exception { } - @After + @AfterEach void tearDown() throws Exception { } @@ -475,7 +473,7 @@ class AESSensitivePropertyProviderTest extends GroovyTestCase { @Test void testShouldDecryptPaddedValue() { // 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" String cipherText = "aYDkDKys1ENr3gp+||sTBPpMlIvHcOLTGZlfWct8r9RY8BuDlDkoaYmGJ/9m9af9tZIVzcnDwvYQAaIKxRGF7vI2yrY7Xd6x9GTDnWGiGiRXlaP458BBMMgfzH2O8" diff --git a/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/AwsKmsSensitivePropertyProviderIT.java b/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/AwsKmsSensitivePropertyProviderIT.java index 30c183c8d0..810bea825c 100644 --- a/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/AwsKmsSensitivePropertyProviderIT.java +++ b/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/AwsKmsSensitivePropertyProviderIT.java @@ -17,10 +17,9 @@ package org.apache.nifi.properties; import org.apache.nifi.properties.configuration.AwsKmsClientProvider; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.mockito.internal.util.io.IOUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +31,10 @@ import java.nio.file.Files; import java.nio.file.Path; 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 * 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()); } - @BeforeClass + @BeforeAll public static void initOnce() throws IOException { initializeBootstrapProperties(); - Assert.assertNotNull(props); + assertNotNull(props); final AwsKmsClientProvider provider = new AwsKmsClientProvider(); final Properties properties = provider.getClientProperties(props).orElse(null); final KmsClient kmsClient = provider.getClient(properties).orElse(null); spp = new AwsKmsSensitivePropertyProvider(kmsClient, properties); - Assert.assertNotNull(spp); + assertNotNull(spp); } - @AfterClass + @AfterAll public static void tearDownOnce() throws IOException { Files.deleteIfExists(mockBootstrapConf); Files.deleteIfExists(mockAWSBootstrapConf); @@ -132,8 +135,8 @@ public class AwsKmsSensitivePropertyProviderIT { String unprotectedValue = spp.unprotect(protectedValue, ProtectedPropertyContext.defaultContext("property")); logger.info("Unprotected Value: " + unprotectedValue); - Assert.assertEquals(SAMPLE_PLAINTEXT, unprotectedValue); - Assert.assertNotEquals(SAMPLE_PLAINTEXT, protectedValue); - Assert.assertNotEquals(protectedValue, unprotectedValue); + assertEquals(SAMPLE_PLAINTEXT, unprotectedValue); + assertNotEquals(SAMPLE_PLAINTEXT, protectedValue); + assertNotEquals(protectedValue, unprotectedValue); } } diff --git a/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/StandardSensitivePropertyProviderFactoryTest.java b/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/StandardSensitivePropertyProviderFactoryTest.java index f9fa266799..c7aa6092e2 100644 --- a/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/StandardSensitivePropertyProviderFactoryTest.java +++ b/nifi-commons/nifi-sensitive-property-provider/src/test/java/org/apache/nifi/properties/StandardSensitivePropertyProviderFactoryTest.java @@ -19,9 +19,9 @@ package org.apache.nifi.properties; import org.apache.commons.io.FilenameUtils; import org.apache.nifi.util.NiFiProperties; import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.mockito.internal.util.io.IOUtil; import java.io.FileOutputStream; @@ -35,12 +35,12 @@ import java.security.Security; import java.util.Properties; import java.util.function.Supplier; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class StandardSensitivePropertyProviderFactoryTest { @@ -56,7 +56,9 @@ public class StandardSensitivePropertyProviderFactoryTest { private static Path nifiProperties; private static String defaultBootstrapContents; - @BeforeClass + private static NiFiProperties niFiProperties; + + @BeforeAll public static void initOnce() throws IOException { Security.addProvider(new BouncyCastleProvider()); tempConfDir = Files.createTempDirectory("conf"); @@ -86,7 +88,7 @@ public class StandardSensitivePropertyProviderFactoryTest { return bootstrapConf; } - @AfterClass + @AfterAll public static void tearDownOnce() throws IOException { Files.deleteIfExists(bootstrapConf); Files.deleteIfExists(hashicorpVaultBootstrapConf); diff --git a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/command/SetSingleUserCredentialsTest.java b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/command/SetSingleUserCredentialsTest.java index 9978cb55b9..4486ca80f4 100644 --- a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/command/SetSingleUserCredentialsTest.java +++ b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/command/SetSingleUserCredentialsTest.java @@ -17,8 +17,8 @@ package org.apache.nifi.authentication.single.user.command; import org.apache.commons.io.FilenameUtils; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; @@ -34,14 +34,14 @@ import java.util.UUID; import java.util.regex.Pattern; import java.util.stream.Collectors; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SetSingleUserCredentialsTest { private static final Pattern BCRYPT_PATTERN = Pattern.compile("\\$2b\\$12\\$.+"); private static final String PROPERTIES_PATH = "/conf/login.nifi.properties"; - @After + @AfterEach public void clearProperties() { System.clearProperty(SetSingleUserCredentials.PROPERTIES_FILE_PATH); } @@ -66,8 +66,8 @@ public class SetSingleUserCredentialsTest { private void assertProvidersUpdated(final Path providersConfigurationPath, final String username) throws IOException { final String providersConfiguration = new String(Files.readAllBytes(providersConfigurationPath), StandardCharsets.UTF_8); - assertTrue("Username not found", providersConfiguration.contains(username)); - assertTrue("Encoded Password not found", BCRYPT_PATTERN.matcher(providersConfiguration).find()); + assertTrue(providersConfiguration.contains(username), "Username not found"); + assertTrue(BCRYPT_PATTERN.matcher(providersConfiguration).find(), "Encoded Password not found"); } private Path getNiFiProperties(final Path providersPath) throws IOException, URISyntaxException { diff --git a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/encoder/BCryptPasswordEncoderTest.java b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/encoder/BCryptPasswordEncoderTest.java index 36b12c89aa..b922d50de9 100644 --- a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/encoder/BCryptPasswordEncoderTest.java +++ b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/encoder/BCryptPasswordEncoderTest.java @@ -16,12 +16,12 @@ */ package org.apache.nifi.authentication.single.user.encoder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.regex.Pattern; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class BCryptPasswordEncoderTest { @@ -31,8 +31,8 @@ public class BCryptPasswordEncoderTest { public void testEncode() { final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); final String encoded = encoder.encode(String.class.getSimpleName().toCharArray()); - assertNotNull("Encoded Password not found", encoded); - assertTrue("Encoded Password bcrypt hash not found", BCRYPT_PATTERN.matcher(encoded).matches()); + assertNotNull(encoded, "Encoded Password not found"); + assertTrue(BCRYPT_PATTERN.matcher(encoded).matches(), "Encoded Password bcrypt hash not found"); } @Test @@ -40,6 +40,6 @@ public class BCryptPasswordEncoderTest { final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); final char[] password = String.class.getSimpleName().toCharArray(); final String encoded = encoder.encode(password); - assertTrue("Encoded Password not matched", encoder.matches(password, encoded)); + assertTrue(encoder.matches(password, encoded), "Encoded Password not matched"); } } diff --git a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/writer/StandardLoginCredentialsWriterTest.java b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/writer/StandardLoginCredentialsWriterTest.java index 0920df8b5a..4efbd53fb1 100644 --- a/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/writer/StandardLoginCredentialsWriterTest.java +++ b/nifi-commons/nifi-single-user-utils/src/test/java/org/apache/nifi/authentication/single/user/writer/StandardLoginCredentialsWriterTest.java @@ -17,7 +17,7 @@ package org.apache.nifi.authentication.single.user.writer; import org.apache.nifi.authentication.single.user.SingleUserCredentials; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.URISyntaxException; @@ -27,7 +27,7 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.UUID; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; public class StandardLoginCredentialsWriterTest { private static final String BLANK_PROVIDERS = "/conf/login-identity-providers.xml"; @@ -64,7 +64,7 @@ public class StandardLoginCredentialsWriterTest { writer.writeLoginCredentials(credentials); final String configuration = new String(Files.readAllBytes(configuredProvidersPath)); - assertTrue("Username not found", configuration.contains(username)); - assertTrue("Password not found", configuration.contains(password)); + assertTrue(configuration.contains(username), "Username not found"); + assertTrue(configuration.contains(password), "Password not found"); } } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/groovy/org/apache/nifi/remote/client/PeerSelectorTest.groovy b/nifi-commons/nifi-site-to-site-client/src/test/groovy/org/apache/nifi/remote/client/PeerSelectorTest.groovy index d004979ea1..f167da0149 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/groovy/org/apache/nifi/remote/client/PeerSelectorTest.groovy +++ b/nifi-commons/nifi-site-to-site-client/src/test/groovy/org/apache/nifi/remote/client/PeerSelectorTest.groovy @@ -23,19 +23,16 @@ import org.apache.nifi.remote.TransferDirection import org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol import org.apache.nifi.remote.util.PeerStatusCache import org.bouncycastle.jce.provider.BouncyCastleProvider -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 +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import java.security.Security import java.util.concurrent.ArrayBlockingQueue -@RunWith(JUnit4.class) class PeerSelectorTest extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(PeerSelectorTest.class) @@ -51,7 +48,7 @@ class PeerSelectorTest extends GroovyTestCase { private static mockPP - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -60,14 +57,14 @@ class PeerSelectorTest extends GroovyTestCase { } } - @Before + @BeforeEach void setUp() { // Mock collaborators mockPSP = mockPeerStatusProvider() mockPP = mockPeerPersistence() } - @After + @AfterEach void tearDown() { } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/TestSiteInfoProvider.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/TestSiteInfoProvider.java index 2d9de21c62..6aea71e289 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/TestSiteInfoProvider.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/TestSiteInfoProvider.java @@ -21,7 +21,7 @@ import org.apache.nifi.remote.protocol.http.HttpProxy; import org.apache.nifi.remote.util.SiteToSiteRestApiClient; import org.apache.nifi.web.api.dto.ControllerDTO; import org.apache.nifi.web.api.dto.PortDTO; -import org.junit.Test; +import org.junit.jupiter.api.Test; import javax.net.ssl.SSLContext; import java.io.IOException; @@ -31,11 +31,11 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; @@ -190,18 +190,7 @@ public class TestSiteInfoProvider { return apiClient; }).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any()); - try { - siteInfoProvider.getSiteToSitePort(); - fail(); - } catch (IOException e) { - } - - try { - siteInfoProvider.getActiveClusterUrl(); - fail(); - } catch (IOException e) { - } - + assertThrows(IOException.class, () -> siteInfoProvider.getSiteToSitePort()); + assertThrows(IOException.class, () -> siteInfoProvider.getActiveClusterUrl()); } - } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/http/TestHttpClient.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/http/TestHttpClient.java index d72662f7bf..437ae781f0 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/http/TestHttpClient.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/http/TestHttpClient.java @@ -16,40 +16,7 @@ */ 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 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.remote.Peer; 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.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +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.ProxyAuthenticator; import org.littleshoot.proxy.impl.DefaultHttpProxyServer; @@ -100,6 +69,40 @@ import org.littleshoot.proxy.impl.ThreadPoolConfiguration; import org.slf4j.Logger; 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 { private static final Logger logger = LoggerFactory.getLogger(TestHttpClient.class); @@ -139,11 +142,11 @@ public class TestHttpClient { controller.setInstanceId("remote-instance-id"); controller.setName("Remote NiFi Flow"); - assertNotNull("Test case should set depending on the test scenario.", inputPorts); + assertNotNull(inputPorts, "Test case should set depending on the test scenario."); controller.setInputPorts(inputPorts); controller.setInputPortCount(inputPorts.size()); - assertNotNull("Test case should set depending on the test scenario.", outputPorts); + assertNotNull(outputPorts, "Test case should set depending on the test scenario."); controller.setOutputPorts(outputPorts); controller.setOutputPortCount(outputPorts.size()); @@ -171,10 +174,10 @@ public class TestHttpClient { final PeersEntity peersEntity = new PeersEntity(); if (req.getLocalPort() == httpConnector.getLocalPort()) { - assertNotNull("Test case should set depending on the test scenario.", peers); + assertNotNull(peers, "Test case should set depending on the test scenario."); peersEntity.setPeers(peers); } else { - assertNotNull("Test case should set depending on the test scenario.", peersSecure); + assertNotNull(peersSecure, "Test case should set depending on the test scenario."); peersEntity.setPeers(peersSecure); } @@ -431,7 +434,7 @@ public class TestHttpClient { out.flush(); } - @BeforeClass + @BeforeAll public static void setup() throws Exception { // Create embedded Jetty server // Use less threads to mitigate Gateway Timeout (504) with proxy test @@ -559,7 +562,7 @@ public class TestHttpClient { .start(); } - @AfterClass + @AfterAll public static void teardown() throws Exception { logger.info("Stopping servers."); try { @@ -600,7 +603,7 @@ public class TestHttpClient { } - @Before + @BeforeEach public void before() throws Exception { testCaseFinished = new CountDownLatch(1); @@ -678,7 +681,7 @@ public class TestHttpClient { } - @After + @AfterEach public void after() throws Exception { testCaseFinished.countDown(); } @@ -774,13 +777,8 @@ public class TestHttpClient { .portName("input-unknown") .build() ) { - try { - client.createTransaction(TransferDirection.SEND); - fail(); - } catch (IOException e) { - logger.info("Exception message: {}", e.getMessage()); - assertTrue(e.getMessage().contains("Failed to determine the identifier of port")); - } + IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.SEND)); + assertTrue(e.getMessage().contains("Failed to determine the identifier of port")); } } @@ -858,7 +856,7 @@ public class TestHttpClient { .build() ) { 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") .build() ) { - try { - client.createTransaction(TransferDirection.SEND); - fail("Handshake exception should be thrown."); - } catch (HandshakeException e) { - } + assertThrows(HandshakeException.class, () -> client.createTransaction(TransferDirection.SEND)); } - } @Test @@ -1112,13 +1105,8 @@ public class TestHttpClient { } - private void completeShouldFail(Transaction transaction) throws IOException { - try { - 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 completeShouldFail(Transaction transaction) { + assertThrows(IllegalStateException.class, () -> transaction.complete()); } private void confirmShouldFail(Transaction transaction) throws IOException { @@ -1131,8 +1119,8 @@ public class TestHttpClient { } @Test + @DisabledOnOs(OS.WINDOWS) public void testSendTimeout() throws Exception { - assumeFalse(isWindowsEnvironment());//skip on windows try ( SiteToSiteClient client = getDefaultBuilder() .timeout(1, TimeUnit.SECONDS) @@ -1151,26 +1139,17 @@ public class TestHttpClient { serverChecksum = "1345413116"; transaction.send(packet); - try { - transaction.confirm(); - fail(); - } catch (IOException e) { - logger.info("An exception was thrown as expected.", e); - assertTrue(e.getMessage().contains("TimeoutException")); - } + IOException e = assertThrows(IOException.class, () -> transaction.confirm()); + assertTrue(e.getMessage().contains("TimeoutException")); completeShouldFail(transaction); } } - private boolean isWindowsEnvironment() { - return System.getProperty("os.name").toLowerCase().startsWith("windows"); - } - @Test + @DisabledOnOs(OS.WINDOWS) public void testSendTimeoutAfterDataExchange() throws Exception { - assumeFalse(isWindowsEnvironment());//skip on windows System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote.protocol.http.HttpClientTransaction", "INFO"); try ( @@ -1197,17 +1176,11 @@ public class TestHttpClient { } } - try { - confirmShouldFail(transaction); - fail("Should be timeout."); - } catch (IOException e) { - logger.info("Exception message: {}", e.getMessage()); - assertTrue(e.getMessage().contains("TimeoutException")); - } + IOException e = assertThrows(IOException.class, () -> confirmShouldFail(transaction)); + assertTrue(e.getMessage().contains("TimeoutException")); completeShouldFail(transaction); } - } @Test @@ -1218,13 +1191,8 @@ public class TestHttpClient { .portName("output-unknown") .build() ) { - try { - client.createTransaction(TransferDirection.RECEIVE); - fail(); - } catch (IOException e) { - logger.info("Exception message: {}", e.getMessage()); - assertTrue(e.getMessage().contains("Failed to determine the identifier of port")); - } + IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.RECEIVE)); + assertTrue(e.getMessage().contains("Failed to determine the identifier of port")); } } @@ -1361,13 +1329,8 @@ public class TestHttpClient { .portName("output-timeout") .build() ) { - try { - client.createTransaction(TransferDirection.RECEIVE); - fail(); - } catch (IOException e) { - logger.info("An exception was thrown as expected.", e); - assertTrue(e instanceof SocketTimeoutException); - } + IOException e = assertThrows(IOException.class, () -> client.createTransaction(TransferDirection.RECEIVE)); + assertTrue(e instanceof SocketTimeoutException); } } @@ -1387,13 +1350,8 @@ public class TestHttpClient { assertNotNull(packet); consumeDataPacket(packet); - try { - transaction.receive(); - fail(); - } catch (IOException e) { - logger.info("An exception was thrown as expected.", e); - assertTrue(e.getCause() instanceof SocketTimeoutException); - } + IOException e = assertThrows(IOException.class, () -> transaction.receive()); + assertTrue(e.getCause() instanceof SocketTimeoutException); confirmShouldFail(transaction); completeShouldFail(transaction); diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/SiteToSiteClientIT.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/SiteToSiteClientIT.java new file mode 100644 index 0000000000..4aa8736b23 --- /dev/null +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/SiteToSiteClientIT.java @@ -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 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(); + } + } +} diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/TestSiteToSiteClient.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/TestSiteToSiteClient.java index 7c70a7a3d1..65d3f363bb 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/TestSiteToSiteClient.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/client/socket/TestSiteToSiteClient.java @@ -20,95 +20,22 @@ import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; 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.SiteToSiteClientConfig; -import org.apache.nifi.remote.protocol.DataPacket; -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.junit.jupiter.api.Test; import org.mockito.Mockito; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; import java.util.LinkedHashSet; -import java.util.Map; 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 { - @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 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 public void testSerialization() { final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder() @@ -132,7 +59,7 @@ public class TestSiteToSiteClient { try { SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class); - Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls()); + assertEquals(clientConfig.getUrls(), clientConfig2.getUrls()); } finally { input.close(); } @@ -163,10 +90,10 @@ public class TestSiteToSiteClient { try { 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. - Assert.assertNotNull(clientConfig.getStateManager()); - Assert.assertNull(clientConfig2.getStateManager()); + assertNotNull(clientConfig.getStateManager()); + assertNull(clientConfig2.getStateManager()); } finally { input.close(); } @@ -181,8 +108,8 @@ public class TestSiteToSiteClient { .urls(urls) .buildConfig(); - Assert.assertEquals("http://node1:8080/nifi", config.getUrl()); - Assert.assertEquals(urls, config.getUrls()); + assertEquals("http://node1:8080/nifi", config.getUrl()); + assertEquals(urls, config.getUrls()); } } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/SiteToSiteTestUtils.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/SiteToSiteTestUtils.java index 1ed23e0348..c0eb074e77 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/SiteToSiteTestUtils.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/SiteToSiteTestUtils.java @@ -16,22 +16,23 @@ */ package org.apache.nifi.remote.protocol; -import static org.junit.Assert.assertEquals; -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 org.apache.nifi.remote.Transaction; +import org.apache.nifi.remote.TransactionCompletion; +import org.apache.nifi.remote.util.StandardDataPacket; +import org.apache.nifi.stream.io.StreamUtils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; -import org.apache.nifi.remote.Transaction; -import org.apache.nifi.remote.TransactionCompletion; -import org.apache.nifi.remote.util.StandardDataPacket; -import org.apache.nifi.stream.io.StreamUtils; + +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class SiteToSiteTestUtils { public static DataPacket createDataPacket(String contents) { @@ -61,7 +62,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); 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()); } @@ -81,7 +82,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); 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()); } @@ -104,7 +105,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); 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()); } @@ -124,36 +125,18 @@ public class SiteToSiteTestUtils { packet = transaction.receive(); assertNull(packet); - try { - transaction.confirm(); - fail(); - } catch (IOException e){ - assertTrue(e.getMessage().contains("Received a BadChecksum response")); - assertEquals(Transaction.TransactionState.ERROR, transaction.getState()); - } + IOException io = assertThrows(IOException.class, () -> transaction.confirm()); + assertTrue(io.getMessage().contains("Received a BadChecksum response")); + assertEquals(Transaction.TransactionState.ERROR, transaction.getState()); - try { - transaction.complete(); - fail("It's not confirmed."); - } catch (IllegalStateException e){ - assertEquals(Transaction.TransactionState.ERROR, transaction.getState()); - } + assertThrows(IllegalStateException.class, () -> transaction.complete()); + assertEquals(Transaction.TransactionState.ERROR, transaction.getState()); } public static void execSendZeroFlowFile(Transaction transaction) throws IOException { assertEquals(Transaction.TransactionState.TRANSACTION_STARTED, transaction.getState()); - - try { - transaction.confirm(); - fail("Nothing has been sent."); - } catch (IllegalStateException e){ - } - - try { - transaction.complete(); - fail("Nothing has been sent."); - } catch (IllegalStateException e){ - } + assertThrows(IllegalStateException.class, () -> transaction.confirm()); + assertThrows(IllegalStateException.class, () -> transaction.complete()); } public static void execSendOneFlowFile(Transaction transaction) throws IOException { @@ -167,7 +150,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); 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()); } @@ -185,7 +168,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); 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()); } @@ -198,21 +181,12 @@ public class SiteToSiteTestUtils { packet = createDataPacket("contents on client 2"); 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 { - transaction.confirm(); - 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()); - } + assertThrows(IllegalStateException.class, () -> transaction.complete()); + assertEquals(Transaction.TransactionState.ERROR, transaction.getState()); } public static void execSendButDestinationFull(Transaction transaction) throws IOException { @@ -229,7 +203,7 @@ public class SiteToSiteTestUtils { TransactionCompletion completion = transaction.complete(); assertEquals(Transaction.TransactionState.TRANSACTION_COMPLETED, transaction.getState()); - assertTrue("Should be backoff", completion.isBackoff()); + assertTrue(completion.isBackoff(), "Should be backoff"); assertEquals(2, completion.getDataPacketsTransferred()); } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/http/TestHttpClientTransaction.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/http/TestHttpClientTransaction.java index 2783cefe9a..590794fc20 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/http/TestHttpClientTransaction.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/http/TestHttpClientTransaction.java @@ -16,32 +16,6 @@ */ 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.remote.Peer; 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.reporting.Severity; import org.apache.nifi.web.api.entity.TransactionResultEntity; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; 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 { private Logger logger = LoggerFactory.getLogger(TestHttpClientTransaction.class); @@ -108,7 +107,7 @@ public class TestHttpClientTransaction { 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 @@ -129,7 +128,7 @@ public class TestHttpClientTransaction { 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"); } @@ -152,7 +151,7 @@ public class TestHttpClientTransaction { 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"); } @@ -176,7 +175,7 @@ public class TestHttpClientTransaction { 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"); } @@ -194,7 +193,7 @@ public class TestHttpClientTransaction { execSendZeroFlowFile(transaction); - assertEquals("Client didn't send anything", 0, clientRequest.toByteArray().length); + assertEquals(0, clientRequest.toByteArray().length, "Client didn't send anything"); } @Test @@ -204,13 +203,10 @@ public class TestHttpClientTransaction { final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId"; doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class)); // Emulate that server returns correct checksum. - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; - commSession.setChecksum("2946083981"); - return null; - } + doAnswer(invocation -> { + HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; + commSession.setChecksum("2946083981"); + return null; }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); TransactionResultEntity resultEntity = new TransactionResultEntity(); 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"; doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class)); // Emulate that server returns correct checksum. - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; - commSession.setChecksum("3359812065"); - return null; - } + doAnswer(invocation -> { + HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; + commSession.setChecksum("3359812065"); + return null; }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); TransactionResultEntity resultEntity = new TransactionResultEntity(); 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"; doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class)); // Emulate that server returns incorrect checksum. - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; - commSession.setChecksum("Different checksum"); - return null; - } + doAnswer(invocation -> { + HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; + commSession.setChecksum("Different checksum"); + return null; }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - TransactionResultEntity serverResult = new TransactionResultEntity(); - serverResult.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode()); - return serverResult; - } + doAnswer(invocation -> { + TransactionResultEntity serverResult = new TransactionResultEntity(); + serverResult.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode()); + return serverResult; }).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(ResponseCode.BAD_CHECKSUM)); 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"; doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class)); // Emulate that server returns correct checksum. - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; - commSession.setChecksum("3359812065"); - return null; - } + doAnswer(invocation -> { + HttpCommunicationsSession commSession = (HttpCommunicationsSession)invocation.getArguments()[0]; + commSession.setChecksum("3359812065"); + return null; }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class)); TransactionResultEntity resultEntity = new TransactionResultEntity(); resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.getCode()); diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/socket/TestSocketClientTransaction.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/socket/TestSocketClientTransaction.java index edae052d12..058602cb70 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/socket/TestSocketClientTransaction.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/protocol/socket/TestSocketClientTransaction.java @@ -30,7 +30,7 @@ import org.apache.nifi.remote.protocol.DataPacket; import org.apache.nifi.remote.protocol.RequestType; import org.apache.nifi.remote.protocol.Response; import org.apache.nifi.remote.protocol.ResponseCode; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; 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.execSendZeroFlowFile; 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.when; @@ -124,7 +124,7 @@ public class TestSocketClientTransaction { assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); Response confirmResponse = Response.read(sentByClient); 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); assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode()); assertEquals(-1, sentByClient.read()); @@ -157,7 +157,7 @@ public class TestSocketClientTransaction { assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); Response confirmResponse = Response.read(sentByClient); 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); assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode()); assertEquals(-1, sentByClient.read()); @@ -188,7 +188,7 @@ public class TestSocketClientTransaction { assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient)); Response confirmResponse = Response.read(sentByClient); 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()); } diff --git a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java index 22b192baa6..d38022c1a3 100644 --- a/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java +++ b/nifi-commons/nifi-site-to-site-client/src/test/java/org/apache/nifi/remote/util/TestSiteToSiteRestApiClient.java @@ -16,21 +16,21 @@ */ package org.apache.nifi.remote.util; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Iterator; import java.util.Set; import static org.apache.nifi.remote.util.SiteToSiteRestApiClient.parseClusterUrls; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestSiteToSiteRestApiClient { private static void assertSingleUri(final String expected, final Set urls) { - Assert.assertEquals(1, urls.size()); - Assert.assertEquals(expected, urls.iterator().next().toString()); + assertEquals(1, urls.size()); + assertEquals(expected, urls.iterator().next().toString()); } @Test @@ -122,57 +122,42 @@ public class TestSiteToSiteRestApiClient { @Test public void testGetUrlsEmpty() throws Exception { - try { - parseClusterUrls(null); - fail("Should fail if cluster URL was not specified."); - } catch (IllegalArgumentException e) { - } + assertThrows(IllegalArgumentException.class, () -> parseClusterUrls(null)); - try { - parseClusterUrls(""); - fail("Should fail if cluster URL was not specified."); - } catch (IllegalArgumentException e) { - } + assertThrows(IllegalArgumentException.class, () -> parseClusterUrls("")); } @Test public void testGetUrlsOne() throws Exception { final Set urls = parseClusterUrls("http://localhost:8080/nifi"); - Assert.assertEquals(1, urls.size()); - Assert.assertEquals("http://localhost:8080/nifi-api", urls.iterator().next()); + assertEquals(1, urls.size()); + assertEquals("http://localhost:8080/nifi-api", urls.iterator().next()); } @Test public void testGetUrlsThree() throws Exception { final Set 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 iterator = urls.iterator(); - Assert.assertEquals("http://host1:8080/nifi-api", iterator.next()); - Assert.assertEquals("http://host2:8080/nifi-api", iterator.next()); - Assert.assertEquals("http://host3:8080/nifi-api", iterator.next()); + assertEquals("http://host1:8080/nifi-api", iterator.next()); + assertEquals("http://host2:8080/nifi-api", iterator.next()); + assertEquals("http://host3:8080/nifi-api", iterator.next()); } @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 { - 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")); - } + assertTrue(exception.getMessage().contains("Different protocols")); } @Test - public void testGetUrlsMalformed() throws Exception { - - try { - parseClusterUrls("http://host1:8080/nifi,host&2:8080,http://host3:8080/nifi"); - fail("Should fail if cluster URLs contain illegal URL."); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("malformed")); - } + public void testGetUrlsMalformed() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> parseClusterUrls("http://host1:8080/nifi,host&2:8080,http://host3:8080/nifi")); + assertTrue(exception.getMessage().contains("malformed")); } } diff --git a/nifi-commons/nifi-socket-utils/src/test/groovy/org/apache/nifi/io/socket/SocketUtilsTest.groovy b/nifi-commons/nifi-socket-utils/src/test/groovy/org/apache/nifi/io/socket/SocketUtilsTest.groovy index 9b3510916b..2ffaf6a7bd 100644 --- a/nifi-commons/nifi-socket-utils/src/test/groovy/org/apache/nifi/io/socket/SocketUtilsTest.groovy +++ b/nifi-commons/nifi-socket-utils/src/test/groovy/org/apache/nifi/io/socket/SocketUtilsTest.groovy @@ -16,25 +16,19 @@ */ package org.apache.nifi.io.socket - import org.apache.nifi.security.util.KeystoreType import org.apache.nifi.security.util.StandardTlsConfiguration import org.apache.nifi.security.util.TlsConfiguration import org.apache.nifi.util.NiFiProperties import org.bouncycastle.jce.provider.BouncyCastleProvider -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import javax.net.ssl.SSLServerSocket import java.security.Security -@RunWith(JUnit4.class) class SocketUtilsTest extends GroovyTestCase { 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 SSLContext sslContext = SslContextFactory.createSslContext(TLS_CONFIGURATION, ClientAuth.NONE) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { Security.addProvider(new BouncyCastleProvider()) @@ -74,16 +68,6 @@ class SocketUtilsTest extends GroovyTestCase { } } - @Before - void setUp() { - - } - - @After - void tearDown() { - - } - @Test void testCreateSSLServerSocketShouldRestrictTlsProtocols() { // Arrange diff --git a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/ServerMain.java b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/ServerMain.java index a266ade5f6..120e778488 100644 --- a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/ServerMain.java +++ b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/ServerMain.java @@ -16,6 +16,13 @@ */ 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.util.Calendar; import java.util.HashSet; @@ -27,14 +34,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; 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; - /** * */ diff --git a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/TCPClient.java b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/TCPClient.java index 1c4b70cbef..c9d88867b2 100644 --- a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/TCPClient.java +++ b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/TCPClient.java @@ -16,13 +16,13 @@ */ package org.apache.nifi.io.nio.example; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.net.Socket; import java.net.SocketException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** */ public class TCPClient { diff --git a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UDPClient.java b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UDPClient.java index 00a00a10d2..9741e94ab0 100644 --- a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UDPClient.java +++ b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UDPClient.java @@ -16,13 +16,13 @@ */ package org.apache.nifi.io.nio.example; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** */ public class UDPClient { diff --git a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UselessStreamConsumer.java b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UselessStreamConsumer.java index 107c087e85..1ce90dfc9b 100644 --- a/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UselessStreamConsumer.java +++ b/nifi-commons/nifi-socket-utils/src/test/java/org/apache/nifi/io/nio/example/UselessStreamConsumer.java @@ -16,11 +16,11 @@ */ package org.apache.nifi.io.nio.example; +import org.apache.nifi.io.nio.consumer.AbstractStreamConsumer; + import java.io.IOException; import java.nio.ByteBuffer; -import org.apache.nifi.io.nio.consumer.AbstractStreamConsumer; - /** * */ diff --git a/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/util/TestFormatUtilsGroovy.groovy b/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/util/TestFormatUtilsGroovy.groovy index 8fc9d0c193..f469d87977 100644 --- a/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/util/TestFormatUtilsGroovy.groovy +++ b/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/util/TestFormatUtilsGroovy.groovy @@ -16,39 +16,23 @@ */ package org.apache.nifi.util - -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 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test import org.slf4j.Logger import org.slf4j.LoggerFactory import java.util.concurrent.TimeUnit -@RunWith(JUnit4.class) class TestFormatUtilsGroovy extends GroovyTestCase { private static final Logger logger = LoggerFactory.getLogger(TestFormatUtilsGroovy.class) - @BeforeClass + @BeforeAll static void setUpOnce() throws Exception { logger.metaClass.methodMissing = { String name, args -> logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") } } - @Before - void setUp() throws Exception { - - } - - @After - void tearDown() throws Exception { - - } - /** * New feature test */ diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java index deb4b9ed24..65f628aec1 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java @@ -17,7 +17,7 @@ package org.apache.nifi.processor; import org.apache.nifi.util.FormatUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.text.DecimalFormatSymbols; import java.util.concurrent.TimeUnit; diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/remote/io/TestCompressionInputOutputStreams.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/remote/io/TestCompressionInputOutputStreams.java index bd30a966b6..c994661d86 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/remote/io/TestCompressionInputOutputStreams.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/remote/io/TestCompressionInputOutputStreams.java @@ -16,7 +16,7 @@ */ 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.ByteArrayOutputStream; @@ -24,10 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import org.apache.nifi.remote.io.CompressionInputStream; -import org.apache.nifi.remote.io.CompressionOutputStream; - -import org.junit.Test; +import static org.junit.Assert.assertTrue; public class TestCompressionInputOutputStreams { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java index 1ab5f31cbb..d2fd26c53f 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java @@ -16,12 +16,15 @@ */ package org.apache.nifi.stream.io; +import org.junit.jupiter.api.Test; + import java.io.ByteArrayInputStream; 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 { final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.UTF_8)); @@ -53,6 +56,7 @@ public class ByteCountingInputStreamTest extends TestCase { assertEquals(bytesAtMark, bcis.getBytesRead()); } + @Test public void testAvailableShouldReturnCorrectCount() throws Exception { // Arrange final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/LimitingInputStreamTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/LimitingInputStreamTest.java index 475a6652c5..9d347bc762 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/LimitingInputStreamTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/LimitingInputStreamTest.java @@ -16,15 +16,21 @@ */ package org.apache.nifi.stream.io; +import org.junit.jupiter.api.Test; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; 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}; + @Test public void testReadLimitNotReached() throws IOException { final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 50); long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); @@ -32,6 +38,7 @@ public class LimitingInputStreamTest extends TestCase { assertFalse(is.hasReachedLimit()); } + @Test public void testReadLimitMatched() throws IOException { final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 10); long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); @@ -39,11 +46,11 @@ public class LimitingInputStreamTest extends TestCase { assertTrue(is.hasReachedLimit()); } + @Test public void testReadLimitExceeded() throws IOException { final LimitingInputStream is = new LimitingInputStream(new ByteArrayInputStream(TEST_BUFFER), 9); final long bytesRead = StreamUtils.copy(is, new ByteArrayOutputStream()); assertEquals(bytesRead, 9); assertTrue(is.hasReachedLimit()); } - } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java index 3f28412c66..a945814ae4 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/TestLeakyBucketThrottler.java @@ -16,8 +16,9 @@ */ package org.apache.nifi.stream.io; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -26,13 +27,16 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; -import org.junit.Ignore; -import org.junit.Test; +import java.util.concurrent.TimeUnit; -@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 { - @Test(timeout = 10000) + @Test + @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS) public void testOutputStreamInterface() throws IOException { // throttle rate at 1 MB/sec 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 { 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 { // throttle rate at 1 MB/sec try (final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024); diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/StreamDemarcatorTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/StreamDemarcatorTest.java index 16af077410..9002202338 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/StreamDemarcatorTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/StreamDemarcatorTest.java @@ -16,13 +16,7 @@ */ package org.apache.nifi.stream.io.util; -import static org.junit.Assert.assertArrayEquals; -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 org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -30,39 +24,26 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; 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") public class StreamDemarcatorTest { @Test public void validateInitializationFailure() { - try { - new StreamDemarcator(null, null, -1); - fail(); - } catch (IllegalArgumentException e) { - // success - } + assertThrows(IllegalArgumentException.class, () ->new StreamDemarcator(null, null, -1)); - try { - new StreamDemarcator(mock(InputStream.class), null, -1); - fail(); - } catch (IllegalArgumentException e) { - // success - } + assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), null, -1)); - try { - new StreamDemarcator(mock(InputStream.class), null, 10, -1); - fail(); - } catch (IllegalArgumentException e) { - // success - } + assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), null, 10, -1)); - try { - new StreamDemarcator(mock(InputStream.class), new byte[0], 10, 1); - fail(); - } catch (IllegalArgumentException e) { - // success - } + assertThrows(IllegalArgumentException.class, () -> new StreamDemarcator(mock(InputStream.class), new byte[0], 10, 1)); } @Test @@ -229,13 +210,13 @@ public class StreamDemarcatorTest { assertNull(scanner.nextToken()); } - @Test(expected = IOException.class) + @Test public void validateMaxBufferSize() throws IOException { String data = "THIS IS MY TEXTTHIS IS MY NEW TEXT THENTHIS IS MY NEWEST TEXT"; ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes()); StreamDemarcator scanner = new StreamDemarcator(is, "".getBytes(StandardCharsets.UTF_8), 20); scanner.nextToken(); - scanner.nextToken(); + assertThrows(IOException.class, () -> scanner.nextToken()); } @Test diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TestLineDemarcator.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TestLineDemarcator.java index 85cf85ebf9..88031d0b7b 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TestLineDemarcator.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TestLineDemarcator.java @@ -17,8 +17,8 @@ package org.apache.nifi.stream.io.util; import org.apache.nifi.stream.io.RepeatingInputStream; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -100,8 +100,9 @@ public class TestLineDemarcator { } @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 " + - "run in automated tests because we have no way to compare from one run to another, so it will only slow down automated tests.") + @Disabled("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 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 { final String lines = "The\nquick\nbrown\nfox\njumped\nover\nthe\nlazy\ndog.\r\n\n"; final byte[] bytes = lines.getBytes(StandardCharsets.UTF_8); diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java index fefd9eb4f3..cc0dc2f03d 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java @@ -16,32 +16,33 @@ */ package org.apache.nifi.stream.io.util; -import static org.junit.Assert.assertEquals; -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.mockito.Mockito.mock; +import org.apache.nifi.stream.io.util.TextLineDemarcator.OffsetInfo; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import org.apache.nifi.stream.io.util.TextLineDemarcator.OffsetInfo; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +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") public class TextLineDemarcatorTest { - @Test(expected = IllegalArgumentException.class) + @Test public void nullStream() { - new TextLineDemarcator(null); + assertThrows(IllegalArgumentException.class, () -> new TextLineDemarcator(null)); } - @Test(expected = IllegalArgumentException.class) + @Test public void illegalBufferSize() { - new TextLineDemarcator(mock(InputStream.class), -234); + assertThrows(IllegalArgumentException.class, () -> new TextLineDemarcator(mock(InputStream.class), -234)); } @Test diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/StringSelectorTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/StringSelectorTest.java index 06158f40b5..38256809c1 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/StringSelectorTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/StringSelectorTest.java @@ -16,7 +16,7 @@ */ package org.apache.nifi.util; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.junit.Assert.assertEquals; diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java index 0838e968b2..19672fa03e 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestNaiveSearchRingBuffer.java @@ -16,10 +16,10 @@ */ package org.apache.nifi.util; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestNaiveSearchRingBuffer { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java index fec1d8698f..5aec5431c9 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/classloader/TestClassLoaderUtils.java @@ -16,6 +16,8 @@ */ package org.apache.nifi.util.file.classloader; +import org.junit.jupiter.api.Test; + import java.io.FilenameFilter; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -24,12 +26,10 @@ import java.nio.file.Paths; import java.util.HashSet; import java.util.Set; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestClassLoaderUtils { @@ -46,25 +46,16 @@ public class TestClassLoaderUtils { final ClassLoader originalClassLoader = this.getClass().getClassLoader(); ClassLoader customClassLoader = ClassLoaderUtils.getCustomClassLoader(null,originalClassLoader, getJarFilenameFilter()); assertTrue(customClassLoader != null); - try{ - customClassLoader.loadClass("TestSuccess"); - }catch (ClassNotFoundException cex){ - assertTrue(cex.getLocalizedMessage().equals("TestSuccess")); - return; - } - fail("exception did not occur, class should not be found"); + ClassNotFoundException cex = assertThrows(ClassNotFoundException.class, () -> customClassLoader.loadClass("TestSuccess")); + assertTrue(cex.getLocalizedMessage().equals("TestSuccess")); } @Test public void testGetCustomClassLoaderWithInvalidPath() { final String jarFilePath = "src/test/resources/FakeTestClassLoaderUtils/TestSuccess.jar"; - try { - ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter()); - }catch(MalformedURLException mex){ - assertTrue(mex.getLocalizedMessage().equals("Path specified does not exist")); - return; - } - fail("exception did not occur, path should not exist"); + MalformedURLException mex = assertThrows(MalformedURLException.class, + () -> ClassLoaderUtils.getCustomClassLoader(jarFilePath, this.getClass().getClassLoader(), getJarFilenameFilter())); + assertTrue(mex.getLocalizedMessage().equals("Path specified does not exist")); } @Test @@ -99,10 +90,10 @@ public class TestClassLoaderUtils { assertEquals(1, urls.length); } - @Test(expected = MalformedURLException.class) + @Test public void testGetURLsForClasspathWithSomeNonExistentAndNoSuppression() throws MalformedURLException { 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 diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestCompoundUpdateMonitor.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestCompoundUpdateMonitor.java index 2c92ba4ac0..f96af10d31 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestCompoundUpdateMonitor.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestCompoundUpdateMonitor.java @@ -16,9 +16,7 @@ */ package org.apache.nifi.util.file.monitor; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileOutputStream; @@ -27,7 +25,9 @@ import java.io.OutputStream; import java.nio.file.Path; 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 { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestSynchronousFileWatcher.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestSynchronousFileWatcher.java index 43abc5beda..0dead15c90 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestSynchronousFileWatcher.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/file/monitor/TestSynchronousFileWatcher.java @@ -16,8 +16,7 @@ */ package org.apache.nifi.util.file.monitor; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.FileOutputStream; @@ -28,7 +27,8 @@ import java.nio.file.Path; import java.nio.file.Paths; 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 { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/locale/TestLocaleOfTestSuite.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/locale/TestLocaleOfTestSuite.java index a7e87a24cc..d2c586221f 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/locale/TestLocaleOfTestSuite.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/locale/TestLocaleOfTestSuite.java @@ -16,15 +16,16 @@ */ package org.apache.nifi.util.locale; -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Locale; 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 * 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 public void testLocaleCI_EN_AU() { 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 boolean isJava8 = Pattern.compile("1\\.8.+").matcher(runtimeJavaVersion).matches(); final String expected = (isJava8 ? "E" : "e"); // tested in Java 8 and Java 11 - Assert.assertEquals(expected, DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); - Assert.assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); + assertEquals(expected, DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); + assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); } /** @@ -53,10 +54,10 @@ public class TestLocaleOfTestSuite { @Test public void testLocaleCI_JA_JP() { 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()); - Assert.assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); + assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); + assertEquals("1,000", NumberFormat.getInstance(locale).format(1000)); } /** @@ -65,9 +66,9 @@ public class TestLocaleOfTestSuite { @Test public void testLocaleCI_FR_FR() { 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()); - Assert.assertEquals("1\u00a0000", NumberFormat.getInstance(locale).format(1000)); + assertEquals("E", DecimalFormatSymbols.getInstance(locale).getExponentSeparator()); + assertEquals("1\u00a0000", NumberFormat.getInstance(locale).format(1000)); } } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java index 4a5e16829e..aadc98b07c 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java @@ -16,12 +16,12 @@ */ package org.apache.nifi.util.text; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; import java.util.Map; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestRegexDateTimeMatcher { @@ -63,7 +63,7 @@ public class TestRegexDateTimeMatcher { final RegexDateTimeMatcher matcher = new RegexDateTimeMatcher.Compiler().compile(entry.getValue()); 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() + ">"); } } } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java index b01b495076..66934f5841 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestRingBuffer.java @@ -16,17 +16,17 @@ */ package org.apache.nifi.util.timebuffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.apache.nifi.util.RingBuffer; +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.concurrent.atomic.AtomicInteger; -import org.apache.nifi.util.RingBuffer; -import org.apache.nifi.util.RingBuffer.ForEachEvaluator; -import org.apache.nifi.util.RingBuffer.IterationDirection; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; /** * diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestTimedBuffer.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestTimedBuffer.java index 39ca3302f8..35ae85e2e9 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestTimedBuffer.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/timebuffer/TestTimedBuffer.java @@ -16,12 +16,12 @@ */ package org.apache.nifi.util.timebuffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import org.junit.jupiter.api.Test; import java.util.concurrent.TimeUnit; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; public class TestTimedBuffer { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java index 69bbe9b61e..6c75df8d13 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java @@ -16,26 +16,26 @@ */ 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.ValidationContext; import org.apache.nifi.components.ValidationResult; import org.apache.nifi.components.Validator; import org.apache.nifi.processor.util.StandardValidators; -import org.junit.Test; +import org.junit.jupiter.api.Test; 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 { @Test diff --git a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/StandardHashiCorpVaultCommunicationServiceIT.java b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/StandardHashiCorpVaultCommunicationServiceIT.java index adca3558c1..6261376dcd 100644 --- a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/StandardHashiCorpVaultCommunicationServiceIT.java +++ b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/StandardHashiCorpVaultCommunicationServiceIT.java @@ -17,14 +17,14 @@ package org.apache.nifi.vault.hashicorp; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultProperties; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * The simplest way to run this test is by installing Vault locally, then running: @@ -46,7 +46,7 @@ public class StandardHashiCorpVaultCommunicationServiceIT { private HashiCorpVaultCommunicationService vcs; - @Before + @BeforeEach public void init() { vcs = new StandardHashiCorpVaultCommunicationService(new HashiCorpVaultProperties.HashiCorpVaultPropertiesBuilder() .setAuthPropertiesFilename(System.getProperty("vault.auth.properties")) diff --git a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestHashiCorpVaultConfiguration.java b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestHashiCorpVaultConfiguration.java index e1ec59ca6d..9dd75f9b6f 100644 --- a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestHashiCorpVaultConfiguration.java +++ b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestHashiCorpVaultConfiguration.java @@ -19,11 +19,10 @@ package org.apache.nifi.vault.hashicorp; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultConfiguration; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultProperties; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultPropertySource; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.vault.authentication.ClientAuthentication; import org.springframework.vault.client.VaultEndpoint; import org.springframework.vault.support.SslConfiguration; @@ -39,6 +38,10 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class TestHashiCorpVaultConfiguration { public static final String VAULT_AUTHENTICATION = "vault.authentication"; public static final String VAULT_TOKEN = "vault.token"; @@ -61,21 +64,21 @@ public class TestHashiCorpVaultConfiguration { private HashiCorpVaultConfiguration config; - @BeforeClass + @BeforeAll public static void initClass() throws IOException { keystoreFile = Files.createTempFile("test", ".jks"); truststoreFile = Files.createTempFile("test", ".jks"); authProps = writeBasicVaultAuthProperties(); } - @AfterClass + @AfterAll public static void cleanUpClass() throws IOException { Files.deleteIfExists(keystoreFile); Files.deleteIfExists(truststoreFile); Files.deleteIfExists(authProps.toPath()); } - @Before + @BeforeEach public void init() throws IOException { propertiesBuilder = new HashiCorpVaultProperties.HashiCorpVaultPropertiesBuilder() .setUri(URI_VALUE) @@ -118,12 +121,12 @@ public class TestHashiCorpVaultConfiguration { config = new HashiCorpVaultConfiguration(new HashiCorpVaultPropertySource(propertiesBuilder.build())); VaultEndpoint endpoint = config.vaultEndpoint(); - Assert.assertEquals("localhost", endpoint.getHost()); - Assert.assertEquals(8200, endpoint.getPort()); - Assert.assertEquals(expectedScheme, endpoint.getScheme()); + assertEquals("localhost", endpoint.getHost()); + assertEquals(8200, endpoint.getPort()); + assertEquals(expectedScheme, endpoint.getScheme()); ClientAuthentication clientAuthentication = config.clientAuthentication(); - Assert.assertNotNull(clientAuthentication); + assertNotNull(clientAuthentication); } @Test @@ -146,20 +149,20 @@ public class TestHashiCorpVaultConfiguration { this.runTest("https"); SslConfiguration sslConfiguration = config.sslConfiguration(); - Assert.assertEquals(keystoreFile.toFile().getAbsolutePath(), sslConfiguration.getKeyStoreConfiguration().getResource().getFile().getAbsolutePath()); - Assert.assertEquals(KEYSTORE_PASSWORD_VALUE, new String(sslConfiguration.getKeyStoreConfiguration().getStorePassword())); - Assert.assertEquals(KEYSTORE_TYPE_VALUE, sslConfiguration.getKeyStoreConfiguration().getStoreType()); - Assert.assertEquals(truststoreFile.toFile().getAbsolutePath(), sslConfiguration.getTrustStoreConfiguration().getResource().getFile().getAbsolutePath()); - Assert.assertEquals(TRUSTSTORE_PASSWORD_VALUE, new String(sslConfiguration.getTrustStoreConfiguration().getStorePassword())); - Assert.assertEquals(TRUSTSTORE_TYPE_VALUE, sslConfiguration.getTrustStoreConfiguration().getStoreType()); - Assert.assertEquals(Arrays.asList(TLS_V_1_3_VALUE), sslConfiguration.getEnabledProtocols()); - Assert.assertEquals(Arrays.asList(TEST_CIPHER_SUITE_VALUE), sslConfiguration.getEnabledCipherSuites()); + assertEquals(keystoreFile.toFile().getAbsolutePath(), sslConfiguration.getKeyStoreConfiguration().getResource().getFile().getAbsolutePath()); + assertEquals(KEYSTORE_PASSWORD_VALUE, new String(sslConfiguration.getKeyStoreConfiguration().getStorePassword())); + assertEquals(KEYSTORE_TYPE_VALUE, sslConfiguration.getKeyStoreConfiguration().getStoreType()); + assertEquals(truststoreFile.toFile().getAbsolutePath(), sslConfiguration.getTrustStoreConfiguration().getResource().getFile().getAbsolutePath()); + assertEquals(TRUSTSTORE_PASSWORD_VALUE, new String(sslConfiguration.getTrustStoreConfiguration().getStorePassword())); + assertEquals(TRUSTSTORE_TYPE_VALUE, sslConfiguration.getTrustStoreConfiguration().getStoreType()); + assertEquals(Arrays.asList(TLS_V_1_3_VALUE), sslConfiguration.getEnabledProtocols()); + assertEquals(Arrays.asList(TEST_CIPHER_SUITE_VALUE), sslConfiguration.getEnabledCipherSuites()); } @Test public void testInvalidTLS() { propertiesBuilder.setUri(URI_VALUE.replace("http", "https")); - Assert.assertThrows(NullPointerException.class, () -> this.runTest("https")); + assertThrows(NullPointerException.class, () -> this.runTest("https")); } @Test @@ -171,7 +174,7 @@ public class TestHashiCorpVaultConfiguration { authProperties = writeVaultAuthProperties(props); propertiesBuilder.setAuthPropertiesFilename(authProperties.getAbsolutePath()); - Assert.assertThrows(IllegalArgumentException.class, () -> this.runTest("http")); + assertThrows(IllegalArgumentException.class, () -> this.runTest("http")); } finally { if (authProperties != null) { Files.deleteIfExists(authProperties.toPath()); @@ -187,7 +190,7 @@ public class TestHashiCorpVaultConfiguration { authProperties = writeVaultAuthProperties(props); propertiesBuilder.setAuthPropertiesFilename(authProperties.getAbsolutePath()); - Assert.assertThrows(IllegalArgumentException.class, () -> this.runTest("http")); + assertThrows(IllegalArgumentException.class, () -> this.runTest("http")); } finally { if (authProperties != null) { Files.deleteIfExists(authProperties.toPath()); diff --git a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestStandardHashiCorpVaultCommunicationService.java b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestStandardHashiCorpVaultCommunicationService.java index 785c5dc893..4eb14eb79d 100644 --- a/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestStandardHashiCorpVaultCommunicationService.java +++ b/nifi-commons/nifi-vault-utils/src/test/java/org/apache/nifi/vault/hashicorp/TestStandardHashiCorpVaultCommunicationService.java @@ -20,9 +20,9 @@ import org.apache.nifi.security.util.TemporaryKeyStoreBuilder; import org.apache.nifi.security.util.TlsConfiguration; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultProperties; import org.apache.nifi.vault.hashicorp.config.HashiCorpVaultSslProperties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.io.File; @@ -40,7 +40,7 @@ public class TestStandardHashiCorpVaultCommunicationService { private HashiCorpVaultSslProperties sslProperties; private File authProps; - @Before + @BeforeEach public void init() throws IOException { authProps = TestHashiCorpVaultConfiguration.writeBasicVaultAuthProperties(); @@ -52,7 +52,7 @@ public class TestStandardHashiCorpVaultCommunicationService { Mockito.when(properties.getSsl()).thenReturn(sslProperties); } - @After + @AfterEach public void cleanUp() throws IOException { Files.deleteIfExists(authProps.toPath()); } diff --git a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java b/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java index 38690a761d..2df2632297 100644 --- a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java +++ b/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java @@ -23,10 +23,10 @@ import org.mockito.junit.MockitoJUnitRunner; import javax.servlet.http.HttpServletRequest; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; -import static org.junit.Assert.assertEquals; @RunWith(MockitoJUnitRunner.class) public class WebUtilsTest { diff --git a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestBlockingQueuePool.java b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestBlockingQueuePool.java index 2492283b28..2fb0181d7a 100644 --- a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestBlockingQueuePool.java +++ b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestBlockingQueuePool.java @@ -17,16 +17,17 @@ package org.apache.nifi.wali; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class TestBlockingQueuePool { private static final Consumer DO_NOTHING = ab -> {}; diff --git a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestHashMapSnapshot.java b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestHashMapSnapshot.java index 692500e409..7733ca1d8a 100644 --- a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestHashMapSnapshot.java +++ b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestHashMapSnapshot.java @@ -17,9 +17,13 @@ package org.apache.nifi.wali; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.wali.DummyRecord; +import org.wali.DummyRecordSerde; +import org.wali.SerDeFactory; +import org.wali.SingletonSerDeFactory; +import org.wali.UpdateType; import java.io.File; import java.io.IOException; @@ -30,14 +34,10 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.wali.DummyRecord; -import org.wali.DummyRecordSerde; -import org.wali.SerDeFactory; -import org.wali.SingletonSerDeFactory; -import org.wali.UpdateType; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestHashMapSnapshot { @@ -45,7 +45,7 @@ public class TestHashMapSnapshot { private DummyRecordSerde serde; private SerDeFactory serdeFactory; - @Before + @BeforeEach public void setup() throws IOException { if (!storageDirectory.exists()) { Files.createDirectories(storageDirectory.toPath()); @@ -135,12 +135,7 @@ public class TestHashMapSnapshot { serde.setThrowOOMEAfterNSerializeEdits(3); - try { - snapshot.writeSnapshot(snapshot.prepareSnapshot(150L)); - Assert.fail("Expected OOME"); - } catch (final OutOfMemoryError oome) { - // expected - } + assertThrows(OutOfMemoryError.class, () -> snapshot.writeSnapshot(snapshot.prepareSnapshot(150L))); final SnapshotRecovery recovery = snapshot.recover(); assertEquals(25L, recovery.getMaxTransactionId()); @@ -184,12 +179,7 @@ public class TestHashMapSnapshot { serde.setThrowIOEAfterNSerializeEdits(3); for (int i = 0; i < 5; i++) { - try { - snapshot.writeSnapshot(snapshot.prepareSnapshot(150L)); - Assert.fail("Expected IOE"); - } catch (final IOException ioe) { - // expected - } + assertThrows(IOException.class, () -> snapshot.writeSnapshot(snapshot.prepareSnapshot(150L))); } final SnapshotRecovery recovery = snapshot.recover(); diff --git a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestLengthDelimitedJournal.java b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestLengthDelimitedJournal.java index c3572f96ba..3ea1e7dc3e 100644 --- a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestLengthDelimitedJournal.java +++ b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestLengthDelimitedJournal.java @@ -17,9 +17,8 @@ package org.apache.nifi.wali; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.wali.DummyRecord; import org.wali.DummyRecordSerde; import org.wali.SerDeFactory; @@ -48,11 +47,12 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; -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.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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestLengthDelimitedJournal { private final File journalFile = new File("target/testLengthDelimitedJournal/testJournal.journal"); @@ -61,7 +61,7 @@ public class TestLengthDelimitedJournal { private ObjectPool streamPool; private static final int BUFFER_SIZE = 4096; - @Before + @BeforeEach public void setupJournal() throws IOException { Files.deleteIfExists(journalFile.toPath()); @@ -217,28 +217,14 @@ public class TestLengthDelimitedJournal { final DummyRecord thirdRecord = new DummyRecord("1", UpdateType.UPDATE); final RecordLookup lookup = key -> secondRecord; - try { - journal.update(Collections.singleton(thirdRecord), lookup); - Assert.fail("Expected IOException"); - } catch (final IOException ioe) { - // expected - } + assertThrows(IOException.class, () -> journal.update(Collections.singleton(thirdRecord), lookup)); serde.setThrowIOEAfterNSerializeEdits(-1); final Collection records = Collections.singleton(thirdRecord); for (int i = 0; i < 10; i++) { - try { - journal.update(records, lookup); - Assert.fail("Expected IOException"); - } catch (final IOException expected) { - } - - try { - journal.fsync(); - Assert.fail("Expected IOException"); - } catch (final IOException expected) { - } + assertThrows(IOException.class, () -> journal.update(records, lookup)); + assertThrows(IOException.class, () -> journal.fsync()); } } } @@ -258,28 +244,14 @@ public class TestLengthDelimitedJournal { final DummyRecord thirdRecord = new DummyRecord("1", UpdateType.UPDATE); final RecordLookup lookup = key -> secondRecord; - try { - journal.update(Collections.singleton(thirdRecord), lookup); - Assert.fail("Expected OOME"); - } catch (final OutOfMemoryError oome) { - // expected - } + assertThrows(OutOfMemoryError.class, () ->journal.update(Collections.singleton(thirdRecord), lookup)); serde.setThrowOOMEAfterNSerializeEdits(-1); final Collection records = Collections.singleton(thirdRecord); for (int i = 0; i < 10; i++) { - try { - journal.update(records, lookup); - Assert.fail("Expected IOException"); - } catch (final IOException expected) { - } - - try { - journal.fsync(); - Assert.fail("Expected IOException"); - } catch (final IOException expected) { - } + assertThrows(IOException.class, () -> journal.update(records, lookup)); + assertThrows(IOException.class, () -> journal.fsync()); } } } diff --git a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestSequentialAccessWriteAheadLog.java b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestSequentialAccessWriteAheadLog.java index 6d24445093..6eee5eedd1 100644 --- a/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestSequentialAccessWriteAheadLog.java +++ b/nifi-commons/nifi-write-ahead-log/src/test/java/org/apache/nifi/wali/TestSequentialAccessWriteAheadLog.java @@ -17,11 +17,9 @@ package org.apache.nifi.wali; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.wali.DummyRecord; import org.wali.DummyRecordSerde; import org.wali.SerDeFactory; @@ -45,20 +43,17 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestSequentialAccessWriteAheadLog { - @Rule - public TestName testName = new TestName(); - - @Test - public void testUpdateWithExternalFile() throws IOException { + public void testUpdateWithExternalFile(TestInfo testInfo) throws IOException { final DummyRecordSerde serde = new DummyRecordSerde(); - final SequentialAccessWriteAheadLog repo = createWriteRepo(serde); + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo, serde); final List records = new ArrayList<>(); for (int i = 0; i < 350_000; i++) { @@ -71,7 +66,7 @@ public class TestSequentialAccessWriteAheadLog { assertEquals(1, serde.getExternalFileReferences().size()); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recovered = recoveryRepo.recoverRecords(); // ensure that we get the same records back, but the order may be different, so wrap both collections @@ -80,9 +75,9 @@ public class TestSequentialAccessWriteAheadLog { } @Test - public void testUpdateWithExternalFileFollowedByInlineUpdate() throws IOException { + public void testUpdateWithExternalFileFollowedByInlineUpdate(TestInfo testInfo) throws IOException { final DummyRecordSerde serde = new DummyRecordSerde(); - final SequentialAccessWriteAheadLog repo = createWriteRepo(serde); + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo, serde); final List records = new ArrayList<>(); for (int i = 0; i < 350_000; i++) { @@ -98,7 +93,7 @@ public class TestSequentialAccessWriteAheadLog { assertEquals(1, serde.getExternalFileReferences().size()); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recovered = recoveryRepo.recoverRecords(); // ensure that we get the same records back, but the order may be different, so wrap both collections @@ -109,8 +104,8 @@ public class TestSequentialAccessWriteAheadLog { } @Test - public void testRecoverWithNoCheckpoint() throws IOException { - final SequentialAccessWriteAheadLog repo = createWriteRepo(); + public void testRecoverWithNoCheckpoint(TestInfo testInfo) throws IOException { + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo); final List records = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -121,7 +116,7 @@ public class TestSequentialAccessWriteAheadLog { repo.update(records, false); repo.shutdown(); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recovered = recoveryRepo.recoverRecords(); // ensure that we get the same records back, but the order may be different, so wrap both collections @@ -130,8 +125,8 @@ public class TestSequentialAccessWriteAheadLog { } @Test - public void testRecoverWithNoJournalUpdates() throws IOException { - final SequentialAccessWriteAheadLog repo = createWriteRepo(); + public void testRecoverWithNoJournalUpdates(TestInfo testInfo) throws IOException { + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo); final List records = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -143,7 +138,7 @@ public class TestSequentialAccessWriteAheadLog { repo.checkpoint(); repo.shutdown(); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recovered = recoveryRepo.recoverRecords(); // ensure that we get the same records back, but the order may be different, so wrap both collections @@ -152,8 +147,8 @@ public class TestSequentialAccessWriteAheadLog { } @Test - public void testRecoverWithMultipleCheckpointsBetweenJournalUpdate() throws IOException { - final SequentialAccessWriteAheadLog repo = createWriteRepo(); + public void testRecoverWithMultipleCheckpointsBetweenJournalUpdate(TestInfo testInfo) throws IOException { + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo); final List records = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -173,7 +168,7 @@ public class TestSequentialAccessWriteAheadLog { repo.shutdown(); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recovered = recoveryRepo.recoverRecords(); // what we expect is the same as what we updated with, except we don't want the DummyRecord for CREATE 4 @@ -187,9 +182,9 @@ public class TestSequentialAccessWriteAheadLog { assertEquals(expected, new HashSet<>(recovered)); } - private SequentialAccessWriteAheadLog createRecoveryRepo() throws IOException { + private SequentialAccessWriteAheadLog createRecoveryRepo(TestInfo testInfo) throws IOException { final File targetDir = new File("target"); - final File storageDir = new File(targetDir, testName.getMethodName()); + final File storageDir = new File(targetDir, testInfo.getTestMethod().get().getName()); final DummyRecordSerde serde = new DummyRecordSerde(); final SerDeFactory serdeFactory = new SingletonSerDeFactory<>(serde); @@ -198,13 +193,13 @@ public class TestSequentialAccessWriteAheadLog { return repo; } - private SequentialAccessWriteAheadLog createWriteRepo() throws IOException { - return createWriteRepo(new DummyRecordSerde()); + private SequentialAccessWriteAheadLog createWriteRepo(TestInfo testInfo) throws IOException { + return createWriteRepo(testInfo, new DummyRecordSerde()); } - private SequentialAccessWriteAheadLog createWriteRepo(final DummyRecordSerde serde) throws IOException { + private SequentialAccessWriteAheadLog createWriteRepo(final TestInfo testInfo, final DummyRecordSerde serde) throws IOException { final File targetDir = new File("target"); - final File storageDir = new File(targetDir, testName.getMethodName()); + final File storageDir = new File(targetDir, testInfo.getTestMethod().get().getName()); deleteRecursively(storageDir); assertTrue(storageDir.mkdirs()); @@ -224,8 +219,8 @@ public class TestSequentialAccessWriteAheadLog { * are able to checkpoint, then update journals, and then recover updates to both the checkpoint and the journals. */ @Test - public void testUpdateThenRecover() throws IOException { - final SequentialAccessWriteAheadLog repo = createWriteRepo(); + public void testUpdateThenRecover(TestInfo testInfo) throws IOException { + final SequentialAccessWriteAheadLog repo = createWriteRepo(testInfo); final DummyRecord firstCreate = new DummyRecord("0", UpdateType.CREATE); repo.update(Collections.singleton(firstCreate), false); @@ -276,7 +271,7 @@ public class TestSequentialAccessWriteAheadLog { repo.shutdown(); - final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(); + final SequentialAccessWriteAheadLog recoveryRepo = createRecoveryRepo(testInfo); final Collection recoveredRecords = recoveryRepo.recoverRecords(); // We should now have records: @@ -309,7 +304,7 @@ public class TestSequentialAccessWriteAheadLog { @Test - @Ignore("For manual performance testing") + @Disabled("For manual performance testing") public void testUpdatePerformance() throws IOException, InterruptedException { final Path path = Paths.get("target/sequential-access-repo"); deleteRecursively(path.toFile()); @@ -343,12 +338,7 @@ public class TestSequentialAccessWriteAheadLog { batch.add(record); } - try { - repo.update(batch, false); - } catch (Throwable t) { - t.printStackTrace(); - Assert.fail(t.toString()); - } + assertThrows(Throwable.class, () -> repo.update(batch, false)); } } }); diff --git a/nifi-commons/nifi-write-ahead-log/src/test/java/org/wali/TestMinimalLockingWriteAheadLog.java b/nifi-commons/nifi-write-ahead-log/src/test/java/org/wali/TestMinimalLockingWriteAheadLog.java index 5f1c589768..7954553110 100644 --- a/nifi-commons/nifi-write-ahead-log/src/test/java/org/wali/TestMinimalLockingWriteAheadLog.java +++ b/nifi-commons/nifi-write-ahead-log/src/test/java/org/wali/TestMinimalLockingWriteAheadLog.java @@ -16,9 +16,10 @@ */ package org.wali; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.BufferedInputStream; import java.io.DataInputStream; @@ -48,11 +49,12 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; @SuppressWarnings("deprecation") public class TestMinimalLockingWriteAheadLog { @@ -140,7 +142,7 @@ public class TestMinimalLockingWriteAheadLog { } @Test - @Ignore("For manual performance testing") + @Disabled("For manual performance testing") public void testUpdatePerformance() throws IOException, InterruptedException { final int numPartitions = 16; @@ -164,25 +166,17 @@ public class TestMinimalLockingWriteAheadLog { for (int j = 0; j < 2; j++) { for (int i = 0; i < numThreads; i++) { - final Thread t = new Thread(new Runnable() { - @Override - public void run() { - final List batch = new ArrayList<>(); + final Thread t = new Thread(() -> { + final List batch = new ArrayList<>(); - for (int i = 0; i < updateCountPerThread / batchSize; i++) { - batch.clear(); - for (int j = 0; j < batchSize; j++) { - final DummyRecord record = new DummyRecord(String.valueOf(i), UpdateType.CREATE); - batch.add(record); - } - - try { - repo.update(batch, false); - } catch (Throwable t) { - t.printStackTrace(); - Assert.fail(t.toString()); - } + for (int i1 = 0; i1 < updateCountPerThread / batchSize; i1++) { + batch.clear(); + for (int j1 = 0; j1 < batchSize; j1++) { + final DummyRecord record = new DummyRecord(String.valueOf(i1), UpdateType.CREATE); + batch.add(record); } + + assertDoesNotThrow(() -> repo.update(batch, false)); } }); @@ -253,34 +247,20 @@ public class TestMinimalLockingWriteAheadLog { long expectedSize = sizeOf(path.toFile()); for (int i = 0; i < 1000; i++) { - try { - final DummyRecord record = new DummyRecord(String.valueOf(i), UpdateType.CREATE); - repo.update(Collections.singleton(record), false); - Assert.fail("Expected IOE but it didn't happen"); - } catch (final IOException ioe) { - // will get IOException because all Partitions have been blacklisted - } + final DummyRecord record = new DummyRecord(String.valueOf(i), UpdateType.CREATE); + assertThrows(IOException.class, () -> repo.update(Collections.singleton(record), false)); } long newSize = sizeOf(path.toFile()); assertEquals(expectedSize, newSize); - try { - repo.checkpoint(); - Assert.fail("Expected OOME but it didn't happen"); - } catch (final OutOfMemoryError oome) { - } + assertThrows(OutOfMemoryError.class, () -> repo.checkpoint()); expectedSize = sizeOf(path.toFile()); for (int i = 0; i < 100000; i++) { - try { - final DummyRecord record = new DummyRecord(String.valueOf(i), UpdateType.CREATE); - repo.update(Collections.singleton(record), false); - Assert.fail("Expected IOE but it didn't happen"); - } catch (final IOException ioe) { - // will get IOException because all Partitions have been blacklisted - } + final DummyRecord record = new DummyRecord(String.valueOf(i), UpdateType.CREATE); + assertThrows(IOException.class, () -> repo.update(Collections.singleton(record), false)); } newSize = sizeOf(path.toFile()); @@ -300,7 +280,7 @@ public class TestMinimalLockingWriteAheadLog { * @throws InterruptedException if a thread is interrupted */ @Test - @Ignore + @Disabled public void tryToCauseThreadingIssue() throws IOException, InterruptedException { System.setProperty("org.slf4j.simpleLogger.log.org.wali", "INFO"); @@ -314,28 +294,20 @@ public class TestMinimalLockingWriteAheadLog { final AtomicReference> writeRepoRef = new AtomicReference<>(); final AtomicBoolean checkpointing = new AtomicBoolean(false); - final Thread bgThread = new Thread(new Runnable() { - @Override - public void run() { - while (true) { - checkpointing.set(true); + final Thread bgThread = new Thread(() -> { + while (true) { + checkpointing.set(true); - final WriteAheadRepository repo = writeRepoRef.get(); - if (repo != null) { - try { - repo.checkpoint(); - } catch (IOException e) { - e.printStackTrace(); - Assert.fail(); - } - } + final WriteAheadRepository repo = writeRepoRef.get(); + if (repo != null) { + assertDoesNotThrow(() -> repo.checkpoint()); + } - checkpointing.set(false); + checkpointing.set(false); - try { - TimeUnit.SECONDS.sleep(5); - } catch (InterruptedException e) { - } + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException e) { } } }); @@ -453,12 +425,7 @@ public class TestMinimalLockingWriteAheadLog { repo.update(firstTransaction, true); repo.update(secondTransaction, true); - try { - repo.update(thirdTransaction, true); - Assert.fail("Did not throw IOException on third transaction"); - } catch (final IOException e) { - // expected behavior. - } + assertThrows(IOException.class, () -> repo.update(thirdTransaction, true)); repo.shutdown(); @@ -667,33 +634,18 @@ public class TestMinimalLockingWriteAheadLog { repo.update(firstTransaction, true); - try { - repo.update(secondTransaction, true); - Assert.fail("Did not throw IOException on second transaction"); - } catch (final IOException e) { - // expected behavior. - } + assertThrows(IOException.class, () -> repo.update(secondTransaction, true)); for (int i = 0; i < 4; i++) { - try { - repo.update(thirdTransaction, true); - Assert.fail("Did not throw IOException on third transaction"); - } catch (final IOException e) { - // expected behavior. - } + assertThrows(IOException.class, () -> repo.update(thirdTransaction, true)); } serde.setThrowIOEAfterNSerializeEdits(-1); final List fourthTransaction = new ArrayList<>(); fourthTransaction.add(new DummyRecord("1", UpdateType.DELETE)); - try { - repo.update(fourthTransaction, true); - Assert.fail("Successfully updated repo for 4th transaction"); - } catch (final IOException e) { - // expected behavior - assertTrue(e.getMessage().contains("All Partitions have been blacklisted")); - } + IOException e = assertThrows(IOException.class, () -> repo.update(fourthTransaction, true)); + assertTrue(e.getMessage().contains("All Partitions have been blacklisted")); repo.shutdown(); serde.setThrowIOEAfterNSerializeEdits(-1); @@ -817,14 +769,10 @@ public class TestMinimalLockingWriteAheadLog { writeRepo.update(Collections.singleton(new SimpleRecord(1L, 1L)), false); writeRepo.update(Collections.singleton(new SimpleRecord(2L, 2L)), false); - try { - // Use a size of 8194 because the BufferedOutputStream has a buffer size of 8192 and we want - // to exceed this for testing purposes. - writeRepo.update(Collections.singleton(new SimpleRecord(3L, 8194L)), false); - Assert.fail("Expected IOException but did not get it"); - } catch (final IOException ioe) { - // expected behavior - } + // Use a size of 8194 because the BufferedOutputStream has a buffer size of 8192 and we want + // to exceed this for testing purposes. + assertThrows(IOException.class, + () -> writeRepo.update(Collections.singleton(new SimpleRecord(3L, 8194L)), false)); final Path partitionDir = path.resolve("partition-0"); final File journalFile = partitionDir.toFile().listFiles()[0]; @@ -835,7 +783,7 @@ public class TestMinimalLockingWriteAheadLog { // Ensure that calling shutdown() didn't write anything to the journal file final long newJournalSize = journalFile.length(); - assertEquals("Calling Shutdown wrote " + (newJournalSize - journalFileSize) + " bytes to the journal file", newJournalSize, journalFile.length()); + assertEquals(newJournalSize, journalFile.length(), "Calling Shutdown wrote " + (newJournalSize - journalFileSize) + " bytes to the journal file"); } private void verifyBlacklistedJournalContents(final File journalFile, final SerDe serde) throws IOException { @@ -940,15 +888,10 @@ public class TestMinimalLockingWriteAheadLog { @Override public void run() { - try { - int counter = 0; - for (final List list : records) { - final boolean forceSync = (++counter == records.size()); - repo.update(list, forceSync); - } - } catch (IOException e) { - Assert.fail("Failed to update: " + e.toString()); - e.printStackTrace(); + int counter = 0; + for (final List list : records) { + final boolean forceSync = (++counter == records.size()); + assertDoesNotThrow(() -> repo.update(list, forceSync)); } } } diff --git a/pom.xml b/pom.xml index 2fc31ddcc3..2d4a4d302a 100644 --- a/pom.xml +++ b/pom.xml @@ -111,7 +111,7 @@ 1.9.6 2.33 1.2.6 - 2.28.2 + 3.11.2