Untitled

 avatar
unknown
plain_text
13 days ago
6.2 kB
5
Indexable
package stepdefs.nns;

import io.cucumber.java.en.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sth.ServiceApiResponse;
import sth.json.JSON;
import sth.nns.clientservice.NNSBaseTest;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class NNSApiTests extends NNSBaseTest {
    private static final Logger log = LoggerFactory.getLogger(NNSApiTests.class);

    @Then("I make a successful request to nns v1 recommendations personalized with {string} and {string} and {string}")
    public void request_and_verify_nns_recommendations_personalized(String xAccountId, String xMso, String clientNnsProfile) {
        ServiceApiResponse nnsResponse = restNNS().apiRecommendationsPersonalized(xAccountId, xMso, clientNnsProfile).get();

        assertTrue(nnsResponse.isSuccess(), "NNS recommendations personalized API call failed with status: " + nnsResponse.status() +
                        " and TXID: " + nnsResponse.txId());
        
        JSON nnsJson = nnsResponse.json();

        // Comprehensive initial validation
        validateNNSResponseStructure(nnsJson);

        // Compare with Search API
        List<String> tmsProgramIds = extractTmsProgramIds(nnsJson.get("results"));
        if (!tmsProgramIds.isEmpty()) {
            compareWithSearchApi(tmsProgramIds);
        }
    }

    /**
     * Performs a comprehensive validation of the entire NNS response structure
     */
    private void validateNNSResponseStructure(JSON nnsJson) {
        // Top-level fields validation
        Set<String> expectedTopLevelFields = new HashSet<>(Arrays.asList(
            "type", "uiHint", "vodOutOfWindow", "name", "twcTvNetworkDisplayMode",
            "results", "num_categories", "total_results", "start_index", 
            "num_results", "max_results", "curationType", "dynamicUris", 
            "dsQueryId", "skeletonShelf"
        ));

        // Check for unexpected or missing top-level fields
        validateJsonFields(nnsJson, expectedTopLevelFields);

        // Validate results structure
        JSON results = nnsJson.get("results");
        assertTrue(results.isArray(), "Results should be an array");
        assertTrue(results.size() > 0, "Results array should not be empty");

        // Validate each result
        for (int i = 0; i < results.size(); i++) {
            JSON result = results.get(i);
            validateResultStructure(result, i);
        }

        // Additional specific validations
        assertEquals("Personalized", nnsJson.get("name"), "Incorrect name");
        assertEquals("personalized", nnsJson.get("curationType"), "Incorrect curationType");
        assertTrue(nnsJson.hasField("dynamicUris") && 
                   ((JSON)nnsJson.get("dynamicUris")).size() > 0, 
                   "Dynamic URIs should be present");
    }

    /**
     * Validates the structure of an individual result
     */
    private void validateResultStructure(JSON result, int index) {
        // Common result fields
        Set<String> commonResultFields = new HashSet<>(Arrays.asList(
            "type", "alphaSortOn", "availableOutOfHome", "details", 
            "title", "uri", "skeletonShelf", "network"
        ));

        // Validate common fields
        validateJsonFields(result, commonResultFields);

        // Validate type-specific details
        String type = result.get("type").toString();
        switch (type) {
            case "event":
                validateEventDetails(result, index);
                break;
            case "episode_list":
                validateEpisodeListDetails(result, index);
                break;
            default:
                fail("Unexpected result type: " + type);
        }
    }

    /**
     * Validates event-specific details
     */
    private void validateEventDetails(JSON event, int index) {
        Set<String> eventFields = new HashSet<>(Arrays.asList(
            "tmsProgramIds", "providerAssetIds", "image_uri", 
            "network", "details", "streamList"
        ));

        validateJsonFields(event, eventFields);

        // Validate network
        validateNetworkStructure(event.get("network"));

        // Validate details
        JSON details = event.get("details");
        Set<String> detailsFields = new HashSet<>(Arrays.asList(
            "genres", "allRatings", "allVPPs", "allIpVPPs", 
            "entitled", "tvodEntitled", "linearEntitledIp", "linearEntitledQam"
        ));
        validateJsonFields(details, detailsFields);
    }

    /**
     * Validates episode list-specific details
     */
    private void validateEpisodeListDetails(JSON episodeList, int index) {
        Set<String> episodeListFields = new HashSet<>(Arrays.asList(
            "tmsSeriesId", "tmsSeriesIdStr", "tmsGuideServiceIds", 
            "image_uri", "network", "details"
        ));

        validateJsonFields(episodeList, episodeListFields);

        // Validate network
        validateNetworkStructure(episodeList.get("network"));

        // Validate details
        JSON details = episodeList.get("details");
        Set<String> detailsFields = new HashSet<>(Arrays.asList(
            "num_assets", "totalEpisodes", "latest_episode", 
            "allRatings", "allVPPs", "allIpVPPs",
            "entitled", "tvodEntitled", "linearEntitledIp", "linearEntitledQam"
        ));
        validateJsonFields(details, detailsFields);
    }

    /**
     * Validates network structure
     */
    private void validateNetworkStructure(JSON network) {
        Set<String> networkFields = new HashSet<>(Arrays.asList(
            "callsign", "image_uri", "name", "product_provider"
        ));
        validateJsonFields(network, networkFields);
    }

    /**
     * Generic method to validate JSON fields
     */
    private void validateJsonFields(JSON json, Set<String> expectedFields) {
        // Check all expected fields are present
        for (String field : expectedFields) {
            assertTrue(json.hasField(field), "Missing expected field: " + field);
        }

        // Optional: You could add additional checks like non-empty values if needed
    }

    // Rest of the existing methods remain the same
}
Editor is loading...
Leave a Comment