Object result in Apache Camel junit test - java

I trying to test the camel output as Object but which fails to get exchange object.This is where it fails Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class). Please help me to solve this.I referred this Right way to test my object in Camel
Customer POJO:
public class Customer {
private String firstName;
private String lastName;
// getters and setters
#Override
public String toString(){
return firstName +":::" + lastName;
}
}
Test Route:
public class FileTest4 extends CamelTestSupport {
#EndpointInject(uri = "direct:teststart")
private Endpoint start;
#EndpointInject(uri = "mock:direct:processActs")
private MockEndpoint processActs;
#EndpointInject(uri = "mock:direct:write2File")
private MockEndpoint write2File;
#EndpointInject(uri = "mock:end")
private MockEndpoint mockEndResult;
#Override
public boolean isUseAdviceWith() {
return true;
}
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("file:/var/file.log&noop=true").routeId("MY_ROUTE").to("direct:processActs");
from("direct:processActs").process(exchange -> {
List<Customer> customers = new ArrayList<>();
customers.add(new Customer("F1", "L1"));
customers.add(new Customer("F2", "L2"));
customers.add(new Customer("F3", "L3"));
exchange.getOut().setBody(customers);
}).to("direct:write2File");
from("direct:write2File").split(simple("${body}")).log("Content: ${body}");
}
};
}
#Override
protected void doPostSetup() throws Exception {
context.getRouteDefinition("MY_ROUTE").adviceWith(context, new AdviceWithRouteBuilder() {
#Override
public void configure() throws Exception {
replaceFromWith("direct:teststart");
weaveAddLast().to("mock:end");
}
});
context.start();
}
#Test
public void testUnmarshal() throws Exception {
mockEndResult.expectedMessageCount(1);
// ArrayIndex Exception here exchanges list is empty
Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class);
assertEquals(resultCustomer.toString(),"F1:::L1");
write2File.expectedBodiesReceived("F1:::L1", "F3:::L3", "F2:::L2");
template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
mockEndResult.assertIsSatisfied();
}
}

It looks like you're inspecting the mock endpoint before you've actually sent any exchanges. Try moving the check to the end of the test, e.g.:
#Test
public void testUnmarshal() throws Exception {
mockEndResult.expectedMessageCount(1);
write2File.expectedBodiesReceived("F1:::L1", "F3:::L3", "F2:::L2");
template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
mockEndResult.assertIsSatisfied();
Customer resultCustomer = processActs.getExchanges().get(0).getIn().getBody(Customer.class);
assertEquals(resultCustomer.toString(),"F1:::L1");
}
UPDATE
On closer inspection, I think you've got your mocks muddled up. Judging by the assertions you want to check that three customers are written out. However your mocks aren't set up for this.
mock:end is added to the end of MY_ROUTE but that will only ever see the entire customer list returned by the processor in direct:processActs
Also the mocks you declare with #EndpointInject don't get involved in the route because you don't actually mock the real endpoints. You can remove all of them apart from mockEndResult.
The following test does pass.
#Test
public void testUnmarshal() throws Exception {
mockEndResult.expectedMessageCount(1);
template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
mockEndResult.assertIsSatisfied();
#SuppressWarnings("unchecked")
List<Customer> customers = mockEndResult.getExchanges().get(0).getIn().getBody(List.class);
assertEquals(customers.get(0).toString(), "F1:::L1");
assertEquals(customers.get(1).toString(), "F2:::L2");
assertEquals(customers.get(2).toString(), "F3:::L3");
}
That might not be what you want to test, though. Instead you could weave the mock endpoint in to the splitter, then you'd be able to assert individual customers.
#Override
protected void doPostSetup() throws Exception {
context.getRouteDefinition("MY_ROUTE").adviceWith(context, new AdviceWithRouteBuilder() {
#Override
public void configure() throws Exception {
replaceFromWith("direct:teststart");
}
});
// give direct:write2File the id 'splitter' to be able to advice it
context.getRouteDefinition("splitter").adviceWith(context, new AdviceWithRouteBuilder() {
#Override
public void configure() throws Exception {
weaveByType(LogDefinition.class).after().to("mock:end");
}
});
context.start();
}
#Test
public void testUnmarshal() throws Exception {
mockEndResult.expectedMessageCount(3);
mockEndResult.expectedBodiesReceived("F1:::L1", "F2:::L2", "F3:::L3");
template.sendBody("direct:teststart", new File("src/test/resources/test.txt"));
mockEndResult.assertIsSatisfied();
}

