citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
445 stars 135 forks source link

BeforeSuite/ AfterSuite, BeforeTest/AfterTest #1092

Closed ZuikovAV closed 5 months ago

ZuikovAV commented 5 months ago

Citrus Version 4.0.0

Question Hi! Could you explain how to work now with BeforeSuite/ AfterSuite, BeforeTest/AfterTest?

In docs for 4.0.0 (https://citrusframework.org/citrus/reference/4.0.0/html/index.html) the examples like this one are still present:

public class MyBeforeSuite extends TestRunnerBeforeSuiteSupport { @Override public void beforeSuite(TestRunner runner) { runner.echo("This action should be executed before suite"); } }

But at 4.0.0 version TestRunnerBeforeSuiteSupport, TestRunnerBeforeTestSupport and etc. don't exist.

bbortt commented 5 months ago

I think you can use BeforeSuite-beans, if you're using Spring. like this:

        @Bean
        public BeforeSuite whatever() {
            return new SequenceBeforeSuite.Builder().actions(new EchoAction.Builder().message("Hello World")).build();
        }

are you using Spring and is this feasible for you? 😉

ZuikovAV commented 5 months ago

are you using Spring and is this feasible for you

Thanks for the reply! Yes, i use Spring.

The problem is i need to implement not only citrus actions but also for example clean database or mq queue, set channel polling interval and etc. @christophd can you help with my question please?

bbortt commented 5 months ago

that's totally possible! just code it like this:

var whatever; // this is your database, or mq client, or "whatever"

[...]

return new SequenceBeforeSuite.Builder().actions(context -> whatever.youWant()).build();

check the citrusframework/citrus-simulator sources if you need more samples 😜

ZuikovAV commented 5 months ago

that's totally possible! just code it like this:

var whatever; // this is your database, or mq client, or "whatever"

[...]

return new SequenceBeforeSuite.Builder().actions(context -> whatever.youWant()).build();

check the citrusframework/citrus-simulator sources if you need more samples 😜

Thanks a lot! It helps me.

ZuikovAV commented 5 months ago

If somebody needs sample for miltiple actions:

return new SequenceBeforeSuite.Builder().actions(context -> whatever.youWant(), context -> whatever.youWant1()).build();

ZuikovAV commented 5 months ago

@bbortt @christophd i've got some questions.

  1. Why it's not possible to set polling interval directly in HttpServerBuilder (In MQ Server we at the same time we have this opportunity)?

    @Bean public HttpServer server() { return new HttpServerBuilder() .port(8080) .timeout(10000) .autoStart(true) .build(); }

  2. How to set polling interval in class with @Configuration? My attempt to deal with it does'nt work. Nothing happens - polling interval remained default 500 ms.

@Configuration public class CitrusSpringConfig {

@Bean
public HttpServer server() {
    return new HttpServerBuilder()
            .port(8080)
            .timeout(10000)
            .autoStart(true)
            .build();
}

@Bean
public BeforeSuite beforeSuite() {

MessageSelectingQueueChannel messageSelectingQueueChannelServerCallBack = new MessageSelectingQueueChannel();

messageSelectingQueueChannelServerCallBack.setBeanName("server.inbound");

    return new SequenceBeforeSuite.Builder().actions
            (context -> messageSelectingQueueChannelServerCallBack.setPollingInterval(10)).build();
}

}

christophd commented 5 months ago

Why it's not possible to set polling interval directly in HttpServerBuilder

This is because this is not a setting on the Http server but on the endpoint adapter and the underlying message channel. If you want to set this on the server component, please use a custom channel endpoint adapter with a custom message channel.

How to set polling interval in class with @Configuration?

You before suite action is not setting the message channel on the server component. Only setting the name to server.inbound is not sufficient in this manner. Instead you should add the message channel as a @Bean with name server.inbound. On the Http server bean you can then add a depends-on predicate to the bean configuration so the server bean depends on the message channel. Then the custom channel is automatically set for the server component.

General question would be why you need to set this polling interval on the server. Usually it is an interval that is used internally. This interval has no effect on the server connections or how the request/response is handled with the clients.

Please let me know if this helps. And I would suggest to close this issue and open another one if there are more questions as it is not really related to the initial problem if this issue. Many thanks!

ZuikovAV commented 5 months ago

Why it's not possible to set polling interval directly in HttpServerBuilder

This is because this is not a setting on the Http server but on the endpoint adapter and the underlying message channel. If you want to set this on the server component, please use a custom channel endpoint adapter with a custom message channel.

How to set polling interval in class with @configuration?

You before suite action is not setting the message channel on the server component. Only setting the name to server.inbound is not sufficient in this manner. Instead you should add the message channel as a @Bean with name server.inbound. On the Http server bean you can then add a depends-on predicate to the bean configuration so the server bean depends on the message channel. Then the custom channel is automatically set for the server component.

General question would be why you need to set this polling interval on the server. Usually it is an interval that is used internally. This interval has no effect on the server connections or how the request/response is handled with the clients.

Please let me know if this helps. And I would suggest to close this issue and open another one if there are more questions as it is not really related to the initial problem if this issue. Many thanks!

Thanks for the reply! I'll try it and return with the results asap.

ZuikovAV commented 5 months ago
return new SequenceBeforeSuite.Builder().actions(context -> whatever.youWant()).build();

It should look like this?

@Bean
public MessageSelectingQueueChannel serverChannel(){

    MessageSelectingQueueChannel messageSelectingQueueChannel = new MessageSelectingQueueChannel();

    messageSelectingQueueChannel.setBeanName("server.inbound");

    messageSelectingQueueChannel.setPollingInterval(10);

    return messageSelectingQueueChannel;
}

@Bean
@DependsOn("serverChannel")
public HttpServer server() {
    return new HttpServerBuilder()
            .port(8080)
            .timeout(10000)
            .autoStart(true)
            .build();
}
christophd commented 5 months ago

Not exactly, please use server.inbound as a bean name for serverChannel

One more thing. Since Citrus 4.0 the default implementation on a server is not MessageSelectingQueueChannel but org.citrusframework.message.DefaultMessageQueue so I'd suggest to change the bean to this type.

Please let me know if this works for you

ZuikovAV commented 5 months ago

org.citrusframework.message.DefaultMessageQueue

Thanks a lot! It works for me!

If somebody interested the sample is the following:

@Bean("server.inbound") public DefaultMessageQueue serverInbound(){

    DefaultMessageQueue defaultMessageQueue = new DefaultMessageQueue("server.inbound");

    defaultMessageQueue.setPollingInterval(10);

    return defaultMessageQueue;
}

@Bean
@DependsOn("server.inbound")
public HttpServer server() {
    return new HttpServerBuilder()
            .port(8080)
            .timeout(10000)
            .autoStart(true)
            .build();
}