PageRenderTime 62ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/sitebricks/src/test/java/com/google/sitebricks/headless/HeadlessReplyTest.java

https://github.com/juven/sitebricks
Java | 230 lines | 169 code | 52 blank | 9 comment | 18 complexity | 79455fdf799f8979e043b1f61ac02795 MD5 | raw file
  1. package com.google.sitebricks.headless;
  2. import static org.easymock.EasyMock.createNiceMock;
  3. import static org.easymock.EasyMock.expect;
  4. import static org.easymock.EasyMock.replay;
  5. import static org.easymock.EasyMock.verify;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.IOException;
  9. import java.util.Collections;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.testng.annotations.Test;
  13. import com.google.common.collect.ImmutableMap;
  14. import com.google.inject.AbstractModule;
  15. import com.google.inject.Guice;
  16. import com.google.inject.Injector;
  17. import com.google.sitebricks.client.transport.Json;
  18. import com.google.sitebricks.client.transport.Text;
  19. import com.google.sitebricks.client.transport.Xml;
  20. import com.google.sitebricks.conversion.Converter;
  21. import com.google.sitebricks.conversion.ConverterRegistry;
  22. import com.google.sitebricks.conversion.StandardTypeConverter;
  23. /**
  24. * A unit test for the reply builder/response populate pipeline.
  25. */
  26. public class HeadlessReplyTest {
  27. private static final String HELLO_THERE = "Hello there!";
  28. private static final String INK_SPOTS = "Ink Spots";
  29. private static final String MAYBE = "Maybe";
  30. private static final int SONG_LENGTH_MAYBE = 3456;
  31. private static final String X_MY_HEADER = "X-My-Header";
  32. private static final String X_MY_HEADER_VAL = "X-My-Haisdjfoiajsd";
  33. private static final String X_YOUR_HEADER = "X-Your-Header";
  34. private static final String X_YOUR_HEADER_VAL = "2838L";
  35. private static class FakeServletOutputStream extends ServletOutputStream {
  36. private final ByteArrayOutputStream bout = new ByteArrayOutputStream();
  37. @Override
  38. public void write(int b) throws IOException {
  39. bout.write(b);
  40. }
  41. @Override
  42. public String toString() {
  43. return bout.toString();
  44. }
  45. }
  46. @Test
  47. public void textReply() throws IOException {
  48. Injector injector = Guice.createInjector();
  49. HeadlessRenderer renderer = injector.getInstance(HeadlessRenderer.class);
  50. HttpServletResponse response = createNiceMock(HttpServletResponse.class);
  51. ServletOutputStream outputStream = fakeServletOutputStream();
  52. // The script to expect for our mock.
  53. response.setStatus(HttpServletResponse.SC_OK);
  54. expect(response.getOutputStream())
  55. .andReturn(outputStream);
  56. response.setContentType(injector.getInstance(Text.class).contentType());
  57. replay(response);
  58. renderer.render(response, Reply.with(HELLO_THERE));
  59. verify(response);
  60. assert HELLO_THERE.equals(outputStream.toString());
  61. }
  62. @Test
  63. public void jsonReply() throws IOException {
  64. Injector injector = Guice.createInjector(new AbstractModule() {
  65. @SuppressWarnings("rawtypes")
  66. @Override
  67. protected void configure() {
  68. bind(ConverterRegistry.class).toInstance(new StandardTypeConverter(Collections.<Converter>emptySet()));
  69. }
  70. });
  71. HeadlessRenderer renderer = injector.getInstance(HeadlessRenderer.class);
  72. HttpServletResponse response = createNiceMock(HttpServletResponse.class);
  73. ServletOutputStream outputStream = fakeServletOutputStream();
  74. // The script to expect for our mock.
  75. response.setStatus(HttpServletResponse.SC_OK);
  76. expect(response.getOutputStream())
  77. .andReturn(outputStream);
  78. response.setContentType(injector.getInstance(Json.class).contentType());
  79. replay(response);
  80. Song maybeByTheInkspots = new Song(MAYBE, INK_SPOTS, SONG_LENGTH_MAYBE);
  81. renderer.render(response, Reply.with(maybeByTheInkspots).as(Json.class));
  82. verify(response);
  83. String output = outputStream.toString();
  84. assert output.contains(MAYBE);
  85. assert output.contains(INK_SPOTS);
  86. assert output.contains("" + SONG_LENGTH_MAYBE);
  87. // Now the real test, unmarshall it back into Java.
  88. Song song = injector.getInstance(Json.class)
  89. .in(new ByteArrayInputStream(output.getBytes()), Song.class);
  90. assert maybeByTheInkspots.hashCode() == song.hashCode();
  91. assert maybeByTheInkspots.equals(song);
  92. }
  93. @Test
  94. public void xmlReplyWithHeaders() throws IOException {
  95. Injector injector = Guice.createInjector();
  96. HeadlessRenderer renderer = injector.getInstance(HeadlessRenderer.class);
  97. HttpServletResponse response = createNiceMock(HttpServletResponse.class);
  98. ServletOutputStream outputStream = fakeServletOutputStream();
  99. ImmutableMap<String, String> headerMap = ImmutableMap.of(
  100. X_MY_HEADER, X_MY_HEADER_VAL,
  101. X_YOUR_HEADER, X_YOUR_HEADER_VAL
  102. );
  103. // The script to expect for our mock.
  104. response.setStatus(HttpServletResponse.SC_OK);
  105. expect(response.getOutputStream())
  106. .andReturn(outputStream);
  107. response.setContentType(injector.getInstance(Xml.class).contentType());
  108. response.setHeader(X_MY_HEADER, X_MY_HEADER_VAL);
  109. response.setHeader(X_YOUR_HEADER, X_YOUR_HEADER_VAL);
  110. replay(response);
  111. Song maybeByTheInkspots = new Song(MAYBE, INK_SPOTS, SONG_LENGTH_MAYBE);
  112. renderer.render(response, Reply.with(maybeByTheInkspots)
  113. .as(Xml.class)
  114. .headers(headerMap)
  115. );
  116. verify(response);
  117. String output = outputStream.toString();
  118. assert output.contains(MAYBE);
  119. assert output.contains(INK_SPOTS);
  120. assert output.contains("" + SONG_LENGTH_MAYBE);
  121. // Now the real test, unmarshall it back into Java.
  122. Song song = injector.getInstance(Xml.class)
  123. .in(new ByteArrayInputStream(output.getBytes()), Song.class);
  124. assert maybeByTheInkspots.hashCode() == song.hashCode();
  125. assert maybeByTheInkspots.equals(song);
  126. }
  127. public static class Song {
  128. private String name;
  129. private String artist;
  130. private int length;
  131. // Needed for Jackson (crappy)
  132. public Song() {
  133. }
  134. public Song(String name, String artist, int length) {
  135. this.name = name;
  136. this.artist = artist;
  137. this.length = length;
  138. }
  139. public String getName() {
  140. return name;
  141. }
  142. public void setName(String name) {
  143. this.name = name;
  144. }
  145. public String getArtist() {
  146. return artist;
  147. }
  148. public void setArtist(String artist) {
  149. this.artist = artist;
  150. }
  151. public int getLength() {
  152. return length;
  153. }
  154. public void setLength(int length) {
  155. this.length = length;
  156. }
  157. @Override
  158. public boolean equals(Object o) {
  159. if (this == o) return true;
  160. if (o == null || getClass() != o.getClass()) return false;
  161. Song song = (Song) o;
  162. if (length != song.length) return false;
  163. if (artist != null ? !artist.equals(song.artist) : song.artist != null) return false;
  164. if (name != null ? !name.equals(song.name) : song.name != null) return false;
  165. return true;
  166. }
  167. @Override
  168. public int hashCode() {
  169. int result = name != null ? name.hashCode() : 0;
  170. result = 31 * result + (artist != null ? artist.hashCode() : 0);
  171. result = 31 * result + length;
  172. return result;
  173. }
  174. }
  175. private static ServletOutputStream fakeServletOutputStream() {
  176. return new FakeServletOutputStream();
  177. }
  178. }