Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.entando.entando.plugins.jacms.web.content.validator.RestContentListRequest;
import org.entando.entando.web.common.annotation.RestAccessControl;
import org.entando.entando.web.common.exceptions.ValidationGenericException;
import org.entando.entando.web.common.exceptions.ValidationUnprocessableEntityException;
import org.entando.entando.web.common.model.PagedMetadata;
import org.entando.entando.web.common.model.PagedRestResponse;
import org.entando.entando.web.common.model.RestResponse;
Expand Down Expand Up @@ -202,18 +203,24 @@ public ResponseEntity<SimpleRestResponse<List<ContentDto>>> addContent(@Valid @R
}

List<ContentDto> response = bodyRequest.stream()
.map(content -> {
this.getContentValidator().validate(content, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
ContentDto result = this.getContentService().addContent(content, userDetails, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
return result;
})
.collect(Collectors.toList());
.map(content -> {
this.getContentValidator().validate(content, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}

this.getContentValidator().validateTypeCode(content, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationUnprocessableEntityException(bindingResult);
}

ContentDto result = this.getContentService().addContent(content, userDetails, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
return result;
})
.collect(Collectors.toList());

return new ResponseEntity<>(new SimpleRestResponse<>(response), HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.agiletec.aps.system.common.entity.IEntityManager;
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
import com.agiletec.plugins.jacms.aps.system.services.content.model.ContentDto;
import java.util.Objects;
import org.entando.entando.aps.system.exception.RestServerError;
import org.entando.entando.aps.system.services.entity.model.EntityDto;
import org.entando.entando.plugins.jacms.aps.system.services.content.IContentService;
Expand All @@ -36,6 +37,8 @@ public class ContentValidator extends EntityValidator {
@Autowired
private IContentManager contentManager;

public static final String ERRCODE_TYPE_INVALID = "5";

public boolean existContent(String code, String status) {
boolean online = (IContentService.STATUS_ONLINE.equalsIgnoreCase(status));
try {
Expand All @@ -46,6 +49,14 @@ public boolean existContent(String code, String status) {
}
}

public void validateTypeCode(Object target, Errors errors) {
EntityDto request = (EntityDto) target;
if (Objects.isNull(this.getEntityManager().getEntityPrototype(request.getTypeCode()))) {
errors.reject(ERRCODE_TYPE_INVALID,"entity.typeCode.invalid");
}
}


@Override
protected IEntityManager getEntityManager() {
return this.contentManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.entando.entando.plugins.jacms.web.content;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -30,6 +32,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand All @@ -38,6 +42,7 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;

@ExtendWith(MockitoExtension.class)
class ContentControllerTest extends AbstractControllerTest {
Expand Down Expand Up @@ -93,7 +98,6 @@ void testAddContent() throws Exception {
ResultActions result = this.performPostContent(mockJson, user);
result.andExpect(status().isOk());
}

@Test
void testUpdateContent() throws Exception {
UserDetails user = this.createUser(true);
Expand All @@ -110,6 +114,27 @@ void testUpdateContent() throws Exception {
result.andExpect(status().isOk());
}

@Test
void shouldReturn422OnInvalidTypeCode() throws Exception {
UserDetails user = this.createUser(true);

doAnswer(invocation -> {
Errors errors = invocation.getArgument(1);
errors.reject("5", "entity.typeCode.invalid");
return null;
}).when(contentValidator).validateTypeCode(any(), any(Errors.class));

String mockJson = "[{\n"
+ " \"id\": \"ART123\",\n"
+ " \"typeCode\": \"XXX\",\n"
+ " \"attributes\": [\n"
+ " {\"code\": \"code1\", \"value\": \"value1\"},\n"
+ " {\"code\": \"code2\", \"value\": \"value2\"}\n"
+ " ]}]";
ResultActions result = this.performPostContent(mockJson, user);
result.andExpect(status().isUnprocessableEntity());
}

private ResultActions performGetContent(String code, String modelId,
boolean online, String langCode, UserDetails user) throws Exception {
String accessToken = mockOAuthInterceptor(user);
Expand Down
1 change: 1 addition & 0 deletions engine/src/main/resources/rest/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ entity.id.mismatch=The entity id specified ''{0}'' does not match with the one
entity.notExists=The entity ''{0}'' does not exist
entity.id.notBlank=Code is required
entity.typeCode.notBlank=TypeCode is required
entity.typeCode.invalid=TypeCode is invalid

#API Consumer
api.consumer.grantType.invalid=''{0}'' is not a valid grant type
Expand Down