package tools; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.ArrayUtils; //import org.apache.commons.lang3.text.WordUtils; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.openqa.selenium.WebDriver; import com.google.common.base.Charsets; import com.google.common.io.Files; public class DynamicDataProvider extends CustomDataProvider { WebDriver driver; private Map>> dataSet; @SuppressWarnings("unchecked") public DynamicDataProvider(String JsonFileName){ String content = null; try { content = Files.toString(new File(System.getProperty("user.dir")+"/json-dataprovider/"+JsonFileName), Charsets.UTF_8); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //dataSet = null; try { dataSet = new ObjectMapper().readValue(content, HashMap.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unchecked") public void assignParrentField(String name, String value, String type, String baseValue){ assignFieldData(name, value, type); if(dataSet.get(name).get("options").containsKey("dependentFields")){ if(((Map>>) dataSet.get(name).get("options").get("dependentFields")).containsKey(value)){ Map>>dependentFields = ((Map>>>) dataSet.get(name).get("options").get("dependentFields")).get(value); for(Entry>> entry : ((Map>>) dependentFields).entrySet()){ assignFieldData(entry.getKey(), baseValue.isEmpty()?"":entry.getValue().get("options").get(baseValue).toString(), entry.getValue().get("options").get("type").toString(), name); } } } } @SuppressWarnings("unchecked") public void assignParrentFieldRequiredOnly(String name, String value, String type){ assignFieldData(name, value, type); if(dataSet.get(name).get("options").containsKey("dependentFields")){ if(((Map>>) dataSet.get(name).get("options").get("dependentFields")).containsKey(value)){ Map>>dependentFields = ((Map>>>) dataSet.get(name).get("options").get("dependentFields")).get(value); for(Entry>> entry : ((Map>>) dependentFields).entrySet()){ if (entry.getValue().get("validation").containsKey("isRequired")&&entry.getValue().get("validation").get("isRequired").toString().equals("true")){ assignFieldData(entry.getKey(), entry.getValue().get("options").get("defaultValue").toString(), entry.getValue().get("options").get("type").toString(), name); } } } } } public List getValidationFields(String validator){ List requiredFields = new ArrayList(); for(Entry>> entry : dataSet.entrySet()) { String fieldName = entry.getKey(); Map> fieldOptions = entry.getValue(); Map fieldValidations = fieldOptions.get("validation"); if(fieldValidations.containsKey(validator)&&(fieldValidations.get(validator).equals("true"))){ requiredFields.add(fieldName); } } return requiredFields; } public List getValidationFields(String validator, Map>>innerDataSet){ List requiredFields = new ArrayList(); for(Entry>> entry : innerDataSet.entrySet()) { String fieldName = entry.getKey(); Map> fieldOptions = entry.getValue(); Map fieldValidations = fieldOptions.get("validation"); if(fieldValidations.containsKey(validator)&&(fieldValidations.get(validator).equals("true"))){ requiredFields.add(fieldName); } } return requiredFields; } public Object[][]GenerateTreeRecord(String baseValue){ for(Entry>> entry : dataSet.entrySet()){ assignParrentField(entry.getKey(), baseValue.isEmpty()?"":entry.getValue().get("options").get(baseValue).toString(), entry.getValue().get("options").get("type").toString(), baseValue); } generateDataProviderSingle(); assignDataProviderSingle(); return returnDataprovider(); } public Map> GenerateClearForm(){ for(Entry>> entry : dataSet.entrySet()){ assignParrentField(entry.getKey(), "", entry.getValue().get("options").get("type").toString(), ""); } Map> result = new HashMap>(fieldsData); fieldsData.clear(); return result; } public Object[][] GenerateRequiredCheckEdit(){ ListrequiredFields = getValidationFields("isRequired"); if(!requiredFields.isEmpty()){ for(String requiredField : requiredFields){ assignParrentFieldRequiredOnly(requiredField, "", dataSet.get(requiredField).get("options").get("type").toString()); assignExpectedErrors(requiredField, Arrays.asList(validationErrorPatterns.get("EmptyField"))); generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } return returnDataprovider(); } public Object[][] GenerateValidationCheckEdit(String validation, Map>validationValues){ ListvalidationFields = getValidationFields(validation); if(!validationFields.isEmpty()){ for(Entry> entry : validationValues.entrySet()){ for(String validationField : validationFields){ assignFieldData(validationField, entry.getKey().toString(), dataSet.get(validationField).get("options").get("type").toString()); assignExpectedErrors(validationField, entry.getValue()); generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } } return returnDataprovider(); } @SuppressWarnings("unchecked") public Object[][] GenerateRequiredCheck(){ ListrequiredFields = getValidationFields("isRequired"); if(!requiredFields.isEmpty()){ for(String requiredField : requiredFields){ for(String requiredField_fill : requiredFields){ if(requiredField_fill.equals(requiredField)){ assignParrentFieldRequiredOnly(requiredField_fill, "", dataSet.get(requiredField_fill).get("options").get("type").toString()); if(requiredField_fill.equals("confirmPassword")){ assignExpectedErrors("password", Arrays.asList(validationErrorPatterns.get("ConfirmPassword"))); } //assignFieldData(requiredField_fill, "", dataSet.get(requiredField_fill).get("options").get("type").toString()); assignExpectedErrors(requiredField_fill, Arrays.asList(validationErrorPatterns.get("EmptyField"))); } else{ String defaultValue = dataSet.get(requiredField_fill).get("options").get("defaultValue").toString(); String type = dataSet.get(requiredField_fill).get("options").get("type").toString(); assignParrentFieldRequiredOnly(requiredField_fill, defaultValue, type); } } generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } //Initializing checking inside dependent fields for(Entry>> entry : dataSet.entrySet()) { if(entry.getValue().get("options").containsKey("dependentFields")){ for(Entry>>>dependentValuesEntry : ((Map>>>) entry.getValue().get("options").get("dependentFields")).entrySet()){ List innerRequiredFields = getValidationFields("isRequired" ,dependentValuesEntry.getValue()); for(String innerRequiredField : innerRequiredFields){ for(String requiredField : requiredFields){ assignParrentFieldRequiredOnly(requiredField, dataSet.get(requiredField).get("options").get("defaultValue").toString(), dataSet.get(requiredField).get("options").get("type").toString()); } removeFieldDataByDependentField(entry.getKey()); for(Entry>>dependentFieldsEntry : dependentValuesEntry.getValue().entrySet()){ if(dependentFieldsEntry.getValue().get("validation").containsKey("isRequired") && dependentFieldsEntry.getValue().get("validation").get("isRequired").toString().equals("true")){ if(dependentFieldsEntry.getKey().equals(innerRequiredField)){ assignFieldData(entry.getKey(), dependentValuesEntry.getKey(), entry.getValue().get("options").get("type").toString()); assignFieldData(dependentFieldsEntry.getKey(), "", dependentFieldsEntry.getValue().get("options").get("type").toString(), entry.getKey()); assignExpectedErrors(dependentFieldsEntry.getKey(), Arrays.asList(validationErrorPatterns.get("EmptyField"))); } else{ assignFieldData(entry.getKey(), dependentValuesEntry.getKey(), entry.getValue().get("options").get("type").toString()); assignFieldData(dependentFieldsEntry.getKey(), dependentFieldsEntry.getValue().get("options").get("defaultValue").toString(), dependentFieldsEntry.getValue().get("options").get("type").toString(), entry.getKey()); } } } generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } } } return returnDataprovider(); } @SuppressWarnings("unchecked") public Object[][] GenerateValidationCheck(String validation, Map>validationValues){ ListvalidationFields = getValidationFields(validation); ListrequiredFields = getValidationFields("isRequired"); if(!validationFields.isEmpty()){ for(Entry> entry : validationValues.entrySet()){ for(String validationField : validationFields){ for(String requiredField : requiredFields){ assignParrentFieldRequiredOnly(requiredField, dataSet.get(requiredField).get("options").get("defaultValue").toString(), dataSet.get(requiredField).get("options").get("type").toString()); } assignFieldData(validationField, entry.getKey().toString(), dataSet.get(validationField).get("options").get("type").toString()); assignExpectedErrors(validationField, entry.getValue()); generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } } for(Entry>> entry : dataSet.entrySet()) { if(entry.getValue().get("options").containsKey("dependentFields")){ for(Entry>>>dependentValuesEntry : ((Map>>>) entry.getValue().get("options").get("dependentFields")).entrySet()){ List innerValidationFields = getValidationFields(validation, dependentValuesEntry.getValue()); if(!innerValidationFields.isEmpty()){ for(Entry> invalidValidationValues : validationValues.entrySet()){ for(String requiredField : requiredFields){ assignParrentFieldRequiredOnly(requiredField, dataSet.get(requiredField).get("options").get("defaultValue").toString(), dataSet.get(requiredField).get("options").get("type").toString()); } removeFieldDataByDependentField(entry.getKey()); assignParrentFieldRequiredOnly(entry.getKey().toString(), dependentValuesEntry.getKey().toString(), entry.getValue().get("options").get("type").toString()); for(String innerValidationField : innerValidationFields){ assignFieldData(innerValidationField, invalidValidationValues.getKey().toString(), dependentValuesEntry.getValue().get(innerValidationField).get("options").get("type").toString(), entry.getKey()); assignExpectedErrors(innerValidationField, invalidValidationValues.getValue()); } generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } } } } } return returnDataprovider(); } public Object[][] GenerateUniqueCheck(){ ListuniqueFields = getValidationFields("isUnique"); ListrequiredFields = getValidationFields("isRequired"); for(String uniqueField : uniqueFields){ for(String requiredField : requiredFields){ assignParrentFieldRequiredOnly(requiredField, dataSet.get(requiredField).get("options").get("defaultValue").toString(), dataSet.get(requiredField).get("options").get("type").toString()); } assignFieldData(uniqueField, dataSet.get(uniqueField).get("options").get("existingValue").toString(), dataSet.get(uniqueField).get("options").get("type").toString()); String errorText = validationErrorPatterns.get("Unique"); String label = dataSet.get(uniqueField).get("options").get("label").toString(); errorText = errorText.replace("%fieldName%", label); assignExpectedErrors(uniqueField, Arrays.asList(errorText)); generateDataProviderSingleWithErrors(); assignDataProviderSingle(); } return returnDataprovider(); } public Object[][] GenerateFullEditCheck(){ return ArrayUtils.addAll( ArrayUtils.addAll( GenerateRequiredCheckEdit(), GenerateValidationCheckEdit("isMultiEmail", multiEmailValidationValues))); } public Object[][] GenerateFullCheck(){ return ArrayUtils.addAll( ArrayUtils.addAll( ArrayUtils.addAll( ArrayUtils.addAll( ArrayUtils.addAll( ArrayUtils.addAll( GenerateRequiredCheck(), GenerateValidationCheck("isImage", imageValidationValues)), GenerateValidationCheck("isPdf", pdfValidationValues)), GenerateValidationCheck("isEmail", emailValidationValues)), GenerateUniqueCheck()), GenerateValidationCheck("isUrl", urlValidationValues)), GenerateValidationCheck("isDate", dateValidationValues)); } }