Mercurial > hg > release > thermostat-0.6
changeset 699:0daa5cf25aec
Implement WebRemove.
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/003753.html
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Thu, 18 Oct 2012 00:23:54 +0200 |
parents | d552470b531b |
children | 1dd0ea9aa8b4 |
files | 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/common/src/main/java/com/redhat/thermostat/web/common/WebRemove.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 | 5 files changed, 208 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java Wed Oct 17 19:08:13 2012 +0200 +++ b/web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java Thu Oct 18 00:23:54 2012 +0200 @@ -62,6 +62,7 @@ import com.redhat.thermostat.common.storage.Update; import com.redhat.thermostat.web.common.RESTQuery; import com.redhat.thermostat.web.common.WebInsert; +import com.redhat.thermostat.web.common.WebRemove; public class RESTStorage extends Storage { @@ -133,8 +134,7 @@ @Override public Remove createRemove() { - // TODO Auto-generated method stub - return null; + return new WebRemove(categoryIds); } @Override @@ -239,17 +239,36 @@ writer.write("&pojo="); writer.write(URLEncoder.encode(gson.toJson(pojo), "UTF-8")); writer.flush(); - - InputStream in = conn.getInputStream(); + checkResponseCode(conn); } catch (IOException ex) { throw new RuntimeException(ex); } } + private void checkResponseCode(HttpURLConnection conn) throws IOException { + int responseCode = conn.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + throw new RuntimeException("Web server returned HTTP code: " + responseCode); + } + } + @Override - public void removePojo(Remove arg0) { - // TODO Auto-generated method stub - + public void removePojo(Remove remove) { + try { + URL url = new URL(endpoint + "/remove-pojo"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + OutputStream out = conn.getOutputStream(); + Gson gson = new Gson(); + OutputStreamWriter writer = new OutputStreamWriter(out); + writer.write("remove="); + writer.write(URLEncoder.encode(gson.toJson(remove), "UTF-8")); + writer.flush(); + checkResponseCode(conn); + } catch (IOException ex) { + throw new RuntimeException(ex); + } } @Override
--- a/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Wed Oct 17 19:08:13 2012 +0200 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Thu Oct 18 00:23:54 2012 +0200 @@ -39,12 +39,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Arrays; import java.util.List; @@ -71,11 +73,13 @@ import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.test.FreePortFinder; 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; +import com.redhat.thermostat.web.common.WebRemove; public class RESTStorageTest { @@ -236,7 +240,6 @@ BufferedReader bufRead = new BufferedReader(reader); String line = URLDecoder.decode(bufRead.readLine(), "UTF-8"); String [] params = line.split("&"); - System.err.println("params: " + line); assertEquals(2, params.length); String[] parts = params[0].split("="); assertEquals("insert", parts[0]); @@ -252,4 +255,43 @@ assertEquals(obj, resultObj); } + @Test + public void testCreateRemove() { + WebRemove remove = (WebRemove) storage.createRemove(); + assertNotNull(remove); + remove = remove.from(category); + assertEquals(42, remove.getCategoryId()); + assertNotNull(remove); + remove = remove.where(key1, "test"); + assertNotNull(remove); + List<Qualifier<?>> qualifiers = remove.getQualifiers(); + assertEquals(1, qualifiers.size()); + Qualifier<?> qualifier = qualifiers.get(0); + assertEquals(key1, qualifier.getKey()); + assertEquals(Criteria.EQUALS, qualifier.getCriteria()); + assertEquals("test", qualifier.getValue()); + } + + @Test + public void testRemovePojo() throws UnsupportedEncodingException, IOException { + Remove remove = storage.createRemove().from(category).where(key1, "test"); + storage.removePojo(remove); + + Gson gson = new Gson(); + StringReader reader = new StringReader(requestBody); + BufferedReader bufRead = new BufferedReader(reader); + String line = URLDecoder.decode(bufRead.readLine(), "UTF-8"); + System.err.println("line: " + line); + String[] parts = line.split("="); + assertEquals("remove", parts[0]); + WebRemove actualRemove = gson.fromJson(parts[1], WebRemove.class); + + assertEquals(42, actualRemove.getCategoryId()); + List<Qualifier<?>> qualifiers = actualRemove.getQualifiers(); + assertEquals(1, qualifiers.size()); + Qualifier<?> qualifier = qualifiers.get(0); + assertEquals(key1, qualifier.getKey()); + assertEquals(Criteria.EQUALS, qualifier.getCriteria()); + assertEquals("test", qualifier.getValue()); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/main/java/com/redhat/thermostat/web/common/WebRemove.java Thu Oct 18 00:23:54 2012 +0200 @@ -0,0 +1,84 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.redhat.thermostat.common.storage.Category; +import com.redhat.thermostat.common.storage.Key; +import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; + +public class WebRemove implements Remove { + + private transient Map<Category, Integer> categoryIds; + private int categoryId; + private List<Qualifier<?>> qualifiers; + + // NOTE: This is needed for de-serialization! + public WebRemove() { + this(null); + } + + public WebRemove(Map<Category, Integer> categoryIds) { + qualifiers = new ArrayList<>(); + this.categoryIds = categoryIds; + } + + @Override + public WebRemove from(Category category) { + categoryId = categoryIds.get(category); + return this; + } + + @Override + public <T> WebRemove where(Key<T> key, T value) { + qualifiers.add(new Qualifier<T>(key, Criteria.EQUALS, value)); + return this; + } + + public int getCategoryId() { + return categoryId; + } + + public List<Qualifier<?>> getQualifiers() { + return qualifiers; + } + +}
--- a/web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java Wed Oct 17 19:08:13 2012 +0200 +++ b/web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java Thu Oct 18 00:23:54 2012 +0200 @@ -14,15 +14,17 @@ import com.google.gson.Gson; import com.redhat.thermostat.common.model.Pojo; -import com.redhat.thermostat.common.storage.Categories; import com.redhat.thermostat.common.storage.Category; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Query; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.Query.Criteria; 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; +import com.redhat.thermostat.web.common.WebRemove; @SuppressWarnings("serial") public class RESTStorageEndPoint extends HttpServlet { @@ -57,6 +59,8 @@ putPojo(req, resp); } else if (cmd.equals("register-category")) { registerCategory(req, resp); + } else if (cmd.equals("remove-pojo")) { + removePojo(req, resp); } } @@ -100,6 +104,21 @@ } @SuppressWarnings({ "rawtypes", "unchecked" }) + private void removePojo(HttpServletRequest req, HttpServletResponse resp) { + String removeParam = req.getParameter("remove"); + WebRemove remove = gson.fromJson(removeParam, WebRemove.class); + Remove targetRemove = storage.createRemove(); + targetRemove = targetRemove.from(getCategoryFromId(remove.getCategoryId())); + List<Qualifier<?>> qualifiers = remove.getQualifiers(); + for (Qualifier qualifier : qualifiers) { + assert (qualifier.getCriteria() == Criteria.EQUALS); + targetRemove = targetRemove.where(qualifier.getKey(), qualifier.getValue()); + } + storage.removePojo(targetRemove); + } + + + @SuppressWarnings({ "rawtypes", "unchecked" }) private void findPojo(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { Reader in = req.getReader();
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageEndpointTest.java Wed Oct 17 19:08:13 2012 +0200 +++ b/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageEndpointTest.java Thu Oct 18 00:23:54 2012 +0200 @@ -71,6 +71,7 @@ import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.test.FreePortFinder; import com.redhat.thermostat.test.FreePortFinder.TryPort; @@ -78,6 +79,7 @@ import com.redhat.thermostat.web.common.RESTQuery; import com.redhat.thermostat.web.common.StorageWrapper; import com.redhat.thermostat.web.common.WebInsert; +import com.redhat.thermostat.web.common.WebRemove; public class RESTStorageEndpointTest { @@ -249,6 +251,39 @@ verify(mockStorage).putPojo(category, true, expected1); } + @Test + public void testRemovePojo() throws IOException { + + Remove mockRemove = mock(Remove.class); + when(mockRemove.from(any(Category.class))).thenReturn(mockRemove); + when(mockRemove.where(any(Key.class), any())).thenReturn(mockRemove); + + when(mockStorage.createRemove()).thenReturn(mockRemove); + + String endpoint = getEndpoint(); + + URL url = new URL(endpoint + "/remove-pojo"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + Map<Category,Integer> categoryIds = new HashMap<>(); + categoryIds.put(category, categoryId); + WebRemove remove = new WebRemove(categoryIds).from(category).where(key1, "test"); + Gson gson = new Gson(); + OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); + out.write("remove="); + gson.toJson(remove, out); + out.write("\n"); + out.flush(); + + assertEquals(200, conn.getResponseCode()); + verify(mockStorage).createRemove(); + verify(mockRemove).from(category); + verify(mockRemove).where(key1, "test"); + verify(mockStorage).removePojo(mockRemove); + } + + private void registerCategory() { try { String endpoint = getEndpoint();