Related

Custom Spring validator not working properly

I'm trying to make artificial CONSTRAINT violation by Spring instead of throwing exception from DB (an expert sad DB-produced errors have high performance cost):
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
#Component
public class AccountValidator implements org.springframework.validation.Validator {
#Autowired
private Validator validator;
private final AccountService accountService;
public AccountValidator(#Qualifier("accountServiceAlias")AccountService accountService) {
this.accountService = accountService;
}
#Override
public boolean supports(Class<?> clazz) {
return AccountRequestDTO.class.equals(clazz);
}
#Override
public void validate(Object target, Errors errors) {
Set<ConstraintViolation<Object>> validates = validator.validate(target);
for (ConstraintViolation<Object> constraintViolation : validates) {
String propertyPath = constraintViolation.getPropertyPath().toString();
String message = constraintViolation.getMessage();
errors.rejectValue(propertyPath, "", message);
}
AccountRequestDTO account = (AccountRequestDTO) target;
if(accountService.getPhone(account.getPhone()) != null){
errors.rejectValue("phone", "", "Validator in action! This number is already in use.");
}
}
}
However, second part of validate() method never works for reasons I cant understand and always pass a call from controller to be handled in try-catch block throwing exception from DB:
public void saveAccount(AccountRequestDTO accountRequestDTO) throws Exception {
LocalDate birthday = LocalDate.parse(accountRequestDTO.getBirthday());
if (LocalDate.from(birthday).until(LocalDate.now(), ChronoUnit.YEARS) < 18) {
throw new RegistrationException("You must be 18+ to register");
}
Account account = new Account(accountRequestDTO.getName(), accountRequestDTO.getSurname(),
accountRequestDTO.getPhone(), birthday, BCrypt.hashpw
(accountRequestDTO.getPassword(), BCrypt.gensalt(4)));
account.addRole(Role.CLIENT);
try {
accountRepository.save(account);
}
catch (RuntimeException exc) {
throw new PersistenceException("Database exception: this number is already in use.");
}
}
Here's a controller method:
#PostMapping("/confirm")
public String signIn(#ModelAttribute("account") #Valid AccountRequestDTO accountRequestDTO,
BindingResult result, Model model) {
accountValidator.validate(accountRequestDTO, result);
if(result.hasErrors()) {
return "/auth/register";
}
try {
accountService.saveAccount(accountRequestDTO);
}
catch (Exception exc) {
model.addAttribute("message", exc.getMessage());
return "/auth/register";
}
return "/auth/login";
}
At service:
#Transactional(readOnly = true)
public String getPhone(String phone){
return accountRepository.getPhone(phone);
}
JpaRepository query:
#Query("SELECT phone FROM Account accounts WHERE phone=:check")
String getPhone(String check);
Tests are green:
#BeforeAll
static void prepare() {
search = new String("0000000000");
}
#BeforeEach
void set_up() {
account = new Account
("Admin", "Adminov", "0000000000", LocalDate.of(2001, 01, 01), "superadmin");
accountRepository.save(account);
}
#Test
void check_if_phone_presents() {
assertThat(accountRepository.getPhone(search).equals(account.getPhone())).isTrue();
}
#Test
void check_if_phone_not_presents() {
String newPhone = "9999999999";
assertThat(accountRepository.getPhone(newPhone)).isNull();
}
#AfterEach
void tear_down() {
accountRepository.deleteAll();
account = null;
}
#AfterAll
static void clear() {
search = null;
}
You need to register your validator.
After we've defined the validator, we need to map it to a specific
event which is generated after the request is accepted.
This can be done in three ways:
Add Component annotation with name “beforeCreateAccountValidator“.
Spring Boot will recognize prefix beforeCreate which determines the
event we want to catch, and it will also recognize WebsiteUser class
from Component name.
#Component("beforeCreateAccountValidator")
public class AccountValidator implements Validator {
...
}
Create Bean in Application Context with #Bean annotation:
#Bean
public AccountValidator beforeCreateAccountValidator () {
return new AccountValidator ();
}
Manual registration:
#SpringBootApplication
public class SpringDataRestApplication implements RepositoryRestConfigurer {
public static void main(String[] args) {
SpringApplication.run(SpringDataRestApplication.class, args);
}
#Override
public void configureValidatingRepositoryEventListener(
ValidatingRepositoryEventListener v) {
v.addValidator("beforeCreate", new AccountValidator ());
}
}

