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.cbioportal.weblegacy.ApiControllerTest.java

@Ignore
@Test// w  w w  . j a  v  a 2s. c  o m
public void geneticProfileDataTest2() throws Exception {
    List<Mutation> mockResponse = getGeneticprofiledataQuery1ServiceMock().subList(0, 2);
    List<DBGeneticProfile> ctMockResponse = getGeneticProfileQuery1ServiceMock();
    Mockito.when(
            geneticProfileMapperLegacyMock.getGeneticProfiles(org.mockito.Matchers.anyListOf(String.class)))
            .thenReturn(ctMockResponse);
    Mockito.when(mutationMapperLegacy.getMutationsDetailed(org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyListOf(String.class), org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyString())).thenReturn(mockResponse);
    this.mockMvc.perform(MockMvcRequestBuilders.get("/geneticprofiledata")
            .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))
            .param("genetic_profile_ids", "brca_tcga_mutations").param("genes", "AKT1")
            .param("sample_ids", "TCGA-AN-A0XR-01,TCGA-GM-A3NW-01").param("sample_list_id", "brca_tcga_all"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(
                    MockMvcResultMatchers.content().contentTypeCompatibleWith("application/json;charset=UTF-8"))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sample_list_id").value("brca_tcga_all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sample_list_id").value("brca_tcga_all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].genetic_profile_id").value("brca_tcga_mutations"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].genetic_profile_id").value("brca_tcga_mutations"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].study_id").value("brca_tcga"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].study_id").value("brca_tcga"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sequencing_center").value("genome.wustl.edu"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sequencing_center").value("genome.wustl.edu"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_status").value("Somatic"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_status").value("Somatic"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_type").value("Missense_Mutation"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_type").value("Missense_Mutation"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].validation_status").value("Untested"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].validation_status").value("Untested"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].entrez_gene_id").value("207"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].hugo_gene_symbol").value("AKT1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sample_id").value("TCGA-AN-A0XR-01"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].amino_acid_change").value("L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].functional_impact_score").value("M"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link")
                    .value("getma.org/?cm=var&var=hg19,14,105246445,A,C&fts=all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link_pdb")
                    .value("getma.org/pdb.php?prot=AKT1_HUMAN&from=6&to=108&var=L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link_msa")
                    .value("getma.org/?cm=msa&ty=f&p=AKT1_HUMAN&rb=6&re=108&var=L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].chr").value("14"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].start_position").value("105246445"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].end_position").value("105246445"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].protein_start_position").value("52"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].protein_end_position").value("52"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_allele").value("A"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_allele").value("C"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].entrez_gene_id").value("207"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].hugo_gene_symbol").value("AKT1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sample_id").value("TCGA-GM-A3NW-01"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].amino_acid_change").value("V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].functional_impact_score").value("N"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link")
                    .value("getma.org/?cm=var&var=hg19,14,105258971,C,G&fts=all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link_pdb")
                    .value("getma.org/pdb.php?prot=AKT1_HUMAN&from=1&to=5&var=V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link_msa")
                    .value("getma.org/?cm=msa&ty=f&p=AKT1_HUMAN&rb=1&re=35&var=V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].chr").value("14"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].start_position").value("105258971"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].end_position").value("105258971"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].protein_start_position").value("4"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].protein_end_position").value("4"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_allele").value("C"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_allele").value("G"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_event_id").doesNotExist())
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_event_id").doesNotExist());
}

From source file:org.cbioportal.weblegacy.ApiControllerTest.java

@Test
@Ignore/*www . ja v a2 s .  co m*/
public void geneticProfileDataTest3() throws Exception {
    List<Mutation> mockResponse = getGeneticprofiledataQuery1ServiceMock().subList(0, 0);
    List<DBGeneticProfile> ctMockResponse = getGeneticProfileQuery1ServiceMock();
    Mockito.when(
            geneticProfileMapperLegacyMock.getGeneticProfiles(org.mockito.Matchers.anyListOf(String.class)))
            .thenReturn(ctMockResponse);
    Mockito.when(mutationMapperLegacy.getMutationsDetailed(org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyListOf(String.class), org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyString())).thenReturn(mockResponse);
    this.mockMvc.perform(MockMvcRequestBuilders.get("/geneticprofiledata")
            .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))
            .param("genetic_profile_ids", "brca_tcga_mutations").param("genes", "unrecognized_gene_identifier")
            .param("sample_ids", "").param("sample_list_id", "brca_tcga_all"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(
                    MockMvcResultMatchers.content().contentTypeCompatibleWith("application/json;charset=UTF-8"))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(0)));
}

