Bearer Token - Notes By ShariqSP

How to Interact with Inventory API using Rest Assured

The InventoryController provides RESTful APIs for managing inventory. Below are the steps to interact with each API method using Rest Assured. Use Swagger UI at Swagger UI to view detailed API documentation and available endpoints.

1. Generate Bearer Token

Endpoint: POST /inventory/token

Purpose: Generates a Bearer token for authentication.

Rest Assured Example:

                    String token;

	@Test
	void aBearerToken()
	{
	 token = RestAssured.given()
            .post("http://apitesting.shariqsp.com:8080/inventory/token")
            .then()
            .statusCode(200)
            .log()
            .all()
            .extract()
            .asString()
            ;
        System.out.println("Generated Token: " + token);
	}
                

Refer to Swagger UI for the full endpoint description.

2. Get All Inventory Items

Endpoint: GET /inventory

Purpose: Fetches all inventory items. Requires a valid Bearer token in the Authorization header.

Rest Assured Example:

                    @Test
	void bBearerTokenPost()
	{

		 System.out.println("test2");
		 JSONObject newItem = new JSONObject();
		 newItem.put("itemName", "samsung");
		 newItem.put("id", "1234");
		 newItem.put("quantity", "10");

		 RestAssured.given()
		    .auth().oauth2(token)
		    .contentType(ContentType.JSON)
		    .body(newItem.toString())
		    .post("http://apitesting.shariqsp.com:8080/inventory")
		    .then()
		    .log().all()  // Logs response details
		    .statusCode(200);


	}
                

Swagger UI provides detailed request and response schemas for this method.

3. Add Inventory Item

Endpoint: POST /inventory

Purpose: Adds a new inventory item. Requires a valid Bearer token in the Authorization header and a JSON body with item details.

Sample Request Body:

                    {
                        "name": "Samsung",
                        "quantity": 10,
                        "price": 100.0
                    }
                

Rest Assured Example:

                    @Test
                    void cBearerTokenGet()
                    {
                        Response response = RestAssured.given()
                                .auth().oauth2(token)
                                .get("http://apitesting.shariqsp.com:8080/inventory")
                                .then()
                                .statusCode(200)
                                .log()
                                .all()
                                .extract()
                                .response();
                            System.out.println("Inventory Items: " + response.asString());
                    }
                

Refer to Swagger UI to see details on the request body and the API's response format.

Important Notes

  • Always generate a new Bearer token before making any API calls if the token is expired.
  • Ensure the Authorization header includes the token in the format: Bearer <token>.
  • Swagger UI provides a comprehensive reference for testing and understanding API behavior.