Skip to content

Commit

Permalink
NIFI-9148 Refactored nifi-scripting-bundle to use JUnit 5.
Browse files Browse the repository at this point in the history
NIFI-9147 Refactored nifi-rules-action-handler-bundle to use JUnit 5.
NIFI-9146 Refactored nifi-riemann-bundle to use JUnit 5.
NIFI-9144 Refactored nifi-registry-bundle to use JUnit 5.

This closes #5360

Signed-off-by: David Handermann <exceptionfactory@apache.org>
  • Loading branch information
MikeThomsen authored and exceptionfactory committed Sep 6, 2021
1 parent 20804ff commit d702506
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
*/
package org.apache.nifi.schemaregistry.services;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.components.ValidationContext;
Expand All @@ -33,8 +24,17 @@
import org.apache.nifi.schema.access.SchemaNotFoundException;
import org.apache.nifi.serialization.record.RecordSchema;
import org.apache.nifi.serialization.record.SchemaIdentifier;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
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;

public class TestAvroSchemaRegistry {

Expand Down Expand Up @@ -63,16 +63,11 @@ public void validateSchemaRegistrationFromrDynamicProperties() throws Exception
SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name(schemaName).build();
RecordSchema locatedSchema = delegate.retrieveSchema(schemaIdentifier);
assertEquals(fooSchemaText, locatedSchema.getSchemaText().get());
try {
delegate.retrieveSchema(SchemaIdentifier.builder().name("barSchema").build());
Assert.fail("Expected a SchemaNotFoundException to be thrown but it was not");
} catch (final SchemaNotFoundException expected) {
}

assertThrows(SchemaNotFoundException.class, () -> delegate.retrieveSchema(SchemaIdentifier.builder().name("barSchema").build()));
}

@Test
public void validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties() throws Exception {
public void validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties() {
String schemaName = "fooSchema";
ConfigurationContext configContext = mock(ConfigurationContext.class);
Map<PropertyDescriptor, String> properties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -37,22 +35,20 @@
import java.util.Queue;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
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;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TestPutRiemann {
@Rule
public final ExpectedException expectedException = ExpectedException.none();

// Holds incoming events to Riemann
private Queue<Proto.Event> eventStream = new LinkedList<Proto.Event>();

@Before
@BeforeEach
public void clearEventStream() {
eventStream.clear();
}
Expand Down Expand Up @@ -174,8 +170,7 @@ public void testInvalidEvents() {
runner.assertAllFlowFilesTransferred(PutRiemann.REL_FAILURE);
}


@Test(expected = AssertionError.class)
@Test
public void testFailedDeref() {
TestRunner runner = getTestRunner(true);
MockFlowFile flowFile = new MockFlowFile(1);
Expand All @@ -184,7 +179,7 @@ public void testFailedDeref() {
flowFile.putAttributes(attributes);
runner.enqueue(flowFile);
try {
runner.run();
assertThrows(AssertionError.class, () -> runner.run());
} catch (ProcessException e) {
runner.assertQueueNotEmpty();
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
import org.apache.nifi.rules.Action;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestActionHandlerLookup {

Expand All @@ -39,7 +40,7 @@ public class TestActionHandlerLookup {
private ActionHandlerLookup actionHandlerLookup;
private TestRunner runner;

@Before
@BeforeEach
public void setup() throws InitializationException {
alertHandler = new MockPropertyActionHandler();
logHandler = new MockPropertyActionHandler();
Expand Down Expand Up @@ -93,7 +94,7 @@ public void testLookupLog() {
assert logHandler.getExecuteContextCalled();
}

@Test(expected = ProcessException.class)
@Test
public void testLookupInvalidActionType() {
final Map<String, String> attributes = new HashMap<>();
final Map<String, Object> metrics = new HashMap<>();
Expand All @@ -103,7 +104,7 @@ public void testLookupInvalidActionType() {
metrics.put("cpu", "90");
final Action action = new Action();
action.setType("FAKE");
actionHandlerLookup.execute(null,action,metrics);
assertThrows(ProcessException.class, () -> actionHandlerLookup.execute(null,action,metrics));
}

private static class MockPropertyActionHandler extends AbstractActionHandlerService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
import org.apache.nifi.util.MockBulletinRepository;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
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;
import static org.mockito.ArgumentMatchers.anyString;

public class TestAlertHandler {
Expand All @@ -57,7 +57,7 @@ public class TestAlertHandler {
private AlertHandler alertHandler;
private MockAlertBulletinRepository mockAlertBulletinRepository;

@Before
@BeforeEach
public void setup() throws InitializationException {
runner = TestRunners.newTestRunner(TestProcessor.class);
mockComponentLog = new MockComponentLog();
Expand Down Expand Up @@ -95,12 +95,7 @@ public void testAlertNoReportingContext() {
final Action action = new Action();
action.setType("ALERT");
action.setAttributes(attributes);
try {
alertHandler.execute(action, metrics);
fail();
} catch (UnsupportedOperationException ex) {
assertTrue(true);
}
assertThrows(UnsupportedOperationException.class, () -> alertHandler.execute(action, metrics));
}

@Test
Expand Down Expand Up @@ -247,11 +242,7 @@ public void testInvalidActionTypeException(){
final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes);
try {
alertHandler.execute(reportingContext, action, metrics);
fail();
} catch (UnsupportedOperationException ex) {
}
assertThrows(UnsupportedOperationException.class, () -> alertHandler.execute(reportingContext, action, metrics));
}

@Test
Expand All @@ -276,12 +267,9 @@ public void testInvalidActionTypeWarn(){
final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes);
try {
alertHandler.execute(reportingContext,action, metrics);
assertTrue(true);
} catch (UnsupportedOperationException ex) {
fail();
}

assertDoesNotThrow(() -> alertHandler.execute(reportingContext,action, metrics));

final String warnMessage = mockComponentLog.getWarnMessage();
assertTrue(StringUtils.isNotEmpty(warnMessage));
assertEquals("This Action Handler does not support actions with the provided type: FAKE",warnMessage);
Expand Down Expand Up @@ -309,12 +297,8 @@ public void testInvalidActionTypeIgnore(){
final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes);
try {
alertHandler.execute(reportingContext,action, metrics);
assertTrue(true);
} catch (UnsupportedOperationException ex) {
fail();
}
assertDoesNotThrow(() -> alertHandler.execute(reportingContext,action, metrics));

final String debugMessage = mockComponentLog.getDebugMessage();
assertTrue(StringUtils.isNotEmpty(debugMessage));
assertEquals("This Action Handler does not support actions with the provided type: FAKE",debugMessage);
Expand All @@ -340,12 +324,7 @@ public void testValidActionType(){
final Action action = new Action();
action.setType("ALERT");
action.setAttributes(attributes);
try {
alertHandler.execute(reportingContext,action, metrics);
assertTrue(true);
} catch (UnsupportedOperationException ex) {
fail();
}
assertDoesNotThrow(() -> alertHandler.execute(reportingContext,action, metrics));
}

private static class MockAlertHandler extends AlertHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@
import org.apache.nifi.rules.Action;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestExpressionHandler {

private TestRunner runner;
private MockComponentLog mockComponentLog;
private ExpressionHandler expressionHandler;

@Before
@BeforeEach
public void setup() throws InitializationException {
runner = TestRunners.newTestRunner(TestProcessor.class);
mockComponentLog = new MockComponentLog();
Expand Down Expand Up @@ -151,12 +152,9 @@ public void testInvalidActionTypeException() {

final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes); try {
expressionHandler.execute(action, metrics);
fail();
} catch (UnsupportedOperationException ex) {
assertTrue(true);
}
action.setAttributes(attributes);

assertThrows(UnsupportedOperationException.class, () -> expressionHandler.execute(action, metrics));
}

@Test
Expand All @@ -173,16 +171,13 @@ public void testInvalidActionTypeWarning() {

final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes); try {
expressionHandler.execute(action, metrics);
} catch (UnsupportedOperationException ex) {
fail();
}
action.setAttributes(attributes);

assertDoesNotThrow(() -> expressionHandler.execute(action, metrics));

final String warnMessage = mockComponentLog.getWarnMessage();
assertTrue(StringUtils.isNotEmpty(warnMessage));
assertEquals("This Action Handler does not support actions with the provided type: FAKE",warnMessage);

}

@Test
Expand All @@ -199,11 +194,9 @@ public void testInvalidActionTypeIgnore() {

final Action action = new Action();
action.setType("FAKE");
action.setAttributes(attributes); try {
expressionHandler.execute(action, metrics);
} catch (UnsupportedOperationException ex) {
fail();
}
action.setAttributes(attributes);

assertDoesNotThrow(() -> expressionHandler.execute(action, metrics));

final String debugMessage = mockComponentLog.getDebugMessage();
assertTrue(StringUtils.isNotEmpty(debugMessage));
Expand All @@ -224,12 +217,7 @@ public void testValidActionType() {
final Action action = new Action();
action.setType("EXPRESSION");
action.setAttributes(attributes);
try {
expressionHandler.execute(action, metrics);
assertTrue(true);
} catch (UnsupportedOperationException ex) {
fail();
}
assertDoesNotThrow(() -> expressionHandler.execute(action, metrics));
}


Expand Down

0 comments on commit d702506

Please sign in to comment.