Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #31 from sbtqa/api-url-param
Browse files Browse the repository at this point in the history
Add ApiUrlParam annotation
  • Loading branch information
kosteman authored Mar 1, 2018
2 parents 71e2476 + 51cd23d commit ae41519
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/java/ru/sbtqa/tag/apifactory/ApiEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void setParamValueByTitle(String title, String value) throws ApiException
List<Field> fieldList = FieldUtilsExt.getDeclaredFieldsWithInheritance(this.getClass());
for (Field field : fieldList) {
for (Annotation annotation : field.getAnnotations()) {
if (annotation instanceof ApiRequestParam
&& ((ApiRequestParam) annotation).title().equals(title)
if (((annotation instanceof ApiRequestParam && ((ApiRequestParam) annotation).title().equals(title))
|| (annotation instanceof ApiUrlParam && ((ApiUrlParam) annotation).title().equals(title)))
&& value != null && !value.isEmpty()) {
field.setAccessible(true);
try {
Expand Down Expand Up @@ -159,6 +159,8 @@ public Object fire(String url) throws ApiException {
HTTP requestMethod = this.getClass().getAnnotation(ApiAction.class).method();
String templateName = this.getClass().getAnnotation(ApiAction.class).template();

url = getFullUrl(url);

Bullet response = null;
Bullet request = null;
Class restImpl = ApiFactory.getApiFactory().getRest();
Expand Down Expand Up @@ -362,6 +364,43 @@ private void setHeaders() throws ApiException {
}
}

/**
* Get url with param if param field exist
*
* @return partUrl withParams
* @throws ru.sbtqa.tag.apifactory.exception.ApiException
*/
private String getFullUrl(String url) throws ApiException {
List<Field> fieldList = FieldUtilsExt.getDeclaredFieldsWithInheritance(this.getClass());
String urlParamString = "";
for (Field field : fieldList) {
ApiUrlParam urlParam = field.getAnnotation(ApiUrlParam.class);
if (urlParam != null && !"".equals(urlParam.title())) {
field.setAccessible(true);
try {
String param = (String) field.get(this);
if (param != null && !param.equals("")) {
if (!"".equals(urlParamString)) {
urlParamString += "&";
}
urlParamString += urlParam.title() + "=" + param;
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
throw new ApiEntryInitializationException("Parameter with title '" + urlParam.title()+ "' is not available", ex);
}
}
}

if (!urlParamString.equals("")) {
if (url.contains("?")) {
url += "&" + urlParamString;
} else {
url += "?" + urlParamString;
}
}
return url;
}

/**
* Get request body. Get request body template from resources
*
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ru/sbtqa/tag/apifactory/annotation/ApiUrlParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.sbtqa.tag.apifactory.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ApiUrlParam {

/**
* By this title parameter will be searched by framework
*
* @return a {@link java.lang.String} object.
*/
public String title();
}

0 comments on commit ae41519

Please sign in to comment.