From source file:org.cbioportal.weblegacy.ApiControllerTest.java

@Ignore
@Test/*from  w  w w  .  jav a  2  s  . c o  m*/
public void geneticProfileDataTest4() throws Exception {
    List<Mutation> mockResponse = getGeneticprofiledataQuery1ServiceMock().subList(0, 2);
    List<DBGeneticProfile> ctMockResponse = getGeneticProfileQuery1ServiceMock();
    Mockito.when(
            geneticProfileMapperLegacyMock.getGeneticProfiles(org.mockito.Matchers.anyListOf(String.class)))
            .thenReturn(ctMockResponse);
    Mockito.when(mutationMapperLegacy.getMutationsDetailed(org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyListOf(String.class), org.mockito.Matchers.anyListOf(String.class),
            org.mockito.Matchers.anyString())).thenReturn(mockResponse);
    this.mockMvc
            .perform(MockMvcRequestBuilders.get("/geneticprofiledata")
                    .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))
                    .param("genetic_profile_ids", "brca_tcga_mutations").param("genes", "AKT1")
                    .param("sample_ids", "TCGA-AN-A0XR-01,TCGA-GM-A3NW-01"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(
                    MockMvcResultMatchers.content().contentTypeCompatibleWith("application/json;charset=UTF-8"))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sample_list_id").doesNotExist())
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sample_list_id").doesNotExist())
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].genetic_profile_id").value("brca_tcga_mutations"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].genetic_profile_id").value("brca_tcga_mutations"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].study_id").value("brca_tcga"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].study_id").value("brca_tcga"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sequencing_center").value("genome.wustl.edu"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sequencing_center").value("genome.wustl.edu"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_status").value("Somatic"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_status").value("Somatic"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_type").value("Missense_Mutation"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_type").value("Missense_Mutation"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].validation_status").value("Untested"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].validation_status").value("Untested"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_read_count_tumor").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_read_count_normal").value("-1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].entrez_gene_id").value("207"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].hugo_gene_symbol").value("AKT1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].sample_id").value("TCGA-AN-A0XR-01"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].amino_acid_change").value("L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].functional_impact_score").value("M"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link")
                    .value("getma.org/?cm=var&var=hg19,14,105246445,A,C&fts=all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link_pdb")
                    .value("getma.org/pdb.php?prot=AKT1_HUMAN&from=6&to=108&var=L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].xvar_link_msa")
                    .value("getma.org/?cm=msa&ty=f&p=AKT1_HUMAN&rb=6&re=108&var=L52R"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].chr").value("14"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].start_position").value("105246445"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].end_position").value("105246445"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].protein_start_position").value("52"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].protein_end_position").value("52"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].reference_allele").value("A"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].variant_allele").value("C"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].entrez_gene_id").value("207"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].hugo_gene_symbol").value("AKT1"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].sample_id").value("TCGA-GM-A3NW-01"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].amino_acid_change").value("V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].functional_impact_score").value("N"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link")
                    .value("getma.org/?cm=var&var=hg19,14,105258971,C,G&fts=all"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link_pdb")
                    .value("getma.org/pdb.php?prot=AKT1_HUMAN&from=1&to=5&var=V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].xvar_link_msa")
                    .value("getma.org/?cm=msa&ty=f&p=AKT1_HUMAN&rb=1&re=35&var=V4L"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].chr").value("14"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].start_position").value("105258971"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].end_position").value("105258971"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].protein_start_position").value("4"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].protein_end_position").value("4"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].reference_allele").value("C"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].variant_allele").value("G"))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].mutation_event_id").doesNotExist())
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].mutation_event_id").doesNotExist());
}

From source file:org.craftercms.engine.view.CrafterPageView.java

@Override
public void afterPropertiesSet() throws Exception {
    String mimeType = getMimeType();
    if (StringUtils.isNotEmpty(mimeType)) {
        setContentType(MediaType.parseMediaType(mimeType).toString() + ";charset=" + DEFAULT_CHARSET);
    } else {//from w  w  w . j a  va 2 s  .com
        setContentType(DEFAULT_CONTENT_TYPE);
    }
}

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