Hystrix: How to capsule multi calls

I am using Hystrix to improve my services. How can I decapsulate the service calls into Hystrix. I know you can create for each call a special hystrix-class, but this would be too much work without using Spring!
I try to describe my problem with pseudocode:
public class HystrixController extends HystrixCommand {
public static void main(String[] args) throws Exception {
HystrixController hystrixController = new HystrixController();
System.out.print(hystrixController.execute());
}
private final ExampleService exampleService;
protected HystrixController() throws Exception {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.exampleService = new ExampleService();
}
// Call 1
public List getItemsAsList() {
return exampleService.getItemsByContractId(contractID);
}
// Call 2
public List getItemsByName() {
return exampleService.getItemsByName(contractID);
}
// How can I isolate the two calls ? The run() only allows me to use one.
#Override
protected List run() throws Exception {
return getItemsAsList();
}
}
In the example you can see it is only possible to execute only one call. I would like to have something like that:
public class HystrixController extends HystrixCommand {
public static void main(String[] args) throws Exception {
HystrixController hystrixController = new HystrixController();
System.out.print(hystrixController.execute(1));
System.out.print(hystrixController.execute(2));
}
private final ExampleService exampleService;
protected HystrixController() throws Exception {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.exampleService = new ExampleService();
}
// Call 1
public List getItemsAsList() {
return exampleService.getItemsByContractId(contractID);
}
// Call 2
public List getItemsByName() {
return exampleService.getItemsByName(contractID);
}
// Multi Threads
#Override
protected List run_getItemsAsList() throws Exception {
return getItemsAsList();
}
#Override
protected List run_getItemsByName() throws Exception {
return getItemsByName();
}
}
Thanks you in advance and I am sorry for my broken English

Camel: Mock a processor to test Route

I am trying to create a unit test for the routing.
I have the following route configuration
from ("direct:getA")
.routeId("get-a").startupOrder(1)
.process(exchange -> {
QueryObject queryObject = exchange.getIn().getBody(QueryObject.class);
exchange.getIn().setHeader(foo, queryObject.getH());
exchange.setOut(exchange.getIn());
})
.choice()
.when(header(foo).isEqualTo(fooConstant.bar))
.process("barProcessor")
.when(header(foo).isEqualTo(fooConstant.bie))
.process("bieProcessor")
.end();
My question is, how can I mock "barProcessor" and "bieProcessor"?
I tried to use adviceWith but I could not retrieve the routeDefinition. The context.getRouteDefinitions() returns an empty list.
Edit:
Below is the code snippet from my test.
RouteDefinition routeDef = context.getRouteDefinition("get-a");
routeDef.adviceWith(context, new AdviceWithRouteBuilder() {
#Override
public void configure() throws Exception {
interceptSendToEndpoint("*barProcessor*").process(
new Processor() {
#Override
public void process(Exchange exchange) {
System.out.println("bar");
}
}
);
interceptSendToEndpoint("*bieProcessor*").process(
new Processor() {
#Override
public void process(Exchange exchange) {
System.out.println("Bie");
}
}
);
}
});
String request = <JSON Request>;
websocket.sendTest(request);
But the context.getRouteDefinition("get-a"); is returning null. And when I also used context.getRouteDefinitions(), it returns an empty list.
I found the cause of this issue.
I forgot to Override the createRouteBuilder and createJndiContext.
#Override
protected RouteBuilder createRouteBuilder() {
return new SampleRoute(<param>);
}
#Override
protected JndiContext createJndiContext() throws Exception {
JndiContext context = new JndiContext();
context.bind("barProcessor", new BarProcessor());
context.bind("bieProcessor", new BieProcessor());
return context;
}
I found the solution from this link: Unit Test

ThreadWeaver always throws IllegalArgumentException

