sections = challengeDefinitions.getChallengeSections();
+ // add a toc
+ html.append("Sections
");
+ html.append("");
+ for(ChallengeSection section : sections){
+ html.append(String.format("- %s
", section.getTitle().replaceAll(" ", "").toLowerCase(), section.getTitle()));
+ }
+ html.append("
");
+
for(ChallengeSection section : sections){
- html.append("" + section.getTitle() + "
");
+ html.append(String.format("", section.getTitle().replaceAll(" ", "").toLowerCase()) + section.getTitle() + "
");
html.append("" + section.getDescription() + "
");
List sectionData = new ArrayList<>();
@@ -429,6 +437,7 @@ private String renderChallengeData(final ChallengeDefinitions challengeDefinitio
}
html.append(renderChallengeData(sectionData));
+ html.append("Back to Section List
");
}
return html.toString();
diff --git a/challenger/src/test/java/uk/co/compendiumdev/challenger/http/completechallenges/ChallengeCompleteTest.java b/challenger/src/test/java/uk/co/compendiumdev/challenger/http/completechallenges/ChallengeCompleteTest.java
index db2612c4..2ed4575b 100644
--- a/challenger/src/test/java/uk/co/compendiumdev/challenger/http/completechallenges/ChallengeCompleteTest.java
+++ b/challenger/src/test/java/uk/co/compendiumdev/challenger/http/completechallenges/ChallengeCompleteTest.java
@@ -14,6 +14,7 @@
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection;
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstance;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -232,6 +233,105 @@ public void canPutTodos400FailCreatePass() {
Assertions.assertTrue(challenger.statusOfChallenge(CHALLENGE.PUT_TODOS_400));
}
+ @Test
+ public void canPutTodosFull200AmendPass() {
+
+ final EntityInstanceCollection todos = ChallengeMain.getChallenger().getThingifier().getThingInstancesNamed("todo", challenger.getXChallenger());
+
+ Map x_challenger_header = getXChallengerHeader(challenger.getXChallenger());
+
+ Map headers = new HashMap<>();
+ headers.putAll(x_challenger_header);
+ headers.put("Content-Type", "application/json");
+
+ EntityInstance aTodo = new ArrayList<>(todos.getInstances()).get(0);
+
+ // amend a todo successfully
+ final HttpResponseDetails response =
+ http.send("/todos/" + aTodo.getPrimaryKeyValue(),
+ "PUT", headers,
+ "{\"title\":\"my put todo\",\"description\":\"a put description\",\"doneStatus\":true}");
+ // complete payload to avoid defaults
+
+ Assertions.assertEquals(200, response.statusCode);
+ Assertions.assertTrue(challenger.statusOfChallenge(CHALLENGE.PUT_TODOS_FULL_200));
+
+
+ }
+
+ @Test
+ public void canPutTodosPartial200AmendPass() {
+
+ final EntityInstanceCollection todos = ChallengeMain.getChallenger().getThingifier().getThingInstancesNamed("todo", challenger.getXChallenger());
+
+ Map x_challenger_header = getXChallengerHeader(challenger.getXChallenger());
+
+ Map headers = new HashMap<>();
+ headers.putAll(x_challenger_header);
+ headers.put("Content-Type", "application/json");
+
+ EntityInstance aTodo = new ArrayList<>(todos.getInstances()).get(0);
+
+ // amend a todo successfully
+ final HttpResponseDetails response =
+ http.send("/todos/" + aTodo.getPrimaryKeyValue(),
+ "PUT", headers,
+ "{\"title\":\"my put todo\"}");
+ // only title is mandatory the rest would be set to defaults
+
+ Assertions.assertEquals(200, response.statusCode);
+ Assertions.assertTrue(challenger.statusOfChallenge(CHALLENGE.PUT_TODOS_PARTIAL_200));
+ }
+
+ @Test
+ public void canPutTodos200MissingTitleAmendPass() {
+
+ final EntityInstanceCollection todos = ChallengeMain.getChallenger().getThingifier().getThingInstancesNamed("todo", challenger.getXChallenger());
+
+ Map x_challenger_header = getXChallengerHeader(challenger.getXChallenger());
+
+ Map headers = new HashMap<>();
+ headers.putAll(x_challenger_header);
+ headers.put("Content-Type", "application/json");
+
+ EntityInstance aTodo = new ArrayList<>(todos.getInstances()).get(0);
+
+ // amend a todo unsuccessfully
+ final HttpResponseDetails response =
+ http.send("/todos/" + aTodo.getPrimaryKeyValue(),
+ "PUT", headers,
+ "{\"description\":\"my description\"}");
+ // title is mandatory so this will fail
+
+ Assertions.assertEquals(400, response.statusCode);
+ Assertions.assertTrue(challenger.statusOfChallenge(CHALLENGE.PUT_TODOS_MISSING_TITLE_400));
+ }
+
+ @Test
+ public void canNotPutTodos400ChangeIdAmendPass() {
+
+ final EntityInstanceCollection todos = ChallengeMain.getChallenger().getThingifier().getThingInstancesNamed("todo", challenger.getXChallenger());
+
+ Map x_challenger_header = getXChallengerHeader(challenger.getXChallenger());
+
+ Map headers = new HashMap<>();
+ headers.putAll(x_challenger_header);
+ headers.put("Content-Type", "application/json");
+
+ EntityInstance aTodo = new ArrayList<>(todos.getInstances()).get(0);
+
+ // amend a todo unsuccessfully
+ final HttpResponseDetails response =
+ http.send("/todos/" + aTodo.getPrimaryKeyValue(),
+ "PUT", headers,
+ String.format("{\"id\":%d, \"description\":\"my description\"}", Integer.parseInt(aTodo.getPrimaryKeyValue()+1))
+ );
+ // title is mandatory so this will fail
+
+ Assertions.assertEquals(400, response.statusCode);
+ Assertions.assertTrue(challenger.statusOfChallenge(CHALLENGE.PUT_TODOS_400_NO_AMEND_ID));
+ }
+
@Test
public void canPostTodos404Pass() {
diff --git a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanCreateTodosWithPOSTTest.java b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostCreateTodosTest.java
similarity index 98%
rename from challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanCreateTodosWithPOSTTest.java
rename to challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostCreateTodosTest.java
index 51e4babf..e15165cf 100644
--- a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanCreateTodosWithPOSTTest.java
+++ b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostCreateTodosTest.java
@@ -7,18 +7,15 @@
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import uk.co.compendiumdev.challenger.payloads.ErrorMessages;
import uk.co.compendiumdev.challenger.payloads.Todo;
import uk.co.compendiumdev.challenger.restassured.api.ChallengesStatus;
import uk.co.compendiumdev.challenger.restassured.api.RestAssuredBaseTest;
import uk.co.compendiumdev.challenger.restassured.api.TodosApi;
-import java.util.ArrayList;
-import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class CanCreateTodosWithPOSTTest extends RestAssuredBaseTest {
+public class CanPostCreateTodosTest extends RestAssuredBaseTest {
@Test
void canCreateATodoWithPost(){
diff --git a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanUpdateTodosWithPOSTTest.java b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostUpdateTodosTest.java
similarity index 96%
rename from challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanUpdateTodosWithPOSTTest.java
rename to challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostUpdateTodosTest.java
index cf89062c..3670f98d 100644
--- a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanUpdateTodosWithPOSTTest.java
+++ b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPostUpdateTodosTest.java
@@ -1,7 +1,5 @@
package uk.co.compendiumdev.challenger.restassured;
-import com.google.gson.Gson;
-import com.google.gson.JsonElement;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
@@ -15,7 +13,7 @@
import java.util.List;
-public class CanUpdateTodosWithPOSTTest extends RestAssuredBaseTest {
+public class CanPostUpdateTodosTest extends RestAssuredBaseTest {
@Test
void canUpdateATodoWithPost(){
diff --git a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPutTodosTest.java b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPutTodosTest.java
index 08878e86..ec157d31 100644
--- a/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPutTodosTest.java
+++ b/challengerAuto/src/test/java/uk/co/compendiumdev/challenger/restassured/CanPutTodosTest.java
@@ -1,5 +1,7 @@
package uk.co.compendiumdev.challenger.restassured;
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
@@ -11,37 +13,191 @@
import uk.co.compendiumdev.challenger.restassured.api.RestAssuredBaseTest;
import uk.co.compendiumdev.challenger.restassured.api.TodosApi;
+import java.util.List;
+
public class CanPutTodosTest extends RestAssuredBaseTest {
@Test
void canFailToCreateTodoWithPut(){
- Todo createMe = new Todo();
- createMe.id = 200;
- createMe.title = "my name " + System.currentTimeMillis();
- createMe.description = "my description " + System.currentTimeMillis();
- createMe.doneStatus = true;
-
- final Response response = RestAssured.
- given().
- header("X-CHALLENGER", xChallenger).
- accept("application/json").
- contentType("application/json").
- body(createMe).
- put(apiPath("/todos/" + createMe.id)).
- then().
- statusCode(400).
- contentType(ContentType.JSON).
- extract().response();
-
- ErrorMessages errors = response.body().as(ErrorMessages.class);
-
- Assertions.assertEquals(1, errors.errorMessages.size());
- Assertions.assertEquals("Cannot create todo with PUT due to Auto fields id", errors.errorMessages.get(0));
-
- ChallengesStatus statuses = new ChallengesStatus();
- statuses.get();
- Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} (400)").status);
- }
+ Todo createMe = new Todo();
+ createMe.id = 200;
+ createMe.title = "my name " + System.currentTimeMillis();
+ createMe.description = "my description " + System.currentTimeMillis();
+ createMe.doneStatus = true;
+
+ final Response response = RestAssured.
+ given().
+ header("X-CHALLENGER", xChallenger).
+ accept("application/json").
+ contentType("application/json").
+ body(createMe).
+ put(apiPath("/todos/" + createMe.id)).
+ then().
+ statusCode(400).
+ contentType(ContentType.JSON).
+ extract().response();
+
+ ErrorMessages errors = response.body().as(ErrorMessages.class);
+
+ Assertions.assertEquals(1, errors.errorMessages.size());
+ Assertions.assertEquals("Cannot create todo with PUT due to Auto fields id", errors.errorMessages.get(0));
+
+ ChallengesStatus statuses = new ChallengesStatus();
+ statuses.get();
+ Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} (400)").status);
+ }
+
+
+ @Test
+ void canAmendATodoWithPut(){
+
+ TodosApi api = new TodosApi();
+ List todos = api.getTodos();
+
+ Todo amendMe = todos.get(0);
+ // amendMe.id = - cannot amend the id as it is auto assigned
+ amendMe.title = "my name " + System.currentTimeMillis(); // title is mandatory and must be in the message
+ amendMe.description = "my description " + System.currentTimeMillis(); // if not present default "" will be set
+ amendMe.doneStatus = true; // if not present then default false will be set
+
+ final Response response = RestAssured.
+ given().
+ header("X-CHALLENGER", xChallenger).
+ accept("application/json").
+ contentType("application/json").
+ body(amendMe).
+ put(apiPath("/todos/" + amendMe.id)).
+ then().
+ statusCode(200).
+ contentType(ContentType.JSON).
+ extract().response();
+
+ Todo amendedTodo = response.body().as(Todo.class);
+
+ Assertions.assertEquals(amendMe.id, amendedTodo.id);
+ Assertions.assertEquals(amendMe.title, amendedTodo.title);
+ Assertions.assertEquals(amendMe.description, amendedTodo.description);
+ Assertions.assertEquals(amendMe.doneStatus, amendedTodo.doneStatus);
+
+ ChallengesStatus statuses = new ChallengesStatus();
+ statuses.get();
+ Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} full (200)").status);
+ }
+
+ @Test
+ void canAmendATodoWithPutUsingDefaults(){
+
+ TodosApi api = new TodosApi();
+ List todos = api.getTodos();
+
+ Todo amendMe = todos.get(0);
+
+ // amendMe.id = - cannot amend the id as it is auto assigned
+ amendMe.title = "my name " + System.currentTimeMillis(); // title is mandatory and must be in the message
+
+ // if description not present default "" will be set
+ // if doneStatus not present then default false will be set
+
+ final JsonElement amendTodoJson = new Gson().toJsonTree(amendMe);
+ amendTodoJson.getAsJsonObject().
+ remove("doneStatus");
+ amendTodoJson.getAsJsonObject().
+ remove("description");
+
+
+
+ final Response response = RestAssured.
+ given().
+ header("X-CHALLENGER", xChallenger).
+ accept("application/json").
+ contentType("application/json").
+ body(amendTodoJson.toString()).
+ put(apiPath("/todos/" + amendMe.id)).
+ then().
+ statusCode(200).
+ contentType(ContentType.JSON).
+ extract().response();
+
+ Todo amendedTodo = response.body().as(Todo.class);
+
+ Assertions.assertEquals(amendMe.id, amendedTodo.id);
+ Assertions.assertEquals(amendMe.title, amendedTodo.title);
+ Assertions.assertEquals("", amendedTodo.description);
+ Assertions.assertEquals(false, amendedTodo.doneStatus);
+
+ ChallengesStatus statuses = new ChallengesStatus();
+ statuses.get();
+ Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} partial (200)").status);
+ }
+
+ @Test
+ void canFailToAmendATodoDueToMissingTitle(){
+
+ TodosApi api = new TodosApi();
+ List todos = api.getTodos();
+
+ Todo amendMe = todos.get(0);
+
+ // amendMe.id = - cannot amend the id as it is auto assigned
+ // title is mandatory and must be in the message
+
+ final JsonElement amendTodoJson = new Gson().toJsonTree(amendMe);
+ amendTodoJson.getAsJsonObject().
+ remove("title");
+
+ final Response response = RestAssured.
+ given().
+ header("X-CHALLENGER", xChallenger).
+ accept("application/json").
+ contentType("application/json").
+ body(amendTodoJson.toString()).
+ put(apiPath("/todos/" + amendMe.id)).
+ then().
+ statusCode(400).
+ contentType(ContentType.JSON).
+ extract().response();
+
+ ErrorMessages error = response.body().as(ErrorMessages.class);
+ Assertions.assertTrue(error.errorMessages.get(0).contains("title : field is mandatory"));
+
+ ChallengesStatus statuses = new ChallengesStatus();
+ statuses.get();
+ Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} no title (400)").status);
+ }
+
+ @Test
+ void canFailToAmendATodoDueToAttemptToChangeId(){
+
+ TodosApi api = new TodosApi();
+ List todos = api.getTodos();
+
+ Todo amendMe = todos.get(0);
+
+ int oldId = amendMe.id;
+ int newId = oldId +1;
+ amendMe.id= newId;
+ // amendMe.id = - cannot amend the id as it is auto assigned
+ // title is mandatory and must be in the message
+
+ final Response response = RestAssured.
+ given().
+ header("X-CHALLENGER", xChallenger).
+ accept("application/json").
+ contentType("application/json").
+ body(amendMe).
+ put(apiPath("/todos/" + oldId)).
+ then().
+ statusCode(400).
+ contentType(ContentType.JSON).
+ extract().response();
+
+ ErrorMessages error = response.body().as(ErrorMessages.class);
+ Assertions.assertTrue(error.errorMessages.get(0).contains(String.format("Can not amend id from %d to %d", oldId, newId)));
+
+ ChallengesStatus statuses = new ChallengesStatus();
+ statuses.get();
+ Assertions.assertTrue(statuses.getChallengeNamed("PUT /todos/{id} no amend id (400)").status);
+ }
}