Search in sources :

Example 26 with MetacardTransformer

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

the class TestAtomTransformer method testNoCreatedDate.

@Test
public void testNoCreatedDate() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getHits()).thenReturn(new Long(1));
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setCreatedDate(null);
    result1.setMetacard(metacard);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1));
    result1.setRelevanceScore(0.3345);
    // 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);
    assertXpathNotExists("atom:feed/atom:entry/atom:published", 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) Test(org.junit.Test)

Example 27 with MetacardTransformer

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

the class TestAtomTransformer method testNoModifiedDate.

@Test
public void testNoModifiedDate() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getHits()).thenReturn(new Long(1));
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setModifiedDate(null);
    result1.setMetacard(metacard);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1));
    result1.setRelevanceScore(0.3345);
    // 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);
    assertXpathExists("atom:feed/atom:entry/atom:updated", 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) Test(org.junit.Test)

Example 28 with MetacardTransformer

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

the class TestAtomTransformer method testItemsPerPageNegativeInteger.

@Test
public void testItemsPerPageNegativeInteger() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getHits()).thenReturn(new Long(1));
    QueryImpl query = new QueryImpl(FILTER_BUILDER.attribute(Metacard.METADATA).text("you"));
    query.setPageSize(-1);
    query.setStartIndex(2);
    query.setRequestsTotalResultsCount(true);
    QueryRequestImpl queryRequestImpl = new QueryRequestImpl(query);
    when(response.getRequest()).thenReturn(queryRequestImpl);
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    result1.setMetacard(metacard);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1));
    // 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("1", "/atom:feed/os:itemsPerPage", output);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 29 with MetacardTransformer

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

the class CswTransformProvider method marshal.

/**
     * Marshals Metacards to an xml. This method is not typically be called directly, instead it is
     * called by another XStream Converter using MarshallingContext.convertAnother();
     *
     * @param o       - metacard to transform.
     * @param writer  - writes the XML.
     * @param context - the marshalling context. Should contain a map entry for {@link
     *                org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_KEY}
     *                {@link org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_VALUE}
     *                to identify which transformer to use. Also contains properties for any
     *                arguments to provide the transformer.
     */
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
    if (o == null) {
        return;
    }
    Metacard metacard = (Metacard) o;
    String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
    String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
    MetacardTransformer transformer;
    if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
        transformer = metacardTransformerManager.getTransformerByProperty(keyArg, valArg);
    } else {
        transformer = metacardTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
    }
    if (transformer == null) {
        throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
    }
    BinaryContent content;
    try {
        content = transformer.transform(metacard, getArguments(context));
    } catch (CatalogTransformerException e) {
        throw new ConversionException("Unable to transform Metacard", e);
    }
    writeXml(content, writer);
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 30 with MetacardTransformer

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

the class DumpCommand method exportMetacard.

private void exportMetacard(File dumpLocation, Metacard metacard) throws IOException, CatalogTransformerException {
    if (SERIALIZED_OBJECT_ID.matches(transformerId)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getOutputFile(dumpLocation, metacard)))) {
            oos.writeObject(new MetacardImpl(metacard));
            oos.flush();
        }
    } else {
        BinaryContent binaryContent;
        if (metacard != null) {
            try (FileOutputStream fos = new FileOutputStream(getOutputFile(dumpLocation, metacard))) {
                for (MetacardTransformer transformer : transformers) {
                    binaryContent = transformer.transform(metacard, new HashMap<>());
                    if (binaryContent != null) {
                        fos.write(binaryContent.getByteArray());
                        break;
                    }
                }
            }
        }
    }
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

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