Many ATG implementations require writing your own Dynamo Servlets. These may consist of "pipeline servlets" or "droplets". Also most ATG applications use "formhandlers" to gather information from a form on a web page. All of this code is likely to require use of the DynamoHttpServletRequest/Response pair. Since these are such common classes, DUST has included some utility code to make it simpler to write tests for code that depends upon these classes.
Here's a simple example showing creation of the request and response pair.
Map<String, String> params = new HashMap<String, String>(); // Add some request parameters, though they don't get used in this test. params.put("color", "red"); // let's try 128k, should be more than enough int bufferSize = 128 * 1024; // Setup request/response pair TestingDynamoHttpServletRequest request = mServletTestUtils.createDynamoHttpServletRequest(params, bufferSize, "GET"); DynamoHttpServletResponse response = mServletTestUtils.createDynamoHttpServletResponse();
The interesting things to note here are:
This is simple. Just write your parameters into a MapString,String . When you call ServletTestUtil.createDynamoRequest(params,buffer,method) your map will be set into the returned request object as if they were parameters passed in a URL.
Let's say you are testing a POST method and you want to similate some content written by your browser. Create your request object:
TestingDynamoHttpServletRequest request = mServletTestUtils.createDynamoHttpServletRequest(params, bufferSize, "GET");
Now get an output stream to which you can write some content:
OutputStream out = request.getClientOutputStream(false); out.write(SimpleDroplet.USERNAME.getBytes()); out.write("=noonan".getBytes());
Finally, be sure to prepare the test request for reading. This method is specific to TestingDynamoHttpServletRequest, it is *not* a method on DynamoHttpServletRequest. It's simply there to setup the backing ByteBuffer so it's ready to be read from.
request.prepareForRead();
After that, your code being tested can happily call getOutputStream() on the response object and write some data to it.
response.getOutputStream(); // Write some XML output out.print("<message>Hello World!</message>");
Back in your JUnit test you can read the content of that output stream simply by calling to toString() method on the response's output stream. That's because the test implementation of the output stream is actually an atg.servlet.ByteArrayServletOutputStream .
For example:
String myCodeWroteThis = response.getOutputStream().toString(); String expectedResponse = "<message>Hello World!</message>" assertEquals(expectedResponse,myCodeWroteThis);
The DynamoHttpServletRequest.resolveName() method currently doesn't work. We'll get some solution to this soon.
The DynamoHttpServletRequest.resolveName() method currently does not work. We might modify it to just look up global components. Still need to figure out the right thing to do here. There isn't a "session" or "request" in a JUnit test so there's no context for looking up session and request scoped objects.
ServletTestUtils creates the DynamoHttpServletRequest and Response objects and minimally initializs these objects. I say minimally because normally in an ATG application the request and response travel down the request pipeline where many operations happen. There isn't a request pipeline when running in a unit test so only the following initialization is done to the request response pair.
That's all. You can still get a lot done, but don't expect the exact same parameters you would see in your fully running application. After all these are unit tests and you are trying to test out some specific functionality, not recreate the entire application in JUnit so I hope it's still helpful.
Feedback and improvement requests are welcome mail:atgdust-general@lists.sourceforge.net