Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-14131 Expanded PMD coverage to include unit and integration tests and fixed all subsequent violations. #9610

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ShutdownHookTest {
private ShutdownHook shutdownHook;

@Test
@SuppressWarnings("PMD.DontCallThreadRun")
void testRunShouldShutdownSchedulersAndProcesses() {
when(runner.getPeriodicStatusReporterManager()).thenReturn(periodicStatusReporterManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public void testConvertToBigDecimalWhenInputStringIsInvalid() {

@Test
public void testConvertToBigDecimalWhenUnsupportedType() {
assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType(new ArrayList<Double>(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10),
assertThrows(IllegalTypeConversionException.class, () -> DataTypeUtils.convertType(new ArrayList<>(), RecordFieldType.DECIMAL.getDecimalDataType(30, 10),
null, StandardCharsets.UTF_8));
}

Expand Down Expand Up @@ -716,8 +716,8 @@ public void testConvertDataTypeBigint() {
final Function<Object, BigInteger> toBigInteger = v -> (BigInteger) DataTypeUtils.convertType(v, RecordFieldType.BIGINT.getDataType(), "field");
assertEquals(new BigInteger("12345678901234567890"), toBigInteger.apply(new BigInteger("12345678901234567890")));
assertEquals(new BigInteger("1234567890123456789"), toBigInteger.apply(1234567890123456789L));
assertEquals(new BigInteger("1"), toBigInteger.apply(1));
assertEquals(new BigInteger("1"), toBigInteger.apply((short) 1));
assertEquals(BigInteger.ONE, toBigInteger.apply(1));
assertEquals(BigInteger.ONE, toBigInteger.apply((short) 1));
// Decimals are truncated.
assertEquals(new BigInteger("3"), toBigInteger.apply(3.4f));
assertEquals(new BigInteger("3"), toBigInteger.apply(3.9f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public void testReadExactlyOnceFields() throws IOException {
fields.add(new MapRecordField("map",
new SimpleRecordField("key", FieldType.STRING, Repetition.EXACTLY_ONE),
new SimpleRecordField("value", FieldType.STRING, Repetition.ZERO_OR_ONE), Repetition.EXACTLY_ONE));
fields.add(new UnionRecordField("union1", Repetition.EXACTLY_ONE, Arrays.asList(new RecordField[] {
fields.add(new UnionRecordField("union1", Repetition.EXACTLY_ONE, List.of(
new SimpleRecordField("one", FieldType.STRING, Repetition.EXACTLY_ONE),
new SimpleRecordField("two", FieldType.INT, Repetition.EXACTLY_ONE)
})));
fields.add(new UnionRecordField("union2", Repetition.EXACTLY_ONE, Arrays.asList(new RecordField[] {
)));
fields.add(new UnionRecordField("union2", Repetition.EXACTLY_ONE, List.of(
new SimpleRecordField("one", FieldType.STRING, Repetition.EXACTLY_ONE),
new SimpleRecordField("two", FieldType.INT, Repetition.EXACTLY_ONE)
})));
)));
final RecordSchema schema = new RecordSchema(fields);

final SchemaRecordReader reader = SchemaRecordReader.fromSchema(schema, new NoOpFieldCache());
Expand Down Expand Up @@ -156,14 +156,14 @@ public void testReadZeroOrOneFields() throws IOException {
fields.add(new MapRecordField("map present",
new SimpleRecordField("key", FieldType.STRING, Repetition.ZERO_OR_ONE),
new SimpleRecordField("value", FieldType.STRING, Repetition.ZERO_OR_MORE), Repetition.ZERO_OR_ONE));
fields.add(new UnionRecordField("union", Repetition.ZERO_OR_ONE, Arrays.asList(new RecordField[] {
fields.add(new UnionRecordField("union", Repetition.ZERO_OR_ONE, List.of(
new SimpleRecordField("one", FieldType.STRING, Repetition.EXACTLY_ONE),
new SimpleRecordField("two", FieldType.INT, Repetition.EXACTLY_ONE)
})));
fields.add(new UnionRecordField("union present", Repetition.ZERO_OR_ONE, Arrays.asList(new RecordField[] {
)));
fields.add(new UnionRecordField("union present", Repetition.ZERO_OR_ONE, List.of(
new SimpleRecordField("one", FieldType.STRING, Repetition.EXACTLY_ONE),
new SimpleRecordField("two", FieldType.INT, Repetition.ZERO_OR_MORE)
})));
)));

final RecordSchema schema = new RecordSchema(fields);

Expand Down Expand Up @@ -266,9 +266,9 @@ public void testReadZeroOrOneFields() throws IOException {
assertTrue(map.containsKey(null));
assertTrue(map.containsKey("key1"));

final List<String> key1Values = Arrays.asList(new String[] {"one", "two"});
final List<String> key1Values = List.of("one", "two");
assertEquals(key1Values, map.get("key1"));
final List<String> nullKeyValues = Arrays.asList(new String[] {"three"});
final List<String> nullKeyValues = List.of("three");
assertEquals(nullKeyValues, map.get(null));

final List<Integer> unionValues = (List<Integer>) valueMap.get("union present");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testRoundTrip() throws IOException {
final FieldMapRecord complexRecord2 = new FieldMapRecord(complexMap2, new RecordSchema(longStringField, longField));

// Create a Union Field that indicates that the type could be either 'complex 1' or 'complex 2'
final UnionRecordField unionRecordField = new UnionRecordField("union", Repetition.ZERO_OR_MORE, Arrays.asList(new RecordField[] {complexField1, complexField2}));
final UnionRecordField unionRecordField = new UnionRecordField("union", Repetition.ZERO_OR_MORE, List.of(complexField1, complexField2));

// Create a Record Schema
final List<RecordField> fields = new ArrayList<>();
Expand Down Expand Up @@ -123,9 +123,9 @@ public void testRoundTrip() throws IOException {
values.put(createField("long string present", FieldType.LONG_STRING), "Long Hello");
values.put(createField("complex present", FieldType.COMPLEX), new FieldMapRecord(complexFieldMap, new RecordSchema(colorField, fruitField)));
values.put(new MapRecordField("map present", createField("key", FieldType.STRING), createField("value", FieldType.INT), Repetition.EXACTLY_ONE), simpleMap);
values.put(unionRecordField, Arrays.asList(new NamedValue[] {
values.put(unionRecordField, List.of(
new NamedValue("complex1", complexRecord1),
new NamedValue("complex2", complexRecord2)}));
new NamedValue("complex2", complexRecord2)));

final FieldMapRecord originalRecord = new FieldMapRecord(values, schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EphemeralKeyStoreBuilderTest {

private static final String KEY_ALGORITHM = "RSA";

private static final char[] KEY_PASSWORD = EphemeralKeyStoreBuilderTest.class.getSimpleName().toCharArray();;
private static final char[] KEY_PASSWORD = EphemeralKeyStoreBuilderTest.class.getSimpleName().toCharArray();

private static KeyPair keyPair;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -48,7 +46,7 @@ public class TestSiteInfoProvider {
@Test
public void testSecure() throws Exception {

final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[]{"https://node1:8443", "https://node2:8443"}));
final Set<String> expectedClusterUrl = Set.of("https://node1:8443", "https://node2:8443");
final String expectedActiveClusterUrl = "https://node2:8443/nifi-api";
final SSLContext expectedSslConText = mock(SSLContext.class);
final HttpProxy expectedHttpProxy = mock(HttpProxy.class);
Expand Down Expand Up @@ -130,7 +128,7 @@ public void testSecure() throws Exception {
@Test
public void testPlain() throws Exception {

final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[]{"http://node1:8443, http://node2:8443"}));
final Set<String> expectedClusterUrl = Set.of("http://node1:8443, http://node2:8443");
final String expectedActiveClusterUrl = "http://node2:8443/nifi-api";

final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
Expand Down Expand Up @@ -168,7 +166,7 @@ public void testPlain() throws Exception {
@Test
public void testConnectException() throws Exception {

final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[]{"http://node1:8443, http://node2:8443"}));
final Set<String> expectedClusterUrl = Set.of("http://node1:8443, http://node2:8443");

final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
siteInfoProvider.setClusterUrls(expectedClusterUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.apache.nifi.remote.protocol.Response;
import org.apache.nifi.remote.protocol.ResponseCode;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -57,7 +55,6 @@

public class TestSocketClientTransaction {

private Logger logger = LoggerFactory.getLogger(TestSocketClientTransaction.class);
private FlowFileCodec codec = new StandardFlowFileCodec();

private SocketClientTransaction getClientTransaction(ByteArrayInputStream bis, ByteArrayOutputStream bos, TransferDirection direction) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public void testAvailableShouldReturnCorrectCount() throws Exception {
int availableBytes = bcis.available();
assertEquals(ALPHABET.length() - 2, availableBytes);

bcis.skip(24);
final long expectedSkip = 24;
final long actualSkip = bcis.skip(expectedSkip);
assertEquals(expectedSkip, actualSkip);

// Assert
int finalAvailableBytes = bcis.available();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public void testComposite() throws Exception {
// Assert integers
final Object[] numbers = (Object[]) record.getValue("numbers");
assertEquals(4, numbers.length);
assertEquals(new BigInteger("0"), numbers[0]);
assertEquals(new BigInteger("1"), numbers[1]);
assertEquals(new BigInteger("2"), numbers[2]);
assertEquals(BigInteger.ZERO, numbers[0]);
assertEquals(BigInteger.ONE, numbers[1]);
assertEquals(BigInteger.TWO, numbers[2]);
assertEquals(new BigInteger("3"), numbers[3]);

// Assert unordered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,23 @@ public void testLocalStatePersistence() throws IOException {
final PutS3Object.MultipartState state1new = processor.getLocalStateIfInS3(mockClient, bucket, cacheKey1);
assertEquals("", state1new.getUploadId());
assertEquals(0L, state1new.getFilePosition().longValue());
assertEquals(new ArrayList<PartETag>(), state1new.getPartETags());
assertEquals(new ArrayList<>(), state1new.getPartETags());
assertEquals(0L, state1new.getPartSize().longValue());
assertEquals(StorageClass.fromValue(StorageClass.Standard.toString()), state1new.getStorageClass());
assertEquals(0L, state1new.getContentLength().longValue());

final PutS3Object.MultipartState state2new = processor.getLocalStateIfInS3(mockClient, bucket, cacheKey2);
assertEquals("1234", state2new.getUploadId());
assertEquals(0L, state2new.getFilePosition().longValue());
assertEquals(new ArrayList<PartETag>(), state2new.getPartETags());
assertEquals(new ArrayList<>(), state2new.getPartETags());
assertEquals(0L, state2new.getPartSize().longValue());
assertEquals(StorageClass.fromValue(StorageClass.Standard.toString()), state2new.getStorageClass());
assertEquals(1234L, state2new.getContentLength().longValue());

final PutS3Object.MultipartState state3new = processor.getLocalStateIfInS3(mockClient, bucket, cacheKey3);
assertEquals("5678", state3new.getUploadId());
assertEquals(0L, state3new.getFilePosition().longValue());
assertEquals(new ArrayList<PartETag>(), state3new.getPartETags());
assertEquals(new ArrayList<>(), state3new.getPartETags());
assertEquals(0L, state3new.getPartSize().longValue());
assertEquals(StorageClass.fromValue(StorageClass.Standard.toString()), state3new.getStorageClass());
assertEquals(5678L, state3new.getContentLength().longValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.nifi.processors.evtx.parser.bxml;

import org.apache.nifi.processors.evtx.parser.BinaryReader;
import org.apache.nifi.processors.evtx.parser.BxmlNodeVisitor;
import org.apache.nifi.processors.evtx.parser.bxml.value.NullTypeNode;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -34,7 +33,6 @@

public class AttributeNodeTest extends BxmlNodeWithTokenAndStringTestBase {
public static final String ATTRIBUTE_NAME = "AttributeName";
private BinaryReader binaryReader;
private AttributeNode attributeNode;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void testInt9ShouldBeLong() throws SQLException, IllegalArgumentException

@Test
public void testConvertToAvroStreamForBigDecimal() throws SQLException, IOException {
final BigDecimal bigDecimal = new BigDecimal(12345D);
final BigDecimal bigDecimal = new BigDecimal("12345");
// If db returns a precision, it should be used.
testConvertToAvroStreamForBigDecimal(bigDecimal, 38, 10, 38, 0);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ public void testConvertToAvroStreamForBigDecimalWithZeroScale() throws SQLExcept
final int defaultScale = 15;

final BigDecimal bigDecimal = new BigDecimal("1.123", new MathContext(dbPrecision));
final BigDecimal expectedValue = new BigDecimal("1");
final BigDecimal expectedValue = BigDecimal.ONE;
testConvertToAvroStreamForBigDecimal(bigDecimal, expectedValue, dbPrecision, dbScale, defaultPrecision, defaultScale, expectedPrecision, expectedScale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class TestFileInfo {

@Test
public void testPermissionModeToString() {
String rwxPerm = FileInfo.permissionToString(0567);
String rwxPerm = FileInfo.permissionToString(Integer.decode("0567"));
assertEquals("r-xrw-rwx", rwxPerm);

// Test with sticky bit
rwxPerm = FileInfo.permissionToString(01567);
rwxPerm = FileInfo.permissionToString(Integer.decode("01567"));
assertEquals("r-xrw-rwx", rwxPerm);

rwxPerm = FileInfo.permissionToString(03);
rwxPerm = FileInfo.permissionToString(Integer.decode("03"));
assertEquals("-------wx", rwxPerm);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@
import org.glassfish.jersey.internal.guava.Predicates;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -70,8 +68,6 @@ public class TestAbstractListProcessor {

private ConcreteListProcessor proc;
private TestRunner runner;
@TempDir
private Path testFolder;

@BeforeEach
public void setup() {
Expand Down Expand Up @@ -363,7 +359,6 @@ private void assertVerificationOutcome(final Outcome expectedOutcome, final Stri

static class EphemeralMapCacheClientService extends AbstractControllerService implements DistributedMapCacheClient {
private final Map<Object, Object> stored = new HashMap<>();
private int fetchCount = 0;

@Override
public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
Expand All @@ -388,7 +383,6 @@ public <K, V> void put(K key, V value, Serializer<K> keySerializer, Serializer<V
@Override
@SuppressWarnings("unchecked")
public <K, V> V get(K key, Serializer<K> keySerializer, Deserializer<V> valueDeserializer) throws IOException {
fetchCount++;
return (V) stored.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.apache.nifi.processor.util.pattern.TestExceptionHandler.ExternalProcedure;
import org.apache.nifi.util.MockComponentLog;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,8 +33,6 @@

public class TestRollbackOnFailure {

private static final Logger logger = LoggerFactory.getLogger(TestRollbackOnFailure.class);

/**
* This can be an example for how to compose an ExceptionHandler instance by reusable functions.
* @param logger used to log messages within functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public void testExtractAvroSchemaWithDefaults() {
assertEquals(42L, avroSchema.getField("long").defaultVal());
assertEquals(2.4D, (float) avroSchema.getField("float").defaultVal(), 0.002D); // Even though we provide a Float, avro converts it into a Double value.
assertEquals(28.1D, (double) avroSchema.getField("double").defaultVal(), 0.002D);
assertEquals(new ArrayList<String>(), avroSchema.getField("stringArray").defaultVal());
assertEquals(new ArrayList<Integer>(), avroSchema.getField("intArray").defaultVal());
assertEquals(new ArrayList<>(), avroSchema.getField("stringArray").defaultVal());
assertEquals(new ArrayList<>(), avroSchema.getField("intArray").defaultVal());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class InferenceSchemaStrategyTest {
{"double", 1D, RecordFieldType.DOUBLE.getDataType()},
{"date", new Date(), RecordFieldType.DATE.getDataType()},
{"decimal", BigDecimal.valueOf(123.456D), RecordFieldType.DECIMAL.getDecimalDataType(6, 3)},
{"array", new ArrayList<String>(), RecordFieldType.ARRAY.getDataType()},
{"array", new ArrayList<>(), RecordFieldType.ARRAY.getDataType()},

// date subclasses
{"time", new Time(System.currentTimeMillis()), RecordFieldType.DATE.getDataType()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ public FileStatus newFile(Path p, FsPermission permission) {
}

public FileStatus newDir(Path p) {
return new FileStatus(DIR_LENGTH, true, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms((short) 0755), "owner", "group", (Path) null, p, true, false, false);
return new FileStatus(DIR_LENGTH, true, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms(Integer.decode("0755").shortValue()), "owner", "group", (Path) null, p, true, false, false);
}

public FileStatus newFile(String p) {
return new FileStatus(FILE_LENGTH, false, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms((short) 0644), "owner", "group", new Path(p));
return new FileStatus(FILE_LENGTH, false, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms(Integer.decode("0644").shortValue()), "owner", "group", new Path(p));
}
public FileStatus newDir(String p) {
return new FileStatus(DIR_LENGTH, true, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms((short) 0755), "owner", "group", new Path(p));
return new FileStatus(DIR_LENGTH, true, 3, 128 * 1024 * 1024, 1523456000000L, 1523457000000L, perms(Integer.decode("0755").shortValue()), "owner", "group", new Path(p));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.net.URISyntaxException;
Expand All @@ -45,8 +43,6 @@
*/
public class JMSConnectionFactoryProviderTest {

private static Logger logger = LoggerFactory.getLogger(JMSConnectionFactoryProviderTest.class);

private static final String HOSTNAME = "myhost";
private static final String PORT = "1234";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class ConsumeKafkaRecordIT extends AbstractConsumeKafkaIT {

private static final int FIRST_PARTITION = 0;

private static final long FIRST_OFFSET = 0;

private TestRunner runner;

@BeforeEach
Expand Down
Loading
Loading