Search in sources :

Example 21 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class TestCswQueryResponseTransformer method testMarshalAcknowledgementWithFailedTransforms.

@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    GetRecordsType query = new GetRecordsType();
    query.setResultType(ResultType.RESULTS);
    query.setMaxRecords(BigInteger.valueOf(6));
    query.setStartPosition(BigInteger.valueOf(0));
    SourceResponse sourceResponse = createSourceResponse(query, 6);
    Map<String, Serializable> args = new HashMap<>();
    args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
    args.put(CswConstants.GET_RECORDS, query);
    PrintWriter printWriter = getSimplePrintWriter();
    MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
    final AtomicLong atomicLong = new AtomicLong(0);
    when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
        if (atomicLong.incrementAndGet() == 2) {
            throw new CatalogTransformerException("");
        }
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
        return bci;
    });
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
    cswQueryResponseTransformer.init();
    BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
    cswQueryResponseTransformer.destroy();
    String xml = new String(content.getByteArray());
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
    assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Also used : Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType) AtomicLong(java.util.concurrent.atomic.AtomicLong) Metacard(ddf.catalog.data.Metacard) PrintWriter(ddf.catalog.transformer.api.PrintWriter) Test(org.junit.Test)

Example 22 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class TestAtomTransformer method testSimple.

@Test
public void testSimple() throws CatalogTransformerException, IOException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    Action viewAction = mock(Action.class);
    when(viewAction.getUrl()).thenReturn(new URL("http://host:80/" + SAMPLE_ID));
    ActionProvider viewActionProvider = mock(ActionProvider.class);
    when(viewActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    ActionProvider resourceActionProvider = mock(ActionProvider.class);
    when(resourceActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    ActionProvider thumbnailActionProvider = mock(ActionProvider.class);
    when(thumbnailActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    AtomTransformer transformer = new AtomTransformer();
    transformer.setViewMetacardActionProvider(viewActionProvider);
    transformer.setResourceActionProvider(resourceActionProvider);
    transformer.setMetacardTransformer(metacardTransformer);
    transformer.setThumbnailActionProvider(thumbnailActionProvider);
    setDefaultSystemConfiguration();
    SourceResponse response1 = mock(SourceResponse.class);
    when(response1.getHits()).thenReturn(new Long(1));
    when(response1.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setSourceId(SAMPLE_SOURCE_ID);
    metacard.setCreatedDate(SAMPLE_DATE_TIME.toDate());
    metacard.setModifiedDate(SAMPLE_DATE_TIME.toDate());
    result1.setMetacard(metacard);
    when(response1.getResults()).thenReturn(Arrays.asList((Result) result1));
    SourceResponse response = response1;
    Double relevanceScore = 0.3345;
    result1.setRelevanceScore(relevanceScore);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    /* feed */
    assertBasicFeedInfo(output, "1");
    /* entry */
    assertXpathEvaluatesTo(SAMPLE_SOURCE_ID, "/atom:feed/atom:entry/fs:resultSource/@fs:sourceId", output);
    assertXpathEvaluatesTo("", "/atom:feed/atom:entry/fs:resultSource", output);
    assertXpathEvaluatesTo(AtomTransformer.URN_CATALOG_ID + SAMPLE_ID, "/atom:feed/atom:entry/atom:id", output);
    assertXpathEvaluatesTo(MetacardStub.DEFAULT_TITLE, "/atom:feed/atom:entry/atom:title", output);
    assertXpathEvaluatesTo(Double.toString(relevanceScore), "/atom:feed/atom:entry/relevance:score", output);
    assertXpathExists("/atom:feed/atom:entry/atom:content", output);
    assertXpathEvaluatesTo(atomDateFormat.format(SAMPLE_DATE_TIME.toDate()), "/atom:feed/atom:entry/atom:published", output);
    assertXpathEvaluatesTo(atomDateFormat.format(SAMPLE_DATE_TIME.toDate()), "/atom:feed/atom:entry/atom:updated", output);
    assertXpathEvaluatesTo("application/xml", "/atom:feed/atom:entry/atom:content/@type", output);
    assertXpathEvaluatesTo(MetacardStub.DEFAULT_TYPE, "/atom:feed/atom:entry/atom:category/@term", output);
    assertXpathEvaluatesTo("1", "count(/atom:feed/atom:entry/georss:where)", output);
    assertXpathEvaluatesTo("1", "count(/atom:feed/atom:entry/georss:where/gml:Point)", output);
    assertXpathEvaluatesTo("56.3 13.3", "/atom:feed/atom:entry/georss:where/gml:Point", output);
    assertXpathEvaluatesTo("3", "count(/atom:feed/atom:entry/atom:link)", output);
    assertXpathExists("/atom:feed/atom:entry/atom:link[@rel='alternate']", output);
    assertXpathExists("/atom:feed/atom:entry/atom:link[@rel='related']", output);
    assertXpathEvaluatesTo("http://host:80/" + SAMPLE_ID, "/atom:feed/atom:entry/atom:link/@href", output);
}
Also used : ActionProvider(ddf.action.ActionProvider) Action(ddf.action.Action) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) URL(java.net.URL) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 23 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class TestAtomTransformer method testEntryElementsComplyToAtomSpecification.

/**
     * The following rules must be followed in order to be compliant with the Atom specification as
     * defined by http://tools.ietf.org/html/rfc4287#section-4.1.2 <br/>
     * "The following child elements are defined by this specification (note that the presence of
     * some of these elements is required):
     * <p>
     * <li/>atom:entry elements MUST contain one or more atom:author elements, unless the atom:entry
     * contains an atom:source element that contains an atom:author element or, in an Atom Feed
     * Document, the atom:feed element contains an atom:author element itself.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:category elements.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:content element.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:contributor elements.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:id element.
     * <p>
     * <li/>atom:entry elements that contain no child atom:content element MUST contain at least one
     * atom:link element with a rel attribute value of "alternate".
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:link element with a rel
     * attribute value of "alternate" that has the same combination of type and hreflang attribute
     * values.
     * <p>
     * <li/>atom:entry elements MAY contain additional atom:link elements beyond those described
     * above.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:published element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:rights element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:source element.
     * <p>
     * <li/>atom:entry elements MUST contain an atom:summary element in either of the following
     * cases:
     * <p>
     * <ul>
     * the atom:entry contains an atom:content that has a "src" attribute (and is thus empty).
     * </ul>
     * <p>
     * <ul>
     * the atom:entry contains content that is encoded in Base64; i.e., the "type" attribute of
     * atom:content is a MIME media type [MIMEREG], but is not an XML media type [RFC3023], does not
     * begin with "text/", and does not end with "/xml" or "+xml".
     * </ul>
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:summary element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:title element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:updated element."
     *
     * @throws CatalogTransformerException
     * @throws IOException
     * @throws SAXException
     * @throws XpathException
     */
@Test
public void testEntryElementsComplyToAtomSpecification() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    ResultImpl result2 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    MetacardStub metacard2 = new MetacardStub("");
    metacard2.setId(SAMPLE_ID + 1);
    result1.setMetacard(metacard);
    result2.setMetacard(metacard2);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1, result2));
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertEntryCompliant(output);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) FileOutputStream(java.io.FileOutputStream) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) File(java.io.File) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 24 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class TestAtomTransformer method testMetacardTransformerBytesNull.

