@Test
public void addDeleteSelection() throws Exception {

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    String name = "notification";
    // Check not in db
    Selection selection = selectionRepository.findOneByName(name);
    assertNull(selection);
    Selection newSelection = new Selection();
    newSelection.setId(10);
    newSelection.setName(name);
    newSelection.setWatchable(true);
    Map<String, String> t = new HashMap<>();
    t.put("fre", "french");
    newSelection.setLabelTranslations(t);
    Gson gson = new GsonBuilder()
        .setFieldNamingStrategy(new JsonFieldNamingStrategy())
        .setExclusionStrategies(new FieldNameExclusionStrategy("_labelTranslations", "watchable"))
        .create();
    String json = gson.toJson(newSelection);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    this.mockHttpSession = loginAsAdmin();
    // Create
    MvcResult r = this.mockMvc.perform(put("/srv/api/userselections")
        .content(json)
        .contentType(API_JSON_EXPECTED_ENCODING)
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().is(201))
        .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
        .andReturn();
    // Check in DB
    Selection createdSelection = selectionRepository.findOneByName(name);
    assertNotNull(createdSelection);
    assertEquals(name, createdSelection.getName());
    assertEquals(false, createdSelection.isWatchable());
    // Check in API
    // Unknown selection set return 404
    this.mockMvc.perform(put("/srv/api/userselections/11111/11111")
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isNotFound());
    // Unknown user return 404
    this.mockMvc.perform(put("/srv/api/userselections/" + createdSelection.getId() + "/11111")
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isNotFound());
    // Unkown metadata return 404
    this.mockMvc.perform(put("/srv/api/userselections/" + createdSelection.getId() + "/1?uuid=ABCD")
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isNotFound());
    String metadataId = importMetadata(context);
    String metadataUuid = _dataManager.getMetadataUuid(metadataId);
    this.mockMvc.perform(put("/srv/api/userselections/" + createdSelection.getId() + "/1?uuid=" + metadataUuid)
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isCreated());
    this.mockMvc.perform(get("/srv/api/userselections/" + createdSelection.getId() + "/1")
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isOk())
        .andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
        .andExpect(jsonPath("$", hasSize(1)))
        .andExpect(jsonPath("$[*]", hasItem(metadataUuid)));
    this.mockMvc.perform(delete("/srv/api/userselections/" + createdSelection.getId() + "/1?uuid=" + metadataUuid)
        .session(this.mockHttpSession)
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().isNoContent());
    // Delete
    this.mockMvc.perform(delete("/srv/api/userselections/" + createdSelection.getId())
        .accept(MediaType.parseMediaType("application/json")))
        .andExpect(status().is(204));
    // Check in DB
    assertFalse(selectionRepository.exists(createdSelection.getId()));
    ;
}