package tools; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.ArrayUtils; public class CustomDataProvider extends ValueErrorsProvider{ public Object[][] dataProvider = new Object[][]{}; public Object[] dataProviderSingle = new Object[]{}; public Map> fieldsData = new HashMap>(); public Map> expectedErrors = new HashMap>(); public Map fieldData = new HashMap(); public CustomDataProvider(){ /*// write JSON to a file try { mapper.writeValue(new File(System.getProperty("user.dir")+"/json-dataprovider/errors/emailErrors.json"), emailValidationValues); } catch (JsonGenerationException 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(); } */ } public void assignFieldData(String name, String value, String type){ fieldData.put("value", value); fieldData.put("type", type); fieldsData.put(name, new HashMap(fieldData)); clearFieldData(); } public void assignFieldData(String name, String value, String type, String dependsOn){ fieldData.put("value", value); fieldData.put("type", type); fieldData.put("dependsOn", dependsOn); fieldsData.put(name, new HashMap(fieldData)); clearFieldData(); } public void removeFieldDataByDependentField(String dependsOn){ Iterator>> it = fieldsData.entrySet().iterator(); while (it.hasNext()) { Entry> entry = it.next(); if(entry.getValue().containsKey("dependsOn")){ if(entry.getValue().get("dependsOn").equals(dependsOn)){ it.remove(); } } } } public void assignExpectedErrors(String name, ListfieldErrors){ expectedErrors.put(name, new ArrayList(fieldErrors)); } public void generateDataProviderSingleWithErrors(){ dataProviderSingle = ArrayUtils.add(dataProviderSingle, new HashMap>(fieldsData)); dataProviderSingle = ArrayUtils.add(dataProviderSingle, new HashMap>(expectedErrors)); clearFieldsData(); clearExpectedErrors(); } public void assignDataProviderSingle(){ dataProvider = ArrayUtils.add(dataProvider, dataProviderSingle); this.dataProviderSingle = new Object[]{}; } public Object[][] returnDataprovider(){ Object[][]result = this.dataProvider; clearFieldsData(); clearFieldData(); clearExpectedErrors(); this.dataProvider = new Object[][]{}; return result; } public void generateDataProviderSingle(){ dataProviderSingle = ArrayUtils.add(dataProviderSingle, new HashMap>(fieldsData)); } public void clearFieldData(){ this.fieldData.clear(); } public void clearFieldsData(){ this.fieldsData.clear(); } public void clearExpectedErrors(){ this.expectedErrors.clear(); } }