Search in sources :

Example 1 with MimeTypeToTransformerMapper

use of ddf.mime.MimeTypeToTransformerMapper in project ddf by codice.

the class CatalogComponentTest method testTransformMetacardNoProducerInputTransformerRegistered.

@Test
public void testTransformMetacardNoProducerInputTransformerRegistered() throws Exception {
    LOGGER.debug("Running testTransformMetacard()");
    // Mock the MimeTypeToTransformerMapper and register it in the OSGi
    // Registry (PojoSR)
    MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
    // bundleContext.registerService(
    // MimeTypeToTransformerMapper.class.getName(), matchingService, null );
    catalogComponent.setMimeTypeToTransformerMapper(matchingService);
    // Mock the MimeTypeToTransformerMapper returning empty list of
    // InputTransformers
    List list = new ArrayList<InputTransformer>();
    when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn(list);
    // Send in sample XML as InputStream to InputTransformer
    InputStream input = IOUtils.toInputStream(xmlInput);
    // Get the InputTransformer registered with the ID associated with the
    // <from> node in the Camel route
    InputTransformer transformer = getTransformer("text/xml", "identity");
    assertNotNull("InputTransformer for text/xml;id=identity not found", transformer);
    // Attempt to transform the XML input into a Metacard
    try {
        transformer.transform(input);
        fail("Should have thrown a CatalogTransformerException");
    } catch (CatalogTransformerException e) {
        assertEquals("Did not find an InputTransformer for MIME Type [text/xml] and id [xml]", e.getMessage());
    }
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 2 with MimeTypeToTransformerMapper

use of ddf.mime.MimeTypeToTransformerMapper in project ddf by codice.

the class TestMimeTypeToTransformerMapperImpl method testNoMatchInputTransformerBaseTypeAndId.

/**
     * Testing a negative case where the mimetypes don't match.
     *
     * Tests if
     * <p>
     * InputTransformer Registered: <br/>
     * {BaseType1 + Id1}. <br/>
     * <br/>
     * User MimeType Provided: <br/>
     * {BaseType2} <br/>
     * <br/>
     * Empty Set should be returned.
     * </p>
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testNoMatchInputTransformerBaseTypeAndId() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), "a1");
    ServiceReference[] refs = { ref1 };
    Object simpleTransformer1 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_XML));
    // then
    assertThat(matches.size(), is(0));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with MimeTypeToTransformerMapper

use of ddf.mime.MimeTypeToTransformerMapper in project ddf by codice.

the class TestMimeTypeToTransformerMapperImpl method testMultiIdMatchExtraParameters.

/**
     * Tests if a multiple id match will return only one item
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testMultiIdMatchExtraParameters() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_JSON), "a1");
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_JSON), "a1");
    ServiceReference ref3 = createMockReference(3, null, null);
    ServiceReference[] refs = { ref3, ref2, ref1 };
    //        InputTransformer simpleTransformer1 = getSimpleTransformer("1");
    //        InputTransformer simpleTransformer2 = getSimpleTransformer("2");
    //        InputTransformer simpleTransformer3 = getSimpleTransformer("3");
    Object simpleTransformer1 = new Object();
    Object simpleTransformer2 = new Object();
    Object simpleTransformer3 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getService(ref2)).thenReturn(simpleTransformer2);
    when(context.getService(ref3)).thenReturn(simpleTransformer3);
    when(ref1.compareTo(ref3)).thenReturn(-1);
    when(ref2.compareTo(ref1)).thenReturn(1);
    when(ref2.compareTo(ref3)).thenReturn(-1);
    when(ref3.compareTo(ref1)).thenReturn(1);
    when(ref3.compareTo(ref2)).thenReturn(1);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON + "; id=a1;charset=UTF-8"));
    // then
    assertThat(matches.size(), is(2));
    assertThat(matches.get(0), is(simpleTransformer2));
    assertThat(matches.get(1), is(simpleTransformer1));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with MimeTypeToTransformerMapper

use of ddf.mime.MimeTypeToTransformerMapper in project ddf by codice.

the class TestMimeTypeToTransformerMapperImpl method testNoMatch.

/**
     * Testing a negative case where the mimetypes don't match.
     *
     * Tests if
     *
     * InputTransformer Registered: <br/>
     * {BaseType1, BaseType2}. <br/>
     * <br/>
     * User MimeType Provided: <br/>
     * {BasetType3} <br/>
     * <br/>
     * Empty Set should be returned.
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testNoMatch() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), null);
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_JSON), null);
    ServiceReference[] refs = { ref1, ref2 };
    Object simpleTransformer1 = new Object();
    Object simpleTransformer2 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getService(ref2)).thenReturn(simpleTransformer2);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_XML));
    // then
    assertThat(matches.size(), is(0));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with MimeTypeToTransformerMapper

use of ddf.mime.MimeTypeToTransformerMapper in project ddf by codice.

the class TestMimeTypeToTransformerMapperImpl method testNullMimeType.

/**
     * We expect to receive all the services to be returned when the user does not provide any mime
     * types
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testNullMimeType() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceReference ref3 = mock(ServiceReference.class);
    when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    when(ref2.getProperty(Constants.SERVICE_RANKING)).thenReturn(2);
    when(ref3.getProperty(Constants.SERVICE_RANKING)).thenReturn(3);
    when(ref1.compareTo(ref2)).thenReturn(-1);
    when(ref1.compareTo(ref3)).thenReturn(-1);
    when(ref2.compareTo(ref1)).thenReturn(1);
    when(ref2.compareTo(ref3)).thenReturn(-1);
    when(ref3.compareTo(ref1)).thenReturn(1);
    when(ref3.compareTo(ref2)).thenReturn(1);
    /*
         * Add the three references out of order.
         */
    ServiceReference[] refs = { ref2, ref3, ref1 };
    Object simpleTransformer1 = new Object();
    Object simpleTransformer2 = new Object();
    Object simpleTransformer3 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getService(ref2)).thenReturn(simpleTransformer2);
    when(context.getService(ref3)).thenReturn(simpleTransformer3);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, null);
    // then
    assertThat(matches.size(), is(3));
    /*
         * Test the sorted order of the references.
         */
    assertThat(matches.get(0), is(simpleTransformer3));
    assertThat(matches.get(1), is(simpleTransformer2));
    assertThat(matches.get(2), is(simpleTransformer1));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)22 MimeType (javax.activation.MimeType)18 Test (org.junit.Test)18 BundleContext (org.osgi.framework.BundleContext)15 ServiceReference (org.osgi.framework.ServiceReference)14 InputTransformer (ddf.catalog.transform.InputTransformer)6 InputStream (java.io.InputStream)5 MockMemoryStorageProvider (ddf.catalog.content.impl.MockMemoryStorageProvider)3 Metacard (ddf.catalog.data.Metacard)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 CatalogProvider (ddf.catalog.source.CatalogProvider)3 FederationStrategy (ddf.catalog.federation.FederationStrategy)2 CreateOperations (ddf.catalog.impl.operations.CreateOperations)2 MetacardFactory (ddf.catalog.impl.operations.MetacardFactory)2 OperationsCatalogStoreSupport (ddf.catalog.impl.operations.OperationsCatalogStoreSupport)2 OperationsMetacardSupport (ddf.catalog.impl.operations.OperationsMetacardSupport)2 OperationsSecuritySupport (ddf.catalog.impl.operations.OperationsSecuritySupport)2 OperationsStorageSupport (ddf.catalog.impl.operations.OperationsStorageSupport)2 QueryOperations (ddf.catalog.impl.operations.QueryOperations)2 ResourceOperations (ddf.catalog.impl.operations.ResourceOperations)2