Skip to content

Commit

Permalink
Merge pull request #44 from zebrunner/develop
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
vdelendik authored Mar 30, 2023
2 parents d0f2855 + d858f94 commit 42702ef
Show file tree
Hide file tree
Showing 9 changed files with 789 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<imgscalr-lib.version>4.2</imgscalr-lib.version>
<gson.version>2.9.0</gson.version>
<jaxb-api.version>2.2.2</jaxb-api.version>
<apache-poi.version>4.1.2</apache-poi.version>
<mokito-core.version>2.18.0</mokito-core.version>
<!-- Never add mockito-all as compile dependency as it has their own invalid hamcrest-all implementation otherwise api fail to common with:
org.hamcrest.core.IsInstanceOf.any(Ljava/lang/Class;)Lorg/hamcrest/Matcher
Expand Down Expand Up @@ -219,6 +220,12 @@
<version>${jaxb-api.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/zebrunner/carina/utils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ public enum Parameter {
ELEMENT_LOADING_STRATEGY("element_loading_strategy"),

PAGE_OPENING_STRATEGY("page_opening_strategy"),


/**
* Specifies whether to search for pages implementations in dependencies.<br>
* Project: carina-webdriver
*/
PAGE_RECURSIVE_REFLECTION("page_recursive_reflection"),

// Amazon
S3_BUCKET_NAME("s3_bucket_name"),

Expand Down
179 changes: 179 additions & 0 deletions src/main/java/com/zebrunner/carina/utils/ParameterGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright 2020-2022 Zebrunner Inc (https://www.zebrunner.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.utils;

import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.utils.commons.SpecialKeywords;
import com.zebrunner.carina.utils.exception.InvalidArgsException;
import com.zebrunner.carina.utils.parser.xls.XLSParser;
import com.zebrunner.carina.utils.resources.L10N;

public class ParameterGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Pattern GENERATE_UUID_PATTERN = Pattern.compile(SpecialKeywords.GENERATE_UUID);
private static final Pattern GENERATE_PATTERN = Pattern.compile(SpecialKeywords.GENERATE);
private static final Pattern GENERATEAN_PATTERN = Pattern.compile(SpecialKeywords.GENERATEAN);
private static final Pattern GENERATEN_PATTERN = Pattern.compile(SpecialKeywords.GENERATEN);
private static final Pattern TESTDATA_PATTERN = Pattern.compile(SpecialKeywords.TESTDATA);
private static final Pattern ENV_PATTERN = Pattern.compile(SpecialKeywords.ENV);
private static final Pattern L10N_PATTERN = Pattern.compile(SpecialKeywords.L10N_PATTERN);
private static final Pattern EXCEL_PATTERN = Pattern.compile(SpecialKeywords.EXCEL);
private static String uuid;

private ParameterGenerator() {
// do nothing
}

public static Object process(String param) {
try {
if (param == null || param.equalsIgnoreCase("nil")) {
return null;
}

Matcher matcher = GENERATE_UUID_PATTERN.matcher(param);
if (matcher.find()) {
return StringUtils.replace(param, matcher.group(), uuid);
}
matcher = GENERATE_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
int size = Integer.parseInt(param.substring(start, end));
return StringUtils.replace(param, matcher.group(), StringGenerator.generateWord(size));
}

matcher = GENERATEAN_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
int size = Integer.parseInt(param.substring(start, end));
return StringUtils.replace(param, matcher.group(), StringGenerator.generateWordAN(size));
}

matcher = GENERATEN_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
int size = Integer.parseInt(param.substring(start, end));
return StringUtils.replace(param, matcher.group(), StringGenerator.generateNumeric(size));
}

matcher = ENV_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
String key = param.substring(start, end);
return StringUtils.replace(param, matcher.group(), Configuration.getEnvArg(key));
}

matcher = TESTDATA_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
String key = param.substring(start, end);
return StringUtils.replace(param, matcher.group(), R.TESTDATA.get(key));
}

matcher = EXCEL_PATTERN.matcher(param);
if (matcher.find()) {
int start = param.indexOf(':') + 1;
int end = param.indexOf('}');
String key = param.substring(start, end);
return StringUtils.replace(param, matcher.group(), getValueFromXLS(key));
}

matcher = L10N_PATTERN.matcher(param);
String initStrL10N = param;
while (matcher.find()) {
int start = param.indexOf(SpecialKeywords.L10N + ":") + 5;
int end = param.indexOf('}');
String key = param.substring(start, end);
param = StringUtils.replace(param, matcher.group(), L10N.getText(key));
}
// in case if L10N pattern was applied
if (!initStrL10N.equalsIgnoreCase(param)) {
return param;
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return param;
}

public static void processMap(Map<String, String> paramsMap) {
paramsMap.entrySet()
.stream()
.filter(Objects::nonNull).forEach(entry -> {
String value = entry.getValue();
if (value == null)
return;
Object param = process(value);
if (param == null)
return;
String newValue = param.toString();
if (!value.equals(newValue)) {
entry.setValue(newValue);
}
});
}

