Untitled

 avatar
unknown
plain_text
19 days ago
3.9 kB
4
Indexable
package sth.clientapi.stepdef;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sth.clientapi.ClientApiValidatorBaseTest;
import sth.clientapi.rest.service.request.ResponseData;

/**
 * Simple demonstration of using REST Assured instead of STH for the channels endpoint
 */
public class IPVSRestAssuredDemo extends ClientApiValidatorBaseTest {
    protected static Logger log = LoggerFactory.getLogger(IPVSRestAssuredDemo.class);
    
    /**
     * Direct replacement for the existing "send channels request" in IPVS.java
     * Uses REST Assured instead of STH
     */
    @Given("send channels request using rest assured")
    public void send_channels_request_with_rest_assured() {
        log.info("Using REST Assured to call channels endpoint");
        
        // Get the base URL for the channels endpoint
        String baseUrl = props().getServiceUrl("IPVS", "CHANNELS");
        
        // Create REST Assured request specification
        RequestSpecification requestSpec = RestAssured.given()
            .contentType(ContentType.JSON);
        
        // Add authorization header and other headers
        if (Authentication.auth != null) {
            if (Authentication.auth.getAccessToken() != null) {
                requestSpec.header("Authorization", "Bearer " + Authentication.auth.getAccessToken());
            }
            
            requestSpec.header("x-client-id", Authentication.auth.getClientType());
            requestSpec.header("x-device-id", Authentication.auth.getDeviceId());
        }
        
        // Add API key header
        requestSpec.header("x-api-key", props().getApiKey(Authentication.auth.getClientType()));
        
        // Execute the request
        Response response = requestSpec
            .when()
            .get(baseUrl);
        
        // Store the response for compatibility with existing code
        String responseBody = response.getBody().asString();
        
        // Create a JSON object from the response string
        try {
            org.json.JSONObject jsonObject = new org.json.JSONObject(responseBody);
            // Store the response in ResponseData for validation
            ResponseData.setResponse("CHANNELS", jsonObject.toString());
            ResponseData.setResponseCode("CHANNELS", response.getStatusCode());
            
            log.info("IPVS channels ResponseCode (REST Assured) -> " + response.getStatusCode());
        } catch (Exception e) {
            log.error("Error parsing JSON response: " + e.getMessage());
            ResponseData.setResponseCode("CHANNELS", response.getStatusCode());
        }
    }
    
    /**
     * Validates channel v3 call - to support INT-083 scenario
     */
    @Then("validate channel v3 call is successful")
    public void validate_channel_v3_call_is_successful() {
        // Get the response code from ResponseData
        int responseCode = ResponseData.getResponseCode("CHANNELS");
        
        // Log the response code
        log.info("Channel v3 call response code: " + responseCode);
        
        // Assert that the response code is 200 (OK)
        assertEquals("Channel v3 call failed with response code: " + responseCode, 
                    200, responseCode);
        
        // Additional validation can be added here if needed
        log.info("Channel v3 call validation successful");
    }
    
    @Override
    public void verifyTestData() {
        // Implementation required by ClientApiValidatorBaseTest
    }
    
    @Override
    public void setProductVersion() {
        // Implementation required by ClientApiValidatorBaseTest
    }
}
Editor is loading...
Leave a Comment