@CitrusXmlTest annotation doesn't find the corresponding xml file

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

@CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
I am trying to set up citrus testing framework in our rest based micro services.  I was following the documentation and I had a simple Java Test class as follows

package com.proxibid.iop.citrus.integration.testing;

import com.consol.citrus.annotations.CitrusTest;
import com.consol.citrus.annotations.CitrusXmlTest;
import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;

import org.junit.Test;


public class FirstCitrusTest extends JUnit4CitrusTestRunner{



  @Test
  @CitrusXmlTest(name = "MyFirstTest")
  public void myFirstTest() {
    echo("${text}");
  }
 
      @Test
      @CitrusTest
      public void testRandomStuff() {
          variable("text", "citrus:randomString(10, UPPERCASE)");
          echo("${text}");
         
          variable("customerId", "123456789");
          echo("${customerId}");

      }
 

}


Also a MyFirstTest.xml taken from the citrus doc...also a file in the same package as Java class as follows- I am just testing out simple stuff to get a basic setup of citrus framework.

<?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:testcase="http://www.citrusframework.org/schema/testcase"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.citrusframework.org/schema/testcase http://www.citrusframework.org/schema/testcase/citrus-testcase-2.6.1.xsd">

 <testcase name="MyFirstTest">
        <description>
            First example showing the basic test case definition elements!
        </description>
        <variables>
            <variable name="text" value="Hello Test Framework"/>
        </variables>
        <actions>
            <echo>
                <message>${text}</message>
            </echo>
        </actions>
    </testcase>
</beans>


In my Java class , though I have the annotation @CitrusXmlTest and have the name property mapped to "MyFirstTest" , when the unit test is run, it doesn't find the corresponding "MyFirstTest.xml" file in the same package and gives me the error that the ${Text}" variable is undefined.


com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'text'

Am I missing any configurations for the Java class to map the "MyFirstTest.xml" file to it? Thanks in advance!


Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
You are mixing Java DSL and XML DSL in your test method. The method is annotated with CitrusXmlTest annotation but also has echo("${text}"); statement.

Methods that are annotated with CitrusXmlTest should have an empty method body as the complete test logic is loaded from Xml file.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
Thank you for the explanation. I did find that one out looking at your other examples. Can we still set some payload information in an external xml file and then call that in Java DSL...sth like

   send(rabbitMQEndpoint)
           .payload("external_file_in_classpath");

Thanks!!
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
Sure you can!

receive(rabbitMQEndpoint)
        .payload(new ClassPathResource("path/to/file/payload.xml"));
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
This post was updated on .
   Thanks! I have a httpclient and I was able to send a message out as follows in a xml test file

<actions>
        <send endpoint="eventExchangeClient">
            <message>
              <resource file="classpath:EventExchangeRequestTest.xml" />
            </message>
            <header>
                <element name="citrus_http_method" value="POST"/>
                       
            </header>
        </send>
    </actions>

I have a httpClient configured in my context file and am trying to do the same test in java dsl using Junit4CitrusTestRunner using an externalResource file as follows:

    SendMessageAction sma = send(new BuilderSupport<SendMessageBuilder>() {
      @SuppressWarnings("deprecation")
      @Override
      public void configure(SendMessageBuilder builder) {
        builder.endpoint(eventXchangeClient).http().header("citrus_http_method", "POST")
            .method(org.springframework.http.HttpMethod.POST)
            .payload("EventRequestTesting.xml");
      }
    });

When I run my junit test, I get a success as follows

HTTP message was sent to endpoint: '<a href="http://localhost:localPort/test/'">http://localhost:localPort/test/'
2016-10-21 14:10:53.449  INFO 8624 --- [           main] com.consol.citrus.Citrus                 :
2016-10-21 14:10:53.449  INFO 8624 --- [           main] com.consol.citrus.Citrus                 : TEST SUCCESS SendMsgToQueueTest.testSendingQueueMessage


MY externalResourceFile is as follows EventRequestTesting.xml

                        <id>10768</id>
                        <routingKey>com.test.testObject.new</routingKey>
                        <action>new</action>
                        <domain>test</domain>

If I change my port number to something, I do get a connection refused as expected and I get a success only in the port that my service is running. So it means it does go to my configured port that my client has. It looks like the external resource file I gave is never being read in my code block.(If I change to any random name in the payload above I still get a success in my test and it says the message was sent to my endpoint, But I never hit my service even in a debug mode). Why does it not read my external resource file? (I have a feeling the send happens first and then my payload is tied to it after that, I even tried using TestDesigner) And how can I check if the server received the message on the other end, atleast an empty message. My message gets lost , never reached my SUT(service).

Adding a receiver I get the following error

 http().client(eventXchangeClient).receive().response(HttpStatus.OK);


com.consol.citrus.exceptions.CitrusRuntimeException: org.w3c.dom.ls.LSException: Content is not allowed in prolog.
        at com.consol.citrus.validation.xml.DomXmlMessageValidator.validateMessagePayload(DomXmlMessageValidator.java:116) ~[citrus-core-2.6.1.jar:na]
       



 Thank you for your patience, It's difficult to set up my junit tests  as many of the doc samples are in TestNG, so please bear with my questions. I appreciate your time. Thanks!



Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
I think you did not follow the instructions in my previous post. You have to use a ClassPathResource in Java DSL in order to load the file content.

send(rabbitMQEndpoint)
        .payload(new ClassPathResource("path/to/file/payload.xml"));
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
Hi-

