Untitled

 avatar
unknown
plain_text
14 days ago
9.0 kB
3
Indexable
import io.cucumber.java.en.Then;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Step definitions for testing the NNS Recommendations Personalized API.
 */
public class NnsRecommendationsStepDefinitions {

    // Required fields in the response
    private static final String[] REQUIRED_RECOMMENDATIONS_FIELDS = {
        "type", "uiHint", "vodOutOfWindow", "name", "twcTvNetworkDisplayMode", 
        "results", "num_categories", "total_results", "start_index", 
        "num_results", "max_results", "dsQueryId", "curationType", "dynamicUris"
    };

    // Required fields in each result item
    private static final String[] REQUIRED_RESULT_FIELDS = {
        "type", "alphaSortOn", "availableOutOfHome", "details", "title", "uri", "skeletonShelf"
    };

    private static final String[] REQUIRED_EVENT_FIELDS = {
        "genres", "allRatings", "allVPPs", "allIpVPPs", "entitled", 
        "tvodEntitled", "linearEntitledIp", "linearEntitledQam"
    };

    /**
     * Tests the NNS Recommendations Personalized API with the given parameters.
     * 
     * @param division The division parameter
     * @param vodId The vodId parameter
     * @param lineup The lineup parameter
     * @param useridhash The useridhash parameter
     */
    @Then("The NNS recommendations personalized call with division {string}, vodId {string}, lineup {string}, and useridhash {string} should return expected data structure")
    public void request_and_verify_nns_recommendations_personalized(String division, String vodId, String lineup, String useridhash) {
        // Create the request parameters
        RequestParams params = new RequestParams();
        params.add("division", division);
        params.add("vodId", vodId);
        params.add("lineup", lineup);
        params.add("path", "1757001972");
        params.add("useridhash", useridhash);
        params.add("recommendationKey", "HNAV_VOD_Free_");
        params.add("profile", "{client_nns_profile}");
        params.add("cacheID", "280");
        params.add("application", "VOD_PORTAL");
        params.add("profileConfigurationEnablePersonalizedRecommendations", "true");
        params.add("profileConfigurationHttpCachingStrategy", "MAX_AGE");
        params.add("profileConfigurationIncludeNielsenReporting", "true");
        params.add("profileConfigurationSearchUseBkm", "true");
        params.add("profileConfigurationTwctvClientUseActionGroupsWholeApp", "true");
        params.add("tvodRent", "true");
        params.add("tvodWatch", "true");
        params.add("cdvrEnabled", "true");
        params.add("deviceLocation", "In_Home");
        params.add("watchOnDemand", "true");
        params.add("hideOnDemand", "false");
        params.add("watchLive", "true");
        
        // Add cookie header
        Map<String, String> headers = new HashMap<>();
        headers.put("Cookie", "dla_marker=9febad42-bf50-4142-8ce1-812361e31a4b");
        
        // Make the API call
        ServiceApiResponse response = restNNS().apiRecommendationsPersonalized(params, headers).get();
        
        // Verify the response status
        assertTrue("NNS recommendations personalized API call failed with status: " + response.status() + 
                   " and TXID: " + response.txId(), response.isSuccess());
        
        // Parse the JSON response
        JSON json = response.json();
        
        // Validate the required fields in the response
        validateRequiredFields(json, REQUIRED_RECOMMENDATIONS_FIELDS, "Root object");
        
        // Validate the results array
        validateResultsArray(json.get("results"));
        
        // Additional validation
        assertEquals("Expected name to be 'Personalized'", "Personalized", json.getString("name"));
        assertEquals("Expected curationType to be 'personalized'", "personalized", json.getString("curationType"));
        assertTrue("Expected at least one dynamic URI", json.getArraySize("dynamicUris") > 0);
    }
    
    /**
     * Validates that the given JSON object contains all required fields.
     */
    private void validateRequiredFields(JSON json, String[] requiredFields, String objectName) {
        for (String field : requiredFields) {
            assertTrue(objectName + " missing required field: " + field, json.hasField(field));
        }
    }
    
