0

I am writing a Restful service using Scala.

On the server side, it has a interface:

trait ICustomerService {
  @GET
  @Path("/{id}")
  @Produces(Array("application/xml"))
  def getCustomer(@PathParam("id") id: Int): StreamingOutput
}

The service works fine and I tested it using web browser.

Now I want to write some automated test to this interface. The way I need to do is to write a RESTEasy client using the same interface:

class CustomerServiceProxy(url : String) {
  RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
  val proxy = ProxyFactory.create(classOf[ICustomerService], url)

  def getCustomer(id: Int): Customer = {
    val streamingOutput = proxy.getCustomer(id)
    <Problem here>
  }
}

This code will not work as the streaming output only allow writing.

How do I write this test class so that I can get what the server write into the streamingoutput from the client side?

Many thanks

Kevin
  • 5,972
  • 17
  • 63
  • 87

1 Answers1

1

The StreamingOutput doesn't allow writing, it performs writing. All you have to do is make your own OutputStream to capture it:

/**
 * Re-buffers data from a JAXRS StreamingOutput into a new InputStream
 */
def rebuffer(so: StreamingOutput): InputStream = {
  val os = new ByteArrayOutputStream
  so.write(os)
  new ByteArrayInputStream(os.toByteArray())
}


def getCustomer(id: Int): Customer = {
  val streamingOutput = proxy.getCustomer(id)
  val inputStream = rebuffer(streamingOutput)
  inputStream.read() // or pass it to an XML parser or whatever
}

Hope this helps!

arya
  • 946
  • 5
  • 14