I did follow a complete file path as you had mentioned, both ways actually a complete path Vs just the fileName, sorry I didn't paste the ClassPathResource(). But I am not sure it is reading the xml file.


    http().client(testClient).send().post().header("citrus_http_method", "POST")
        .contentType("application/json").payload(new ClassPathResource("src/test/resources/clientTesting.xml"));

  http().client(eventXchangeClient).receive().response(HttpStatus.OK);


I also tried a send as follows

   send(testClient).http().header("citrus_http_method", "POST")
        .payload("<id>10768</id><routingKey>com.proxibid.domain.AH.new</routingKey>")
        .method(HttpMethod.POST);


citrus-context.xml as follow3s


<citrus-http:client id="testClient"
                    request-url="http://localhost:8081/event"
                    request-method="POST"
                    content-type="application/json"
                    timeout="60000"/>

If you see citrus message below, it does hit my endpoint( I have my service running in port 8081 and I have configured as a testClient endpoint  in my citrus-context.xml) along with my code above to send a http post. It seems to have hit the endpoint according to Citrus but the message has never reached my service controller at all. If I send a different port no, it duly says connection refused. If I try to check the response I get in my endpoint, I get a "Content not allowed in prolog" error, a LSParseException when trying to read the response. Have you seen this kind of error before. Thanks for your help!

   


2016-10-24 10:04:21.767  INFO 9908 --- [           main] com.consol.citrus.actions.EchoAction     : http://localhost:8081/v1/event
2016-10-24 10:04:21.914  INFO 9908 --- [           main] c.consol.citrus.http.client.HttpClient   : HTTP message was sent to endpoint: 'http://localhost:8081/event'


[Fatal Error] :1:1: Content is not allowed in prolog.
2016-10-24 10:04:21.930  INFO 9908 --- [           main] com.consol.citrus.Citrus                 :
2016-10-24 10:04:21.934 ERROR 9908 --- [           main] com.consol.citrus.Citrus                 : TEST FAILED SendMsgToQueueTest.testSendingMessage <com.proxibid.iop.citrus.integration> Nested exception is:

com.consol.citrus.exceptions.CitrusRuntimeException: org.w3c.dom.ls.LSException: Content is not allowed in prolog.
        at com.consol.citrus.validation.xml.DomXmlMessageValidator.validateMessagePayload(DomXmlMessageValidator.java:116) ~[citrus-core-2.6.1.jar:na]
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
I have been giving a wrong format in my testClient.xml( was sending a xml based message when the application/type is json). A very silly overlook..that was my bad. I just figured that out and I was able to send the message across..I still get the
com.consol.citrus.exceptions.CitrusRuntimeException: org.w3c.dom.ls.LSException: Content is not allowed in prolog.
        at com.consol.citrus.validation.xml.DomXmlMessageValidator.validateMessagePayload(DomXmlMessageValidator.java:116) ~[citrus-core-2.6.1.jar:na]

when I try to receive and validate an OK message from the client as follows

http().client(eventXchangeClient).receive().response(HttpStatus.OK); How do I see the message coming back from the endpoint to check to see if the encoding is an issue? Thanks



Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
what you receive is a plaintext message body, while Citrus is handling XML payloads by default. So you have to set the messageType in receive operations:

http().client(eventXchangeClient)
        .receive()
        .response(HttpStatus.OK)
        .messageType(MessageType.PLAINTEXT); 

Also I think the path to the classpath file resource is still not working. You have to remove src/test/resources/ from the path as it is already included in the classpath filepath.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
This post was updated on .
Hi Christoph-

Thanks for your help..I appreciate your quick response, you rock!..I figured yesterday it was a plaintext coming back and I changed the response type to say plaintext and it worked. I was going to comment about it here and you had your response too :).  Thanks for taking the time to respond patiently to all requests coming through. The Classpath resource was working, it was redundant though after you mentioned I have it removed and pointed to just the filename and it finds it just the same as you had mentioned. I have one working test, and I feel good about it. I am trying to put all the citrus-application.properties file, citrus-context.xml and the external payloads in src/test/resources/citrus ( one more package level) and the beans  fail to load in citrus-context.xml. Do we have to configure the new package level with citrus? Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

rbernard
I added a src/test/resources/citrus package.

I changed the citrus-application.properties to point to it as follows

#citrus configuration
citrus.spring.application.context=classpath*:/citrus/citrus-context.xml

It seems to load the spring application context for citrus now. And I mentioned the package name in the payload too as follows and it finds the external payload file.

 http().client(testClient).send().post().header("citrus_http_method", "POST")
        .contentType("application/json")
        .payload(new ClassPathResource("/citrus/clientRequestTest.xml"));

I hope this is the correct approach.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
Looks good to me!
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Mounika
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Mounika
This post was updated on .
HI,

Can we pass an xml file name dynamically  in @CitrusXmlTest(name="") like loading from property file(citrus-application.properties)?
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
You can use packageScan option in @CitrusXmlTest annotation. This will load all XML test case definitions in this given package and execute those tests. The XML tests can then use different test names.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Mounika
Thanks Christoph
     I looked into it but i have many files in that package but i want to run only one based on my requirement. Assist me in doing so as i cant change the name in the test case every time.
Reply | Threaded
Open this post in threaded view
|

Re: @CitrusXmlTest annotation doesn't find the corresponding xml file

Christoph Deppisch
Administrator
You should have a Java test method for each XML test then. After doing that you can use a System property setting while calling the Maven build.

mvn install -Dit.test=MyTestClassName#myTestMethodName

This will execute exactly that single test method which should load the XML file then.