TemplateResource { protected String updateStormAlias(String alias, AliasManager aliasMan, String templateId) { if (StringUtils.isEmpty(alias)) { throw new IllegalArgumentException(String.format("Alias cannot be empty. Got %s", alias)); } else { Alias fullAlias = aliasMan.load(alias); String originalTemplateId = null; if (fullAlias == null) { fullAlias = new Alias(); fullAlias.setId(alias); fullAlias.setItemId(templateId); aliasMan.save(fullAlias); } else { originalTemplateId = fullAlias.getItemId(); fullAlias.setItemId(templateId); aliasMan.update(fullAlias); } return originalTemplateId; } }  @GET @Path("/item/{id}") @Produces(MediaType.APPLICATION_JSON) Response getTemplate(@PathParam("id") String id, @Context Request request); Response instantiateTemplate(Document metadataXml, ParsedMetadata parsedMetadata, String id, String content); @POST @Path("/storm") @Produces(MediaType.APPLICATION_JSON) @ConfiguredRolesAllowed(ConfiguredRolesAllowedDynamicFeature.CCH_ADMIN_USER_PROP) Response instantiateStormTemplate(
			@FormDataParam("layerId") String layerId,
			@FormDataParam("activeStorm") String active,
			@FormDataParam("alias") String alias,
			@FormDataParam("copyType") String copyType,
			@FormDataParam("copyVal") String copyVal,
			@FormDataParam("trackId") String trackId,
			@FormDataParam("file") String metadataFile);  }
@Test(expected = IllegalArgumentException.class) public void testThatUpdatingNullAliasThrowsException() { instance.updateStormAlias(null, mockAliasMan, "doesn't matter"); }
@Test(expected = IllegalArgumentException.class) public void testThatUpdatingBlankAliasThrowsException() { instance.updateStormAlias("", mockAliasMan, "doesn't matter"); }
@Test public void testThatUpdatingNonExistentAliasCreatesANewAlias() { String aliasId = "foo"; String templateId = "bar"; Alias expectedAlias = new Alias(); expectedAlias.setId(aliasId); expectedAlias.setItemId(templateId); when(mockAliasMan.load(aliasId)).thenReturn(null); String originalTemplateId = instance.updateStormAlias(aliasId, mockAliasMan, templateId); assertNull(originalTemplateId); verify(mockAliasMan).save(eq(expectedAlias)); verify(mockAliasMan, never()).update(any()); }
@Test public void testThatUpdatingExistingAliasWorks() { String aliasId = "foo"; String existingTemplateId = "bar"; String newTemplateId = "baz"; Alias existingAlias = new Alias(); existingAlias.setId(aliasId); existingAlias.setItemId(existingTemplateId); Alias updatedAlias = new Alias(); updatedAlias.setId(aliasId); updatedAlias.setItemId(newTemplateId); when(mockAliasMan.load(aliasId)).thenReturn(existingAlias); String aliasOriginalTemplateId = instance.updateStormAlias(aliasId, mockAliasMan, newTemplateId); assertEquals(existingTemplateId, aliasOriginalTemplateId); verify(mockAliasMan).update(eq(updatedAlias)); verify(mockAliasMan, never()).save(any()); }