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-9146 to NIFI-9148 - 4 bundles refactored for JUnit 5 #5360

Closed
wants to merge 7 commits into from
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.hamcrest.MatcherAssert;
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.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 All @@ -78,7 +78,7 @@ public void setup() throws InitializationException {
@Test
public void testValidService() {
runner.assertValid(alertHandler);
assertThat(alertHandler, instanceOf(AlertHandler.class));
MatcherAssert.assertThat(alertHandler, instanceOf(AlertHandler.class));
MikeThomsen marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand All @@ -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.hamcrest.MatcherAssert;
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.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
MikeThomsen marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -55,7 +56,7 @@ public void setup() throws InitializationException {
@Test
public void testValidService() {
runner.assertValid(expressionHandler);
assertThat(expressionHandler, instanceOf(ExpressionHandler.class));
MatcherAssert.assertThat(expressionHandler, instanceOf(ExpressionHandler.class));
MikeThomsen marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
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