Example usage for org.springframework.http MediaType parseMediaType

List of usage examples for org.springframework.http MediaType parseMediaType

Introduction

In this page you can find the example usage for org.springframework.http MediaType parseMediaType.

Prototype

public static MediaType parseMediaType(String mediaType) 

Source Link

Document

Parse the given String into a single MediaType .

Usage

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * This test will simply add Relationships between Items with a useForPlace attribute set to false for the
 * RelationshipType. We want to test that the Relationships that are created will still have their place
 * attributes handled in a correct way/*from  w w  w.  ja v  a  2 s .c  o  m*/
 * @throws Exception
 */
@Test
public void addRelationshipsNotUseForPlace() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col2).withTitle("Author2").withIssueDate("2016-02-13")
            .withAuthor("Smith, Maria").withRelationshipType("Person").build();

    Item author3 = ItemBuilder.createItem(context, col2).withTitle("Author3").withIssueDate("2016-02-13")
            .withAuthor("Maybe, Maybe").withRelationshipType("Person").build();

    Item orgUnit1 = ItemBuilder.createItem(context, col3).withTitle("OrgUnit1").withIssueDate("2015-01-01")
            .withRelationshipType("OrgUnit").build();

    RelationshipType isOrgUnitOfPersonRelationshipType = relationshipTypeService.findbyTypesAndLabels(context,
            entityTypeService.findByEntityType(context, "Person"),
            entityTypeService.findByEntityType(context, "OrgUnit"), "isOrgUnitOfPerson", "isPersonOfOrgUnit");
    context.restoreAuthSystemState();
    String adminToken = getAuthToken(admin.getEmail(), password);

    // This is essentially a sequence of adding Relationships by POST and then checking with GET to see
    // if the place is being set properly.
    MvcResult mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author1.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    ObjectMapper mapper = new ObjectMapper();

    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String firstRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(0)));

    mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author2.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    mapper = new ObjectMapper();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    String secondRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(1)));

    mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author3.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    mapper = new ObjectMapper();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    String thirdRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(2)));
}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * This test will simply add Relationships between Items with a useForPlace attribute set to false for the
 * RelationshipType. We want to test that the Relationships that are created will still have their place
 * attributes handled in a correct way. It will then delete a Relationship and once again ensure that the place
 * attributes are being handled correctly.
 * @throws Exception/*w ww . j av a 2s .  co  m*/
 */
@Test
public void addAndDeleteRelationshipsNotUseForPlace() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col2).withTitle("Author2").withIssueDate("2016-02-13")
            .withAuthor("Smith, Maria").withRelationshipType("Person").build();

    Item author3 = ItemBuilder.createItem(context, col2).withTitle("Author3").withIssueDate("2016-02-13")
            .withAuthor("Maybe, Maybe").withRelationshipType("Person").build();

    Item orgUnit1 = ItemBuilder.createItem(context, col3).withTitle("OrgUnit1").withIssueDate("2015-01-01")
            .withRelationshipType("OrgUnit").build();

    RelationshipType isOrgUnitOfPersonRelationshipType = relationshipTypeService.findbyTypesAndLabels(context,
            entityTypeService.findByEntityType(context, "Person"),
            entityTypeService.findByEntityType(context, "OrgUnit"), "isOrgUnitOfPerson", "isPersonOfOrgUnit");
    context.restoreAuthSystemState();
    String adminToken = getAuthToken(admin.getEmail(), password);

    // This is essentially a sequence of adding Relationships by POST and then checking with GET to see
    // if the place is being set properly.
    MvcResult mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author1.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    ObjectMapper mapper = new ObjectMapper();

    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String firstRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(0)));

    mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author2.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    mapper = new ObjectMapper();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    String secondRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(1)));

    mvcResult = getClient(adminToken)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isOrgUnitOfPersonRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + author3.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + orgUnit1.getID()))
            .andExpect(status().isCreated()).andReturn();
    mapper = new ObjectMapper();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    String thirdRelationshipIdString = String.valueOf(map.get("id"));

    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(2)));

    // Here we will delete the secondRelationship and then verify that the others have their place handled properly
    getClient(adminToken).perform(delete("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isNoContent());

    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(0)));

    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("rightPlace", is(1)));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

@Ignore
public void putRelationshipAdminAccess() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();//from w ww.  j  av a 2s. co m
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1").withAuthor("Testy, TEst")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    context.restoreAuthSystemState();

    String token = getAuthToken(admin.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id)
                    .contentType(MediaType.parseMediaType(
                            org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author2.getID()))
            .andExpect(status().isOk()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.rightId", is(author2.getID().toString())));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 1 and author 2
 * Verify this is possible for a user with WRITE permissions on author 1 and author 2
 *//*w  w  w.jav a 2 s.c om*/
@Ignore
public void putRelationshipWriteAccessOnAuthors() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1").withAuthor("Testy, TEst")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, author1, Constants.WRITE, user);
    authorizeService.addPolicy(context, author2, Constants.WRITE, user);

    context.restoreAuthSystemState();

    String token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author2.getID()))
            .andExpect(status().isOk()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.rightId", is(author2.getID().toString())));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 1 and author 2
 * Verify this is possible for a user with WRITE permissions on publication 1
 *///from ww  w  . j ava 2  s  .co m
@Ignore
public void putRelationshipWriteAccessOnPublication() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1").withAuthor("Testy, TEst")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, publication, Constants.WRITE, user);

    context.restoreAuthSystemState();

    String token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author2.getID()))
            .andExpect(status().isOk()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.rightId", is(author2.getID().toString())));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 2 and author 1
 * Verify this is possible for a user with WRITE permissions on publication 1 and publication 2
 *//*from  w  w  w. j  a  v a 2s .  c  om*/
@Ignore
public void putRelationshipWriteAccessOnPublications() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication1 = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withAuthor("Testy, TEst").withIssueDate("2015-01-01").withRelationshipType("Publication").build();
    Item publication2 = ItemBuilder.createItem(context, col3).withTitle("Publication2")
            .withAuthor("Testy, TEstzeaze").withIssueDate("2015-01-01").withRelationshipType("Publication")
            .build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, publication1, Constants.WRITE, user);
    authorizeService.addPolicy(context, publication2, Constants.WRITE, user);

    context.restoreAuthSystemState();

    String token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication1.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication2.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isOk()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.leftId", is(publication2.getID().toString())));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 2 and author 1
 * Verify this is possible for a user with WRITE permissions on author 1
 *//* www .ja v a2 s.  com*/
