TagsApi { @ApiOperation( value = "Create a tag", notes = "If labels are not defined, a default label is created " + "with the category name for all languages.", nickname = "putTag") @RequestMapping( method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_JSON_VALUE } ) @ResponseStatus(HttpStatus.CREATED) @ApiResponses(value = { @ApiResponse(code = 201, message = "Tag created. Return the new tag identifier."), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN) }) @PreAuthorize("hasRole('UserAdmin')") public ResponseEntity<Integer> putTag( @ApiParam( name = "category" ) @RequestBody MetadataCategory category ) throws Exception { MetadataCategory existingCategory = categoryRepository.findOne(category.getId()); if (existingCategory != null) { throw new IllegalArgumentException(String.format( "A tag with id '%d' already exist", category.getId() )); } else { java.util.List<Language> allLanguages = langRepository.findAll(); Map<String, String> labelTranslations = category.getLabelTranslations(); for (Language l : allLanguages) { String label = labelTranslations.get(l.getId()); category.getLabelTranslations().put(l.getId(), label == null ? category.getName() : label); } categoryRepository.save(category); return new ResponseEntity(HttpStatus.NO_CONTENT); } }  @ApiOperation( value = "Get tags", notes = "", nickname = "getTags") @RequestMapping( produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) @ApiResponses(value = { @ApiResponse(code = 200, message = "List of tags.") }) @ResponseBody List<org.fao.geonet.domain.MetadataCategory> getTags(
    ); @ApiOperation( value = "Create a tag", notes = "If labels are not defined, a default label is created " + "with the category name for all languages.", nickname = "putTag") @RequestMapping( method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_JSON_VALUE } ) @ResponseStatus(HttpStatus.CREATED) @ApiResponses(value = { @ApiResponse(code = 201, message = "Tag created. Return the new tag identifier."), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN) }) @PreAuthorize("hasRole('UserAdmin')") ResponseEntity<Integer> putTag(
        @ApiParam(
            name = "category"
        )
        @RequestBody
            MetadataCategory category
    ); @ApiOperation( value = "Get a tag", notes = "", nickname = "getTag") @RequestMapping( value = "/{tagIdentifier}", produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) @ApiResponses(value = { @ApiResponse(code = 200, message = "Tag details.") }) @ResponseBody org.fao.geonet.domain.MetadataCategory getTag(
        @ApiParam(
            value = "Tag identifier",
            required = true
        )
        @PathVariable
            Integer tagIdentifier
    ); @ApiOperation( value = "Update a tag", notes = "", nickname = "updateTag") @RequestMapping( value = "/{tagIdentifier}", method = RequestMethod.PUT) @PreAuthorize("hasRole('UserAdmin')") @ResponseStatus(HttpStatus.NO_CONTENT) @ApiResponses(value = { @ApiResponse(code = 204, message = "Tag updated."), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN) }) @ResponseBody ResponseEntity updateTag(
        @ApiParam(
            value = "Tag identifier",
            required = true
        )
        @PathVariable
            int tagIdentifier,
        @ApiParam(
            name = "category"
        )
        @RequestBody
            MetadataCategory category
    ); @ApiOperation( value = "Remove a tag", notes = "", nickname = "deleteTag") @RequestMapping( value = "/{tagIdentifier}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) @ApiResponses(value = { @ApiResponse(code = 204, message = "Tag removed."), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN) }) @PreAuthorize("hasRole('UserAdmin')") ResponseEntity deleteTag(
        @ApiParam(
            value = "Tag identifier",
            required = true
        )
        @PathVariable
            Integer tagIdentifier
    );  }
@Test public void putTag() throws Exception { MetadataCategory category = _categoriesRepo.findOneByName("newcategory"); Assert.assertNull(category); MetadataCategory newCategory = new MetadataCategory(); newCategory.setId(-99); newCategory.setName("newcategory"); Gson gson = new GsonBuilder() .setFieldNamingStrategy(new JsonFieldNamingStrategy()) .setExclusionStrategies(new FieldNameExclusionStrategy("_labelTranslations", "_records")) .create(); String json = gson.toJson(newCategory); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockHttpSession = loginAsAdmin(); this.mockMvc.perform(put("/srv/api/tags") .content(json) .contentType(API_JSON_EXPECTED_ENCODING) .session(this.mockHttpSession) .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); MetadataCategory categoryCreated = _categoriesRepo.findOneByName(newCategory.getName()); Assert.assertNotNull(categoryCreated); }