@Test
public void createRelationshipWriteAccessLeftItem() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();//from   w w w .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 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(
                            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 firstRelationshipIdString = String.valueOf(map.get("id"));

    getClient().perform(get("/api/core/relationships/" + firstRelationshipIdString)).andExpect(status().isOk())
            .andExpect(jsonPath("$.leftId", is(publication.getID().toString())))
            .andExpect(jsonPath("$.rightId", is(author1.getID().toString())));

}

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

@Test
public void createRelationshipWriteAccessRightItem() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();//  w  ww .j a  va 2s  .c o  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 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(user.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 firstRelationshipIdString = String.valueOf(map.get("id"));

    getClient().perform(get("/api/core/relationships/" + firstRelationshipIdString)).andExpect(status().isOk())
            .andExpect(jsonPath("$.leftId", is(publication.getID().toString())))
            .andExpect(jsonPath("$.rightId", is(author1.getID().toString())));
}

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

@Test
public void createRelationshipNoWriteAccess() throws Exception {

    context.turnOffAuthorisationSystem();

    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity).withName("Sub Community")
            .build();/*from   ww w . j  a v a2  s  . c o  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 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(user.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().isForbidden()).andReturn();

}

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

/**
 * This method will test the addition of a mixture of plain-text metadatavalues and relationships to then
 * verify that the place property is still being handled correctly.
 * @throws Exception/*  ww w  .j  a  v a2s.  c  o m*/
 */
