# HG changeset patch # User Roman Kennke # Date 1347995597 -7200 # Node ID 593c88ea5d0b4507ea8728f167877ef0e3853091 # Parent a325470a7366e5df828f08c3208a896f610563b6 Implement findPojo() as REST service. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003269.html diff -r a325470a7366 -r 593c88ea5d0b common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoDAOTest.java --- a/common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoDAOTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -56,11 +56,11 @@ import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; +import com.redhat.thermostat.common.storage.QueryTestHelper; 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.common.storage.Update; -import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class AgentInfoDAOTest { diff -r a325470a7366 -r 593c88ea5d0b common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoDAOTest.java --- a/common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoDAOTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -55,10 +55,10 @@ import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; +import com.redhat.thermostat.common.storage.QueryTestHelper; 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.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class BackendInfoDAOTest { diff -r a325470a7366 -r 593c88ea5d0b common/core/src/test/java/com/redhat/thermostat/common/dao/VmInfoDAOTest.java --- a/common/core/src/test/java/com/redhat/thermostat/common/dao/VmInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/VmInfoDAOTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -60,10 +60,10 @@ import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; +import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.common.storage.Query.Criteria; import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.common.storage.Update; -import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class VmInfoDAOTest { diff -r a325470a7366 -r 593c88ea5d0b common/core/src/test/java/com/redhat/thermostat/common/storage/QueryTestHelper.java --- a/common/core/src/test/java/com/redhat/thermostat/common/storage/QueryTestHelper.java Tue Sep 18 10:48:11 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/storage/QueryTestHelper.java Tue Sep 18 21:13:17 2012 +0200 @@ -41,6 +41,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.redhat.thermostat.common.storage.Query.Criteria; + public class QueryTestHelper { @SuppressWarnings("unchecked") @@ -59,4 +61,12 @@ when(mockRemove.where(any(Key.class), any())).thenReturn(mockRemove); return mockRemove; } + + @SuppressWarnings("unchecked") + public static Query createMockQuery() { + Query mockQuery = mock(Query.class); + when(mockQuery.from(any(Category.class))).thenReturn(mockQuery); + when(mockQuery.where(any(Key.class), any(Criteria.class), any())).thenReturn(mockQuery); + return mockQuery; + } } diff -r a325470a7366 -r 593c88ea5d0b pom.xml --- a/pom.xml Tue Sep 18 10:48:11 2012 +0200 +++ b/pom.xml Tue Sep 18 21:13:17 2012 +0200 @@ -109,6 +109,7 @@ unix-process-handler keyring thread + web diff -r a325470a7366 -r 593c88ea5d0b web/client/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/client/pom.xml Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,93 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat-web + 0.4.0-SNAPSHOT + + + thermostat-web-client + jar + + Thermostat Web Client + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + org.eclipse.jetty + jetty-server + 8.1.5.v20120716 + test + + + org.eclipse.jetty + jetty-webapp + 8.1.5.v20120716 + test + + + + com.google.code.gson + gson + 2.2.2 + + + + com.redhat.thermostat + thermostat-web-common + ${project.version} + + + + + diff -r a325470a7366 -r 593c88ea5d0b web/client/pom.xml~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/client/pom.xml~ Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,100 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat-web + 0.4.0-SNAPSHOT + + + thermostat-web-server + war + + Thermostat Web Server + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + org.eclipse.jetty + jetty-server + 8.1.5.v20120716 + test + + + org.eclipse.jetty + jetty-webapp + 8.1.5.v20120716 + test + + + + + javax.servlet + servlet-api + 2.5 + + + + com.google.code.gson + gson + 2.2.2 + + + + com.redhat.thermostat + thermostat-common-core + ${project.version} + + + + + diff -r a325470a7366 -r 593c88ea5d0b web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/client/src/main/java/com/redhat/thermostat/web/client/RESTStorage.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,187 @@ +/* + * 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 + * . + * + * 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.client; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; + +import com.google.gson.Gson; +import com.redhat.thermostat.common.model.Pojo; +import com.redhat.thermostat.common.storage.Category; +import com.redhat.thermostat.common.storage.Connection; +import com.redhat.thermostat.common.storage.ConnectionKey; +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.Update; +import com.redhat.thermostat.web.common.RESTQuery; + +public class RESTStorage extends Storage { + + private String endpoint; + + @Override + public ConnectionKey createConnectionKey(Category arg0) { + return new ConnectionKey() {}; + } + + @Override + public Query createQuery() { + return new RESTQuery(); + } + + @Override + public Remove createRemove() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Update createUpdate() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Cursor findAllPojos(Query arg0, Class arg1) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Cursor findAllPojosFromCategory(Category arg0, + Class arg1) { + // TODO Auto-generated method stub + return null; + } + + @Override + public T findPojo(Query query, Class resultClass) { + try { + ((RESTQuery) query).setResultClassName(resultClass.getName()); + URL url = new URL(endpoint + "/find-pojo"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoInput(true); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + OutputStream out = conn.getOutputStream(); + Gson gson = new Gson(); + OutputStreamWriter writer = new OutputStreamWriter(out); + gson.toJson(query, writer); + writer.flush(); + + InputStream in = conn.getInputStream(); + T result = gson.fromJson(new InputStreamReader(in), resultClass); + return result; + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String getAgentId() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Connection getConnection() { + // TODO Auto-generated method stub + return null; + } + + @Override + public long getCount(Category arg0) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public InputStream loadFile(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void purge() { + // TODO Auto-generated method stub + + } + + @Override + public void putPojo(Category arg0, boolean arg1, Pojo arg2) { + // TODO Auto-generated method stub + + } + + @Override + public void removePojo(Remove arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void saveFile(String arg0, InputStream arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void setAgentId(UUID arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void updatePojo(Update arg0) { + // TODO Auto-generated method stub + + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + +} diff -r a325470a7366 -r 593c88ea5d0b web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/RESTStorageTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,144 @@ +/* + * 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 + * . + * + * 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.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.Reader; +import java.util.Collection; +import java.util.List; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.Gson; +import com.redhat.thermostat.common.storage.Category; +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.web.common.Qualifier; +import com.redhat.thermostat.web.common.RESTQuery; + +public class RESTStorageTest { + + private Server server; + + private String requestBody; + + private String responseBody; + + @Before + public void setUp() throws Exception { + server = new Server(8080); + server.setHandler(new AbstractHandler() { + + @Override + 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(); + while (true) { + int read = reader.read(); + if (read == -1) { + break; + } + body.append((char) read); + } + requestBody = body.toString(); + + // Send response body. + response.setStatus(HttpServletResponse.SC_OK); + if (responseBody != null) { + response.getWriter().write(responseBody); + } + baseRequest.setHandled(true); + } + }); + server.start(); + } + + @After + public void tearDown() throws Exception { + server.stop(); + server.join(); + } + + @Test + public void testFindPojo() { + RESTStorage storage = new RESTStorage(); + storage.setEndpoint("http://localhost:8080/"); + + TestObj obj = new TestObj(); + obj.setProperty1("fluffor"); + Gson gson = new Gson(); + responseBody = gson.toJson(obj); + + Key key1 = new Key<>("property1", true); + Query query = storage.createQuery().from(new Category("test", key1)).where(key1, Criteria.EQUALS, "fluff"); + + TestObj result = storage.findPojo(query, TestObj.class); + RESTQuery restQuery = gson.fromJson(requestBody, RESTQuery.class); + + Category actualCategory = restQuery.getCategory(); + assertEquals("test", actualCategory.getName()); + Collection> keys = actualCategory.getKeys(); + assertEquals(1, keys.size()); + assertTrue(keys.contains(new Key("property1", true))); + List> qualifiers = restQuery.getQualifiers(); + assertEquals(1, qualifiers.size()); + Qualifier qual = qualifiers.get(0); + assertEquals(new Key("property1", true), qual.getKey()); + assertEquals(Criteria.EQUALS, qual.getCriteria()); + assertEquals("fluff", qual.getValue()); + + assertEquals("fluffor", result.getProperty1()); + } +} diff -r a325470a7366 -r 593c88ea5d0b web/client/src/test/java/com/redhat/thermostat/web/client/TestObj.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/client/src/test/java/com/redhat/thermostat/web/client/TestObj.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,54 @@ +/* + * 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 + * . + * + * 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.client; + +import com.redhat.thermostat.common.model.Pojo; + +public class TestObj implements Pojo { + + + private String property1; + + public void setProperty1(String property1) { + this.property1 = property1; + } + + public String getProperty1() { + return property1; + } +} diff -r a325470a7366 -r 593c88ea5d0b web/common/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/pom.xml Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,68 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat-web + 0.4.0-SNAPSHOT + + + thermostat-web-common + jar + + Thermostat Web Common + + + + + junit + junit + test + + + + com.redhat.thermostat + thermostat-common-core + ${project.version} + + + + diff -r a325470a7366 -r 593c88ea5d0b web/common/pom.xml~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/pom.xml~ Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,94 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat-web + 0.4.0-SNAPSHOT + + + thermostat-web-client + war + + Thermostat Web Client + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + org.eclipse.jetty + jetty-server + 8.1.5.v20120716 + test + + + org.eclipse.jetty + jetty-webapp + 8.1.5.v20120716 + test + + + + + com.google.code.gson + gson + 2.2.2 + + + + com.redhat.thermostat + thermostat-common-core + ${project.version} + + + + + diff -r a325470a7366 -r 593c88ea5d0b web/common/src/main/java/com/redhat/thermostat/web/common/Qualifier.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/main/java/com/redhat/thermostat/web/common/Qualifier.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,77 @@ +/* + * 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 + * . + * + * 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.Key; +import com.redhat.thermostat.common.storage.Query.Criteria; + +public class Qualifier { + private Key key; + private Criteria criteria; + private T value; + + public Qualifier() { + this(null, null, null); + } + + public Qualifier(Key key, Criteria criteria, T value) { + this.key = key; + this.criteria = criteria; + this.value = value; + } + + public Key getKey() { + return key; + } + public void setKey(Key key) { + this.key = key; + } + public Criteria getCriteria() { + return criteria; + } + public void setCriteria(Criteria criteria) { + this.criteria = criteria; + } + public T getValue() { + return value; + } + public void setValue(T value) { + this.value = value; + } + +} \ No newline at end of file diff -r a325470a7366 -r 593c88ea5d0b web/common/src/main/java/com/redhat/thermostat/web/common/RESTQuery.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/main/java/com/redhat/thermostat/web/common/RESTQuery.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,92 @@ +/* + * 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 + * . + * + * 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 com.redhat.thermostat.common.storage.Category; +import com.redhat.thermostat.common.storage.Key; +import com.redhat.thermostat.common.storage.Query; + +public class RESTQuery implements Query { + + private Category category; + private List> qualifiers; + private String resultClassName; + + public RESTQuery() { + qualifiers = new ArrayList<>(); + } + + @Override + public Query from(Category category) { + this.category = category; + return this; + } + + @Override + public Query where(Key key, Criteria criteria, T value) { + qualifiers.add(new Qualifier<>(key, criteria, value)); + return this; + } + + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } + + public List> getQualifiers() { + return qualifiers; + } + + public void setQualifiers(List> qualifiers) { + this.qualifiers = qualifiers; + } + + public String getResultClassName() { + return resultClassName; + } + + public void setResultClassName(String resultClassName) { + this.resultClassName = resultClassName; + } +} diff -r a325470a7366 -r 593c88ea5d0b web/common/src/main/java/com/redhat/thermostat/web/common/StorageWrapper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/main/java/com/redhat/thermostat/web/common/StorageWrapper.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,51 @@ +/* + * 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 + * . + * + * 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.Storage; + +public class StorageWrapper { + + private static Storage storage; + public static Storage getStorage() { + return storage; + } + public static void setStorage(Storage storage) { + StorageWrapper.storage = storage; + } +} diff -r a325470a7366 -r 593c88ea5d0b web/common/src/test/java/RESTQueryTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/common/src/test/java/RESTQueryTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,68 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import java.util.List; + +import org.junit.Test; + +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.web.common.Qualifier; +import com.redhat.thermostat.web.common.RESTQuery; + +/* + * 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 + * . + * + * 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. + */ + +public class RESTQueryTest { + + @Test + public void test() { + RESTQuery query = new RESTQuery(); + Key key1 = new Key<>("testkey", true); + Category category = new Category("test", key1); + query.from(category).where(key1, Criteria.EQUALS, "fluff"); + + List> qualifiers = query.getQualifiers(); + assertEquals(1, qualifiers.size()); + Qualifier qualifier = qualifiers.get(0); + assertEquals(key1, qualifier.getKey()); + assertEquals(Criteria.EQUALS, qualifier.getCriteria()); + assertEquals("fluff", qualifier.getValue()); + + assertSame(category, query.getCategory()); + } +} diff -r a325470a7366 -r 593c88ea5d0b web/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/pom.xml Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,59 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat + 0.4.0-SNAPSHOT + + + thermostat-web + pom + + Thermostat Web + + + common + server + client + + + diff -r a325470a7366 -r 593c88ea5d0b web/server/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/pom.xml Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,114 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat-web + 0.4.0-SNAPSHOT + + + thermostat-web-server + war + + Thermostat Web Server + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + com.redhat.thermostat + thermostat-web-client + ${project.version} + test + + + + + org.eclipse.jetty + jetty-server + 8.1.5.v20120716 + test + + + org.eclipse.jetty + jetty-webapp + 8.1.5.v20120716 + test + + + + + com.redhat.thermostat + thermostat-common-core + ${project.version} + + + + + javax.servlet + servlet-api + 2.5 + + + + com.google.code.gson + gson + 2.2.2 + + + + com.redhat.thermostat + thermostat-web-common + ${project.version} + + + + + diff -r a325470a7366 -r 593c88ea5d0b web/server/pom.xml~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/pom.xml~ Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,107 @@ + + + + 4.0.0 + + + com.redhat.thermostat + thermostat + 0.4.0-SNAPSHOT + + + thermostat-web + war + + Thermostat Web + + + + JBoss repository + https://repository.jboss.org/nexus/content/groups/public-jboss/ + + + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + org.eclipse.jetty + jetty-server + 8.1.5.v20120716 + test + + + org.eclipse.jetty + jetty-webapp + 8.1.5.v20120716 + test + + + + + javax.servlet + servlet-api + 2.5 + + + + com.google.code.gson + gson + 2.2.2 + + + + com.redhat.thermostat. + thermostat-common-core + ${project.version} + + + + + diff -r a325470a7366 -r 593c88ea5d0b web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/src/main/java/com/redhat/thermostat/web/server/RESTStorageEndPoint.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,61 @@ +package com.redhat.thermostat.web.server; + +import java.io.IOException; +import java.io.Reader; +import java.util.List; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.google.gson.Gson; +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; + +@SuppressWarnings("serial") +public class RESTStorageEndPoint extends HttpServlet { + + private Storage storage; + + public void init() { + storage = StorageWrapper.getStorage(); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String uri = req.getRequestURI(); + System.err.println("request uri: " + uri); + int lastPartIdx = uri.lastIndexOf("/"); + String cmd = uri.substring(lastPartIdx + 1); + if (cmd.equals("find-pojo")) { + findPojo(req, resp); + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void findPojo(HttpServletRequest req, HttpServletResponse resp) throws IOException { + try { + Reader in = req.getReader(); + Gson gson = new Gson(); + RESTQuery query = gson.fromJson(in, RESTQuery.class); + Class resultClass = Class.forName(query.getResultClassName()); + Query targetQuery = storage.createQuery(); + targetQuery = targetQuery.from(query.getCategory()); + List> qualifiers = query.getQualifiers(); + for (Qualifier q : qualifiers) { + targetQuery = targetQuery.where(q.getKey(), q.getCriteria(), q.getValue()); + } + Object result = storage.findPojo(targetQuery, resultClass); + resp.setStatus(HttpServletResponse.SC_OK); + resp.setContentType("application/json"); + gson.toJson(result, resp.getWriter()); + resp.flushBuffer(); + } catch (ClassNotFoundException e) { + resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "result class not found"); + } + } + +} diff -r a325470a7366 -r 593c88ea5d0b web/server/src/main/webapp/WEB-INF/web.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/src/main/webapp/WEB-INF/web.xml Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,17 @@ + + + + Archetype Created Web Application + + + reststorage-servlet + com.redhat.thermostat.web.server.RESTStorageEndPoint + + + + reststorage-servlet + /storage/* + + diff -r a325470a7366 -r 593c88ea5d0b web/server/src/test/java/com/redhat/thermostat/web/server/QueryTestHelper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/src/test/java/com/redhat/thermostat/web/server/QueryTestHelper.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,77 @@ +/* + * 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 + * . + * + * 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.server; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.redhat.thermostat.common.storage.Category; +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.Update; + +public class QueryTestHelper { + + @SuppressWarnings("unchecked") + public static Update createMockUpdate() { + Update mockUpdate = mock(Update.class); + when(mockUpdate.from(any(Category.class))).thenReturn(mockUpdate); + when(mockUpdate.where(any(Key.class), any())).thenReturn(mockUpdate); + when(mockUpdate.set(any(Key.class), any())).thenReturn(mockUpdate); + return mockUpdate; + } + + @SuppressWarnings("unchecked") + public static Remove createMockRemove() { + Remove mockRemove = mock(Remove.class); + when(mockRemove.from(any(Category.class))).thenReturn(mockRemove); + when(mockRemove.where(any(Key.class), any())).thenReturn(mockRemove); + return mockRemove; + } + + @SuppressWarnings("unchecked") + public static Query createMockQuery() { + Query mockQuery = mock(Query.class); + when(mockQuery.from(any(Category.class))).thenReturn(mockQuery); + when(mockQuery.where(any(Key.class), any(Criteria.class), any())).thenReturn(mockQuery); + return mockQuery; + } +} diff -r a325470a7366 -r 593c88ea5d0b web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/server/src/test/java/com/redhat/thermostat/web/server/RESTStorageTest.java Tue Sep 18 21:13:17 2012 +0200 @@ -0,0 +1,126 @@ +/* + * 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 + * . + * + * 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.server; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.same; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.model.Pojo; +import com.redhat.thermostat.common.storage.Category; +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.Storage; +import com.redhat.thermostat.web.client.RESTStorage; +import com.redhat.thermostat.web.common.StorageWrapper; + +public class RESTStorageTest { + + public static class TestClass implements Pojo { + private String key1; + private int key2; + public String getKey1() { + return key1; + } + public void setKey1(String key1) { + this.key1 = key1; + } + public int getKey2() { + return key2; + } + public void setKey2(int key2) { + this.key2 = key2; + } + } + + private Server server; + + private Storage mockStorage; + + @Before + public void setUp() throws Exception { + mockStorage = mock(Storage.class); + StorageWrapper.setStorage(mockStorage); + + server = new Server(8080); + server.setHandler(new WebAppContext("src/main/webapp", "/")); + server.start(); + } + + @After + public void tearDown() throws Exception { + server.stop(); + server.join(); + } + + @Test + public void testFind() { + // Configure mock storage. + TestClass expected = new TestClass(); + expected.setKey1("fluff"); + expected.setKey2(42); + when(mockStorage.findPojo(any(Query.class), same(TestClass.class))).thenReturn(expected); + + Query mockQuery = QueryTestHelper.createMockQuery(); + when(mockStorage.createQuery()).thenReturn(mockQuery); + + RESTStorage restStorage = new RESTStorage(); + restStorage.setEndpoint("http://localhost:8080/storage"); + Query query = restStorage.createQuery(); + Key key1 = new Key<>("key1", true); + Key key2 = new Key<>("key2", false); + Category category = new Category("test", key1, key2); + query.from(category).where(key1, Criteria.EQUALS, "fluff"); + + TestClass result = restStorage.findPojo(query, TestClass.class); + + assertEquals("fluff", result.getKey1()); + assertEquals(42, result.getKey2()); + verify(mockStorage).createQuery(); + verify(mockStorage).findPojo(any(Query.class), same(TestClass.class)); + } +}