@Ignore
public void putRelationshipWriteAccessOnAuthor() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication1 = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withAuthor("Testy, TEst").withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    Item publication2 = ItemBuilder.createItem(context, col3).withTitle("Publication2")
            .withAuthor("Testy, TEstzea").withIssueDate("2015-01-01").withRelationshipType("Publication")
            .build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, author1, Constants.WRITE, user);

    context.restoreAuthSystemState();

    String token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult = getClient(getAuthToken(admin.getEmail(), password))
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication1.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication2.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isOk()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.leftId", is(publication2.getID().toString())));

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 1 and author 2
 * Verify this is NOT possible for a user without WRITE permissions
 *//*  w w  w.ja v a2s  .  co  m*/
@Ignore
public void putRelationshipNoAccess() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1").withAuthor("Testy, TEst")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    context.restoreAuthSystemState();

    String token = getAuthToken(admin.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));
    token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author2.getID()))
            .andExpect(status().isForbidden()).andReturn();

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 1 and author 2
 * Verify this is NOT possible for a user with WRITE permissions on author 1
 *//*from  www.  j a  v a2  s . c  o m*/
@Ignore
public void putRelationshipOnlyAccessOnOneAuthor() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1").withAuthor("Testy, TEst")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, author1, Constants.WRITE, user);

    context.restoreAuthSystemState();

    String token = getAuthToken(admin.getEmail(), password);

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));
    token = getAuthToken(user.getEmail(), password);

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author2.getID()))
            .andExpect(status().isForbidden()).andReturn();

}

From source file:org.dspace.app.rest.RelationshipRestRepositoryIT.java

/**
 * Create a relationship between publication 1 and author 1
 * Change it to a relationship between publication 2 and author 1
 * Verify this is NOT possible for a user with WRITE permissions on publication 1
 *//*from w  ww  . j a v  a 2 s.  c  o  m*/
@Ignore
public void putRelationshipOnlyAccessOnOnePublication() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();
    Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
    Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
    Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();

    Item author1 = ItemBuilder.createItem(context, col1).withTitle("Author1").withIssueDate("2017-10-17")
            .withAuthor("Smith, Donald").withRelationshipType("Person").build();

    Item author2 = ItemBuilder.createItem(context, col1).withTitle("Author2").withIssueDate("2017-10-12")
            .withAuthor("Smith, Donalaze").withRelationshipType("Person").build();

    Item publication1 = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withAuthor("Testy, TEst").withIssueDate("2015-01-01").withRelationshipType("Publication").build();
    Item publication2 = ItemBuilder.createItem(context, col3).withTitle("Publication2")
            .withAuthor("Testy, TEstzeaze").withIssueDate("2015-01-01").withRelationshipType("Publication")
            .build();

    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

    EPerson user = EPersonBuilder.createEPerson(context).withNameInMetadata("first", "last")
            .withEmail("testaze@gmail.com").withPassword(password)
            .withLanguage(I18nUtil.getDefaultLocale().getLanguage()).build();
    context.setCurrentUser(user);

    authorizeService.addPolicy(context, publication1, Constants.WRITE, user);

    String token = getAuthToken(user.getEmail(), password);

    context.restoreAuthSystemState();

    MvcResult mvcResult = getClient(token)
            .perform(post("/api/core/relationships")
                    .param("relationshipType", isAuthorOfPublicationRelationshipType.getID().toString())
                    .contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication1.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isCreated()).andReturn();

    ObjectMapper mapper = new ObjectMapper();
    String content = mvcResult.getResponse().getContentAsString();
    Map<String, Object> map = mapper.readValue(content, Map.class);
    String id = String.valueOf(map.get("id"));

    MvcResult mvcResult2 = getClient(token)
            .perform(put("/api/core/relationships/" + id).contentType(MediaType.parseMediaType("text/uri-list"))
                    .content("https://localhost:8080/spring-rest/api/core/items/" + publication2.getID() + "\n"
                            + "https://localhost:8080/spring-rest/api/core/items/" + author1.getID()))
            .andExpect(status().isForbidden()).andReturn();

    getClient(token).perform(get("/api/core/relationships/" + id)).andExpect(status().isOk())
            .andExpect(jsonPath("$.leftId", is(publication1.getID().toString())));

}