package tools; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import com.google.common.base.Charsets; import com.google.common.io.Files; public class ValueErrorsProvider { public Map> emailValidationValues = new HashMap>(); public Map> multiEmailValidationValues = new HashMap>(); public Map> urlValidationValues = new HashMap>(); public Map> dateValidationValues = new HashMap>(); public Map> imageValidationValues = new HashMap>(); public Map> pdfValidationValues = new HashMap>(); public Map validationErrorPatterns = new HashMap(); private Map> errorsDataSet; //private Map validationErrorPatterns; @SuppressWarnings("unchecked") public ValueErrorsProvider(){ String content = null; try { content = Files.toString(new File(System.getProperty("user.dir")+"/json-dataprovider/errors/errorPatterns.json"), Charsets.UTF_8); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { validationErrorPatterns = 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(); } getResource("multiEmailErrors.json"); multiEmailValidationValues = generateIncorectValuesProvider(); getResource("emailErrors.json"); emailValidationValues = generateIncorectValuesProvider(); getResource("urlErrors.json"); urlValidationValues = generateIncorectValuesProvider(); getResource("dateErrors.json"); dateValidationValues = generateIncorectValuesProvider(); getResource("imageErrors.json"); imageValidationValues = generateIncorectValuesProvider(); getResource("pdfErrors.json"); pdfValidationValues = generateIncorectValuesProvider(); } @SuppressWarnings("unchecked") public void getResource(String JsonFileName){ String content = null; try { content = Files.toString(new File(System.getProperty("user.dir")+"/json-dataprovider/errors/"+JsonFileName), Charsets.UTF_8); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { errorsDataSet = 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 Map> generateIncorectValuesProvider(){ Map> validationValues = new HashMap>(); List errors = new ArrayList(); for(Entry> entry : errorsDataSet.entrySet()){ if(entry.getValue().containsKey("errors")){ errors.clear(); for(String error : ((List)entry.getValue().get("errors"))){ if(validationErrorPatterns.containsKey(error)){ String errorText = validationErrorPatterns.get(error); if(entry.getValue().containsKey("replace")){ for(Entry replace : ((Map)entry.getValue().get("replace")).entrySet()){ errorText = errorText.replace("%"+replace.getKey()+"%", replace.getValue()); } } errors.add(errorText); } else{ throw new IllegalStateException("Invalid Error Code: "+error); } } validationValues.put(entry.getKey(), new ArrayList(errors)); } } return validationValues; } }