MultiSelectorDynamicExtractor extends DynamicExtractor { @Override public String extract(AttributeDetail attributeDetail, String url, Document articleDoc) throws Exception { for(SubExtractor subEx : attributeDetail.sub_types){ AttributeDetail detailCopy = copyAttrDetail(attributeDetail); detailCopy.extractor_args = subEx.extractor_args; String result = DynamicExtractor.build(subEx.extractor_type).extract(detailCopy, url, articleDoc); if(result!=null && !StringUtils.isBlank(result)){ return result; } } return null; }  @Override String extract(AttributeDetail attributeDetail, String url, Document articleDoc); AttributeDetail copyAttrDetail(AttributeDetail attributeDetail);  }
@Test public void shouldFailIfNoSelectorsMatch() throws Exception{ Document document = createMock(Document.class); MultiSelectorDynamicExtractor extr = new MultiSelectorDynamicExtractor(); AttributeDetail attrDetail = new AttributeDetail(); attrDetail.sub_types = new ArrayList<SubExtractor>(); assertNull(extr.extract(attrDetail, null, document)); replay(document); verify(document); }
@Test public void shouldOnlyRunFirstExtractorIfItGetsAMatch() throws Exception { Document document = createMock(Document.class); Elements elements = createMock(Elements.class); Element element = createMock(Element.class); MultiSelectorDynamicExtractor extr = new MultiSelectorDynamicExtractor(); AttributeDetail attrDetail = new AttributeDetail(); SubExtractor extr1 = new SubExtractor(); SubExtractor extr2 = new SubExtractor(); extr1.extractor_type = "FirstElementTextValue"; extr1.extractor_args = Arrays.asList("selector"); extr2.extractor_type = "FirstElementTextValue"; extr2.extractor_args = Arrays.asList("selector2"); attrDetail.sub_types = Arrays.asList(extr1, extr2); expect(document.select("selector")).andReturn(elements); expect(elements.first()).andReturn(element); expect(element.text()).andReturn("notEmpty"); replay(document, elements, element); assertEquals("notEmpty",extr.extract(attrDetail, null, document)); verify(document, elements, element); }
@Test public void shouldRunSecondExtractorIfFirstDoesntMatch() throws Exception { Document document = createMock(Document.class); Elements elements = createMock(Elements.class); Element element = createMock(Element.class); Elements elements2 = createMock(Elements.class); Element element2 = createMock(Element.class); MultiSelectorDynamicExtractor extr = new MultiSelectorDynamicExtractor(); AttributeDetail attrDetail = new AttributeDetail(); SubExtractor extr1 = new SubExtractor(); SubExtractor extr2 = new SubExtractor(); extr1.extractor_type = "FirstElementTextValue"; extr1.extractor_args = Arrays.asList("selector"); extr2.extractor_type = "FirstElementTextValue"; extr2.extractor_args = Arrays.asList("selector2"); attrDetail.sub_types = Arrays.asList(extr1, extr2); expect(document.select("selector")).andReturn(elements); expect(elements.first()).andReturn(element); expect(element.text()).andReturn(""); expect(document.select("selector2")).andReturn(elements2); expect(elements2.first()).andReturn(element2); expect(element2.text()).andReturn("notEmpty"); replay(document, elements, element, elements2, element2); assertEquals("notEmpty",extr.extract(attrDetail, null, document)); verify(document, elements, element, elements2, element2); }