Как написать модульный тест для весенней интеграции XML-кода - PullRequest
0 голосов
/ 07 сентября 2018

Я новичок в интеграции с Spring, я хочу написать тестовые примеры для моего приложения. Я работаю над приложением, в котором интерфейс интеграции будет вызываться другой системой с вводом XML, мы преобразуем входной XML с использованием XSLT и сделаем вызов другой системе, и ответ будет отправлен вызывающей стороне.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
    xmlns:int-http="http://www.springframework.org/schema/integration/http"
    xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
        http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-4.2.xsd
        http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-4.2.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.0.xsd">

        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="locations">
                <list>
                    <value>application.properties</value>
                    <value>application-${spring.profiles.active}.properties</value>
                </list>
             </property>
        </bean>

        <bean id="MessagePrinter" class="com.oms.integration.messagelogger.MessagePrinter"></bean>

        <int:channel id="logIncommingCaptureRequestChannel"></int:channel>

        <int:channel id="transformedCaptureRequestToCardinalChannel"></int:channel>

        <int:channel id="incommingCaptureRequestToCardinal"></int:channel>

        <int:channel id="CaptureRequestToCardinalChannel"></int:channel>

        <int:channel id="logCaptureResponseFromCardinal"></int:channel>

        <int:channel id="transformCaptureResponseFromCardinal"></int:channel>

        <int:channel id="logTransformResponseFromCardinal"></int:channel>

        <int:channel id="ResponseFromCardinalToYantraChannel"></int:channel>



        <int-http:inbound-gateway request-channel="logIncommingCaptureRequestChannel" supported-methods="POST" path="/fp075" reply-channel="ResponseFromCardinalToYantraChannel"/>

        <int-http:outbound-gateway request-channel="CaptureRequestToCardinalChannel" url="${Paypal_Url}={data}" expected-response-type="java.lang.String" http-method="GET" reply-channel="logCaptureResponseFromCardinal">
            <int-http:uri-variable name="data" expression="payload"/>
        </int-http:outbound-gateway>

        <int:service-activator ref="MessagePrinter" input-channel="logIncommingCaptureRequestChannel" method="printIncommingCaptureRequestFromYantra" output-channel="incommingCaptureRequestToCardinal"></int:service-activator> 


        <int:service-activator ref="MessagePrinter" input-channel="transformedCaptureRequestToCardinalChannel" method="printTransformedCaptureRequestFromYantraToCardinal" output-channel="CaptureRequestToCardinalChannel"></int:service-activator> 

         <int:service-activator ref="MessagePrinter" input-channel="logCaptureResponseFromCardinal" method="printCaptureResponseFromCardinal" output-channel="transformCaptureResponseFromCardinal"></int:service-activator>

        <int:service-activator ref="MessagePrinter" method="printTransformedResponseFromCardinal" input-channel="logTransformResponseFromCardinal" output-channel="ResponseFromCardinalToYantraChannel"></int:service-activator>


        <int-xml:xslt-transformer
        input-channel="incommingCaptureRequestToCardinal"
        xsl-resource="classpath:/templates/FP075/DSW_XSLT_YANTRA_To_Paypal_Capture_Request_FP075.xslt"
        output-channel="transformedCaptureRequestToCardinalChannel"
        id="TransformIncommingCaptureRequest">
        <int-xml:xslt-param name="MsgType"
            value="${PP_Cardinal_MsgType_FP075}" />
        <int-xml:xslt-param name="Version"
            value="${PP_Cardinal_Version}" />
        <int-xml:xslt-param name="ProcessorId"
            value="${PP_Cardinal_ProcessorId}" />
        <int-xml:xslt-param name="MerchantId"
            value="${PP_Cardinal_MerchantId}" />
        <int-xml:xslt-param name="TransactionPwd"
            value="${PP_Cardinal_TransactionPwd}" />
    </int-xml:xslt-transformer>
        <int-xml:xslt-transformer
        id="transformCaptureResponse"
        input-channel="transformCaptureResponseFromCardinal"
        xsl-resource="classpath:/templates/FP075/DSW_XSLT_YANTRA_To_Paypal_Capture_Response_FP075.xslt"
        output-channel="logTransformResponseFromCardinal">
    </int-xml:xslt-transformer>
</beans>

1 Ответ

0 голосов
/ 07 сентября 2018

Поскольку у вас есть <int-http:inbound-gateway> и <int-http:outbound-gateway>, я бы посоветовал вам взглянуть на Mock Spring MVC: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-framework

Серверная сторона может выглядеть так:

@RunWith(SpringRunner.class)
@WebAppConfiguration
@DirtiesContext
public class MyTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc =
                MockMvcBuilders.webAppContextSetup(this.wac)
                        .build();
    }

Для клиентской стороны вам нужно использовать что-то вроде MockMvcClientHttpRequestFactory и вставьте его в HttpRequestExecutingMessageHandler для упомянутого <int-http:outbound-gateway>.

Итак, в итоге ваша основная конфигурация остается прежней, и вся структура потока остается неизменной. Вы отправляете фиктивные запросы через this.mockMvc.perform() и ожидаете ответа после обработки потока.

...