Search in sources :

Example 96 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class MimeTypeToTransformerMapperImplTest method testSingleMimeTypeServiceProperty.

/**
 * Tests the case where the ServiceReference Properties does not have a list of MimeTypes, instead
 * it provides a single String for the MimeType
 *
 * @throws MimeTypeParseException
 * @throws InvalidSyntaxException
 */
@Test
public void testSingleMimeTypeServiceProperty() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref = mock(ServiceReference.class);
    ServiceReference[] refs = { ref };
    when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(0);
    when(ref.getProperty(MimeTypeToTransformerMapper.MIME_TYPE_KEY)).thenReturn(MediaType.APPLICATION_JSON);
    Object simpleTransformer1 = new Object();
    // when
    when(context.getService(ref)).thenReturn(simpleTransformer1);
    when(context.getServiceReferences(isA(String.class), isNull())).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON));
    // then
    assertThat(matches.size(), is(1));
}
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 97 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class MimeTypeToTransformerMapperImplTest method testOnlyBaseTypeMatch.

/**
 * Tests if a basetype matches that the list will return the correct sorted list.
 *
 * @throws MimeTypeParseException
 * @throws InvalidSyntaxException
 */
@Test
public void testOnlyBaseTypeMatch() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), "a1");
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_JSON), "a2");
    ServiceReference ref3 = createMockReference(3, null, null);
    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())).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON));
    // then
    assertThat(matches.size(), is(1));
    assertThat(matches.get(0), is(simpleTransformer2));
}
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 98 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class MimeTypeToTransformerMapperImplTest method testOnlyBaseTypeMatch2.

/**
 * Tests if a basetype matches that the list will return the correct sorted list.
 *
 * @throws MimeTypeParseException
 * @throws InvalidSyntaxException
 */
@Test
public void testOnlyBaseTypeMatch2() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), "a1");
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_JSON), "a2");
    ServiceReference ref3 = createMockReference(3, Arrays.asList(MediaType.APPLICATION_JSON), "a3");
    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(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);
    when(context.getServiceReferences(isA(String.class), isNull())).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON));
    // then
    assertThat(matches.size(), is(2));
    assertThat(matches.get(0), is(simpleTransformer3));
    assertThat(matches.get(1), is(simpleTransformer2));
}
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 99 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class MimeTypeToTransformerMapperImplTest method testNoMatch.

/**
 * Testing a negative case where the mimetypes don't match.
 *
 * <p>Tests if
 *
 * <p>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())).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 100 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class MimeTypeToTransformerMapperImplTest method testNoMatchInputTransformerBaseTypeAndId.

/**
 * Testing a negative case where the mimetypes don't match.
 *
 * <p>Tests if
 *
 * <p>InputTransformer Registered: <br>
 * {BaseType1 + Id1}. <br>
 * <br>
 * User MimeType Provided: <br>
 * {BaseType2} <br>
 * <br>
 * Empty Set should be returned.
 *
 * @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())).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)

Aggregations

MimeType (javax.activation.MimeType)146 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)36 MimeTypeParseException (javax.activation.MimeTypeParseException)34 URI (java.net.URI)28 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)25 HashMap (java.util.HashMap)25 BundleContext (org.osgi.framework.BundleContext)25 ServiceReference (org.osgi.framework.ServiceReference)25 CatalogFramework (ddf.catalog.CatalogFramework)20 Serializable (java.io.Serializable)16 ResourceResponse (ddf.catalog.operation.ResourceResponse)15 IOException (java.io.IOException)14 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)11 InputStream (java.io.InputStream)10 StringWriter (java.io.StringWriter)10 MetacardCreationException (ddf.catalog.data.MetacardCreationException)9 Resource (ddf.catalog.resource.Resource)9 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)9 File (java.io.File)9