Search in sources :

Example 31 with InputTransformer

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

the class CatalogServiceImplTest method mcardIdTest.

private String mcardIdTest(Metacard metacard, UuidGenerator uuidGenerator) throws Exception {
    CatalogFramework framework = mock(CatalogFramework.class);
    when(framework.create(isA(CreateStorageRequest.class))).thenAnswer(args -> {
        ContentItem item = ((CreateStorageRequest) args.getArguments()[0]).getContentItems().get(0);
        item.getMetacard().setAttribute(new AttributeImpl(Core.ID, item.getId()));
        return new CreateResponseImpl(null, new HashMap<>(), Collections.singletonList(item.getMetacard()));
    });
    HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    when(inputTransformer.transform(any())).thenReturn(metacard);
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    String generatedMcardId = UUID.randomUUID().toString();
    when(uuidGenerator.generateUuid()).thenReturn(generatedMcardId);
    catalogService.setUuidGenerator(uuidGenerator);
    when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
    addMatchingService(catalogService, Collections.singletonList(inputTransformer));
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
    attachments.add(attachment);
    ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
    Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
    attachments.add(attachment1);
    MultipartBody multipartBody = new MultipartBody(attachments);
    return catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) ContentItem(ddf.catalog.content.data.ContentItem) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl) BundleContext(org.osgi.framework.BundleContext)

Example 32 with InputTransformer

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

the class IntegrationTest method testInputAndOutput.

@Test
public void testInputAndOutput() throws CatalogTransformerException, IOException {
    Parser parser = new XmlParser();
    InputTransformer inputTransformer = new XmlInputTransformer(parser);
    MetacardMarshaller metacardMarshaller = new MetacardMarshallerImpl(parser, new PrintWriterProviderImpl());
    MetacardTransformer outputTransformer = new XmlMetacardTransformer(metacardMarshaller);
    InputStream input = getClass().getResourceAsStream("/extensibleMetacard.xml");
    Metacard metacard = inputTransformer.transform(input);
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
    BinaryContent output = outputTransformer.transform(metacard, mockArguments);
    String outputString = new String(output.getByteArray());
    // TODO test equivalence with XMLUnit.
    LOGGER.info(outputString);
}
Also used : XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) Attribute(ddf.catalog.data.Attribute) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) InputStream(java.io.InputStream) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) InputTransformer(ddf.catalog.transform.InputTransformer) BinaryContent(ddf.catalog.data.BinaryContent) Parser(org.codice.ddf.parser.Parser) XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardMarshallerImpl(ddf.catalog.transformer.xml.MetacardMarshallerImpl) Metacard(ddf.catalog.data.Metacard) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) Test(org.junit.Test)

Example 33 with InputTransformer

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

the class GeoJsonInputTransformerTest method testGuessTransformer.

@Test
public void testGuessTransformer() throws IOException, CatalogTransformerException {
    SortedServiceList mockSortedServiceList = mock(SortedServiceList.class);
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputTransformer.transform(mock(InputStream.class))).thenThrow(new CatalogTransformerException());
    when(mockSortedServiceList.stream()).thenReturn(Stream.of(mockInputTransformer));
    transformer.setInputTransformers(mockSortedServiceList);
    Metacard metacard = transformer.transform(new ByteArrayInputStream(samplePointJsonText().getBytes()));
    ArgumentCaptor<ByteArrayInputStream> inputStreamCaptor = ArgumentCaptor.forClass(ByteArrayInputStream.class);
    verify(mockSortedServiceList).stream();
    verify(mockInputTransformer).transform(inputStreamCaptor.capture());
    assertThat(metacard.getMetacardType().getName(), is("ddf.metacard"));
    assertThat(metacard.getTitle(), is(DEFAULT_TITLE));
}
Also used : Metacard(ddf.catalog.data.Metacard) SortedServiceList(org.codice.ddf.platform.util.SortedServiceList) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) InputTransformer(ddf.catalog.transform.InputTransformer) Test(org.junit.Test)

Example 34 with InputTransformer

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

the class TestCswTransformProvider method testUnmarshalMissingNamespaces.

@Test
public void testUnmarshalMissingNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    Map<String, String> namespaces = new HashMap<>();
    namespaces.put("xmlns:csw", "http://www.opengis.net/cat/csw/2.0.2");
    namespaces.put("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    namespaces.put("xmlns:dct", "http://purl.org/dc/terms/");
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecordMissingNamespaces()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
    context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, OTHER_SCHEMA);
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputTransformer, times(1)).transform(captor.capture());
    InputStream inStream = captor.getValue();
    String result = IOUtils.toString(inStream);
    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(getRecord(), result);
}
Also used : HashMap(java.util.HashMap) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 35 with InputTransformer

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

the class TestCswTransformProvider method testUnmarshalCopyPreservesNamespaces.

@Test
public void testUnmarshalCopyPreservesNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    StaxDriver driver = new StaxDriver();
    driver.setRepairingNamespace(true);
    driver.getQnameMap().setDefaultNamespace(CswConstants.CSW_OUTPUT_SCHEMA);
    driver.getQnameMap().setDefaultPrefix(CswConstants.CSW_NAMESPACE_PREFIX);
    // Have to use XppReader in order to preserve the namespaces.
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecord()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, "http://example.com/schema");
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputTransformer, times(1)).transform(captor.capture());
    InputStream inStream = captor.getValue();
    String result = IOUtils.toString(inStream);
    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(getRecord(), result);
}
Also used : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Aggregations

InputTransformer (ddf.catalog.transform.InputTransformer)56 InputStream (java.io.InputStream)36 Metacard (ddf.catalog.data.Metacard)29 Test (org.junit.Test)27 ByteArrayInputStream (java.io.ByteArrayInputStream)24 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)21 ArrayList (java.util.ArrayList)20 IOException (java.io.IOException)17 ServiceReference (org.osgi.framework.ServiceReference)17 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)16 BundleContext (org.osgi.framework.BundleContext)14 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 CatalogFramework (ddf.catalog.CatalogFramework)9 MimeType (javax.activation.MimeType)9 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)8 Result (ddf.catalog.data.Result)7 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)6 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)6 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6