use of org.entando.entando.web.guifragment.model.GuiFragmentRequestBody in project entando-core by entando.
the class GuiFragmentValidator method validate.
@Override
public void validate(Object target, Errors errors) {
GuiFragmentRequestBody request = (GuiFragmentRequestBody) target;
String code = request.getCode();
try {
if (null != this.guiFragmentManager.getGuiFragment(code)) {
errors.rejectValue("code", ERRCODE_FRAGMENT_ALREADY_EXISTS, new String[] { code }, "guifragment.exists");
} else if (code.length() > 50) {
errors.rejectValue("code", ERRCODE_FRAGMENT_INVALID_CODE, new String[] {}, "guifragment.code.invalid");
} else if (!code.matches("^[a-zA-Z0-9_]*$")) {
errors.rejectValue("code", ERRCODE_FRAGMENT_INVALID_CODE, new String[] {}, "guifragment.code.invalid");
}
this.validateGuiCode(request, errors);
} catch (Exception e) {
logger.error("Error extracting fragment {}", code, e);
throw new RestServerError("error extracting fragment", e);
}
}
use of org.entando.entando.web.guifragment.model.GuiFragmentRequestBody in project entando-core by entando.
the class GuiFragmentValidatorTest method validateRightFragment.
@Test
public void validateRightFragment() throws Exception {
when(this.guiFragmentManager.getGuiFragment("not_existing")).thenReturn(null);
GuiFragmentRequestBody request = new GuiFragmentRequestBody("not_existing", "<h1>code</h1>");
MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
validator.validate(request, bindingResult);
Assert.assertFalse(bindingResult.hasErrors());
Assert.assertEquals(0, bindingResult.getErrorCount());
}
use of org.entando.entando.web.guifragment.model.GuiFragmentRequestBody in project entando-core by entando.
the class GuiFragmentValidatorTest method validateInvalidFragmentCode_1.
@Test
public void validateInvalidFragmentCode_1() throws Exception {
String code = "very_long";
for (int i = 0; i < 10; i++) {
code += code;
}
when(this.guiFragmentManager.getGuiFragment(code)).thenReturn(null);
GuiFragmentRequestBody request = new GuiFragmentRequestBody(code, "<h1>prova</h1>");
MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
validator.validate(request, bindingResult);
Assert.assertTrue(bindingResult.hasErrors());
Assert.assertEquals(1, bindingResult.getErrorCount());
}
use of org.entando.entando.web.guifragment.model.GuiFragmentRequestBody in project entando-core by entando.
the class GuiFragmentValidatorTest method validateExistingFragment.
@Test
public void validateExistingFragment() throws Exception {
GuiFragment existing = new GuiFragment();
existing.setCode("existing");
when(this.guiFragmentManager.getGuiFragment("existing")).thenReturn(existing);
GuiFragmentRequestBody request = new GuiFragmentRequestBody("existing", "<h1>code</h1>");
MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
validator.validate(request, bindingResult);
Assert.assertTrue(bindingResult.hasErrors());
Assert.assertEquals(1, bindingResult.getErrorCount());
}
use of org.entando.entando.web.guifragment.model.GuiFragmentRequestBody in project entando-core by entando.
the class GuiFragmentControllerTest method should_validate_put_path_mismatch.
@Test
public void should_validate_put_path_mismatch() throws ApsSystemException, Exception {
UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
String accessToken = mockOAuthInterceptor(user);
ObjectMapper mapper = new ObjectMapper();
GuiFragmentRequestBody requestBody = new GuiFragmentRequestBody();
requestBody.setCode("__new_fragment_");
requestBody.setGuiCode("<h1>This is the fragment</h1>");
String payload = mapper.writeValueAsString(requestBody);
this.controller.setGuiFragmentValidator(new GuiFragmentValidator());
ResultActions result = mockMvc.perform(put("/fragments/{fragmentCode}", "new_fragment").content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
result.andExpect(status().isBadRequest());
String response = result.andReturn().getResponse().getContentAsString();
System.out.println(response);
}
Aggregations