MessageFormatController extends BaseController { @RequestMapping(path = "/create", method = RequestMethod.GET) public String createMessageFormat(final MessageFormatForm messageFormatForm, final Model model) { setupBreadCrumbs(model, "Create", null); return "configuration/messageFormat/create"; }  @RequestMapping(path = "", method = RequestMethod.GET) String index(final Model model); @RequestMapping(path = "/create", method = RequestMethod.GET) String createMessageFormat(final MessageFormatForm messageFormatForm, final Model model); @RequestMapping(path = "/edit/{id}", method = RequestMethod.GET) String editMessageFormat(
        @PathVariable final Long id,
        final MessageFormatForm messageFormatForm,
        final Model model,
        final RedirectAttributes redirectAttributes); @RequestMapping(path = "/update", method = RequestMethod.POST) String create(
        @Valid final MessageFormatForm messageFormatForm,
        final BindingResult bindingResult,
        final RedirectAttributes redirectAttributes,
        @RequestParam final Map<String, String> allRequestParams); @RequestMapping(path = "/delete/{id}", method = RequestMethod.POST) String deleteCluster(@PathVariable final Long id, final RedirectAttributes redirectAttributes);  }
@Test @Transactional public void testPostUpdate_updatingExistingButWithInvalidJar() throws Exception { final MessageFormat messageFormat = messageFormatTestTools.createMessageFormat("MyMessageFormat" + System.currentTimeMillis()); final String expectedName = messageFormat.getName(); final String expectedClasspath = messageFormat.getClasspath(); final String expectedJarName = messageFormat.getJar(); final String expectedJarContents = "OriginalContents"; final String expectedParametersJson = messageFormat.getOptionParameters(); final Path expectedJarPath = Paths.get(deserializerUploadPath, messageFormat.getJar()); FileTestTools.createDummyFile(deserializerUploadPath + messageFormat.getJar(), expectedJarContents); final MockMultipartFile jarUpload = new MockMultipartFile("file", "testPlugins.jar", null, "MyContent".getBytes(Charsets.UTF_8)); mockMvc .perform(multipart("/configuration/messageFormat/update") .file(jarUpload) .with(user(adminUserDetails)) .with(csrf()) .param("id", String.valueOf(messageFormat.getId())) .param("name", "Updated Name") .param("classpath", "made.up.classpath") .param("customOptionNames", "option1") .param("customOptionNames", "option2") .param("customOptionValues", "value1") .param("customOptionValues", "value2")) .andExpect(model().hasErrors()) .andExpect(model().attributeHasFieldErrors("messageFormatForm", "file")) .andExpect(status().isOk()); final MessageFormat updatedMessageFormat = messageFormatRepository.findById(messageFormat.getId()).get(); assertNotNull("Has message format", updatedMessageFormat); assertEquals("Name not updated", expectedName, updatedMessageFormat.getName()); assertEquals("classpath not updated", expectedClasspath, updatedMessageFormat.getClasspath()); assertEquals("Jar name not updated", expectedJarName, updatedMessageFormat.getJar()); assertFalse("Should not be a default format", updatedMessageFormat.isDefaultFormat()); assertEquals("Parameters not updated", expectedParametersJson, updatedMessageFormat.getOptionParameters()); assertTrue("File should exist", Files.exists(expectedJarPath)); final String jarContents = FileTestTools.readFile(expectedJarPath.toString()); assertEquals("Jar contents should not have changed", expectedJarContents, jarContents); Files.deleteIfExists(expectedJarPath); }
@Test @Transactional public void testPostUpdate_updatingExistingNoJarUploaded() throws Exception { final MessageFormat messageFormat = messageFormatTestTools.createMessageFormat("MyMessageFormat" + System.currentTimeMillis()); final String originalClasspath = messageFormat.getClasspath(); final String originalJarName = messageFormat.getJar(); final String originalJarContents = "OriginalContents"; final Path originalJarPath = Paths.get(deserializerUploadPath, messageFormat.getJar()); FileTestTools.createDummyFile(deserializerUploadPath + messageFormat.getJar(), originalJarContents); final InputStream emptyFile = null; final MockMultipartFile jarUpload = new MockMultipartFile("file", "testPlugins.jar", null, emptyFile); final String newName = "MyUpdatedName" + System.currentTimeMillis(); final String newClasspath = "my new class path"; mockMvc .perform(multipart("/configuration/messageFormat/update") .file(jarUpload) .with(user(adminUserDetails)) .with(csrf()) .param("id", String.valueOf(messageFormat.getId())) .param("name", newName) .param("classpath", newClasspath) .param("customOptionNames", "option1") .param("customOptionNames", "option2") .param("customOptionValues", "value1") .param("customOptionValues", "value2")) .andExpect(model().hasNoErrors()) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/configuration/messageFormat")); final MessageFormat updatedMessageFormat = messageFormatRepository.findById(messageFormat.getId()).get(); assertNotNull("Has message format", updatedMessageFormat); assertEquals("Name updated", newName, updatedMessageFormat.getName()); assertEquals("classpath was NOT updated", originalClasspath, updatedMessageFormat.getClasspath()); assertNotEquals("classpath was NOT updated", newClasspath, updatedMessageFormat.getClasspath()); assertEquals("Jar name not updated", originalJarName, updatedMessageFormat.getJar()); assertFalse("Should not be a default format", updatedMessageFormat.isDefaultFormat()); final String expectedOptionsJson = "{\"option1\":\"value1\",\"option2\":\"value2\"}"; assertEquals("Parameters should have been updated", expectedOptionsJson, updatedMessageFormat.getOptionParameters()); assertTrue("File should exist", Files.exists(originalJarPath)); final String jarContents = FileTestTools.readFile(originalJarPath.toString()); assertEquals("Jar contents should not have changed", originalJarContents, jarContents); Files.deleteIfExists(originalJarPath); }
@Test @Transactional public void testPostUpdate_updatingExistingWithValidJarSameName() throws Exception { final MessageFormat messageFormat = messageFormatTestTools.createMessageFormat("MyMessageFormat" + System.currentTimeMillis()); final String originalJarName = messageFormat.getJar(); final String originalJarContents = "OriginalContents"; final Path originalJarPath = Paths.get(deserializerUploadPath, messageFormat.getJar()); FileTestTools.createDummyFile(deserializerUploadPath + messageFormat.getJar(), originalJarContents); final InputStream fileInputStream = getClass().getClassLoader().getResourceAsStream("testDeserializer/testPlugins.jar"); final MockMultipartFile jarUpload = new MockMultipartFile("file", "testPlugins.jar", null, fileInputStream); final String newName = "MyUpdatedName" + System.currentTimeMillis(); final String newClasspath = "examples.deserializer.ExampleDeserializer"; mockMvc .perform(multipart("/configuration/messageFormat/update") .file(jarUpload) .with(user(adminUserDetails)) .with(csrf()) .param("id", String.valueOf(messageFormat.getId())) .param("name", newName) .param("classpath", newClasspath)) .andExpect(model().hasNoErrors()) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/configuration/messageFormat")); final MessageFormat updatedMessageFormat = messageFormatRepository.findById(messageFormat.getId()).get(); assertNotNull("Has message format", updatedMessageFormat); assertEquals("Name updated", newName, updatedMessageFormat.getName()); assertEquals("classpath updated", newClasspath, updatedMessageFormat.getClasspath()); assertNotEquals("Jar name not updated", originalJarName, updatedMessageFormat.getJar()); assertFalse("Should not be a default format", updatedMessageFormat.isDefaultFormat()); assertEquals("No parameters should be empty", "{}", updatedMessageFormat.getOptionParameters()); assertFalse("File should NOT exist", Files.exists(originalJarPath)); final Path newJarPath = Paths.get(deserializerUploadPath, updatedMessageFormat.getJar()); assertTrue("New jar should exist", Files.exists(newJarPath)); Files.deleteIfExists(newJarPath); }
@Test @Transactional public void testGetEdit_existingMessageFormat() throws Exception { final MessageFormat format = messageFormatTestTools.createMessageFormat("MyMessageFormat" + System.currentTimeMillis()); format.setOptionParameters("{\"myOption1\":\"myValue1\",\"myOption2\":\"myValue2\"}"); messageFormatRepository.save(format); mockMvc .perform(get("/configuration/messageFormat/edit/" + format.getId()) .with(user(adminUserDetails))) .andExpect(status().isOk()) .andExpect(content().string(containsString(format.getName()))) .andExpect(content().string(containsString(format.getClasspath()))) .andExpect(content().string(containsString("value=\"" + format.getId() + "\""))) .andExpect(content().string(containsString("myValue1"))) .andExpect(content().string(containsString("myValue2"))) .andExpect(content().string(containsString("myOption1"))) .andExpect(content().string(containsString("myOption2"))); }
@Test @Transactional public void testPostDelete_notUsed() throws Exception { final MessageFormat format = messageFormatTestTools.createMessageFormat("Format 1"); final long formatId = format.getId(); final Path expectedJarPath = Paths.get(deserializerUploadPath, format.getJar()); FileTestTools.createDummyFile(expectedJarPath.toString(), "MyContests"); assertTrue("Sanity test", Files.exists(expectedJarPath)); mockMvc .perform(post("/configuration/messageFormat/delete/" + formatId) .with(user(adminUserDetails)) .with(csrf())) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/configuration/messageFormat")); assertFalse("Should NOT have message format", messageFormatRepository.existsById(formatId)); assertFalse("Should have been removed", Files.exists(expectedJarPath)); }
@Test @Transactional public void testPostDelete_inUse() throws Exception { final MessageFormat format = messageFormatTestTools.createMessageFormat("Format 1"); final long formatId = format.getId(); final View view = viewTestTools.createViewWithFormat("My Name", format); final Path expectedJarPath = Paths.get(deserializerUploadPath, format.getJar()); FileTestTools.createDummyFile(expectedJarPath.toString(), "MyContests"); assertTrue("Sanity test", Files.exists(expectedJarPath)); mockMvc .perform(post("/configuration/messageFormat/delete/" + formatId) .with(user(adminUserDetails)) .with(csrf())) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/configuration/messageFormat")); final MessageFormat messageFormat = messageFormatRepository.findById(formatId).get(); assertNotNull("Should NOT have removed message format", messageFormat); assertTrue("Jar should not have been removed", Files.exists(expectedJarPath)); Files.deleteIfExists(expectedJarPath); }