Mercurial > hg > release > thermostat-0.5
changeset 646:5ea7630208db
Implement RESTStorage.putPojo().
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003359.html
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Thu, 27 Sep 2012 15:41:52 +0200 |
parents | f10b214c0461 |
children | 9088495a62bc |
files | common/core/src/main/java/com/redhat/thermostat/common/storage/Category.java web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java web/client/src/test/java/com/redhat/thermostat/web/client/TestObj.java web/common/src/main/java/com/redhat/thermostat/web/common/WebInsert.java web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageEndpointTest.java |
diffstat | 7 files changed, 206 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/Category.java Tue Sep 25 00:09:21 2012 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Category.java Thu Sep 27 15:41:52 2012 +0200 @@ -40,6 +40,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class Category { private final String name; @@ -95,4 +96,12 @@ public String toString() { return getName(); } + + public boolean equals(Object o) { + if (! (o instanceof Category)) { + return false; + } + Category other = (Category) o; + return Objects.equals(name, other.name) && keys.equals(other.keys); + } }
--- a/web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java Tue Sep 25 00:09:21 2012 -0400 +++ b/web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java Thu Sep 27 15:41:52 2012 +0200 @@ -58,6 +58,7 @@ import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.common.storage.Update; import com.redhat.thermostat.web.common.RESTQuery; +import com.redhat.thermostat.web.common.WebInsert; public class RESTStorage extends Storage { @@ -162,9 +163,27 @@ } @Override - public void putPojo(Category arg0, boolean arg1, Pojo arg2) { - // TODO Auto-generated method stub + public void putPojo(Category category, boolean replace, Pojo pojo) { + try { + WebInsert insert = new WebInsert(category, replace, pojo.getClass().getName()); + URL url = new URL(endpoint + "/put-pojo"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestMethod("POST"); + OutputStream out = conn.getOutputStream(); + Gson gson = new Gson(); + OutputStreamWriter writer = new OutputStreamWriter(out); + writer.write("insert="); + gson.toJson(insert, writer); + writer.write("&pojo="); + gson.toJson(pojo, writer); + writer.flush(); + InputStream in = conn.getInputStream(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } } @Override
--- a/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Tue Sep 25 00:09:21 2012 -0400 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Thu Sep 27 15:41:52 2012 +0200 @@ -41,8 +41,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -61,6 +63,7 @@ import org.junit.Test; import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; import com.redhat.thermostat.common.storage.Categories; import com.redhat.thermostat.common.storage.Category; import com.redhat.thermostat.common.storage.Cursor; @@ -71,6 +74,7 @@ import com.redhat.thermostat.test.FreePortFinder.TryPort; import com.redhat.thermostat.web.common.Qualifier; import com.redhat.thermostat.web.common.RESTQuery; +import com.redhat.thermostat.web.common.WebInsert; public class RESTStorageTest { @@ -116,7 +120,6 @@ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - // Read request body. StringBuilder body = new StringBuilder(); Reader reader = request.getReader(); @@ -128,7 +131,6 @@ body.append((char) read); } requestBody = body.toString(); - // Send response body. response.setStatus(HttpServletResponse.SC_OK); if (responseBody != null) { @@ -213,4 +215,33 @@ assertFalse(results.hasNext()); } + @Test + public void testPut() throws IOException, JsonSyntaxException, ClassNotFoundException { + RESTStorage storage = new RESTStorage(); + storage.setEndpoint("http://localhost:" + port + "/"); + TestObj obj = new TestObj(); + obj.setProperty1("fluff"); + + storage.putPojo(category, true, obj); + + Gson gson = new Gson(); + StringReader reader = new StringReader(requestBody); + BufferedReader bufRead = new BufferedReader(reader); + String line = bufRead.readLine(); + String [] params = line.split("&"); + assertEquals(2, params.length); + String[] parts = params[0].split("="); + assertEquals("insert", parts[0]); + WebInsert insert = gson.fromJson(parts[1], WebInsert.class); + assertEquals(category, insert.getCategory()); + assertEquals(true, insert.isReplace()); + assertEquals(TestObj.class.getName(), insert.getPojoClass()); + + parts = params[1].split("="); + assertEquals(2, parts.length); + assertEquals("pojo", parts[0]); + Object resultObj = gson.fromJson(parts[1], Class.forName(insert.getPojoClass())); + assertEquals(obj, resultObj); + } + }
--- a/web/client/src/test/java/com/redhat/thermostat/web/client/TestObj.java Tue Sep 25 00:09:21 2012 -0400 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/TestObj.java Thu Sep 27 15:41:52 2012 +0200 @@ -51,4 +51,12 @@ public String getProperty1() { return property1; } + + public boolean equals(Object o) { + if (! (o instanceof TestObj)) { + return false; + } + TestObj other = (TestObj) o; + return property1.equals(other.property1); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/main/java/com/redhat/thermostat/web/common/WebInsert.java Thu Sep 27 15:41:52 2012 +0200 @@ -0,0 +1,83 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + + +package com.redhat.thermostat.web.common; + +import com.redhat.thermostat.common.storage.Category; + +public class WebInsert { + + private Category category; + private boolean replace; + private String pojoClass; + + public WebInsert() { + this(null, false, null); + } + + public WebInsert(Category category, boolean replace, String pojoClass) { + this.category = category; + this.replace = replace; + this.pojoClass = pojoClass; + } + + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } + + public boolean isReplace() { + return replace; + } + + public void setReplace(boolean replace) { + this.replace = replace; + } + + public String getPojoClass() { + return pojoClass; + } + + public void setPojoClass(String pojoClass) { + this.pojoClass = pojoClass; + } + + +}
--- a/web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java Tue Sep 25 00:09:21 2012 -0400 +++ b/web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java Thu Sep 27 15:41:52 2012 +0200 @@ -10,12 +10,14 @@ import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; +import com.redhat.thermostat.common.model.Pojo; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.web.common.Qualifier; import com.redhat.thermostat.web.common.RESTQuery; import com.redhat.thermostat.web.common.StorageWrapper; +import com.redhat.thermostat.web.common.WebInsert; @SuppressWarnings("serial") public class RESTStorageEndPoint extends HttpServlet { @@ -38,6 +40,22 @@ findPojo(req, resp); } else if (cmd.equals("find-all")) { findAll(req, resp); + } else if (cmd.equals("put-pojo")) { + putPojo(req, resp); + } + } + + private void putPojo(HttpServletRequest req, HttpServletResponse resp) { + try { + String insertParam = req.getParameter("insert"); + WebInsert insert = gson.fromJson(insertParam, WebInsert.class); + Class<? extends Pojo> pojoCls = (Class<? extends Pojo>) Class.forName(insert.getPojoClass()); + String pojoParam = req.getParameter("pojo"); + Pojo pojo = gson.fromJson(pojoParam, pojoCls); + storage.putPojo(insert.getCategory(), insert.isReplace(), pojo); + resp.setStatus(HttpServletResponse.SC_OK); + } catch (ClassNotFoundException ex) { + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageEndpointTest.java Tue Sep 25 00:09:21 2012 -0400 +++ b/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageEndpointTest.java Thu Sep 27 15:41:52 2012 +0200 @@ -72,6 +72,7 @@ import com.redhat.thermostat.web.client.RESTStorage; import com.redhat.thermostat.web.common.RESTQuery; import com.redhat.thermostat.web.common.StorageWrapper; +import com.redhat.thermostat.web.common.WebInsert; public class RESTStorageEndpointTest { @@ -90,6 +91,13 @@ public void setKey2(int key2) { this.key2 = key2; } + public boolean equals(Object o) { + if (! (o instanceof TestClass)) { + return false; + } + TestClass other = (TestClass) o; + return key1.equals(other.key1) && key2 == other.key2; + } } private Server server; @@ -204,6 +212,32 @@ assertEquals(43, results[1].getKey2()); } + @Test + public void testPutPojo() throws IOException { + + TestClass expected1 = new TestClass(); + expected1.setKey1("fluff1"); + expected1.setKey2(42); + + String endpoint = getEndpoint(); + URL url = new URL(endpoint + "/put-pojo"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + WebInsert insert = new WebInsert(category, true, TestClass.class.getName()); + Gson gson = new Gson(); + OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); + out.write("insert="); + gson.toJson(insert, out); + out.flush(); + out.write("&pojo="); + gson.toJson(expected1, out); + out.write("\n"); + out.flush(); + assertEquals(200, conn.getResponseCode()); + verify(mockStorage).putPojo(category, true, expected1); + } + private String getEndpoint() { return "http://localhost:" + port + "/storage"; }