How to use init method of com.qaprosoft.carina.core.foundation.api.AbstractApiMethod class

Best Carina code snippet using com.qaprosoft.carina.core.foundation.api.AbstractApiMethod.init

Source:AbstractApiMethod.java Github

copy

Full Screen

...58 private boolean logRequest = Configuration.getBoolean(Parameter.LOG_ALL_JSON);59 private boolean logResponse = Configuration.getBoolean(Parameter.LOG_ALL_JSON);60 public AbstractApiMethod()61 {62 init(getClass());63 bodyContent = new StringBuilder();64 request = given();65 request.contentType(ContentType.TEXT);66 }67 68 public AbstractApiMethod(String contentType)69 {70 init(getClass());71 bodyContent = new StringBuilder();72 request = given();73 request.contentType(contentType);74 }75 @SuppressWarnings("rawtypes")76 private void init(Class clazz)77 {78 String typePath = R.API.get(clazz.getSimpleName());79 if (typePath == null)80 {81 throw new RuntimeException("Method type and path are not specified for: " + clazz.getSimpleName());82 }83 if(typePath.contains(":"))84 {85 methodType = HttpMethodType.valueOf(typePath.split(":")[0]);86 methodPath = typePath.split(":")[1];87 }88 else89 {90 methodType = HttpMethodType.valueOf(typePath);91 }92 93 }94 public void setHeaders(String... headerKeyValues)95 {96 for (String headerKeyValue : headerKeyValues)97 {98 String key = headerKeyValue.split("=")[0];99 String value = headerKeyValue.split("=")[1];100 request.header(key, value);101 }102 }103 public void addUrlParameter(String key, String value)104 {105 if (value != null)106 {107 request.queryParam(key, value);108 }109 }110 public void addParameter(String key, String value)111 {112 request.param(key, value.replace(" ", "%20"));113 }114 public void addParameterIfNotNull(String key, String value)115 {116 if (value != null)117 {118 this.addParameter(key, value);119 }120 }121 122 public void addBodyParameter(String key, Object value)123 {124 if (bodyContent.length() != 0)125 {126 bodyContent.append("&");127 }128 bodyContent.append(key + "=" + value);129 }130 protected void addBodyParameterIfNotNull(String key, Object value)131 {132 if (value != null)133 {134 addBodyParameter(key, value);135 }136 }137 138 public void addCookie(String key, String value)139 {140 request.given().cookie(key, value);141 }142 143 public void addCookies(Map<String, String> cookies)144 {145 request.given().cookies(cookies);146 }147 public void replaceUrlPlaceholder(String placeholder, String value)148 {149 if (value != null)150 {151 methodPath = methodPath.replace("${" + placeholder + "}", value);152 }153 else154 {155 methodPath = methodPath.replace("${" + placeholder + "}", "");156 methodPath = StringUtils.removeEnd(methodPath, "/");157 }158 }159 public void expectResponseStatus(HttpResponseStatusType status)160 {161 request.expect().statusCode(status.getCode());162 request.expect().statusLine(Matchers.containsString(status.getMessage()));163 }164 public <T> void expectResponseContains(Matcher<T> key, Matcher<T> value)165 {166 request.expect().body(key, value);167 }168 public void expectValueByXpath(String xPath, String value)169 {170 request.expect().body(Matchers.hasXPath(xPath), Matchers.containsString(value));171 }172 public void expectValueByXpath(String xPath, String value1, String value2)173 {174 request.expect().body(Matchers.hasXPath(xPath), Matchers.anyOf(Matchers.containsString(value1), Matchers.containsString(value2)));175 }176 public <T> void expectResponseContains(Matcher<T> value)177 {178 request.expect().body(value);179 }180 public <T> void expectResponseContains(String key, Matcher<T> value)181 {182 request.expect().body(key, value);183 }184 public <T> void expectResponseContainsXpath(String xPath)185 {186 request.expect().body(HasXPath.hasXPath(xPath));187 }188 189 public Response callAPI()190 {191 if (bodyContent.length() != 0)192 request.body(bodyContent.toString());193 Response rs = null;194 PrintStream ps = null;195 if (logRequest || logResponse)196 {197 ps = new PrintStream(new LoggingOutputStream(LOGGER, Level.INFO));198 }199 if (logRequest)200 request.filter(new RequestLoggingFilter(ps));201 if (logResponse)202 request.filter(new ResponseLoggingFilter(ps));203 try204 {205 rs = HttpClient.send(request, methodPath, methodType);206 } finally207 {208 if (ps != null)209 ps.close();210 }211 return rs;212 }213 214 /**215 * @deprecated use {@link #callAPI()} instead. 216 * 217 * @return String218 */219 @Deprecated220 public String call()221 {222 Response response = callAPI();223 return response != null ? response.asString() : null;224 }225 226 public void expectInResponse(Matcher<?> matcher)227 {228 request.expect().body(matcher);229 }230 231 public void expectInResponse(String locator, Matcher<?> value)232 {233 request.expect().body(locator, value);234 }235 public String getMethodPath()236 {237 return methodPath;238 }239 public void setMethodPath(String methodPath)240 {241 RestAssured.reset();242 this.methodPath = methodPath;243 }244 public void setBodyContent(String content)245 {246 this.bodyContent = new StringBuilder(content);247 }248 249 public RequestSpecification getRequest()250 {251 return request;252 }253 public void setLogRequest(boolean logRequest)254 {255 this.logRequest = logRequest;256 }257 public void setLogResponse(boolean logResponse)258 {259 this.logResponse = logResponse;260 }261 public void ignoreSSLCerts()262 {263 SSLContext sslContext = null;264 try265 {266 sslContext = SSLContext.getInstance("TLS");267 } catch (NoSuchAlgorithmException e)268 {269 throw new RuntimeException(e);270 }271 TrustManager[] trustManagerArray = { new NullX509TrustManager() };272 try273 {274 sslContext.init(null, trustManagerArray, null);275 } catch (KeyManagementException e)276 {277 throw new RuntimeException(e);278 }279 SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, new NullHostnameVerifier());280 SSLConfig sslConfig = new SSLConfig();281 sslConfig = sslConfig.sslSocketFactory(socketFactory);282 sslConfig = sslConfig.x509HostnameVerifier(new NullHostnameVerifier());283 RestAssuredConfig cfg = new RestAssuredConfig();284 cfg = cfg.sslConfig(sslConfig);285 request = request.config(cfg);286 }287 public void setSSLContext(SSLContext sslContext)288 {...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import org.apache.log4j.Logger;2import org.testng.annotations.BeforeClass;3import org.testng.annotations.Test;4import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;5import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType;6import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType.Family;7import com.qaprosoft.carina.core.foundation.utils.Configuration;8public class AbstractApiMethodTest {9 private static final Logger LOGGER = Logger.getLogger(AbstractApiMethodTest.class);10 public void init() {11 Configuration.get(Configuration.Parameter.URL);12 }13 public void testInit() {14 AbstractApiMethod abstractApiMethod = new AbstractApiMethod("api/methods/get.json");15 HttpResponseStatusType status = abstractApiMethod.callAPI().assertStatus();16 LOGGER.info("Status code: " + status.getStatusCode());17 LOGGER.info("Status family: " + status.getFamily());18 LOGGER.info("Status reason: " + status.getReasonPhrase());19 LOGGER.info("Status info: " + status.getInfo());20 }21 public void testInitWithUrl() {22 HttpResponseStatusType status = abstractApiMethod.callAPI().assertStatus();23 LOGGER.info("Status code: " + status.getStatusCode());24 LOGGER.info("Status family: " + status.getFamily());25 LOGGER.info("Status reason: " + status.getReasonPhrase());26 LOGGER.info("Status info: " + status.getInfo());27 }28 public void testInitWithUrlAndHeaders() {29 public void addHeaders() {30 addHeader("Content-Type", "application/json");31 addHeader("Accept", "application/json");32 }33 };34 HttpResponseStatusType status = abstractApiMethod.callAPI().assertStatus();35 LOGGER.info("Status code: " + status.getStatusCode());36 LOGGER.info("Status family: " + status.getFamily());37 LOGGER.info("Status reason: " + status.getReasonPhrase());38 LOGGER.info("Status info: " + status.getInfo());39 }40 public void testInitWithUrlAndHeadersAndParams() {

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;2public class InitMethod extends AbstractApiMethod {3 public InitMethod() {4 super();5 }6}7import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;8public class InitMethod extends AbstractApiMethod {9 public InitMethod(String url) {10 super(url);11 }12}13import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;14public class InitMethod extends AbstractApiMethod {15 public InitMethod(String url, String method) {16 super(url, method);17 }18}19import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;20public class InitMethod extends AbstractApiMethod {21 public InitMethod(String url, String method, String contentType) {22 super(url, method, contentType);23 }24}25import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;26public class InitMethod extends AbstractApiMethod {27 public InitMethod(String url, String method, String contentType, String requestType) {28 super(url, method, contentType, requestType);29 }30}31import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;32public class InitMethod extends AbstractApiMethod {33 public InitMethod(String url, String method, String contentType, String requestType, String responseType) {34 super(url, method, contentType, requestType, responseType);35 }36}37import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;38public class InitMethod extends AbstractApiMethod {39 public InitMethod(String url, String method, String contentType, String requestType, String responseType, String body) {40 super(url, method, contentType, requestType, responseType, body);41 }42}43import com.qaprosoft.carina

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1public class GetBooksApiMethod extends AbstractApiMethod {2 public GetBooksApiMethod() {3 super(null, "api/books/_get/rq.json");4 }5}6public class GetBooksApiMethod extends AbstractApiMethod {7 public GetBooksApiMethod() {8 super(null, "api/books/_get/rq.json");9 }10}11GetBooksApiMethod getBooksApiMethod = new GetBooksApiMethod();12getBooksApiMethod.expectResponseStatus(HttpResponseStatusType.OK_200);13getBooksApiMethod.validateResponseAgainstJSONSchema("api/books/_get/rs.schema.json");14getBooksApiMethod.validateResponseAgainstJSONSchema("api/books/_get/rs.schema.json");15GetBooksApiMethod getBooksApiMethod = new GetBooksApiMethod();16getBooksApiMethod.expectResponseStatus(HttpResponseStatusType.OK_200);17getBooksApiMethod.validateResponseAgainstJSONSchema("api/books/_get/rs.schema.json");18getBooksApiMethod.validateResponseAgainstJSONSchema("api/books/_get/rs.schema.json");19public class GetBookByIdApiMethod extends AbstractApiMethod {20 public GetBookByIdApiMethod(int bookId) {21 super(null, "api/books/{book_id}/_get/rq.json");22 replaceUrlPlaceholder("book_id", String.valueOf(bookId));23 }24}25public class GetBookByIdApiMethod extends AbstractApiMethod {26 public GetBookByIdApiMethod(int bookId) {27 super(null, "api/books/{book_id}/_get/rq.json");28 replaceUrlPlaceholder("book_id", String.valueOf(bookId));29 }30}31GetBookByIdApiMethod getBookByIdApiMethod = new GetBookByIdApiMethod(1);32getBookByIdApiMethod.expectResponseStatus(HttpResponseStatusType.OK_200);

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1public class TestApi extends AbstractApiMethod {2 public TestApi() {3 super(null, null);4 }5}6public class TestApi extends AbstractApiMethod {7 public TestApi() {8 super(null, null);9 }10}11public class TestApi extends AbstractApiMethod {12 public TestApi() {13 super(null, null);14 }15}16public class TestApi extends AbstractApiMethod {17 public TestApi() {18 super(null, null);19 }20}21public class TestApi extends AbstractApiMethod {22 public TestApi() {23 super(null, null);24 }25}26public class TestApi extends AbstractApiMethod {27 public TestApi() {28 super(null, null);29 }30}31public class TestApi extends AbstractApiMethod {32 public TestApi() {33 super(null, null);34 }35}36public class TestApi extends AbstractApiMethod {37 public TestApi() {38 super(null, null);39 }40}41public class TestApi extends AbstractApiMethod {42 public TestApi() {43 super(null, null);44 }45}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.api.AbstractApiMethod;2public class ApiMethodInitMethodExample {3 public static void main(String[] args) {4 AbstractApiMethod apiMethod = new AbstractApiMethod("/api/users/2") {5 };6 apiMethod.callAPI();7 Response apiResponse = apiMethod.callAPI();8 System.out.println(apiResponse.getBody().asString());9 System.out.println(apiResponse.getHeaders());10 System.out.println(apiResponse.getStatusCode());11 System.out.println(apiResponse.getStatusLine());12 System.out.println(apiResponse.getCookies());13 }14}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful