Skip to content

Commit

Permalink
CAMEL-21196: modernized assertions in camel-core.model (#15728)
Browse files Browse the repository at this point in the history
  • Loading branch information
LostArtist authored Sep 26, 2024
1 parent 61a7b48 commit 6235790
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.junit.jupiter.api.Test;

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

/**
* Unit test to verify end-user exceptions for miss configuration
Expand All @@ -30,17 +30,15 @@ public class ProcessorTypeConfigurationTest extends ContextTestSupport {

@Test
public void testProcessorRefMissConfigured() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:in").process("hello");
}
});
fail("Should have thrown IllegalArgumentException");
} catch (Exception e) {
assertEquals("No bean could be found in the registry for: hello of type: org.apache.camel.Processor",
e.getCause().getMessage());
}
}
}, "Should have thrown IllegalArgumentException");

assertEquals("No bean could be found in the registry for: hello of type: org.apache.camel.Processor",
e.getCause().getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.camel.processor.OnCompletionTest;
import org.junit.jupiter.api.Test;

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

public class RouteConfigurationOnCompletionTest extends ContextTestSupport {

Expand Down Expand Up @@ -58,12 +58,8 @@ public void testFail() throws Exception {
getMockEndpoint("mock:fail").expectedMessageCount(1);
getMockEndpoint("mock:result").expectedMessageCount(0);

try {
template.sendBody("direct:start", "Kaboom");
fail("Should have thrown exception");
} catch (Exception e) {
// expected
}
assertThrows(Exception.class, () -> template.sendBody("direct:start", "Kaboom"),
"Should have thrown exception");

assertMockEndpointsSatisfied();
}
Expand All @@ -75,12 +71,8 @@ public void testOkAndFail() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

template.sendBody("direct:start", "Hello World");
try {
template.sendBody("direct:start", "Kaboom");
fail("Should throw exception");
} catch (Exception e) {
// expected
}
assertThrows(Exception.class, () -> template.sendBody("direct:start", "Kaboom"),
"Should throw exception");

assertMockEndpointsSatisfied();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSupport {

Expand Down Expand Up @@ -84,12 +84,9 @@ public void configuration() {

getMockEndpoint("mock:error").expectedBodiesReceived("Bye World");

try {
template.sendBody("direct:start", "Hello World");
fail("Should throw exception");
} catch (Exception e) {
// expected
}
assertThrows(Exception.class, () -> template.sendBody("direct:start", "Hello World"),
"Should throw exception");

template.sendBody("direct:start2", "Bye World");

assertMockEndpointsSatisfied();
Expand Down Expand Up @@ -242,12 +239,12 @@ public void configuration() {
routeConfiguration("foo").onException(IllegalArgumentException.class).handled(true).to("mock:foo");
}
};
try {
context.addRoutesConfigurations(rcb);
fail("Should throw exception");
} catch (IllegalArgumentException e) {
assertEquals("Route configuration already exists with id: foo", e.getMessage());
}

IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> context.addRoutesConfigurations(rcb),
"Should throw exception");

assertEquals("Route configuration already exists with id: foo", e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.camel.builder.RouteConfigurationBuilder;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Fail.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class RoutesConfigurationErrorHandlerTest extends ContextTestSupport {

Expand Down Expand Up @@ -115,12 +115,9 @@ public void configure() {

getMockEndpoint("mock:error").expectedBodiesReceived("Bye World");

try {
template.sendBody("direct:start", "Hello World");
fail("Should throw exception");
} catch (Exception e) {
// expected
}
assertThrows(Exception.class, () -> template.sendBody("direct:start", "Hello World"),
"Should throw exception");

template.sendBody("direct:start2", "Bye World");

assertMockEndpointsSatisfied();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,93 +20,88 @@
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;

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 StartingRoutesErrorReportedTest extends ContextTestSupport {

@Test
public void testInvalidFrom() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:start?foo=bar").routeId("route1").to("mock:result");
}
});
context.start();
fail();
} catch (Exception e) {
assertTrue(e.getMessage().startsWith(
"Failed to create route route1: Route(route1)[From[direct:start?foo=bar] -> [To[mock:result]... because of"));
}
});

assertTrue(e.getMessage().startsWith(
"Failed to create route route1: Route(route1)[From[direct:start?foo=bar] -> [To[mock:result]... because of"));
}

@Test
public void testInvalidTo() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:start").routeId("route2").to("direct:result?foo=bar");
}
});
context.start();
fail();
} catch (Exception e) {
assertTrue(
e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:"));
}
});

assertTrue(
e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:"));
}

@Test
public void testMaskPassword() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("stub:foo?password=secret&beer=yes").routeId("route2").to("direct:result?foo=bar");
}
});
context.start();
fail();
} catch (Exception e) {
assertTrue(
e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:"));
}
});

assertTrue(
e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:"));
}

@Test
public void testInvalidBean() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:start").routeId("route3").to("mock:foo").bean("");
}
});
context.start();
fail("Should have thrown exception");
} catch (Exception e) {
assertTrue(e.getMessage().startsWith("Failed to create route route3 at: >>> Bean[ref:] <<< in route:"));
}
}, "Should have thrown exception");

assertTrue(e.getMessage().startsWith("Failed to create route route3 at: >>> Bean[ref:] <<< in route:"));
}

@Test
public void testUnavailableDataFormatOnClasspath() {
try {
Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:start").routeId("route3").unmarshal().jaxb().log("Will never get here");
}
});
context.start();
fail("Should have thrown exception");
} catch (Exception e) {
assertTrue(e.getMessage().contains(
"Ensure that the data format is valid and the associated Camel component is present on the classpath"));
}
}, "Should have thrown exception");

assertTrue(e.getMessage().contains(
"Ensure that the data format is valid and the associated Camel component is present on the classpath"));
}

@Override
Expand Down

0 comments on commit 6235790

Please sign in to comment.