@Test
public void getRelated() throws Exception {

    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    MockHttpSession mockHttpSession = loginAsAdmin();
    addThumbnails(this.context);
    final MEFLibIntegrationTest.ImportMetadata importMetadata = new MEFLibIntegrationTest.ImportMetadata(this, context);
    importMetadata.getMefFilesToLoad().add("/org/fao/geonet/api/records/samples/mef2-related.zip");
    importMetadata.invoke();
    final String MAIN_UUID = "655bb8a1-0324-470f-8dff-bb64e849291c";
    final String DATASET_UUID = "842f9143-fd7d-452c-96b4-425ca1281642";
    mockMvc.perform(get("/srv/api/records/" + MAIN_UUID + "/related")
        .session(mockHttpSession)
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING));
    mockMvc.perform(get("/srv/api/records/" + MAIN_UUID + "/related")
        .session(mockHttpSession)
        .accept(MediaType.APPLICATION_XML))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_XML))
        .andExpect(xpath("/related/onlines").exists());
    mockMvc.perform(get("/srv/api/records/" + this.uuid + "/related")
        .param("type", RelatedItemType.thumbnails.toString())
        .session(mockHttpSession)
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
        //.andExpect(jsonPath("$.children").isNotEmpty())
        .andExpect(jsonPath("$." + RelatedItemType.thumbnails, notNullValue()));
    // Request each type
    for (RelatedItemType type : RelatedItemType.values()) {
        if (type == RelatedItemType.hassources ||
            type == RelatedItemType.related ||
            type == RelatedItemType.hasfeaturecats ||
            type == RelatedItemType.brothersAndSisters ||
            type == RelatedItemType.thumbnails) {
            // TODO modify mef2-related.zip test metadata to contain a valid hassources value
            continue;
        }
        String uuidToTest = MAIN_UUID;
        if (type == RelatedItemType.datasets) {
            uuidToTest = DATASET_UUID;
        }
        mockMvc.perform(get("/srv/api/records/" + uuidToTest + "/related")
            .param("type", type.toString())
            .session(mockHttpSession)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
            .andExpect(jsonPath("$." + type).isNotEmpty())
            .andExpect(jsonPath("$").value(hasKey(type.toString())));
        mockMvc.perform(get("/srv/api/records/" + uuidToTest + "/related")
            .param("type", type.toString())
            .session(mockHttpSession)
            .accept(MediaType.APPLICATION_XML))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_XML))
            .andExpect(xpath("/related/" + type.toString() + "/item").exists());
    }
    // Check start and row parameters
    mockMvc.perform(get("/srv/api/records/" + MAIN_UUID + "/related")
        .param("type", RelatedItemType.children.toString())
        .param("start", "2")
        .param("rows", "1")
        .session(mockHttpSession)
        .accept(MediaType.APPLICATION_JSON))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
        .andExpect(jsonPath("$." + RelatedItemType.children).isArray())
        .andExpect(jsonPath("$." + RelatedItemType.children, hasSize(1)));
    mockMvc.perform(get("/srv/api/records/" + MAIN_UUID + "/related")
        .param("type", RelatedItemType.children.toString())
        .param("start", "2")
        .param("rows", "1")
        .session(mockHttpSession)
        .accept(MediaType.APPLICATION_XML))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_XML))
        .andExpect(xpath("/related/" + RelatedItemType.children + "/item").exists())
        .andExpect(xpath("/related/" + RelatedItemType.children + "/item").nodeCount(1));
}