I am trying to use Google ThreadWeaver to write a unit test for concurrent code. No matter what I do, I will get an IllegalArgumentException. I am still working with an example, but even that does not work. This is what I tried:
public class ExampleTest {
public static class ExampleMain implements MainRunnable<Example> {
private Example example;
#Override
public Class<Example> getClassUnderTest() {
return Example.class;
}
#Override
public String getMethodName() {
return null;
}
#Override
public Method getMethod() throws NoSuchMethodException {
return null;
}
#Override
public void initialize() throws Exception {
example = new Example();
}
#Override
public Example getMainObject() {
return example;
}
#Override
public void terminate() throws Exception {
}
#Override
public void run() throws Exception {
example.test("second");
}
}
public static class ExampleSecondary implements SecondaryRunnable<Example, ExampleMain> {
private ExampleMain exampleMain;
#Override
public void initialize(ExampleMain main) throws Exception {
exampleMain = main;
}
#Override
public void terminate() throws Exception {
}
#Override
public boolean canBlock() {
return false;
}
#Override
public void run() throws Exception {
exampleMain.getMainObject().test("main");
}
}
public static class Example {
private List<String> list = new ArrayList<String>();
public String test(String s) {
System.out.println("1" + s);
list.add(s);
System.out.println("2" + s);
return list.get(0);
}
}
#Test
public void testThreadWeaver() throws Exception {
ClassInstrumentation instrumentation = Instrumentation.getClassInstrumentation(Example.class);
Method tested = Example.class.getDeclaredMethod("test", String.class);
Method breakpoint = List.class.getDeclaredMethod("add", Object.class);
CodePosition codePosition = instrumentation.afterCall(tested, breakpoint);
InterleavedRunner.interleave(new ExampleMain(), new ExampleSecondary(), Arrays.asList(codePosition)).throwExceptionsIfAny();
}
}
The stack trace says:
java.lang.IllegalArgumentException: Class Example is not instrumented
at
com.google.testing.threadtester.CallLoggerFactory.getClassInstrumentation(CallLoggerFactory.java:108)
at
com.google.testing.threadtester.Instrumentation.getClassInstrumentation(Instrumentation.java:65)
at MyTest.testThreadWeaver(MyTest.java:92
I followed the instructions at the official Google code webpage, but it does not seem to work. Any ideas?
ThreadWeaver needs to instrument your classes in order to add breakpoints to your methods. Therefore, you cannot run the tests with JUnit directly but you must run your test from a specific test runner. For your case this would be ThreadedTestRunner. The actual test methods must then be annotated with #ThreadedTest instead of #Test. This should work:
#Test
public void startTest() throws Exception {
new ThreadedTestRunner().runTests(getClass(), Example.class);
}
#ThreadedTest
public void testThreadWeaver() throws Exception {
// here comes your test
}

How to test multiple RouteBuilders in Apache Camel

I want to test multiple camel RouteBuilder in a single unit test
what I have:
Custom camel processor that changes state
public class MyProcessor implements Processor {
MyState state;
public MyProcessor(MyState state) {this.state = state;}
#Override
public void process(Exchange exchange) throws Exception {
state.setState(state.getState() + 5);
}}
Two simple RouteBuilders: first routes messages from "direct:start" to "direct:endroute1" second pick up messages from "direct:endroute1" and routes somewhere "mock:endroute2"
public class MyRouteBuilder1 extends RouteBuilder {
MyState state;
public MyRouteBuilder1(MyState state) {this.state = state;}
#Override
public void configure() throws Exception {
from("direct:start").process(new MyProcessor(state)).to("direct:endroute1");
}}
public class MyRouteBuilder2 extends RouteBuilder {
MyState state;
public MyRouteBuilder2(MyState state) {this.state = state;}
#Override
public void configure() throws Exception {
from("direct:endroute1").process(new MyProcessor(state)).to("mock:endroute2");
}}
Writing unit test for a single route builder is straightforward:
public class MyTest extends CamelTestSupport {
MyState state = new MyStateImpl();
#EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
#Test
public void testSingleRoute() throws Exception {
resultEndpoint.expectedMessageCount(1);
template.sendBody("direct:start", new Object());
assertTrue(state.getState() == 5);
resultEndpoint.assertIsSatisfied();
}
#Override
protected RouteBuilder createRouteBuilder() {
return new MyRouteBuilder1(state) {
public void configure() throws Exception{
super.configure();
from("direct:endroute1").to("mock:result");
}
};
}
}
What I really want to do is somehow to override CamelTestSupport.createRouteBuilder that will test whole chain of message processing from direct:start to mock:endroute2. As a result state.getState() should be 10
Try to override method:
protected RouteBuilder[] createRouteBuilders() {...}
from CamelTestSupport. It's available since version 2.17
you can just add multiple RouteBuilders to the context using the context.addRoutes(RouteBuilder) API
see this unit test for an example:
https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java
You could use one RouteBuilder including the routes of multiple other RouteBuilders.

Categories