Search in sources :

Example 6 with BundleIcon

use of org.motechproject.server.web.dto.BundleIcon in project motech by motech.

the class BundleIconServiceImplTest method shouldReturnDefaultIconWhenBundleDoesNotContainIcon.

@Test
public void shouldReturnDefaultIconWhenBundleDoesNotContainIcon() {
    setupBundleRetrieval();
    BundleIcon bundleIcon = bundleIconService.getBundleIconById(BUNDLE_ID, null);
    byte[] expectedIcon = readDefaultIcon(DEFAULT_PATH + DEFAULT_ICON);
    assertArrayEquals(expectedIcon, bundleIcon.getIcon());
    assertEquals(expectedIcon.length, bundleIcon.getContentLength());
    assertEquals(ICON_MIME, bundleIcon.getMime());
    for (String iconName : BundleIcon.ICON_LOCATIONS) {
        verify(bundle).getResource(iconName);
    }
}
Also used : BundleIcon(org.motechproject.server.web.dto.BundleIcon) Test(org.junit.Test)

Example 7 with BundleIcon

use of org.motechproject.server.web.dto.BundleIcon in project motech by motech.

the class BundleIconServiceImplTest method shouldReturnIconWhenGivenBundleName.

@Test
public void shouldReturnIconWhenGivenBundleName() {
    setupBundleRetrieval();
    when(bundle.getResource("icon.gif")).thenReturn(getDefaultIconUrl(DEFAULT_PATH + DEFAULT_ICON));
    BundleIcon bundleIcon = bundleIconService.getBundleIconByName(BUNDLE_NAME, null);
    byte[] expectedIcon = readDefaultIcon(DEFAULT_PATH + DEFAULT_ICON);
    assertArrayEquals(expectedIcon, bundleIcon.getIcon());
    assertEquals(ICON_MIME, bundleIcon.getMime());
}
Also used : BundleIcon(org.motechproject.server.web.dto.BundleIcon) Test(org.junit.Test)

Example 8 with BundleIcon

use of org.motechproject.server.web.dto.BundleIcon in project motech by motech.

the class BundleIconServiceImpl method loadBundleIcon.

private BundleIcon loadBundleIcon(URL iconURL) {
    InputStream is = null;
    try {
        URLConnection urlConn = iconURL.openConnection();
        is = urlConn.getInputStream();
        String mime = urlConn.getContentType();
        byte[] image = IOUtils.toByteArray(is);
        return new BundleIcon(image, mime);
    } catch (IOException e) {
        throw new MotechException("Error loading icon.", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) BundleIcon(org.motechproject.server.web.dto.BundleIcon) IOException(java.io.IOException) URLConnection(java.net.URLConnection)

Example 9 with BundleIcon

use of org.motechproject.server.web.dto.BundleIcon in project motech by motech.

the class BundleIconServiceImpl method getBundleIcon.

private BundleIcon getBundleIcon(Bundle bundle, String defaultIcon) {
    BundleIcon bundleIcon = null;
    if (bundle != null) {
        for (String iconLocation : ICON_LOCATIONS) {
            URL iconURL = bundle.getResource(iconLocation);
            if (iconURL != null) {
                bundleIcon = loadBundleIcon(iconURL);
                break;
            }
        }
    }
    if (bundleIcon == null) {
        URL defaultIconURL;
        if (defaultIcon == null) {
            defaultIconURL = getClass().getResource(DEFAULT_PATH + DEFAULT_ICON);
        } else {
            defaultIconURL = getClass().getResource(DEFAULT_PATH + defaultIcon);
        }
        bundleIcon = loadBundleIcon(defaultIconURL);
    }
    return bundleIcon;
}
Also used : BundleIcon(org.motechproject.server.web.dto.BundleIcon) URL(java.net.URL)

Aggregations

BundleIcon (org.motechproject.server.web.dto.BundleIcon)9 Test (org.junit.Test)6 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 MotechException (org.motechproject.commons.api.MotechException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1