Search in sources :

Example 1 with EndpointEntry

use of ru.sbtqa.tag.api.EndpointEntry in project page-factory-2 by sbtqa.

the class PlaceholderUtils method replaceJsonTemplatePlaceholders.

/**
 * Replace Json template placeholders in string on parameters
 *
 * @param jsonString replace placeholders in this json string
 * @param parameters replace these parameters
 * @return json string with replaced placeholders
 */
public static String replaceJsonTemplatePlaceholders(EndpointEntry entry, String jsonString, Map<String, Object> parameters) {
    Set<Map.Entry<String, Object>> mandatoryValues = parameters.entrySet().stream().filter(stringObjectEntry -> stringObjectEntry.getValue() != null).collect(Collectors.toSet());
    for (Map.Entry<String, Object> parameter : mandatoryValues) {
        String parameterName = parameter.getKey();
        Object parameterValue = parameter.getValue();
        if (isFieldExists(entry, parameterName)) {
            Field declaredField = FieldUtils.getAllFieldsList(entry.getClass()).stream().filter(field -> field.getName().equals(parameterName)).findFirst().orElseThrow(() -> new AutotestError("This error should never appear"));
            jsonString = replacePlaceholder(jsonString, declaredField, parameterName, parameterValue);
        } else {
            jsonString = replacePlaceholder(jsonString, null, parameterName, parameterValue);
        }
    }
    return jsonString;
}
Also used : Arrays(java.util.Arrays) ToNumberPolicy(com.google.gson.ToNumberPolicy) Iterator(java.util.Iterator) Set(java.util.Set) TypeToken(com.google.common.reflect.TypeToken) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Map(java.util.Map) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Pattern(java.util.regex.Pattern) AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Field(java.lang.reflect.Field) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) Map(java.util.Map)

Example 2 with EndpointEntry

use of ru.sbtqa.tag.api.EndpointEntry in project page-factory-2 by sbtqa.

the class EndpointManager method bootstrapEndpoint.

private static EndpointEntry bootstrapEndpoint(Class<?> entry) {
    try {
        Constructor<EndpointEntry> constructor = (Constructor<EndpointEntry>) entry.getConstructor();
        constructor.setAccessible(true);
        EndpointEntry endpoint = constructor.newInstance();
        EndpointContext.setCurrentEndpoint(endpoint);
        return endpoint;
    } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        throw new RestPluginException("Can't initialize current entry parameters", ex);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) RestPluginException(ru.sbtqa.tag.api.exception.RestPluginException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

EndpointEntry (ru.sbtqa.tag.api.EndpointEntry)2 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ToNumberPolicy (com.google.gson.ToNumberPolicy)1 String.format (java.lang.String.format)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 FieldUtils (org.apache.commons.lang3.reflect.FieldUtils)1 RestPluginException (ru.sbtqa.tag.api.exception.RestPluginException)1 AutotestError (ru.sbtqa.tag.qautils.errors.AutotestError)1