π State Mutation, Wire Protocols & End-to-End CRUD Chaining
While read-only GET operations confirm retrieval stability, enterprise APIs rely heavily on state-mutating HTTP methods to manage critical business entities. POST handles initial resource creation, returning 201 Created alongside canonical resource identifiers and Location headers. PUT enforces full entity replacement, mandating complete payloads to preserve idempotency. PATCH applies surgical, partial delta mutations under RFC 7396 standards, updating only designated attributes without overwriting unmodified fields. Finally, DELETE removes resources, returning 204 No Content and guaranteeing that subsequent retrieval attempts yield 404 Not Found tombstones. Mastering these four verbs in REST Assuredβwhile orchestrating dynamic ID extraction across sequential TestNG test methodsβensures that state management, schema validation, and database transitions execute reliably across continuous delivery pipelines.
END-TO-END CRUD STATE MUTATION TOPOGRAPHY:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 1: POST /v1/students (CREATE ENTITY) β
β Payload: {"fullName": "Nikita Sharma", "email": "nikita@domain.com", "credits": 4} β
β Status: 201 Created βββΊ Extract generated resource ID: STU-88219 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Pass STU-88219 into state context)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 2: GET /v1/students/STU-88219 (VERIFY INITIAL CREATION) β
β Status: 200 OK βββΊ Assert fullName == "Nikita Sharma", credits == 4 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Full idempotent entity replacement)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 3: PUT /v1/students/STU-88219 (COMPLETE RESOURCE REPLACEMENT) β
β Payload: Complete Student entity with mutated credits: {"credits": 16, ...all other fields} β
β Status: 200 OK (or 204 No Content) βββΊ Assert entity fully overwritten β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Partial delta attribute mutation)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 4: PATCH /v1/students/STU-88219 (SURGICAL DELTA UPDATE - RFC 7396) β
β Payload: {"academicStatus": "HONORS_DEAN_LIST"} (Only modified attributes sent!) β
β Status: 200 OK βββΊ Assert academicStatus updated while credits remain 16 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Resource deletion and cleanup)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 5: DELETE /v1/students/STU-88219 (TOMBSTONE REMOVAL) β
β Status: 204 No Content (or 200 OK) βββΊ Record purged from primary database β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ (Verification of tombstone)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 6: GET /v1/students/STU-88219 (ASSERT TOMBSTONE NON-EXISTENCE) β
β Status: 404 Not Found βββΊ Confirms entity is no longer retrievable β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
POST request with a JSON payload. Asserts 201 Created, checks the RFC-compliant Location header, and extracts the generated resource identifier for downstream test methods.
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
public class PostCreationTest {
// Shared execution context variable for sequential suite execution
public static String createdStudentId;
@Test(description = "Execute POST request to provision a new Student entity")
public void testCreateStudentEntity() {
// Construct dynamic JSON payload using a Map
Map<String, Object> payload = new HashMap<>();
payload.put("fullName", "Nikita Sharma");
payload.put("email", "nikita.sharma@nikita-infotech.com");
payload.put("academicTrack", "ENTERPRISE_AUTOMATION");
payload.put("enrolledCredits", 4);
createdStudentId = given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.header("X-Tenant-Id", "MH-PUNE-01")
.body(payload)
.log().ifValidationFails()
.when()
.post("/students")
.then()
.log().ifValidationFails()
// 1. Assert standard 201 Created status
.statusCode(201)
.statusLine(containsString("Created"))
// 2. Validate Location header pointing to canonical resource URI
.header("Location", containsString("/v1/students/STU-"))
// 3. Validate response body attributes
.body("status", equalTo("SUCCESS"))
.body("data.id", startsWith("STU-"))
.body("data.fullName", equalTo("Nikita Sharma"))
.body("data.createdAt", notNullValue())
// 4. Extract generated student ID for subsequent tests
.extract()
.path("data.id");
System.out.println("[+] Student successfully provisioned. Generated ID: " + createdStudentId);
}
}
PUT request for complete entity replacement. Demonstrates sending the complete updated object state, asserting 200 OK, and proving idempotency by issuing consecutive identical calls.
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class PutReplacementTest {
@Test(description = "Execute PUT request to replace the complete Student entity")
public void testFullEntityReplacement() {
String studentId = PostCreationTest.createdStudentId != null
? PostCreationTest.createdStudentId : "STU-88219";
// Under REST standards, PUT represents a COMPLETE REPLACEMENT.
// All entity fields (both modified and unmodified) must be submitted:
Map<String, Object> replacementPayload = new HashMap<>();
replacementPayload.put("id", studentId);
replacementPayload.put("fullName", "Nikita Sharma"); // Unmodified
replacementPayload.put("email", "nikita.sharma@nikita-infotech.com"); // Unmodified
replacementPayload.put("academicTrack", "CLOUD_DEVOPS_QA"); // MUTATED
replacementPayload.put("enrolledCredits", 16); // MUTATED
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", studentId)
.contentType(ContentType.JSON)
.body(replacementPayload)
.when()
.put("/students/{studentId}")
.then()
.statusCode(200)
.body("status", equalTo("SUCCESS"))
.body("data.academicTrack", equalTo("CLOUD_DEVOPS_QA"))
.body("data.enrolledCredits", equalTo(16));
System.out.println("[β] PUT replacement executed cleanly for student: " + studentId);
}
@Test(description = "Verify PUT idempotency: repeating the call yields identical state")
public void testPutIdempotency() {
String studentId = PostCreationTest.createdStudentId != null
? PostCreationTest.createdStudentId : "STU-88219";
Map<String, Object> payload = new HashMap<>();
payload.put("id", studentId);
payload.put("fullName", "Nikita Sharma");
payload.put("email", "nikita.sharma@nikita-infotech.com");
payload.put("academicTrack", "CLOUD_DEVOPS_QA");
payload.put("enrolledCredits", 16);
// Second identical PUT execution: must yield exact same 200 state without side effects
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", studentId)
.contentType(ContentType.JSON)
.body(payload)
.when()
.put("/students/{studentId}")
.then()
.statusCode(200)
.body("data.enrolledCredits", equalTo(16));
System.out.println("[β] PUT idempotency confirmed: repeat invocation produced identical state.");
}
}
PATCH. Sends a minimal delta payload modifying a single attribute (academicStatus), and confirms that unmodified properties (credits, email) remain unchanged.
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class PatchDeltaTest {
@Test(description = "Execute PATCH request to update only a single field without touching others")
public void testPartialDeltaPatch() {
String studentId = PostCreationTest.createdStudentId != null
? PostCreationTest.createdStudentId : "STU-88219";
// Under RFC 7396 (JSON Merge Patch), send ONLY the attributes being modified:
Map<String, Object> deltaPayload = new HashMap<>();
deltaPayload.put("academicStatus", "HONORS_DEAN_LIST");
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", studentId)
// Some APIs require Content-Type: application/merge-patch+json
.contentType("application/json")
.body(deltaPayload)
.when()
.patch("/students/{studentId}")
.then()
.statusCode(200)
// Assert patched field successfully changed:
.body("data.academicStatus", equalTo("HONORS_DEAN_LIST"))
// CRITICAL: Assert unmodified fields were NOT reset to null or defaults!
.body("data.fullName", equalTo("Nikita Sharma"))
.body("data.enrolledCredits", equalTo(16))
.body("data.academicTrack", equalTo("CLOUD_DEVOPS_QA"));
System.out.println("[β] PATCH delta applied successfully. Unmodified fields remained intact.");
}
}
DELETE. Asserts 204 No Content with an empty body, and verifies that subsequent retrieval calls return 404 Not Found.
package com.nikita.restassured.crud;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyString;
public class DeleteTeardownTest {
@Test(description = "Execute DELETE to purge student entity and verify 204 No Content")
public void testDeleteStudentEntity() {
String studentId = PostCreationTest.createdStudentId != null
? PostCreationTest.createdStudentId : "STU-88219";
// Step 1: Issue DELETE command
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", studentId)
.when()
.delete("/students/{studentId}")
.then()
// 204 No Content represents standard successful deletion with no body
.statusCode(204)
.body(emptyString());
System.out.println("[β] DELETE issued for student " + studentId + ". Server responded 204 No Content.");
// Step 2: Issue immediate GET to prove entity is gone (Tombstone Audit)
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", studentId)
.when()
.get("/students/{studentId}")
.then()
.statusCode(404)
.statusLine(containsString("Not Found"));
System.out.println("[β] Tombstone confirmed: GET request returned 404 Not Found.");
}
@Test(description = "Verify DELETE idempotency: deleting already-purged resource yields 404")
public void testDeleteAlreadyPurgedResource() {
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.pathParam("studentId", "STU-NON-EXISTENT-999")
.when()
.delete("/students/{studentId}")
.then()
.statusCode(404);
System.out.println("[β] Repeating DELETE on non-existent resource returned 404 Not Found.");
}
}
dependsOnMethods. Ensures tests run in strict sequential order (Create β Read β Update β Patch β Delete β Verify Tombstone), halting if an earlier step fails.
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
public class FullCrudLifecycleSuite {
private String studentId;
private static final String BASE_URI = "https://staging-api.nikita-infotech.com/v1";
@Test(priority = 1, description = "1. CREATE: POST new student entity")
public void step1_createStudent() {
Map<String, Object> body = new HashMap<>();
body.put("fullName", "Aarav Deshmukh");
body.put("email", "aarav.deshmukh@nikita-infotech.com");
body.put("academicTrack", "FULLSTACK_SDET");
body.put("enrolledCredits", 6);
studentId = given()
.baseUri(BASE_URI)
.contentType(ContentType.JSON)
.body(body)
.when()
.post("/students")
.then()
.statusCode(201)
.extract().path("data.id");
System.out.println("[Step 1] Created Student ID: " + studentId);
}
@Test(priority = 2, dependsOnMethods = "step1_createStudent", description = "2. READ: Verify creation via GET")
public void step2_verifyStudent() {
given()
.baseUri(BASE_URI)
.pathParam("id", studentId)
.when()
.get("/students/{id}")
.then()
.statusCode(200)
.body("data.fullName", equalTo("Aarav Deshmukh"))
.body("data.enrolledCredits", equalTo(6));
System.out.println("[Step 2] Verified Student ID: " + studentId);
}
@Test(priority = 3, dependsOnMethods = "step2_verifyStudent", description = "3. UPDATE: Full PUT replacement")
public void step3_fullUpdateStudent() {
Map<String, Object> putBody = new HashMap<>();
putBody.put("id", studentId);
putBody.put("fullName", "Aarav Deshmukh");
putBody.put("email", "aarav.deshmukh@nikita-infotech.com");
putBody.put("academicTrack", "FULLSTACK_SDET");
putBody.put("enrolledCredits", 18); // Modified
given()
.baseUri(BASE_URI)
.pathParam("id", studentId)
.contentType(ContentType.JSON)
.body(putBody)
.when()
.put("/students/{id}")
.then()
.statusCode(200)
.body("data.enrolledCredits", equalTo(18));
System.out.println("[Step 3] Replaced entity via PUT: credits = 18");
}
@Test(priority = 4, dependsOnMethods = "step3_fullUpdateStudent", description = "4. PATCH: Partial delta mutation")
public void step4_partialPatchStudent() {
Map<String, Object> patchBody = new HashMap<>();
patchBody.put("academicTrack", "CLOUD_DEVOPS_QA");
given()
.baseUri(BASE_URI)
.pathParam("id", studentId)
.contentType(ContentType.JSON)
.body(patchBody)
.when()
.patch("/students/{id}")
.then()
.statusCode(200)
.body("data.academicTrack", equalTo("CLOUD_DEVOPS_QA"))
.body("data.enrolledCredits", equalTo(18)); // Preserved
System.out.println("[Step 4] Patched track via PATCH: track = CLOUD_DEVOPS_QA");
}
@Test(priority = 5, dependsOnMethods = "step4_partialPatchStudent", description = "5. DELETE: Purge entity")
public void step5_deleteStudent() {
given()
.baseUri(BASE_URI)
.pathParam("id", studentId)
.when()
.delete("/students/{id}")
.then()
.statusCode(204);
System.out.println("[Step 5] Purged student via DELETE");
}
@Test(priority = 6, dependsOnMethods = "step5_deleteStudent", description = "6. VERIFY: Confirm 404 Tombstone")
public void step6_verifyTombstone() {
given()
.baseUri(BASE_URI)
.pathParam("id", studentId)
.when()
.get("/students/{id}")
.then()
.statusCode(404);
System.out.println("[Step 6] Confirmed 404 Tombstone state. CRUD suite complete!");
}
}
application/x-www-form-urlencoded using formParam() for OAuth authorization endpoints and form submissions.
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
public class FormEncodedPostTest {
@Test(description = "Submit x-www-form-urlencoded POST request for OAuth2 token acquisition")
public void testFormUrlEncodedPost() {
given()
.baseUri("https://auth.nikita-infotech.com")
// Set Content-Type to application/x-www-form-urlencoded
.contentType(ContentType.URLENC)
// Use formParam() instead of body() for form key-value pairs
.formParam("grant_type", "client_credentials")
.formParam("client_id", "sdet-qa-worker")
.formParam("client_secret", "Secret#Enterprise2026Key")
.formParam("scope", "students.write students.read")
.when()
.post("/oauth/v2/token")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("token_type", equalTo("Bearer"))
.body("access_token", notNullValue())
.body("expires_in", equalTo(3600));
System.out.println("[β] Form-URL-Encoded POST request executed and validated successfully.");
}
}
ObjectNode and ArrayNode to dynamically assemble and mutate nested JSON payloads for POST and PUT requests in Java.
package com.nikita.restassured.crud;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class JacksonDynamicPayloadTest {
@Test(description = "Build a dynamic nested JSON payload using Jackson ObjectNode for POST")
public void testDynamicJsonAssembly() {
ObjectMapper mapper = new ObjectMapper();
// 1. Root JSON Object
ObjectNode rootNode = mapper.createObjectNode();
rootNode.put("fullName", "Rohan Verma");
rootNode.put("email", "rohan." + UUID.randomUUID().toString().substring(0, 6) + "@nikita-infotech.com");
rootNode.put("academicTrack", "ENTERPRISE_AUTOMATION");
rootNode.put("enrolledCredits", 8);
// 2. Nested Contact Object
ObjectNode contactNode = rootNode.putObject("contactInfo");
contactNode.put("phone", "+91-9876543210");
contactNode.put("city", "Pune");
contactNode.put("postalCode", "411015");
// 3. Nested Array Node
ArrayNode coursesNode = rootNode.putArray("initialCourseCodes");
coursesNode.add("API-PRO-101");
coursesNode.add("REST-JAVA-201");
// Pass ObjectNode directly to .body(); REST Assured serializes it automatically:
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.contentType(ContentType.JSON)
.body(rootNode)
.when()
.post("/students")
.then()
.statusCode(201)
.body("status", equalTo("SUCCESS"))
.body("data.contactInfo.city", equalTo("Pune"))
.body("data.initialCourseCodes.size()", equalTo(2));
System.out.println("[β] Dynamically constructed Jackson JSON tree posted and verified.");
}
}
HTTP 400), unsupported media types like XML when JSON is expected (HTTP 415), and missing mandatory body fields (HTTP 422).
package com.nikita.restassured.crud;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
public class NegativeMutatingValidationsTest {
@Test(description = "Verify submitting invalid JSON string produces HTTP 400 Bad Request")
public void testMalformedJsonSyntaxProduces400() {
// Syntactically broken JSON missing closing brace
String malformedJson = "{\"fullName\": \"Broken JSON\", \"credits\": ";
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.contentType(ContentType.JSON)
.body(malformedJson)
.when()
.post("/students")
.then()
.statusCode(400)
.body("title", equalTo("Bad Request"))
.body("detail", containsString("Malformed JSON"));
System.out.println("[β] 400 Bad Request asserted for malformed JSON syntax.");
}
@Test(description = "Verify unsupported XML media type produces HTTP 415 Unsupported Media Type")
public void testUnsupportedMediaTypeProduces415() {
String xmlBody = "<student><name>XML Test</name></student>";
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.contentType(ContentType.XML) // Server accepts only application/json
.body(xmlBody)
.when()
.post("/students")
.then()
.statusCode(415)
.statusLine(containsString("Unsupported Media Type"));
System.out.println("[β] 415 Unsupported Media Type asserted when submitting XML.");
}
@Test(description = "Verify missing mandatory fields produces HTTP 422 Unprocessable Entity")
public void testMissingMandatoryFieldsProduces422() {
// Payload missing required 'email' and 'academicTrack'
String incompleteJson = "{\"fullName\": \"Only Name Provided\"}";
given()
.baseUri("https://staging-api.nikita-infotech.com/v1")
.contentType(ContentType.JSON)
.body(incompleteJson)
.when()
.post("/students")
.then()
.statusCode(422)
.body("title", equalTo("Validation Failed"))
.body("invalidParams.name", containsString("email"));
System.out.println("[β] 422 Unprocessable Entity asserted for missing required attributes.");
}
}
HTTP CRUD VERBS & WIRE BEHAVIOR SPECIFICATION MATRIX:
ββββββββββββββββ¬βββββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β HTTP VERB β REST ASSURED DSL β IDEMPOTENT? β EXPECTED STATUS β PRIMARY OPERATIONAL ROLE β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β POST β .when().post() β NO β 201 Created β Provisions new resource; β
β β β β 200 OK β returns Location header β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β GET β .when().get() β YES (Safe) β 200 OK β Retrieves entity state; β
β β β β 404 Not Found β no side-effects on data β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β PUT β .when().put() β YES β 200 OK β Full entity replacement; β
β β β β 204 No Content β complete payload required β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β PATCH β .when().patch() β NO (Typically) β 200 OK β Partial delta mutation; β
β β β β 204 No Content β preserves unmodified fieldsβ
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β DELETE β .when().delete() β YES β 204 No Content β Removes resource; β
β β β β 200 OK / 404 β subsequent GET yields 404 β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β HEAD β .when().head() β YES (Safe) β 200 OK β Fetches headers only; β
β β β β β zero response body payload β
ββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β OPTIONS β .when().options()β YES (Safe) β 200 / 204 β Returns allowed methods in β
β β β β β 'Allow' transport header β
ββββββββββββββββ΄βββββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββββββββββββ€
β TOP 5 CRUD TESTING BEST PRACTICES: β
β 1. Validate the Location header on POST: Confirm the server returned the canonical resource URI. β
β 2. Test PUT with full replacement payloads: Verify all properties overwrite the prior state. β
β 3. Confirm PATCH leaves untouched fields alone: Verify that omitted fields are not reset to null. β
β 4. Audit tombstones after DELETE: Always issue a follow-up GET to confirm 404 Not Found. β
β 5. Use TestNG dependsOnMethods for CRUD chains: Ensure teardown executes only after creation. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