    /**
     * Validates the results array in the response.
     */
    private void validateResultsArray(JSON results) {
        assertTrue("Results should be an array", results.isArray());
        assertTrue("Results array should not be empty", results.size() > 0);
        
        // Validate each result item
        for (int i = 0; i < results.size(); i++) {
            JSON result = results.get(i);
            validateRequiredFields(result, REQUIRED_RESULT_FIELDS, "Result item " + i);
            
            // Validate details object
            JSON details = result.get("details");
            assertNotNull("Details object should not be null", details);
            
            // Validate details based on result type
            String type = result.getString("type");
            if ("event".equals(type)) {
                validateEventDetails(details);
            } else if ("episode_list".equals(type)) {
                validateEpisodeListDetails(details);
            }
            
            // Validate network if present
            if (result.hasField("network")) {
                validateNetwork(result.get("network"));
            }
            
            // Validate streamList if present
            if (result.hasField("streamList")) {
                validateStreamList(result.get("streamList"));
            }
        }
    }
    
    /**
     * Validates the details object for an event.
     */
    private void validateEventDetails(JSON details) {
        validateRequiredFields(details, REQUIRED_EVENT_FIELDS, "Event details");
        
        // Validate genres array
        JSON genres = details.get("genres");
        assertTrue("Genres should be an array", genres.isArray());
        if (genres.size() > 0) {
            for (int i = 0; i < genres.size(); i++) {
                assertTrue("Genre " + i + " should have a name", genres.get(i).hasField("name"));
            }
        }
        
        // Validate ratings, VPPs, and entitled flags
        validateArrayField(details, "allRatings");
        validateArrayField(details, "allVPPs");
        validateArrayField(details, "allIpVPPs");
    }
    
    /**
     * Validates the details object for an episode list.
     */
    private void validateEpisodeListDetails(JSON details) {
        assertTrue("Episode list details missing num_assets", details.hasField("num_assets"));
        assertTrue("Episode list details missing totalEpisodes", details.hasField("totalEpisodes"));
        
        // Validate latest_episode if present
        if (details.hasField("latest_episode")) {
            JSON latestEpisode = details.get("latest_episode");
            assertTrue("Latest episode should have type", latestEpisode.hasField("type"));
            assertTrue("Latest episode should have details", latestEpisode.hasField("details"));
            assertTrue("Latest episode should have title", latestEpisode.hasField("title"));
        }
        
        // Validate entitled flags if present
        if (details.hasField("entitled")) {
            assertNotNull("Entitled flag should not be null", details.get("entitled"));
        }
    }
    
    /**
     * Validates the network object.
     */
    private void validateNetwork(JSON network) {
        String[] requiredNetworkFields = {"callsign", "image_uri", "name", "product_provider"};
        validateRequiredFields(network, requiredNetworkFields, "Network");
    }
    
    /**
     * Validates the stream list array.
     */
    private void validateStreamList(JSON streamList) {
        assertTrue("Stream list should be an array", streamList.isArray());
        
        for (int i = 0; i < streamList.size(); i++) {
            JSON stream = streamList.get(i);
            assertTrue("Stream " + i + " missing type", stream.hasField("type"));
            
            if (stream.hasField("streamProperties")) {
                JSON props = stream.get("streamProperties");
                assertTrue("Stream properties should have entitled field", props.hasField("entitled"));
            }
            
            if (stream.hasField("network")) {
                validateNetwork(stream.get("network"));
            }
        }
    }
    
    /**
     * Validates that the given field is an array.
     */
    private void validateArrayField(JSON json, String fieldName) {
        if (json.hasField(fieldName)) {
            Object field = json.get(fieldName);
            assertTrue(fieldName + " is not an array", (field instanceof JSON) && ((JSON) field).isArray());
        }
    }
}
Editor is loading...
Leave a Comment