ExtractElementDataModifier extends AbstractDataModifierJob<String> { @Override public String modifyData(String data) throws ProcessingException { String result = StringUtils.EMPTY; Document parsedCode = Jsoup.parse(data); if (elementId != null) { result = modifyDataForElementParam(parsedCode); } if (elementClass != null) { result = modifyDataForClassParam(parsedCode); } return result; }  @Override void setParameters(Map<String, String> params); @Override String modifyData(String data); @Override String getInfo(); static final String NAME; static final String PARAM_ELEMENT_ID; static final String PARAM_ELEMENT_CLASS; }
@Test public void modifyData_whenDataWithElementIdAreDefined_returnDataWithInput() throws ProcessingException, ParametersException { configureProperlyElementId(extractElementDataModifier, "element_id"); String expectedOutput = "<input id=\"input_id\" />"; String inputData = "<div id='element_id'><form>" + expectedOutput + "</form></div>"; extractElementDataModifier.modifyData(inputData); }
@Test public void modifyData_whenDataWithOneClassAttributeIsDefined_returnDataWithThisClass() throws ProcessingException, ParametersException { configureProperlyClassAttribute(extractElementDataModifier, "class_name"); String expectedOutput = "<form class=\"class_name\"> \n <input id=\"input_id\" /> \n</form>"; String inputData = "<div>" + expectedOutput + "</div>"; extractElementDataModifier.modifyData(inputData); }
@Test public void modifyData_whenDataWithTwoClassAttributeIsDefined_returnDataForThoseTwoClasses() throws ProcessingException, ParametersException { configureProperlyClassAttribute(extractElementDataModifier, "class_name"); String expectedOutput = "<span class=\"class_name\"> Test 1 </span>\n<span class=\"class_name\"> Test 2 </span>"; String inputData = "<div>" + expectedOutput + "</div>"; String actualData = extractElementDataModifier.modifyData(inputData); assertEquals(actualData, expectedOutput); }
@Test(expected = ProcessingException.class) public void modifyData_noClassAttributeFoundInDataIsDefinedWithClassAttributeProvided_throwsException() throws ProcessingException, ParametersException { configureProperlyClassAttribute(extractElementDataModifier, "class_name"); String inputData = "<div><form class=\"different_class_name\"> \n <input id=\"input_id\" /> \n</form></div>"; String actualData = extractElementDataModifier.modifyData(inputData); assertEquals(actualData, ""); }
@Test(expected = ProcessingException.class) public void modifyData_noElementIdFoundInDataIsDefinedWithElementIdProvided_throwsException() throws ProcessingException, ParametersException { configureProperlyElementId(extractElementDataModifier, "element_id"); String inputData = "<div><form class=\"different_class_name\"> \n <input id=\"input_id\" /> \n</form></div>"; String actualData = extractElementDataModifier.modifyData(inputData); assertEquals(actualData, ""); }