mercredi 25 février 2015

how to mock apache-camel EL

i have the following RouteBuilder.



public class MyRoute extends RouteBuilder {

@Override
public void configure() {
errorHandler(deadLetterChannel(errorHandlerEndpoint)
.maximumRedeliveries(1)
.redeliveryDelay(2000)
.retryAttemptedLogLevel(LoggingLevel.WARN));

from(fromEndpoint).routeId("fetchPreprocessingResultFromMarklogicRoute")
.setHeader("principal").method(principalService, "getPrincipal").id("getPrincipalServiceMethod") // put principal to header for latter use
.multicast()
.filter(xpath("//files[@ml-generated='true']"))
.choice()
.when(PredicateBuilder.isEqualTo(constant(EPrincipal.VETCENTER.principal()), simple("${header.principal.principal.principal}")))
.log("Sending VetCenter preprocessing report...")
.inOnly(toVetCenterEmailNotificationEndpoint)
.when(PredicateBuilder.isEqualTo(constant(EPrincipal.OSTEOTHEK.principal()), simple("${header.principal.principal.principal}")))
.log("Sending Osteothek preprocessing report...")
.inOnly(toOsteothekEmailNotificationEndpoint)
.otherwise()
.log("Principal unknown. Don't sending a preprocessing report...")
.inOnly("direct:trash") // @ToDo: Write test
.end()
.inOnly(toCleaningPhaseDecisionEndpoint)
.inOnly(deleteImportDirsEndpoint)
.end();

}
}


This is the JUnit-Test:



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context-test.xml"})
public class FetchPreprocessingResultFromMarklogicRoute_Test extends CamelTestSupport{
private RouteBuilder route;

@Value("${fetchPreprocessingResultFromMarklogicRoute.fromEndpoint}")
private String testFromEndpoint;

@Value("${fetchPreprocessingResultFromMarklogicRoute.toCleaningPhaseDecisionEndpoint}")
private String toCleaningPhaseDecisionEndpoint;
@Value("${fetchPreprocessingResultFromMarklogicRoute.vetcenter.toEmailNotificationEndpoint}")
private String toEmailNotificationVetCenterEndpoint;
@Value("${fetchPreprocessingResultFromMarklogicRoute.osteothek.toEmailNotificationEndpoint}")
private String toEmailNotificationOsteothekEndpoint;
@Value("${fetchPreprocessingResultFromMarklogicRoute.deleteImportDirsEndpoint}")
private String testDeleteImportDirsEndpoint;
@Value("${fetchPreprocessingResultFromMarklogicRoute.errorHandlerEndpoint}")
private String testErrorHandlerEndpoint;

private MockEndpoint emailNotificationVetCenterMOCKEndpoint;
@Mock
private PrincipalService principalServiceMOCK;

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
if(route == null){

route = new MyRoute(
testFromEndpoint,
toCleaningPhaseDecisionEndpoint,
toEmailNotificationVetCenterEndpoint,
toEmailNotificationOsteothekEndpoint,
testDeleteImportDirsEndpoint,
testErrorHandlerEndpoint,
principalServiceMOCK);
}
return route;
}

@Override
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

Principal principal = new Principal();
principal.setPrincipal(EPrincipal.VETCENTER);

Mockito.when(principalServiceMOCK.getPrincipal("anyIdentificator")).thenReturn(principal);
super.setUp();
}


@Test
public void test() throws InterruptedException{
InputStream resourceInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testdata/importreport/inc/import-log-Vetcenter.xml");
String message = new Scanner(resourceInputStream).useDelimiter("\\A").next();
emailNotificationVetCenterMOCKEndpoint = getMockEndpoint(toEmailNotificationVetCenterEndpoint);

emailNotificationVetCenterMOCKEndpoint.expectedBodiesReceived(message);
// Herausforderung ist, die Datei aus dem Verzeichnis ablesen und camel Regeln zu prüfen
template.sendBody(testFromEndpoint, message);

emailNotificationVetCenterMOCKEndpoint.assertIsSatisfied();
}
}


i don't know how to make a mock for this snippet:



.when(PredicateBuilder.isEqualTo(constant(EPrincipal.VETCENTER.principal()), simple("${header.principal.principal.principal}")))


more exact for this part:



simple("${header.principal.principal.principal}")


It is expression language and i have no idiea how to create a mock for it. Is there other solution to create a unit test for it?


very many thanks for help


Aucun commentaire:

Enregistrer un commentaire