Untitled
unknown
plain_text
a month ago
17 kB
6
Indexable
@Integration @API Scenario Outline: Channels v4 API returns expected response * The IPVS channels v4 call with valid headers "<x-accountId>" and "<x-mso>" and "<ip>" should return expected channel data structure Examples: | x-accountId | x-mso | ip | | LNK.8347:8347100040106596 | TWC | 1.2.3.4 | | CHARTER.8286:8286101621622343 | CHARTER | 1.2.3.4 | package stepdefs.ipvs; import io.cucumber.java.en.*; import sth.ResponseData; import sth.ServiceApiResponse; import sth.ipvs.ipvs.IPVSBaseTest; import sth.json.JSON; import sth.ipvs.saint.restservice.models.MarketLineup; import com.google.gson.GsonBuilder; import java.util.Arrays; import java.util.List; import java.util.Objects; public class IPVSApiTests extends IPVSBaseTest { private static final List<String> REQUIRED_CHANNEL_FIELDS = Arrays.asList("name", "ncsNetworkId", "logoUrl", "services"); private static final List<String> REQUIRED_LINEAR_FIELDS = Arrays.asList( "callSign", "serviceName", "ncsServiceId", "tmsGuideId", "entitled", "online", "qam", "4k", "mystroServiceId", "cdvrRecordable", "availableInMarket", "availableOutOfMarket" ); private static final List<String> REQUIRED_VOD_FIELDS = Arrays.asList( "serviceName", "ncsServiceId", "entitled" ); @When("I make a successful IPVS lineupv1 call with {string} and {string}") public void call_api_smarttv_lineupv1 (String account, String mso) { ResponseData.setData("accountId", account); ResponseData.setData("mso", mso); ServiceApiResponse response = restIPVS().apiSmarttvLineupV1(account,mso,"1.2.3.4").get(); ResponseData.setResponse("apiSmarttvLineupV1", Objects.requireNonNull(response.json())); assertTrue(response.status().isSuccess(), response.uri() + " call is failing with " + response.status() + " and TXID: " + response.txId()); assertNotNull(response.json().get("market"), "Required field 'market' not found"); assertNotNull(response.json().get("lineupId"), "Required field 'lineupID' not found"); assertNotNull(response.json().get("headend"), "Required field 'headend' not found"); assertNotNull(response.json().get("vodId"), "Required field 'vodId' not found"); } @Then("the IPVS lineupv1 response should match the SAINT apicustomer response") public void match_lineupv1_with_saint_customer_call() { JSON ipvsResponse = ResponseData.getResponse("apiSmarttvLineupV1"); ServiceApiResponse saintResponse = restSAINT().apiCustomer("1.2.3.4", ResponseData.getData("accountId"), "marketlineup", ResponseData.getData("mso")).get(); assertTrue(saintResponse.status().isSuccess(), saintResponse.uri() + " call is failing with " + saintResponse.status() + " and TXID: " + saintResponse.txId()); MarketLineup saintLineup = new GsonBuilder().create().fromJson(saintResponse.json().get("marketLineup").toString(), MarketLineup.class); assertNotNull(saintLineup.market, "Required field 'market' not found"); assertNotNull(saintLineup.lineupId, "Required field 'lineupID' not found"); assertEquals(ipvsResponse.get("market").toString(), saintLineup.market, "Market value does not match; Saint response ID: " + saintResponse.txId()); assertEquals(ipvsResponse.get("vodId").toString(), saintLineup.market, "Market value does not match; Saint response ID: " + saintResponse.txId()); assertEquals(ipvsResponse.get("lineupId").toString(), saintLineup.lineupId.toString(), "Market value does not match; Saint response ID: " + saintResponse.txId()); assertEquals(ipvsResponse.get("headend").toString(), saintLineup.market+"_"+saintLineup.lineupId, "Market value does not match; Saint response ID: " + saintResponse.txId()); } @Then("The IPVS channels v4 call with valid headers {string} and {string} and {string} should return expected channel data structure") public void request_and_verify_ipvs_channelsv4_data(String xAccountId, String xMso, String ip) { ResponseData.setData("accountId", xAccountId); ResponseData.setData("mso", xMso); ResponseData.setData("ip", ip); ServiceApiResponse ipvsResponse = restIPVS().apiSmarttvChannelsV4(xAccountId, xMso, ip, null, null, null, null, null).get(); ResponseData.setResponse("apiSmarttvChannelsV4", Objects.requireNonNull(ipvsResponse.json())); assertTrue(ipvsResponse.isSuccess(), ipvsResponse.uri() + " Call failed with " + ipvsResponse.status() + " and TXID: " + ipvsResponse.txId()); JSON json = ipvsResponse.json(); assertTrue(json.size() > 0, "Response array should not be empty"); for (int i=0; i<json.size(); i++) { validateChannelData(json.get(i)); } } private void validateChannelData(JSON channelData) { for (String field : REQUIRED_CHANNEL_FIELDS) { assertTrue(channelData.hasField(field), "Channel object missing '" + field + "' field"); } assertTrue(channelData.hasField("services"), "Channel object missing 'services' field"); JSON services = channelData.get("services"); if (services.hasField("linear")) { validateLinearServices(services.get("linear")); } if (services.hasField("vod")) { validateVodServices(services.get("vod")); } } private void validateLinearServices(JSON linearServices) { String errors = ""; for(int i=0; i<linearServices.size(); i++) { JSON linear = linearServices.get(i); for (String field : REQUIRED_LINEAR_FIELDS) { if(!linear.hasField(field)) { errors += "Linear service at index " + i + " missing '" + field + "' field\n"; } } errors += validateArrayField(linear, "channelNumbers", errors, i); errors += validateArrayField(linear, "genres", errors, i); } if(!errors.isEmpty()) { System.out.println("VALIDATION WARNINGS: " + errors); } } private void validateVodServices(JSON vodServices) { String errors = ""; for(int i=0; i<vodServices.size(); i++) { JSON vod = vodServices.get(i); for (String field : REQUIRED_VOD_FIELDS) { if(!vod.hasField(field)) { errors += "VOD service at index " + i + " missing '" + field + "' field\n"; } } errors += validateArrayField(vod, "productProviders", errors, i); } if(!errors.isEmpty()) { System.out.println("VALIDATION WARNINGS: " + errors); } } private String validateArrayField(JSON json, String fieldName, String errors, int index) { if (json.hasField(fieldName)) { Object field = json.get(fieldName); if (!(field instanceof JSON) || !((JSON) field).isArray()) { return "Service at index " + index + ": " + fieldName + " is not an array\n"; } } return ""; } @Override public void verifyTestData() {} } public IPVSApiController apiSmarttvChannelsV4 (String xAccountId, String xMso, String queryIp, String customerGuid, String xPiForwardedFor, Boolean includeAdultChannels, Boolean includeQAM, Boolean includeUnentitled) { request.reset(); request.path(API); request.path(SMARTTV); request.path(CHANNELS); request.path(V4); if (xAccountId != null) { headerXAccountid(xAccountId); } if (xMso != null) { headerXMso(xMso); } queryIp(queryIp); headerCustomerGuid(customerGuid); headerXPiForwardedFor(xPiForwardedFor); if (includeAdultChannels != null) { queryIncludeadultchannels(includeAdultChannels); } if (includeQAM != null) { queryIncludeqam(includeQAM); } if (includeUnentitled != null) { queryIncludeunentitled(includeUnentitled); } return this; } /** * Path method for endpoint "api/smarttv/channels/v4" * This method is used to build only the request path for endpoint "api/smarttv/channels/v4". * Use this method if you would like to build requests using the builder pattern * with the corresponding query parameter builder helper methods in this class. */ public IPVSApiController apiSmarttvChannelsV4 () { request.reset(); request.path(API); request.path(SMARTTV); request.path(CHANNELS); request.path(V4); return this; } package sth; import org.apache.commons.io.IOUtils; import org.testng.Assert; import sth.http.ServiceResponse; import sth.http.client.Request; import sth.http.client.Response; import sth.http.common.HttpStatus; import sth.json.JSON; import sth.xml.XML; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * A wrapper around the Request/Response dialog between HTTP client and server. For specific * use cases further methods could be added (for example to deserialize proprietary formats). */ public class ServiceApiResponse implements ServiceResponse { private Request request; private Response response; public ServiceApiResponse(Request request, Response response) { this.request = request; this.response = response; } public String txId() {return request.getHeader("x-request-id");} /** * Get the {@link Request} object that was executed to create the {@link Response} */ public Request request() { return request; } /** * Get the underlying {@link Response} object */ public Response response() { return response; } /** * True if the response status code is in the success range (200-299) */ public boolean isSuccess() { return response.status().isSuccess(); } /** * Assert that the response status code was in the success range (200-299) */ public ServiceApiResponse assertSuccess() { String errorMessage = "Request failed with status code " + response.status() + "\nRequest URL: " + request.uri() + "\nTime: " + System.currentTimeMillis() + " ; TxId: " + header("x-trace-id"); if (response.getHeader("X-Failure-Message") != null) errorMessage = errorMessage + "\nX-Failure-Message: " + header("X-Failure-Message"); if (response.getHeader("Failure-Trace") != null) errorMessage = errorMessage + "\nFailure-Trace: " + header("Failure-Trace"); try { if (response.getBody() != null) errorMessage = errorMessage + "\nBody: " + IOUtils.toString(response.bodyInputStream(), StandardCharsets.UTF_8).replaceAll("<.*?>", ""); } catch (IOException e){System.out.println("VSIERROR, IOUtils.toString: " + e.getMessage());} Assert.assertTrue(isSuccess(), errorMessage); return this; } /** * True if the response status code is in the error range (>=400) */ public boolean isError() { return response.status().isError(); } /** * Parse the response body to a JSON object */ public JSON json() { return response.as(JSON.class); } /** * Parse the response body to an XML object */ public XML xml() { return response.as(XML.class); } /** * Get the response body as a String */ public String responseString() { return response.as(String.class); } /** * Get HTTP status from response */ public HttpStatus status() { return response.status(); } /** * Get the HTTP status code of the response */ public int statusCode() { return response.status().code(); } /** * Get the request URI that was executed */ public String uri() { return request.uri().toString(); } /** * Get a response header */ public String header(String headerName) { return response.header(headerName); } @Override public String toString() { String contentType = response.header("content-type"); if (contentType.contains("json")) { return json().toString(3); } else if (contentType.contains("xml")) { return xml().toString(3); } else { return responseString(); } } } package sth; import sth.template.rest.TemplateResponse; import sth.json.JSON; import java.util.HashMap; import java.util.LinkedHashMap; public class ResponseData { private static LinkedHashMap<String, HashMap<String, Object>> responseDataMap = new LinkedHashMap<>(); private static HashMap<String, Object> responseData = new HashMap<>(); public static void setKeyId(String keyId) { if (!responseDataMap.containsKey(keyId)) { responseDataMap.put(keyId, responseData); } } public static void setQueryParams(Object param, Object value) { // TODO Auto-generated method stub } public static void removeData(String key) { try { responseData.remove(key); } catch (Exception e) { // Key not found; System.out.println("VSIERR: Failed to remove key " + key + ". Error: " + e.getMessage()); } } public static void clear() { responseData.clear();} public static void setResponse(JSON json) {responseData.put("response", json);} public static void setResponse(String endpoint, JSON json) {responseData.put(endpoint, json);} public static void setResponseheader(TemplateResponse response) { responseData.put("_x-trace-id", response.header("x-trace-id")); responseData.put("_responseCode", response.statusCode()); responseData.put("timestamp", String.valueOf(System.currentTimeMillis())); responseData.put("Lantern-Source", response.header("lantern-source")); } public static void setResponseheader(String endpoint, TemplateResponse response) { responseData.put(endpoint+"_x-trace-id", response.header("x-trace-id")); responseData.put(endpoint+"_responseCode", response.statusCode()); responseData.put(endpoint+"_timestamp", String.valueOf(System.currentTimeMillis())); responseData.put(endpoint+"_URI", response.uri()); responseData.put(endpoint+"_Lantern-Source", response.header("lantern-source")); } public static Object getLanternResponseheader(String endpoint){ return responseData.get(endpoint+"_Lantern-Source"); } public static void removeResponseheader(String endpoint) { responseData.remove(endpoint+"_URI"); responseData.remove(endpoint+"_timestamp"); responseData.remove(endpoint+"_x-trace-id"); responseData.remove(endpoint+"_responseCode"); responseData.remove(endpoint); } public static JSON getResponse() {return (JSON) responseData.get("response");} public static JSON getResponse(String endpoint) {return (JSON) responseData.get(endpoint);} public static String getTransactionId() {return (String) responseData.get("x-trace-id");} public static String getTransactionId(String endpoint) {return (String) responseData.get(endpoint+"_x-trace-id");} public static String getTimestamp(String endpoint) {return (String) responseData.get(endpoint+"_timestamp");} public static String getRequestUri(String endpoint) {return (String) responseData.get(endpoint+"_URI");} public static String getData(String value) {return (String) responseData.get(value);} public static int getIntData(String value) {return (int) responseData.get(value);} //New Method /** * Returns 0 if, and only if, { #responseData.get(endpoint+"responseCode")} is {@code null}. * * Otherwise, returns the last Response Code received from the { #endpoint} */ public static int getResponseCode(String endpoint) { if (!endpoint.isEmpty()) endpoint = endpoint + "_"; try { return (int) responseData.get(endpoint+"responseCode"); } catch (NullPointerException e) { return 0; } } public static int getResponseCode() {return getResponseCode("");} public static void setData(String key, JSON value) {responseData.put(key,value);} public static void setData(String key, boolean value) {responseData.put(key,value);} public static void setData(String key, String value) {responseData.put(key,value);} public static void setData(String key, int value) {responseData.put(key,value);} public static void printData() {responseData.forEach((key, value) -> System.out.println(key + " : " + value));} public static void printData(String key) {System.out.println(responseData.get(key));} public static boolean getBooleanData(String key) { try { return Boolean.parseBoolean((String) responseData.get(key)); } catch (Exception e) { return (boolean) responseData.get(key); } } }
Editor is loading...
Leave a Comment