Search in sources :

Example 1 with GuiFragmentRequestBody

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);
    }
}
Also used : GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) RestServerError(org.entando.entando.aps.system.exception.RestServerError)

Example 2 with GuiFragmentRequestBody

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());
}
Also used : GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 3 with GuiFragmentRequestBody

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());
}
Also used : GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 4 with GuiFragmentRequestBody

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());
}
Also used : GuiFragment(org.entando.entando.aps.system.services.guifragment.GuiFragment) GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 5 with GuiFragmentRequestBody

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);
}
Also used : GuiFragmentValidator(org.entando.entando.web.guifragment.validator.GuiFragmentValidator) UserDetails(com.agiletec.aps.system.services.user.UserDetails) GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Aggregations

GuiFragmentRequestBody (org.entando.entando.web.guifragment.model.GuiFragmentRequestBody)7 Test (org.junit.Test)6 MapBindingResult (org.springframework.validation.MapBindingResult)5 GuiFragment (org.entando.entando.aps.system.services.guifragment.GuiFragment)2 UserDetails (com.agiletec.aps.system.services.user.UserDetails)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RestServerError (org.entando.entando.aps.system.exception.RestServerError)1 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)1 GuiFragmentValidator (org.entando.entando.web.guifragment.validator.GuiFragmentValidator)1 ResultActions (org.springframework.test.web.servlet.ResultActions)1