@Test
public void addRelationshipsAndMetadataToValidatePlaceTest() 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")
            .withPersonIdentifierFirstName("Donald").withPersonIdentifierLastName("Smith")
            .withRelationshipType("Person").build();

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

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

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();
    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

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

    context.restoreAuthSystemState();
    // Here we create our first Relationship to the Publication to give it a dc.contributor.author virtual
    // metadata field.
    MvcResult mvcResult = getClient(adminToken)
            .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 firstRelationshipIdString = String.valueOf(map.get("id"));

    // Here we call the relationship and verify that the relationship's leftplace is 0
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    context.turnOffAuthorisationSystem();

    // Make sure we grab the latest instance of the Item from the database
    publication = itemService.find(context, publication.getID());
    // Add a plain text dc.contributor.author value
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
    itemService.update(context, publication);

    List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    // Ensure that the list of dc.contributor.author values now holds two values ("Smith, Donald" and "plain text")
    assertEquals(2, list.size());
    for (MetadataValue mdv : list) {
        // Here we want to ensure that the "plain text" metadatavalue has place 1 because it was added later than
        // the Relationship, so the "Smith, Donald" should have place 0 and "plain text" should have place 1
        if (StringUtils.equals(mdv.getValue(), "plain text")) {
            assertEquals(1, mdv.getPlace());
        }
    }
    // Testing what was describe above
    MetadataValue author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    MetadataValue author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());

    context.restoreAuthSystemState();
    // Verfiy the leftPlace of our relationship is still 0.
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    // Create another Relationship for the Publication, thus creating a third dc.contributor.author mdv
    mvcResult = getClient(adminToken)
            .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/" + author2.getID()))
            .andExpect(status().isCreated()).andReturn();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    // Grab the ID for the second relationship for future calling in the rest
    String secondRelationshipIdString = String.valueOf(map.get("id"));

    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
    // Ensure that we now have three dc.contributor.author mdv ("Smith, Donald", "plain text", "Smith, Maria"
    // In that order which will be checked below the rest call
    assertEquals(3, list.size());
    // Perform the REST call to the relationship to ensure its leftPlace is 2 even though it's only the second
    // Relationship. Note that leftPlace 1 was skipped due to the dc.contributor.author plain text value and
    // This is expected behaviour and should be tested
    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(2)));

    author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());
    MetadataValue author2MD = list.get(2);
    assertEquals("Smith, Maria", author2MD.getValue());

    context.turnOffAuthorisationSystem();
    // Ensure we have the latest instance of the Item from the database
    publication = itemService.find(context, publication.getID());
    // Add a fourth dc.contributor.author mdv
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
    itemService.update(context, publication);

    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    // Assert that the list of dc.contributor.author mdv is now of size 4 in the following order:
    // "Smith, Donald", "plain text", "Smith, Maria", "plain text two"
    assertEquals(4, list.size());
    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text two")) {
            assertEquals(3, mdv.getPlace());
        }
    }

    author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());
    author2MD = list.get(2);
    assertEquals("Smith, Maria", author2MD.getValue());
    MetadataValue author3MD = list.get(3);
    assertEquals("plain text two", author3MD.getValue());

    // Create the third Relationship thus adding a fifth dc.contributor.author mdv
    mvcResult = getClient(adminToken)
            .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/" + author3.getID()))
            .andExpect(status().isCreated()).andReturn();

    content = mvcResult.getResponse().getContentAsString();
    map = mapper.readValue(content, Map.class);
    // Save the third Relationship's ID for future calling
    String thirdRelationshipIdString = String.valueOf(map.get("id"));

    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
    // Assert that our dc.contributor.author mdv list is now of size 5
    assertEquals(5, list.size());
    // Assert that the third Relationship has leftPlace 4, even though 3 relationships were created.
    // This is because the plain text values 'occupy' place 1 and 3.
    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(4)));

    // Assert that the list is of size 5 and in the following order:
    // "Smith, Donald", "plain text", "Smith, Maria", "plain text two", "Maybe, Maybe"
    // Thus the order they were added in
    author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());
    author2MD = list.get(2);
    assertEquals("Smith, Maria", author2MD.getValue());
    author3MD = list.get(3);
    assertEquals("plain text two", author3MD.getValue());
    MetadataValue author4MD = list.get(4);
    assertEquals("Maybe, Maybe", author4MD.getValue());

    context.turnOffAuthorisationSystem();
    // The following additions of Metadata will perform the same sequence of logic and tests as described above
    publication = itemService.find(context, publication.getID());
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
    itemService.update(context, publication);

    context.restoreAuthSystemState();

    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    assertEquals(6, list.size());
    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text three")) {
            assertEquals(5, mdv.getPlace());
        }
    }

    author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());
    author2MD = list.get(2);
    assertEquals("Smith, Maria", author2MD.getValue());
    author3MD = list.get(3);
    assertEquals("plain text two", author3MD.getValue());
    author4MD = list.get(4);
    assertEquals("Maybe, Maybe", author4MD.getValue());
    MetadataValue author5MD = list.get(5);
    assertEquals("plain text three", author5MD.getValue());

    context.turnOffAuthorisationSystem();

    publication = itemService.find(context, publication.getID());
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text four");
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text five");
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text six");
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text seven");
    itemService.update(context, publication);

    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    assertEquals(10, list.size());
    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text four")) {
            assertEquals(6, mdv.getPlace());
        }
        if (StringUtils.equals(mdv.getValue(), "plain text five")) {
            assertEquals(7, mdv.getPlace());
        }
        if (StringUtils.equals(mdv.getValue(), "plain text six")) {
            assertEquals(8, mdv.getPlace());
        }
        if (StringUtils.equals(mdv.getValue(), "plain text seven")) {
            assertEquals(9, mdv.getPlace());
        }
    }

    author0MD = list.get(0);
    assertEquals("Smith, Donald", author0MD.getValue());
    author1MD = list.get(1);
    assertEquals("plain text", author1MD.getValue());
    author2MD = list.get(2);
    assertEquals("Smith, Maria", author2MD.getValue());
    author3MD = list.get(3);
    assertEquals("plain text two", author3MD.getValue());
    author4MD = list.get(4);
    assertEquals("Maybe, Maybe", author4MD.getValue());
    author5MD = list.get(5);
    assertEquals("plain text three", author5MD.getValue());
    MetadataValue author6MD = list.get(6);
    assertEquals("plain text four", author6MD.getValue());
    MetadataValue author7MD = list.get(7);
    assertEquals("plain text five", author7MD.getValue());
    MetadataValue author8MD = list.get(8);
    assertEquals("plain text six", author8MD.getValue());
    MetadataValue author9MD = list.get(9);
    assertEquals("plain text seven", author9MD.getValue());

    list = itemService.getMetadata(publication, "dc", "contributor", Item.ANY, Item.ANY);
    assertEquals(10, list.size()); //same size as authors
    list = itemService.getMetadata(publication, "dc", Item.ANY, Item.ANY, Item.ANY);
    assertEquals(16, list.size()); //also includes title, 4 date fields, uri
    list = itemService.getMetadata(publication, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
    assertEquals(20, list.size()); //also includes type and 3 relation.isAuthorOfPublication values
}

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

