class TemplateResource {

	protected void orphanOriginalStormTemplate(String originalTemplateId, ItemManager itemMan) {
		//can only orphan the original if there was an original
		if (!StringUtils.isEmpty(originalTemplateId)) {
			Item originalTemplate = itemMan.load(originalTemplateId);
			if (null == originalTemplate) {
				throw new EntityNotFoundException(String.format(
					"Could not find template with ID '%s'. "
					+ "Perhaps the template Item was deleted "
					+ "between the time it was persisted and the time it was read?",
					originalTemplateId));
			} else {
				//only orphan original template if it was a storm template
				if (Type.storms.equals(originalTemplate.getType())) {
					boolean success = itemMan.orphan(originalTemplate);
					if (!success) {
						throw new PersistenceException(
							String.format(
								"Could not orphan template with ID '%s'",
								originalTemplateId
							)
						);
					}
				}
			}
		}
	}

}

class TemplateResourceTest {

	private TemplateResource instance;
	private ItemManager mockItemMan;
	private AliasManager mockAliasMan;

	@Test
	public void testThatSuccessfulItemManagerOrphaningThrowsNoException() {

		String id = "foo";
		Item item = new Item();
		item.setType(Type.storms);
		
		//simulate Item being present in DB
		when(mockItemMan.load(id)).thenReturn(item);
		//simulate success orphaning item
		when(mockItemMan.orphan(item)).thenReturn(true);
		instance.orphanOriginalStormTemplate(id, mockItemMan);
		verify(mockItemMan).orphan(eq(item));
	}

}