Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import static org.hamcrest.CoreMatchers.*;
  2. import static org.junit.Assert.*;
  3.  
  4. import java.io.ByteArrayInputStream;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletInputStream;
  11.  
  12. import org.junit.Test;
  13. import org.slim3.datastore.Datastore;
  14. import org.slim3.tester.ControllerTestCase;
  15.  
  16. import com.shin1ogawa.meta.Slim3ModelMeta;
  17. import com.shin1ogawa.model.Slim3Model;
  18.  
  19. public class JsonControllerTest extends ControllerTestCase {
  20.  
  21. static final String PATH = "/json";
  22.  
  23. static final Slim3ModelMeta meta = Slim3ModelMeta.get();
  24.  
  25. @Test public void post() throws NullPointerException, IllegalArgumentException,
  26. IOException, ServletException {
  27. int beforeCount = tester.count(Slim3Model.class);
  28. String content = "{\"prop1\":\"hoge\"}";
  29. tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
  30. tester.request.setMethod("POST");
  31. tester.start(PATH);
  32.  
  33. assertThat(tester.getController(), is(instanceOf(JsonController.class)));
  34. assertThat(tester.response.getStatus(), is(200));
  35. assertThat(tester.response.getContentType(), is("application/json"));
  36.  
  37. assertThat("postなので1件増える", tester.count(Slim3Model.class), is(beforeCount + 1));
  38. Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
  39. assertThat(model.getProp1(), is("hoge"));
  40. }
  41.  
  42. @Test public void put() throws NullPointerException, IllegalArgumentException,
  43. IOException, ServletException {
  44. int beforeCount = tester.count(Slim3Model.class);
  45. String content = "{\"prop1\":\"hoge\"}";
  46. tester.request.addParameter("id", "8");
  47. tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
  48. tester.request.setMethod("PUT");
  49. tester.start(PATH);
  50.  
  51. assertThat(tester.getController(), is(instanceOf(JsonController.class)));
  52. assertThat(tester.response.getStatus(), is(200));
  53. assertThat(tester.response.getContentType(), is("application/json"));
  54.  
  55. assertThat("putでは増えない", tester.count(Slim3Model.class), is(beforeCount));
  56. Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
  57. assertThat(model.getKey(), is(Datastore.createKey(meta, "8")));
  58. assertThat(model.getProp1(), is("hoge"));
  59. }
  60.  
  61. @Test public void postWithId() throws NullPointerException,
  62. IllegalArgumentException, IOException, ServletException {
  63. int beforeCount = tester.count(Slim3Model.class);
  64. String content = "{\"prop1\":\"hoge\"}";
  65. tester.request.addParameter("id", "8");
  66. tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
  67. tester.request.setMethod("POST");
  68. tester.start(PATH);
  69.  
  70. assertThat(tester.getController(), is(instanceOf(JsonController.class)));
  71. assertThat(tester.response.getStatus(), is(200));
  72. assertThat(tester.response.getContentType(), is("application/json"));
  73.  
  74. assertThat("id有りPostはPut操作となり、増えない", tester.count(Slim3Model.class), is(beforeCount));
  75. Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
  76. assertThat(model.getKey(), is(Datastore.createKey(meta, "8")));
  77. assertThat(model.getProp1(), is("hoge"));
  78. }
  79.  
  80. static class JsonInputStream extends ServletInputStream {
  81. ByteArrayInputStream in;
  82.  
  83. JsonInputStream(byte[] content) {
  84. this.in = new ByteArrayInputStream(content);
  85. }
  86.  
  87. @Override public int available() throws IOException {
  88. return in.available();
  89. }
  90.  
  91. @Override public int read() throws IOException {
  92. return in.read();
  93. }
  94.  
  95. @Override public int read(byte[] b, int off, int len) throws IOException {
  96. return in.read(b, off, len);
  97. }
  98. }
  99.  
  100. @Override public void setUp() throws Exception {
  101. super.setUp();
  102. List<Slim3Model> entities = new ArrayList<Slim3Model>();
  103. for (int i = 0; i < 10; i++) {
  104. Slim3Model model = new Slim3Model();
  105. model.setKey(Datastore.createKey(Slim3Model.class, String.valueOf(i)));
  106. model.setProp1(String.valueOf(i));
  107. entities.add(model);
  108. }
  109. Datastore.put(entities);
  110. }
  111. }
Add Comment
Please, Sign In to add comment