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);
}
}
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());
}
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);
}
}
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;
}
Aggregations