Forum Discussion

Unibet_Support's avatar
Unibet_Support
Contributor
13 years ago

[Resolved] Deploying mockservice as war

Hi,
Iam using soapuipro 4.5.1 and trying to use the mockserver as war so that i can deploy it alongside my main web app and run integration tests using mockserver.
So i have 2 questions in this regard
1)How to pass project properties to this mockserver servlet inside the war?
so if iam using something like
def testFilesDir = context.expand( '${#Project#testfilesdir}' )
in my mockserver script step,how would this work when i deploy it onto app server

2)why does this export option always export in unarchived fashion?


Thanks

10 Replies

  • Hi!

    1) No, there are no easy way to pass properties to a deployed MockService. One walk around could be to use global properties defined in the settings.xml file that is used by the MockService and then changing that file prior to launching the MockService in the servlet container.

    2) If you fill in the full path for both the war file that you want to generate and the directory in which you want to create an exploded war file in the Deploy As War dialog, it should work.

    ---

    Regards

    Erik, SmartBear Support
  • Hi,
    Thanks for your response.
    I want to deploy this mockserver war along with my application as part of automated tests(So that my main app can talk to this mockserver app).
    Iam reading response files which i use in mockserver to send response back.So the idea i had was to pass in the value of this response files directory path through maven property in pom.
    Is there any other way to achieve this?

    Thanks
  • Hi,

    perhaps you could just set the folder in a system property (from the command-line) and read that in your script with System.getProperty(..) ?

    Does that make sense?

    regards!

    /Ole
    SmartBear Software
  • Hi,
    Yes.It does make sense.But i realised that having file directory path harcoded isnt a feasible option as we would have nightmares doing automated deployments and stuff.
    So what i did was export soapui war and set it as a maven project and than add these files in src/resources directory so that its accessible under classpath.
    So this is my groovy script code.

    def httpResponse = mockRequest.httpResponse
    def fileInputStream= this.getClass().getClassLoader().getParent().getResourceAsStream(mockServerResponseDir + responseFileName)
    httpResponse.setStatus(httpStatusCode)
    def length = IOUtils.copy(fileInputStream, httpResponse.getOutputStream())
    httpResponse.setContentLength( ( int )length )
    httpResponse.setContentType( 'application/json' )
    fileInputStream.close()

    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)


    But this is causing me issues with setting content-type.Somewhere this ContentTypeHandler.getContentTypeFromFilename() method is getting called and the content-type is set to application/octet-stream which is default.Any ideas why its happening like this?

    Thanks
  • Hi,

    ok.. and if you call this "resource" from soapUI (using an HTTP TestStep) you get an application/octet-stream content-type!?

    /Ole
    SmartBear Software
  • Hi,
    Here is the code.
    When i did that from HTTP test step i get content type as null.
    I think its better if you could set this up locally and try to return a json/or any other file for that matter and see if the content type is being set.
    Iam using soapui-pro 4.5.1

    Thanks


    <con:onRequestScript>
    import org.apache.commons.lang.StringUtils
    import java.util.logging.Level
    import java.util.logging.Logger
    import org.apache.commons.io.IOUtils

    def logger = Logger.getLogger( this.getClass().getName() );
    def queryString = mockRequest.getHttpRequest().getQueryString()
    def testfilesdir = context.expand( '${#Project#testfilesdir}' )
    def httpStatusCode = javax.servlet.http.HttpServletResponse.SC_OK

    String mockServerResponseDir = "testfilesdir/responsefrommockserver/"

    String responseFileName = "filenotfound";
    if(StringUtils.contains(queryString,"term=PSV")){
    responseFileName = "response.json";
    }else if(StringUtils.contains(queryString,"term=UNKNOWN")){
    responseFileName = "notfound.json";
    httpStatusCode = javax.servlet.http.HttpServletResponse.SC_NOT_FOUND
    }

    def httpResponse = mockRequest.httpResponse
    def fileInputStream= this.getClass().getClassLoader().getParent().getResourceAsStream(mockServerResponseDir
    + responseFileName)
    httpResponse.setStatus(httpStatusCode)
    def length = IOUtils.copy(fileInputStream, httpResponse.getOutputStream())
    httpResponse.setContentLength( ( int )length )
    httpResponse.setContentType( 'application/json' )
    fileInputStream.close()

    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)

    </con:onRequestScript>
  • Hi,

    You probably need to make sure that the contentType is set before you open for writing.

    I.e. move httpResponse.setContentType( 'application/json' ) up higher:


    def httpResponse = mockRequest.httpResponse
    httpResponse.setContentType( 'application/json' )
    def fileInputStream= this.getClass().getClassLoader().getParent().getResourceAsStream(mockServerResponseDir + responseFileName)
    httpResponse.setStatus(httpStatusCode)
    // contentType needs to be set before this line
    def length = IOUtils.copy(fileInputStream, httpResponse.getOutputStream())


    Does this work for you?

    / Henrik