Permalink
Browse files

Correcting Boundary conditions

  • Loading branch information...
1 parent 079c069 commit 69c5b9c801014594b0ac48488f7e838063c708df @css181 css181 committed Apr 17, 2012
@@ -281,27 +281,38 @@ public String playerRemovalRequest(@PathParam("facebookID") long facebookID) {
// Performs validation on answers submitted, and adjust points.
@POST
- @Path("/GameAnswers/{playerID}/{id1}/{id2}/{id3}/{name1}/{name2}/{name3}")
+ @Path("/GameAnswers/{playerID}/{id1}/{id2}/{id3}")
@Consumes("application/json")
public String postForAnswers(@PathParam("playerID") long playerID,
@PathParam("id1") long id1, @PathParam("id2") long id2,
- @PathParam("id3") long id3, @PathParam("name1") String name1,
- @PathParam("name2") String name2, @PathParam("name3") String name3) {
-
+ @PathParam("id3") long id3, ArrayList<String> JsonInput) {
+
Player player = getPlayerByFacebookID(em, playerID);
User user1 = getUserByFacebookID(em, id1);
User user2 = getUserByFacebookID(em, id2);
User user3 = getUserByFacebookID(em, id3);
+ String name1 = "";
+ String name2 = "";
+ String name3 = "";
if (null == player || null == user1 || null == user2 || null == user3) {
// This should never happen, but lets exit gracefully if it somehow
// does.
return "Sorry, there was an error trying to validate your answers. Please try again.";
}
-
+ if(JsonInput.size()!=3){
+ //This needs to be the 3 names the Player is guessing
+ return "Sorry, we couldn't understand your 3 guesses. Please try again";
+ }
+ else {
+ name1 = JsonInput.get(0);
+ name2 = JsonInput.get(1);
+ name3 = JsonInput.get(2);
+ }
+
boolean correctName1 = user1.getName().equals(name1);
boolean correctName2 = user2.getName().equals(name2);
boolean correctName3 = user3.getName().equals(name3);
-
+
String returnString = "";
long pointChange = 0;
if (correctName1) {
@@ -325,18 +336,18 @@ public String postForAnswers(@PathParam("playerID") long playerID,
returnString += "Third entry was INCORRECT \n";
pointChange -= 10;
}
-
+
returnString += "You will have a total of [" + Math.abs(pointChange)
+ "] points ";
if (pointChange > 0) {
returnString += "added!";
} else {
returnString += "deducted.";
}
-
+
player.setPoints(player.getPoints() + pointChange);
em.persist(player);
-
+
return returnString;
}// postForAnswers
@@ -52,34 +52,36 @@
var isDEBUG = false; //Print out debug statements to concole
- function doPostAnswers(playerID, ID1, ID2, ID3, name1, name2, name3){
- var POSTAnswersURL = "rest/webService/GameAnswers/" + playerID + "/" +
- ID1 + "/" + ID2 + "/" + ID3 + "/" + name1 + "/" + name2 + "/" + name3;
+ function doPostAnswers(playerID, ID1, ID2, ID3, JSONInput){
+ var POSTAnswersURL = "rest/webService/GameAnswers/" + playerID + "/" +
+ ID1 + "/" + ID2 + "/" + ID3;
var myObj = null;
var xmlhttp = null;
if (window.XMLHttpRequest) {
- xmlhttp = new XMLHttpRequest();
- if ( typeof xmlhttp.overrideMimeType != 'undefined') {
- xmlhttp.overrideMimeType('application/json');
- }
+ xmlhttp = new XMLHttpRequest();
+ if ( typeof xmlhttp.overrideMimeType != 'undefined') {
+ xmlhttp.overrideMimeType('application/json');
+ }
} else if (window.ActiveXObject) {
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
+ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
- alert('Your browser does not support xmlhttprequests. Sorry.');
+ alert('Your browser does not support xmlhttprequests. Sorry.');
}
xmlhttp.open('POST', POSTAnswersURL, false);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
- xmlhttp.send();
+ xmlhttp.send(JSONInput);
alert(xmlhttp.responseText);
window.location='index.html';
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
+ //myObj = eval ( '(' + xmlhttp.responseText + ')' );
if(isDEBUG){
alert("POST Success");
- }
+ }
+ //alert(xmlhttp.responseText);
document.getElementById('submitMessage').innerHTML = xmlhttp.responseText;
}
else {
@@ -88,9 +90,9 @@
} else {
// wait for the call to complete
}
- };
+ };
return null;
- }
+ }
function getURLParam( varname ) {
@@ -127,7 +129,8 @@
var name1 = document.getElementById('inputName1').value;
var name2 = document.getElementById('inputName2').value;
var name3 = document.getElementById('inputName3').value;
- doPostAnswers(playerID, ID1, ID2, ID3, name1, name2, name3);
+ var JSONInput = "[\"" + name1 + "\",\"" + name2 + "\",\"" + name3 + "\"]";
+ doPostAnswers(playerID, ID1, ID2, ID3, JSONInput);
}
//Execute this code to display Info on the Player
@@ -264,8 +264,8 @@ public void testSubmitAllWrongAnswers(){
//Submit 3 incorrect answers to our WebService as a POST request
String targetURL = "http://localhost:8080/"+ appName +"/rest/webService/GameAnswers/" +
- "1005/67890/76543/89012/Four%20Friend/Five%20Friend/One%20Friend";
- String JSONInput = "";
+ "1005/67890/76543/89012";
+ String JSONInput = "[\"" + "Four Friend" + "\",\"" + "Five Friend" + "\",\"" + "One Friend" + "\"]";
String response = TestUtils.doPOST(targetURL, JSONInput);
//Test that we get the correct String back from the incorrect answers and our points were deducted
@@ -292,8 +292,8 @@ public void testSubmitAllCorrectAnswers(){
//Submit 3 correct answers to our WebService as a POST request
String targetURL = "http://localhost:8080/"+ appName +"/rest/webService/GameAnswers/" +
- "1005/67890/76543/89012/One%20Friend/Two%20Friend/Three%20Friend";
- String JSONInput = "";
+ "1005/67890/76543/89012";
+ String JSONInput = "[\"" + "One Friend" + "\",\"" + "Two Friend" + "\",\"" + "Three Friend" + "\"]";
String response = TestUtils.doPOST(targetURL, JSONInput);
//Test that we get the correct String back from the incorrect answers and our points were deducted
@@ -308,4 +308,34 @@ public void testSubmitAllCorrectAnswers(){
Assert.assertTrue(response.equals(expectedResponse));
Assert.assertTrue(playerWith5Friends.getPoints()==(playerPointsOriginal + 30));
}
+
+ @Test
+ public void testSubmitAllBlankAnswers(){
+ String appName = "TutorialDemo";
+ Player playerWith5Friends = TestUtils.getPlayer(1005);
+
+ //Take note of the player's points before they submit the blank answers
+ long playerPointsOriginal = playerWith5Friends.getPoints();
+
+ //Submit 3 blank answers to our WebService as a POST request
+ String targetURL = "http://localhost:8080/"+ appName +"/rest/webService/GameAnswers/" +
+ "1005/67890/76543/89012";
+ String JSONInput = "[\"" + "\",\"" + "\",\"" + "\"]";
+ String response = TestUtils.doPOST(targetURL, JSONInput);
+
+ //Test that we get the correct String back from the blank answers and our points were deducted
+ String expectedResponse = "First entry was INCORRECT "
+ + "Second entry was INCORRECT "
+ + "Third entry was INCORRECT "
+ + "You will have a total of [" + 30
+ + "] points deducted.";
+
+ System.out.println("\n\n\n\n\n\n response: " + response);
+ System.out.println("\n\n\n\n\n\n");
+
+ //Re-GET the player now that the score should be updated
+ playerWith5Friends = TestUtils.getPlayer(1005);
+ Assert.assertTrue(response.equals(expectedResponse));
+ Assert.assertTrue(playerWith5Friends.getPoints()==(playerPointsOriginal - 30));
+ }
}
Binary file not shown.
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
-#Mon Apr 16 19:31:12 EDT 2012
+#Mon Apr 16 20:02:44 EDT 2012
version=1.0.0-SNAPSHOT
groupId=come.example
m2e.projectName=TutorialDemo

0 comments on commit 69c5b9c

Please sign in to comment.