CRUD LIFECYCLE AUTOMATION: POST, PUT, PATCH & DELETE IN REST ASSURED

Architect complete entity lifecycles in Java: POST entity provisioning (201 Created), PUT full replacement (200/204), PATCH partial updates (RFC 7396 Merge Patch), DELETE tombstone purging (204 No Content), idempotent state verifications, and TestNG dependent method chaining.

πŸ”„ 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.

1. End-to-End CRUD Lifecycle & State Transition Architecture
The architectural state transition diagram of a full CRUD test cycle, tracking how an entity is created, verified, completely replaced, partially patched, and permanently purged.
CRUD Entity State Transition Topography
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                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Architectural Standard: A complete CRUD automation suite must leave the database in its original clean state. Every entity created in a POST test should be purged via DELETE in the final teardown step, preventing test data accumulation in staging clusters.
Real-Time Example 1: POST Request: Entity Creation, Location Header & ID Extraction
Submitting a 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.
Java (POST Request: Provisioning & ID Extraction)
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);
    }
}
POST Execution Telemetry: POST /v1/students -> HTTP 201 Created [194ms] Location: https://staging-api.nikita-infotech.com/v1/students/STU-88219 Extracted ID: STU-88219 Entity successfully committed to database.
Real-Time Example 2: PUT Request: Full Resource Replacement & Idempotency
Executing a PUT request for complete entity replacement. Demonstrates sending the complete updated object state, asserting 200 OK, and proving idempotency by issuing consecutive identical calls.
Java (PUT Request: Full Entity Replacement & Idempotency Verification)
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.");
    }
}
PUT Execution Output: PUT /v1/students/STU-88219 -> HTTP 200 OK [124ms] academicTrack updated to "CLOUD_DEVOPS_QA", enrolledCredits updated to 16. Second PUT execution returned 200 OK with identical payload (Idempotency Verified).
Real-Time Example 3: PATCH Request: Surgical Delta Mutation (RFC 7396)
Applying partial updates using PATCH. Sends a minimal delta payload modifying a single attribute (academicStatus), and confirms that unmodified properties (credits, email) remain unchanged.
Java (PATCH Request: Delta Mutation & Non-Destructive Verification)
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.");
    }
}
PATCH Execution Output: PATCH /v1/students/STU-88219 -> HTTP 200 OK [82ms] Delta Applied: academicStatus = "HONORS_DEAN_LIST" Unmodified fields verified: fullName, enrolledCredits, and academicTrack were preserved.
Real-Time Example 4: DELETE Request: Entity Teardown & 204 No Content
Executing resource deletion via DELETE. Asserts 204 No Content with an empty body, and verifies that subsequent retrieval calls return 404 Not Found.
Java (DELETE Request & 404 Tombstone Verification)
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.");
    }
}
DELETE Execution Trace: DELETE /v1/students/STU-88219 -> HTTP 204 No Content [62ms] (Body: 0 bytes) GET /v1/students/STU-88219 -> HTTP 404 Not Found [38ms] Tombstone audit confirmed: Resource permanently removed from system.
Real-Time Example 5: Orchestrating Full CRUD Suites with TestNG dependsOnMethods
Chaining a complete CRUD test suite inside a single TestNG class using dependsOnMethods. Ensures tests run in strict sequential order (Create βž” Read βž” Update βž” Patch βž” Delete βž” Verify Tombstone), halting if an earlier step fails.
Java (Full Sequential CRUD TestNG Suite)
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!");
    }
}
TestNG Suite Execution Summary: [Step 1] Created Student ID: STU-10492 [Step 2] Verified Student ID: STU-10492 [Step 3] Replaced entity via PUT: credits = 18 [Step 4] Patched track via PATCH: track = CLOUD_DEVOPS_QA [Step 5] Purged student via DELETE [Step 6] Confirmed 404 Tombstone state. CRUD suite complete! PASSED: 6 of 6 steps executed in sequential order.
Real-Time Example 6: Handling URL-Encoded Form Bodies & Form-Data in POST
Submitting non-JSON payloads. Demonstrates handling application/x-www-form-urlencoded using formParam() for OAuth authorization endpoints and form submissions.
Java (Submitting Form-URLEncoded Payloads with formParam())
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.");
    }
}
Form-URL-Encoded Wire Trace: POST /oauth/v2/token HTTP/1.1 Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1 Body: grant_type=client_credentials&client_id=sdet-qa-worker&client_secret=Secret%23... Status: 200 OK | Token minted successfully.
Real-Time Example 7: Handling Dynamic Payloads via Jackson ObjectMapper
Constructing dynamic request bodies. Demonstrates using Jackson's ObjectNode and ArrayNode to dynamically assemble and mutate nested JSON payloads for POST and PUT requests in Java.
Java (Dynamic JSON Building with Jackson ObjectNode)
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.");
    }
}
Jackson Tree Serialization: Serialized JSON Body: { "fullName": "Rohan Verma", "email": "rohan.a1b2c3@nikita-infotech.com", "academicTrack": "ENTERPRISE_AUTOMATION", "enrolledCredits": 8, "contactInfo": { "phone": "+91-9876543210", "city": "Pune", "postalCode": "411015" }, "initialCourseCodes": ["API-PRO-101", "REST-JAVA-201"] } POST /v1/students -> HTTP 201 Created
Real-Time Example 8: Negative Mutating Validations: 400 Bad Request & 415 Media Type
Negative testing for state-mutating requests. Validates submitting malformed JSON (HTTP 400), unsupported media types like XML when JSON is expected (HTTP 415), and missing mandatory body fields (HTTP 422).
Java (Negative Mutating Tests: 400, 415 & 422 Validations)
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.");
    }
}
Negative Mutation Output: POST /v1/students (Malformed JSON) -> HTTP 400 Bad Request [38ms] (PASSED) POST /v1/students (ContentType.XML) -> HTTP 415 Unsupported Media Type [24ms] (PASSED) POST /v1/students (Missing Fields) -> HTTP 422 Unprocessable Entity [46ms] (PASSED)
Real-Time Example 9: HTTP Verbs & CRUD Specifications Reference Matrix
A comprehensive reference summary pairing HTTP verbs, idempotency characteristics, expected status codes, payload requirements, and operational best practices for REST Assured engineers.
HTTP CRUD Verbs & Wire Behavior Specification Matrix
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.     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Engineering Best Practice: Understanding the distinct contract requirements of POST, PUT, PATCH, and DELETE ensures comprehensive test coverage. By enforcing complete payloads on PUT, asserting non-destructive deltas on PATCH, and confirming 404 tombstones after DELETE, you validate the complete lifecycle of enterprise API entities.