private static String getValueFromXLS(String xlsSheetKey) {
if (StringUtils.isEmpty(xlsSheetKey)) {
throw new InvalidArgsException("Invalid excel key, should be 'xls_file#sheet#key'.");
}

String xls = xlsSheetKey.split("#")[0];
String sheet = xlsSheetKey.split("#")[1];
String key = xlsSheetKey.split("#")[2];

return XLSParser.parseValue(xls, sheet, key);
}

public static String getUUID() {
return uuid;
}

public static void setUUID(String uUID) {
uuid = uUID;
}

/**
* Generate hash by class name, method name and arg values.
*
* @param args Object[] test method arguments
* @param method ITestNGMethod
* @return String hash
*/
public static String hash(Object[] args, ITestNGMethod method) {
String toHash = "";
toHash += Arrays.hashCode(args);
toHash += method.getMethodName();
toHash += (method.getRealClass());
return String.valueOf(toHash.hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,25 @@ private SpecialKeywords() {
public static final String PLACEHOLER = "\\$\\{[^\\{\\}]*\\}";

public static final String MUST_OVERRIDE = "{must_override}";
/**
* @deprecated use {@link com.zebrunner.carina.utils.commons.SpecialKeywords#DS_ARGS} to set args for data provider from config file
*/
@Deprecated(forRemoval = true, since = "1.0.3")
public static final String EXCEL_DS_ARGS = "{excel_ds_args}";
/**
* @deprecated use {@link com.zebrunner.carina.utils.commons.SpecialKeywords#DS_UID} to set tuid for data provider from config file
*/
@Deprecated(forRemoval = true, since = "1.0.3")
public static final String EXCEL_DS_UID = "{excel_ds_uid}";
/**
* @deprecated unused
*/
@Deprecated(forRemoval = true, since = "1.0.3")
public static final String EXCEL_DS_FLAG = "{excel_ds_flag}";
/**
* @deprecated use {@link com.zebrunner.carina.utils.commons.SpecialKeywords#DS_FILE} to set data provider's path from config file
*/
@Deprecated(forRemoval = true, since = "1.0.3")
public static final String EXCEL_DS_FILE = "{excel_ds_file}";
public static final String EXCEL_DS_SHEET = "{excel_ds_sheet}";
@Deprecated(since = "8.0.1", forRemoval = true)
Expand All @@ -61,6 +77,10 @@ private SpecialKeywords() {
public static final String DS_CUSTOM_PROVIDER = "{ds_custom_provider}";
public static final String DS_ARGS = "{ds_args}";
public static final String DS_UID = "{ds_uid}";
/**
* @deprecated unused
*/
@Deprecated(forRemoval = true, since = "1.0.3")
public static final String DS_FLAG = "{ds_flag}";
public static final String DS_FILE = "{ds_file}";
public static final String DS_EXECUTE_COLUMN = "{ds_execute_column}";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.zebrunner.carina.utils.parser.xls;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zebrunner.carina.utils.ParameterGenerator;

public abstract class AbstractTable {

protected List<String> headers;
protected List<Map<String, String>> dataRows;
protected String executeColumn;
protected String executeValue;

protected AbstractTable() {
headers = Collections.synchronizedList(new LinkedList<String>());
dataRows = Collections.synchronizedList(new LinkedList<Map<String, String>>());
}

protected AbstractTable(String executeColumn, String executeValue) {
this();
this.executeColumn = executeColumn;
this.executeValue = executeValue;
}

public List<String> getHeaders() {
return headers;
}

public List<Map<String, String>> getDataRows() {
return dataRows;
}

public String getExecuteColumn() {
return executeColumn;
}

public void setExecuteColumn(String executeColumn) {
this.executeColumn = executeColumn;
}

public String getExecuteValue() {
return executeValue;
}

public void setExecuteValue(String executeValue) {
this.executeValue = executeValue;
}

public void setHeaders(Collection<String> row) {
headers.clear();
headers.addAll(row);
}

public abstract void addDataRow(List<String> row);

public void processTable() {
for (Map<String, String> row : dataRows) {
ParameterGenerator.processMap(row);
}
}

public List<List<Map<String, String>>> getGroupedDataProviderMap(String fieldName) {
// add unique group values
Set<String> groupValues = new LinkedHashSet<>();
for (Map<String, String> item : dataRows) {
String value = item.get(fieldName);
groupValues.add(value);
}

// group maps into lists, that has the same unique group value
List<List<Map<String, String>>> groupedList = new ArrayList<>();
for (String groupBy : groupValues) {
List<Map<String, String>> groupOfRows = new ArrayList<>();
for (Map<String, String> item : dataRows) {
String value = item.get(fieldName);
if (value.equals(groupBy)) {
groupOfRows.add(item);
}
}
groupedList.add(groupOfRows);
}

return groupedList;
}
}
Loading

0 comments on commit 42702ef

Please sign in to comment.