Skip to content

Commit

Permalink
added a put test
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Wester committed Jul 18, 2012
1 parent e867824 commit 6334320
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 24 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
<artifactId>riak-client</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/force/taas/qf/model/TestResult.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/force/taas/qf/model/TestStatusEnum.java
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 22 additions & 5 deletions src/main/java/com/force/taas/qf/resource/TestResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

}
75 changes: 75 additions & 0 deletions src/test/java/com/force/taas/qf/PersistenceTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 6334320

Please sign in to comment.