Skip to content

Commit

Permalink
java client starter
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Feb 25, 2014
1 parent 8da29ca commit 4bf8619
Show file tree
Hide file tree
Showing 18 changed files with 758 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.classpath
.project
.settings/
target/

*.class

# Package Files #
Expand Down
70 changes: 70 additions & 0 deletions client-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.claysnow</groupId>
<artifactId>checkout-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Checkout client</name>
<description>Client app for mutation testing workshop</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>0.33</version>
<executions>
<!-- bound here to test, most commonly bound to verify -->
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
<configuration>
<timestampedReports>false</timestampedReports>
<threads>2</threads>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
70 changes: 70 additions & 0 deletions client-java/src/main/java/uk/co/claysnow/checkout/app/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package uk.co.claysnow.checkout.app;

import java.io.PrintStream;

import uk.co.claysnow.checkout.domain.Team;
import uk.co.claysnow.checkout.service.CheckoutService;
import uk.co.claysnow.checkout.service.JerseyRestCall;
import uk.co.claysnow.checkout.service.JsonHandler;

import com.sun.jersey.api.client.Client;

public class App {

private final CheckoutClient client;
private final PrintStream out;

App(CheckoutClient client, PrintStream out) {
this.client = client;
this.out = out;
}

public static void main(String[] args) {
if (checkArgs(args)) {
Client webClient = Client.create();
JerseyRestCall rest = new JerseyRestCall(webClient, args[1],
Integer.valueOf(args[2]));
CheckoutService service = new CheckoutService(rest);

CheckoutClient client = new CheckoutClient(service, new JsonHandler());
App app = new App(client, System.out);
app.runCommand(args[0], args[3]);
} else {
showUsage();
}
}

void runCommand(String command, String team) {
if ("register".equalsIgnoreCase(command)) {
Team registeredTeam = client.registerTeam(team);
out.println("Team registered as " + registeredTeam.getName());
} else if ("requirements".equalsIgnoreCase(command)) {
out.println("Server says :- "
+ client.requestRequirements(new Team(team)));
}

}

private static void showUsage() {
System.out.println("Usage: CheckoutConsole <op> <url> <port> <team>\n"
+ " <op> : 'register', 'requirements' or 'batch'\n"
+ " <url> : The URL of the server e.g. '172.16.66.1'\n"
+ " <port> : Port of server e.g 9988\n"
+ " <team>: The team's name\n");

}

private static boolean checkArgs(String[] args) {
return (args.length == 4 && isNumeric(args[2]));
}

private static boolean isNumeric(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException ex) {
return false;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package uk.co.claysnow.checkout.app;

import uk.co.claysnow.checkout.domain.Batch;
import uk.co.claysnow.checkout.domain.Team;
import uk.co.claysnow.checkout.service.BadResponseException;
import uk.co.claysnow.checkout.service.CheckoutService;
import uk.co.claysnow.checkout.service.JsonHandler;
import uk.co.claysnow.checkout.service.Response;

public class CheckoutClient {

private final CheckoutService service;
private final JsonHandler jsonHandler;

public CheckoutClient(CheckoutService service, JsonHandler jsonHandler) {
this.service = service;
this.jsonHandler = jsonHandler;
}

public Team registerTeam(String name) {
Response response = service.registerTeam(jsonHandler.toJson(new Team(name), Team.class));
errorIfCodeNot(201, response);

// use name from remote service in case requested name was truncated
return jsonHandler.fromJson(response.jsonBody, Team.class);

}


public String requestRequirements(Team team) {
Response response = service.requestRequirements(team.getName());
errorIfCodeNot(200, response);

// use name from remote service in case requested name was truncated
return response.jsonBody;
}

public Batch requestBatch(Team t) {
Response response = service.requestBatch(t.getName());
errorIfCodeNot(200, response);

return jsonHandler.fromJson(response.jsonBody, Batch.class);
}


private void errorIfCodeNot(int expectedCode, Response response) {
if ( response.httpResponseCode != expectedCode ) {
throw new BadResponseException(response.jsonBody);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.co.claysnow.checkout.domain;

import java.util.Collections;
import java.util.List;

public final class Basket {

private final Integer basketId;
private final List<Item> items;

public Basket(Integer basketId, List<Item> items){
this.basketId = basketId;
this.items = items;
}

public Integer getBasketId() {
return basketId;
}

public List<Item> getItems() {
return Collections.unmodifiableList(items);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.co.claysnow.checkout.domain;

import java.util.Collections;
import java.util.List;


public final class Batch {

private final List<Basket> baskets;

public Batch(List<Basket> baskets) {
this.baskets = baskets;
}

public List<Basket> getBaskets() {
return Collections.unmodifiableList(baskets);
}

}
21 changes: 21 additions & 0 deletions client-java/src/main/java/uk/co/claysnow/checkout/domain/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.co.claysnow.checkout.domain;

public final class Item {

private final String itemCode;
private final Integer quantity;

public Item(String itemCode, int quantity) {
this.itemCode = itemCode;
this.quantity = quantity;
}

public String getItemCode() {
return itemCode;
}

public Integer getQuantity() {
return quantity;
}

}
15 changes: 15 additions & 0 deletions client-java/src/main/java/uk/co/claysnow/checkout/domain/Team.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.co.claysnow.checkout.domain;

public final class Team {

private final String name;

public Team(String name) {
this.name = name;
}

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.co.claysnow.checkout.service;

public final class BadResponseException extends RuntimeException {

private static final long serialVersionUID = 1L;

public BadResponseException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.co.claysnow.checkout.service;


public class CheckoutService {

private final RestCall rest;

public CheckoutService(RestCall rest) {
this.rest = rest;
}

public Response registerTeam(String json) {
return rest.put("/Checkout/Team", json);
}

public Response requestRequirements(String team) {
return rest.get("/Checkout/Requirements/" + team);
}

public Response requestBatch(String team) {
return rest.get("/Checkout/Batch/" + team);
}

public Response requestPriceList(String team) {
return rest.get("/Checkout/PriceList/" + team);
}

public Response submitTotals(String team, String json) {
return rest.put("/Checkout/Batch/" + team, json);
}

public Response requestScore(String team) {
return rest.get("/Checkout/Score/" + team);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uk.co.claysnow.checkout.service;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
* Makes rest calls using Jersey
*
*/
public class JerseyRestCall implements RestCall {

private final Client client;
private final String server;

public JerseyRestCall(Client client, String server, int port) {
this.client = client;
this.server = "http://" + server + ":" + port;
}

@Override
public Response get(String url) {
WebResource webResource = client.resource(server + url);
ClientResponse response = webResource.type("application/json").get(
ClientResponse.class);
return response(response);
}

@Override
public Response put(String url, String json) {
WebResource webResource = client.resource(server + url);

ClientResponse clientResponse = webResource.type("application/json").put(
ClientResponse.class, json);

return response(clientResponse);
}

private Response response(ClientResponse response) {
return new Response(response.getStatus(), response.getEntity(String.class));
}

}
Loading

0 comments on commit 4bf8619

Please sign in to comment.