From 633432049df9e9fb4283755206d6d2b0435e218c Mon Sep 17 00:00:00 2001 From: Greg Wester Date: Wed, 18 Jul 2012 09:19:19 -0700 Subject: [PATCH] added a put test --- pom.xml | 10 +++ .../com/force/taas/qf/model/TestResult.java | 90 +++++++++++++++++++ .../force/taas/qf/model/TestStatusEnum.java | 29 ++++++ .../force/taas/qf/resource/TestResource.java | 27 ++++-- ...MainTest.java => JerseyApiClientTest.java} | 38 ++++---- .../com/force/taas/qf/PersistenceTest.java | 75 ++++++++++++++++ 6 files changed, 245 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/force/taas/qf/model/TestResult.java create mode 100644 src/main/java/com/force/taas/qf/model/TestStatusEnum.java rename src/test/java/com/force/taas/qf/{MainTest.java => JerseyApiClientTest.java} (67%) create mode 100644 src/test/java/com/force/taas/qf/PersistenceTest.java diff --git a/pom.xml b/pom.xml index 68eff41..9f4f9ff 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,16 @@ riak-client 1.0.5 + + org.apache.httpcomponents + httpclient + 4.2.1 + + + org.apache.commons + commons-io + 1.3.2 + diff --git a/src/main/java/com/force/taas/qf/model/TestResult.java b/src/main/java/com/force/taas/qf/model/TestResult.java new file mode 100644 index 0000000..ef5df8b --- /dev/null +++ b/src/main/java/com/force/taas/qf/model/TestResult.java @@ -0,0 +1,90 @@ +// Copyright (c) 2012 Gregory D. Wester +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.force.taas.qf.model; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author gwester + * + */ +@XmlRootElement +public class TestResult { + public TestStatusEnum status; + public Date reportDateTime; + public int runTimeInMillis; + public String fullClassName; + public String testName; + public String result; + + public TestResult() { } + + public TestResult(TestStatusEnum status, Date reportDateTime, + int runTimeInMillis, String fullClassName, String testName, + String result) { + this.status = status; + this.reportDateTime = reportDateTime; + this.runTimeInMillis = runTimeInMillis; + this.fullClassName = fullClassName; + this.testName = testName; + this.result = result; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((fullClassName == null) ? 0 : fullClassName.hashCode()); + result = prime * result + + ((reportDateTime == null) ? 0 : reportDateTime.hashCode()); + result = prime * result + + ((testName == null) ? 0 : testName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TestResult other = (TestResult) obj; + if (fullClassName == null) { + if (other.fullClassName != null) + return false; + } else if (!fullClassName.equals(other.fullClassName)) + return false; + if (reportDateTime == null) { + if (other.reportDateTime != null) + return false; + } else if (!reportDateTime.equals(other.reportDateTime)) + return false; + if (testName == null) { + if (other.testName != null) + return false; + } else if (!testName.equals(other.testName)) + return false; + return true; + } + + public String getKey() { + return String.valueOf(hashCode()); + } +} diff --git a/src/main/java/com/force/taas/qf/model/TestStatusEnum.java b/src/main/java/com/force/taas/qf/model/TestStatusEnum.java new file mode 100644 index 0000000..971cb41 --- /dev/null +++ b/src/main/java/com/force/taas/qf/model/TestStatusEnum.java @@ -0,0 +1,29 @@ +// Copyright (c) 2012 Gregory D. Wester +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.force.taas.qf.model; + +import javax.xml.bind.annotation.XmlEnum; + +/** + * + * @author gwester + * + */ +@XmlEnum(String.class) +public enum TestStatusEnum { + SUCCESS, + FAILURE, + SKIPPED, + ERROR; +} diff --git a/src/main/java/com/force/taas/qf/resource/TestResource.java b/src/main/java/com/force/taas/qf/resource/TestResource.java index 259281c..7c6ed33 100644 --- a/src/main/java/com/force/taas/qf/resource/TestResource.java +++ b/src/main/java/com/force/taas/qf/resource/TestResource.java @@ -13,12 +13,17 @@ // limitations under the License. package com.force.taas.qf.resource; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; -import com.basho.riak.client.RiakFactory; import com.force.taas.qf.PersistenceService; +import com.force.taas.qf.model.TestResult; /** * @@ -27,10 +32,22 @@ */ @Path("/tests") public class TestResource { - + @GET - @Produces("text/plain") - public String getIt() throws Exception { - return PersistenceService.getBucket().getName(); + @Produces({MediaType.APPLICATION_JSON}) + @PathParam("{packageName}/{className}/{testName}") + public TestResult doGetByFullTestName(@PathParam("packageName") String packageName, + @PathParam("className") String className, + @PathParam("testName") String testName) throws Exception { + + return new TestResult(); + } + + @PUT + @Consumes({MediaType.APPLICATION_JSON}) + public Response doPut(TestResult testResult) throws Exception { + PersistenceService.getBucket().store(testResult.getKey(), testResult).execute(); + return Response.status(204).build(); } + } diff --git a/src/test/java/com/force/taas/qf/MainTest.java b/src/test/java/com/force/taas/qf/JerseyApiClientTest.java similarity index 67% rename from src/test/java/com/force/taas/qf/MainTest.java rename to src/test/java/com/force/taas/qf/JerseyApiClientTest.java index c399bda..2ac777a 100644 --- a/src/test/java/com/force/taas/qf/MainTest.java +++ b/src/test/java/com/force/taas/qf/JerseyApiClientTest.java @@ -13,32 +13,31 @@ // limitations under the License. package com.force.taas.qf; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + import org.glassfish.grizzly.http.server.HttpServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; -import com.sun.jersey.core.header.MediaTypes; +import com.force.taas.qf.model.TestResult; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; -import junit.framework.TestCase; /** * * @author gwester * */ -public class MainTest extends TestCase { +public class JerseyApiClientTest { private HttpServer httpServer; - private WebResource r; - public MainTest(String testName) { - super(testName); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - + @Before + public void setUp() throws IOException { //start the Grizzly2 web container httpServer = WebServer.startServer(); @@ -47,19 +46,20 @@ protected void setUp() throws Exception { r = c.resource(WebServer.BASE_URI); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); - + @After + public void tearDown() { httpServer.stop(); + httpServer = null; + r = null; } /** * Test to see that the message "Got it!" is sent in the response. */ - public void testBucketExists() { - String responseMsg = r.path("tests").get(String.class); - assertEquals("prodtest", responseMsg); + @Test + public void testJsonMessage() { + TestResult result = r.path("tests").queryParam("testClass", "common.api").get(TestResult.class); + assertEquals("Check for run time", 0, result.runTimeInMillis); } } diff --git a/src/test/java/com/force/taas/qf/PersistenceTest.java b/src/test/java/com/force/taas/qf/PersistenceTest.java new file mode 100644 index 0000000..6c3e621 --- /dev/null +++ b/src/test/java/com/force/taas/qf/PersistenceTest.java @@ -0,0 +1,75 @@ +package com.force.taas.qf; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URI; +import java.util.Date; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.codehaus.jettison.mapped.Configuration; +import org.codehaus.jettison.mapped.MappedNamespaceConvention; +import org.codehaus.jettison.mapped.MappedXMLStreamWriter; +import org.glassfish.grizzly.http.server.HttpServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.force.taas.qf.model.TestResult; +import com.force.taas.qf.model.TestStatusEnum; + +public class PersistenceTest { + private HttpServer httpServer; + + @Before + public void setUp() throws IOException { + httpServer = WebServer.startServer(); + } + + @After + public void tearDown() { + httpServer.stop(); + httpServer = null; + } + + @Test + public void testPutInDb() throws Exception { + TestResult recentResult = new TestResult(TestStatusEnum.SUCCESS, new Date(System.currentTimeMillis()), + 0, "MyTestClazz", "myTest", + "`T`Loglines"); + + HttpClient client = new DefaultHttpClient(); + URIBuilder builder = new URIBuilder(); + builder.setScheme("http").setHost("localhost").setPort(9998).setPath("/tests"); + URI uri = builder.build(); + HttpPut request = new HttpPut(uri); + request.setHeader("Content-Type", "application/json"); + request.setHeader("Accept", "application/json"); + + JAXBContext jc = JAXBContext.newInstance(TestResult.class); + Marshaller marshaller = jc.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + StringWriter writer = new StringWriter(); + XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(new MappedNamespaceConvention(new Configuration()), writer); + + marshaller.marshal(recentResult, xmlStreamWriter); + request.setEntity(new StringEntity(writer.toString())); + HttpResponse response = client.execute(request); + + assertEquals("Wrong HTTP response code", 204, response.getStatusLine().getStatusCode()); + } +}