/**
 * This method will test the deletion of a plain-text metadatavalue to then
 * verify that the place property is still being handled correctly.
 * @throws Exception/*  w  w w . j a  v  a 2s. co  m*/
 */
@Test
public void deleteMetadataValueAndValidatePlace() 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")
            .withPersonIdentifierFirstName("Donald").withPersonIdentifierLastName("Smith")
            .withRelationshipType("Person").build();

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

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

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();
    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

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

    // First create the structure of 5 metadatavalues just like the additions test.
    context.restoreAuthSystemState();
    // This post request will add a first relationship to the publiction and thus create a first set of metadata
    // For the author values, namely "Donald Smith"
    MvcResult mvcResult = getClient(adminToken)
            .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 firstRelationshipIdString = String.valueOf(map.get("id"));

    // This test will double check that the leftPlace for this relationship is indeed 0
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    context.turnOffAuthorisationSystem();
    // We retrieve the publication again to ensure that we have the latest DB object of it
    publication = itemService.find(context, publication.getID());
    // Add a plain text metadatavalue to the publication
    // After this addition, the list of authors should like like "Donald Smith", "plain text"
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text")) {
            // Ensure that this is indeed the second metadatavalue in the list of authors for the publication
            assertEquals(1, mdv.getPlace());
        }
    }

    // This test checks again that the first relationship is still on leftplace 0 and not altered
    // Because of the MetadataValue addition
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    // Creates another Relationship for the Publication and thus adding a third metadata value for the author
    mvcResult = getClient(adminToken)
            .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/" + author2.getID()))
            .andExpect(status().isCreated()).andReturn();

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

    // This test verifies that the newly created Relationship is on leftPlace 2, because the first relationship
    // is on leftPlace 0 and the plain text metadataValue occupies the place 1
    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(2)));
    context.turnOffAuthorisationSystem();
    // Get the publication from the DB again to ensure that we have the latest object
    publication = itemService.find(context, publication.getID());
    // Add a fourth metadata value to the publication
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text two")) {
            // Ensure that this plain text metadata value is on the fourth place (place 3) for the publication
            assertEquals(3, mdv.getPlace());
        }
    }

    // The list should currently look like this: "Donald Smith", "plain text", "Maria Smith", "plain text two"

    // This creates a third relationship for the publication and thus adding a fifth value for author metadatavalues
    mvcResult = getClient(adminToken)
            .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/" + author3.getID()))
            .andExpect(status().isCreated()).andReturn();

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

    // This verifies that the newly created third relationship is on leftPlace 4.
    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(4)));

    context.turnOffAuthorisationSystem();
    publication = itemService.find(context, publication.getID());
    // Create another plain text metadata value on the publication
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text three")) {
            // Verify that this plain text value is indeed the 6th author in the list (place 5)
            assertEquals(5, mdv.getPlace());
        }
    }

    // Now we will have a dc.contributor.author metadatavalue list of size 6 in the following order:
    // "Smith, Donald", "plain text", "Smith, Maria", "plain text two", "Maybe, Maybe", "plain text three"
    List<MetadataValue> authors = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
    List<MetadataValue> listToRemove = new LinkedList<>();
    for (MetadataValue metadataValue : authors) {
        if (StringUtils.equals(metadataValue.getValue(), "plain text two")) {
            listToRemove.add(metadataValue);
        }
    }

    context.turnOffAuthorisationSystem();
    // Remove the "plain text two" metadatavalue. Ensure that all mdvs prior to that in the list are unchanged
    // And ensure that the ones coming after this mdv have its place lowered by one.
    itemService.removeMetadataValues(context, publication, listToRemove);

    itemService.update(context, publication);
    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text")) {
            assertEquals(1, mdv.getPlace());
        }
    }
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text three")) {
            assertEquals(4, mdv.getPlace());
        }
    }

    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));
    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(2)));
    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(3)));

}

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

/**
 * This method will test the deletion of a Relationship to then
 * verify that the place property is still being handled correctly.
 * @throws Exception// ww w  . j a  v a 2  s  .c o  m
 */
