Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.hamcrest.CoreMatchers.*;
- import static org.junit.Assert.*;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.ServletInputStream;
- import org.junit.Test;
- import org.slim3.datastore.Datastore;
- import org.slim3.tester.ControllerTestCase;
- import com.shin1ogawa.meta.Slim3ModelMeta;
- import com.shin1ogawa.model.Slim3Model;
- public class JsonControllerTest extends ControllerTestCase {
- static final String PATH = "/json";
- static final Slim3ModelMeta meta = Slim3ModelMeta.get();
- @Test public void post() throws NullPointerException, IllegalArgumentException,
- IOException, ServletException {
- int beforeCount = tester.count(Slim3Model.class);
- String content = "{\"prop1\":\"hoge\"}";
- tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
- tester.request.setMethod("POST");
- tester.start(PATH);
- assertThat(tester.getController(), is(instanceOf(JsonController.class)));
- assertThat(tester.response.getStatus(), is(200));
- assertThat(tester.response.getContentType(), is("application/json"));
- assertThat("postなので1件増える", tester.count(Slim3Model.class), is(beforeCount + 1));
- Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
- assertThat(model.getProp1(), is("hoge"));
- }
- @Test public void put() throws NullPointerException, IllegalArgumentException,
- IOException, ServletException {
- int beforeCount = tester.count(Slim3Model.class);
- String content = "{\"prop1\":\"hoge\"}";
- tester.request.addParameter("id", "8");
- tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
- tester.request.setMethod("PUT");
- tester.start(PATH);
- assertThat(tester.getController(), is(instanceOf(JsonController.class)));
- assertThat(tester.response.getStatus(), is(200));
- assertThat(tester.response.getContentType(), is("application/json"));
- assertThat("putでは増えない", tester.count(Slim3Model.class), is(beforeCount));
- Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
- assertThat(model.getKey(), is(Datastore.createKey(meta, "8")));
- assertThat(model.getProp1(), is("hoge"));
- }
- @Test public void postWithId() throws NullPointerException,
- IllegalArgumentException, IOException, ServletException {
- int beforeCount = tester.count(Slim3Model.class);
- String content = "{\"prop1\":\"hoge\"}";
- tester.request.addParameter("id", "8");
- tester.request.setInputStream(new JsonInputStream(content.getBytes("utf-8")));
- tester.request.setMethod("POST");
- tester.start(PATH);
- assertThat(tester.getController(), is(instanceOf(JsonController.class)));
- assertThat(tester.response.getStatus(), is(200));
- assertThat(tester.response.getContentType(), is("application/json"));
- assertThat("id有りPostはPut操作となり、増えない", tester.count(Slim3Model.class), is(beforeCount));
- Slim3Model model = meta.jsonToModel(tester.response.getOutputAsString());
- assertThat(model.getKey(), is(Datastore.createKey(meta, "8")));
- assertThat(model.getProp1(), is("hoge"));
- }
- static class JsonInputStream extends ServletInputStream {
- ByteArrayInputStream in;
- JsonInputStream(byte[] content) {
- this.in = new ByteArrayInputStream(content);
- }
- @Override public int available() throws IOException {
- return in.available();
- }
- @Override public int read() throws IOException {
- return in.read();
- }
- @Override public int read(byte[] b, int off, int len) throws IOException {
- return in.read(b, off, len);
- }
- }
- @Override public void setUp() throws Exception {
- super.setUp();
- List<Slim3Model> entities = new ArrayList<Slim3Model>();
- for (int i = 0; i < 10; i++) {
- Slim3Model model = new Slim3Model();
- model.setKey(Datastore.createKey(Slim3Model.class, String.valueOf(i)));
- model.setProp1(String.valueOf(i));
- entities.add(model);
- }
- Datastore.put(entities);
- }
- }
Add Comment
Please, Sign In to add comment