@Test
public void testMetacardTransformerBytesNull() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = mock(MetacardTransformer.class);
    BinaryContent metacardTransformation = mock(BinaryContent.class);
    when(metacardTransformation.getByteArray()).thenReturn(null);
    when(metacardTransformer.transform(isA(Metacard.class), isNull(Map.class))).thenReturn(metacardTransformation);
    AtomTransformer transformer = getConfiguredAtomTransformer(metacardTransformer, true);
    SourceResponse response = getSourceResponseStub(SAMPLE_ID, null);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    assertXpathEvaluatesTo(TEXT_TYPE, "/atom:feed/atom:entry/atom:content/@type", output);
    assertXpathEvaluatesTo(SAMPLE_ID, "/atom:feed/atom:entry/atom:content", output);
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) BinaryContent(ddf.catalog.data.BinaryContent) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 25 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class TestAtomTransformer method testNoSiteName.

@Test
public void testNoSiteName() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    AtomTransformer transformer = getConfiguredAtomTransformer(metacardTransformer, true);
    SourceResponse response1 = mock(SourceResponse.class);
    when(response1.getHits()).thenReturn(new Long(1));
    when(response1.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setSourceId(null);
    result1.setMetacard(metacard);
    when(response1.getResults()).thenReturn(Arrays.asList((Result) result1));
    SourceResponse response = response1;
    Double relevanceScore = 0.3345;
    result1.setRelevanceScore(relevanceScore);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    assertXpathEvaluatesTo(AtomTransformer.DEFAULT_SOURCE_ID, "/atom:feed/atom:entry/fs:resultSource/@fs:sourceId", output);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

MetacardTransformer (ddf.catalog.transform.MetacardTransformer)30 BinaryContent (ddf.catalog.data.BinaryContent)26 Test (org.junit.Test)21 SourceResponse (ddf.catalog.operation.SourceResponse)19 Metacard (ddf.catalog.data.Metacard)16 Result (ddf.catalog.data.Result)12 HashMap (java.util.HashMap)12 ResultImpl (ddf.catalog.data.impl.ResultImpl)10 FileOutputStream (java.io.FileOutputStream)9 Map (java.util.Map)9 File (java.io.File)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)4 ServiceReference (org.osgi.framework.ServiceReference)4 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 Serializable (java.io.Serializable)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Action (ddf.action.Action)2 ActionProvider (ddf.action.ActionProvider)2 CatalogFramework (ddf.catalog.CatalogFramework)2