@Test
public void deleteRelationshipsAndValidatePlace() 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")
            .withPersonIdentifierFirstName("Donald").withPersonIdentifierLastName("Smith")
            .withRelationshipType("Person").build();

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

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

    Item publication = ItemBuilder.createItem(context, col3).withTitle("Publication1")
            .withIssueDate("2015-01-01").withRelationshipType("Publication").build();
    RelationshipType isAuthorOfPublicationRelationshipType = relationshipTypeService.findbyTypesAndLabels(
            context, entityTypeService.findByEntityType(context, "Publication"),
            entityTypeService.findByEntityType(context, "Person"), "isAuthorOfPublication",
            "isPublicationOfAuthor");

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

    // First create the structure of 5 metadatavalues just like the additions test.
    context.restoreAuthSystemState();
    // This post request will add a first relationship to the publiction and thus create a first set of metadata
    // For the author values, namely "Donald Smith"
    MvcResult mvcResult = getClient(adminToken)
            .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 firstRelationshipIdString = String.valueOf(map.get("id"));

    // This test will double check that the leftPlace for this relationship is indeed 0
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    context.turnOffAuthorisationSystem();
    // We retrieve the publication again to ensure that we have the latest DB object of it
    publication = itemService.find(context, publication.getID());
    // Add a plain text metadatavalue to the publication
    // After this addition, the list of authors should like like "Donald Smith", "plain text"
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text")) {
            // Ensure that this is indeed the second metadatavalue in the list of authors for the publication
            assertEquals(1, mdv.getPlace());
        }
    }

    // This test checks again that the first relationship is still on leftplace 0 and not altered
    // Because of the MetadataValue addition
    getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(0)));

    // Creates another Relationship for the Publication and thus adding a third metadata value for the author
    mvcResult = getClient(adminToken)
            .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/" + author2.getID()))
            .andExpect(status().isCreated()).andReturn();

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

    // This test verifies that the newly created Relationship is on leftPlace 2, because the first relationship
    // is on leftPlace 0 and the plain text metadataValue occupies the place 1
    getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(2)));
    context.turnOffAuthorisationSystem();
    // Get the publication from the DB again to ensure that we have the latest object
    publication = itemService.find(context, publication.getID());
    // Add a fourth metadata value to the publication
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text two")) {
            // Ensure that this plain text metadata value is on the fourth place (place 3) for the publication
            assertEquals(3, mdv.getPlace());
        }
    }

    // The list should currently look like this: "Donald Smith", "plain text", "Maria Smith", "plain text two"

    // This creates a third relationship for the publication and thus adding a fifth value for author metadatavalues
    mvcResult = getClient(adminToken)
            .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/" + author3.getID()))
            .andExpect(status().isCreated()).andReturn();

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

    // This verifies that the newly created third relationship is on leftPlace 4.
    getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
            .andExpect(status().isOk()).andExpect(jsonPath("leftPlace", is(4)));

    context.turnOffAuthorisationSystem();
    publication = itemService.find(context, publication.getID());
    // Create another plain text metadata value on the publication
    itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
    itemService.update(context, publication);
    context.restoreAuthSystemState();
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text three")) {
            // Verify that this plain text value is indeed the 6th author in the list (place 5)
            assertEquals(5, mdv.getPlace());
        }
    }

    // Now we will have a dc.contributor.author metadatavalue list of size 6 in the following order:
    // "Smith, Donald", "plain text", "Smith, Maria", "plain text two", "Maybe, Maybe", "plain text three"

    // Now we delete the second relationship, the one that made "Smith, Maria" metadatavalue
    // Ensure that all metadatavalues before this one in the list still hold the same place
    // Ensure that all the metadatavalues after this one have their place lowered by one
    getClient(adminToken).perform(delete("/api/core/relationships/" + secondRelationshipIdString));

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

    publication = itemService.find(context, publication.getID());
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text")) {
            assertEquals(1, mdv.getPlace());
        }
    }
    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text two")) {
            assertEquals(2, mdv.getPlace());
        }
    }

    list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);

    for (MetadataValue mdv : list) {
        if (StringUtils.equals(mdv.getValue(), "plain text three")) {
            assertEquals(4, mdv.getPlace());
        }
    }

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

}