diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/EntityTypeLabelRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/EntityTypeLabelRestController.java new file mode 100644 index 0000000000..14d01b8c76 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/EntityTypeLabelRestController.java @@ -0,0 +1,67 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import java.sql.SQLException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.dspace.app.rest.converter.ConverterService; +import org.dspace.app.rest.model.EntityTypeRest; +import org.dspace.app.rest.utils.ContextUtil; +import org.dspace.app.rest.utils.Utils; +import org.dspace.content.EntityType; +import org.dspace.content.factory.ContentServiceFactory; +import org.dspace.content.service.EntityTypeService; +import org.dspace.core.Context; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * This controller will handle all the incoming calls on the /api/core/entitytypes/label/<:entity-type-label> endpoint + * where the entity-type-label parameter can be filled in to match a specific entityType by label + * There's always at most one entity type per label. + *

+ * It responds with: + *

+ * The single entity type if there's a match + * 404 if the entity type doesn't exist + * + * @author Maria Verdonck (Atmire) on 2019-12-13 + */ +@RestController +@RequestMapping("/api/" + EntityTypeRest.CATEGORY + "/" + EntityTypeRest.NAME_PLURAL) +public class EntityTypeLabelRestController { + + protected final EntityTypeService entityTypeService = ContentServiceFactory.getInstance().getEntityTypeService(); + + @Autowired + protected ConverterService converter; + + @Autowired + protected Utils utils; + + @GetMapping("/label/{entity-type-label}") + public EntityTypeRest get(HttpServletRequest request, HttpServletResponse response, + @PathVariable("entity-type-label") String label) { + Context context = ContextUtil.obtainContext(request); + try { + EntityType entityType = this.entityTypeService.findByEntityType(context, label); + if (entityType == null) { + throw new ResourceNotFoundException("There was no entityType found with label: " + label); + } + return converter.toRest(entityType, utils.obtainProjection()); + } catch (SQLException e) { + throw new RuntimeException(e.getMessage(), e); + } + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/EntityTypeRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/EntityTypeRest.java index fecd065815..454e5a82b6 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/EntityTypeRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/EntityTypeRest.java @@ -17,6 +17,7 @@ import org.dspace.app.rest.RestResourceController; public class EntityTypeRest extends BaseObjectRest { public static final String NAME = "entitytype"; + public static final String NAME_PLURAL = "entitytypes"; public static final String CATEGORY = "core"; public String getCategory() { diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/EntityTypeLabelRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/EntityTypeLabelRestControllerIT.java new file mode 100644 index 0000000000..e75d639cd7 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/EntityTypeLabelRestControllerIT.java @@ -0,0 +1,48 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.dspace.app.rest.test.AbstractEntityIntegrationTest; +import org.dspace.content.EntityType; +import org.dspace.content.service.EntityTypeService; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Integration tests for the {@link org.dspace.app.rest.EntityTypeLabelRestController} controlled endpoints + * + * @author Maria Verdonck (Atmire) on 16/12/2019 + */ +public class EntityTypeLabelRestControllerIT extends AbstractEntityIntegrationTest { + + @Autowired + private EntityTypeService entityTypeService; + + @Test + public void testGetEntityTypeByLabel_ExistingLabel() throws Exception { + String testLabel = "Person"; + EntityType entityType = entityTypeService.findByEntityType(context, testLabel); + getClient().perform(get("/api/core/entitytypes/label/" + testLabel)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(entityType.getID()))) + .andExpect(jsonPath("$.label", containsString(testLabel))); + } + + @Test + public void testGetEntityTypeByLabel_NonExistentLabel() throws Exception { + String testLabel = "Person2"; + getClient().perform(get("/api/core/entitytypes/label" + testLabel)) + .andExpect(status().isNotFound()); + } +}