@SuppressWarnings("unchecked") @Test void testCreateEntities() throws Exception { mockMvc .perform( post(HREF_ENTITY_COLLECTION) .content("{entities:[{id:'p1', name:'Piet'}, {id:'p2', name:'Pietje'}]}") .contentType(APPLICATION_JSON)) .andExpect(status().isCreated()) .andExpect(content().contentType(APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.location", is("/api/v2/entity?q=id=in=(\"p1\",\"p2\")"))) .andExpect(jsonPath("$.resources", hasSize(2))) .andExpect(jsonPath("$.resources[0].href", is("/api/v2/entity/p1"))) .andExpect(jsonPath("$.resources[1].href", is("/api/v2/entity/p2"))); verify(dataService).add(eq(ENTITY_NAME), (Stream<Entity>) any(Stream.class)); }
@SuppressWarnings("java:S2259") @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) { ServletUriComponentsBuilder uriBuilder = createUriBuilder(); final EntityType meta = dataService.getEntityType(entityTypeId); try { final List<Entity> entities = request.getEntities().stream() .map(e -> this.restService.toEntity(meta, e)) .collect(toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<>(); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { entities.stream() .map(attribute -> (Attribute) attribute) .forEach(attribute -> dataService.getMeta().addAttribute(attribute)); } else { this.dataService.add(entityTypeId, entities.stream()); } entities.forEach( entity -> { restService.updateMappedByEntities(entity); String id = entity.getIdValue().toString(); ids.add(id); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( UriUtils.createEntityUriPath(uriBuilder, entityTypeId, id))); }); responseBody.setLocation( UriUtils.createEntitiesUriPath( uriBuilder, entityTypeId, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } }
RestControllerV2 { @SuppressWarnings("java:S2259") @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) { ServletUriComponentsBuilder uriBuilder = createUriBuilder(); final EntityType meta = dataService.getEntityType(entityTypeId); try { final List<Entity> entities = request.getEntities().stream() .map(e -> this.restService.toEntity(meta, e)) .collect(toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<>(); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { entities.stream() .map(attribute -> (Attribute) attribute) .forEach(attribute -> dataService.getMeta().addAttribute(attribute)); } else { this.dataService.add(entityTypeId, entities.stream()); } entities.forEach( entity -> { restService.updateMappedByEntities(entity); String id = entity.getIdValue().toString(); ids.add(id); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( UriUtils.createEntityUriPath(uriBuilder, entityTypeId, id))); }); responseBody.setLocation( UriUtils.createEntitiesUriPath( uriBuilder, entityTypeId, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } } }
RestControllerV2 { @SuppressWarnings("java:S2259") @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) { ServletUriComponentsBuilder uriBuilder = createUriBuilder(); final EntityType meta = dataService.getEntityType(entityTypeId); try { final List<Entity> entities = request.getEntities().stream() .map(e -> this.restService.toEntity(meta, e)) .collect(toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<>(); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { entities.stream() .map(attribute -> (Attribute) attribute) .forEach(attribute -> dataService.getMeta().addAttribute(attribute)); } else { this.dataService.add(entityTypeId, entities.stream()); } entities.forEach( entity -> { restService.updateMappedByEntities(entity); String id = entity.getIdValue().toString(); ids.add(id); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( UriUtils.createEntityUriPath(uriBuilder, entityTypeId, id))); }); responseBody.setLocation( UriUtils.createEntitiesUriPath( uriBuilder, entityTypeId, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } } RestControllerV2(
      DataService dataService,
      UserPermissionEvaluator permissionService,
      RestService restService,
      LocalizationService localizationService,
      PermissionSystemService permissionSystemService,
      RepositoryCopier repoCopier); }
RestControllerV2 { @SuppressWarnings("java:S2259") @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) { ServletUriComponentsBuilder uriBuilder = createUriBuilder(); final EntityType meta = dataService.getEntityType(entityTypeId); try { final List<Entity> entities = request.getEntities().stream() .map(e -> this.restService.toEntity(meta, e)) .collect(toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<>(); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { entities.stream() .map(attribute -> (Attribute) attribute) .forEach(attribute -> dataService.getMeta().addAttribute(attribute)); } else { this.dataService.add(entityTypeId, entities.stream()); } entities.forEach( entity -> { restService.updateMappedByEntities(entity); String id = entity.getIdValue().toString(); ids.add(id); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( UriUtils.createEntityUriPath(uriBuilder, entityTypeId, id))); }); responseBody.setLocation( UriUtils.createEntitiesUriPath( uriBuilder, entityTypeId, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } } RestControllerV2(
      DataService dataService,
      UserPermissionEvaluator permissionService,
      RestService restService,
      LocalizationService localizationService,
      PermissionSystemService permissionSystemService,
      RepositoryCopier repoCopier); @Autowired @Deprecated @Transactional(readOnly = true) @GetMapping("/version") Map<String, String> getVersion(
      @Value("${molgenis.version:@null}") String molgenisVersion,
      @Value("${molgenis.build.date:@null}") String molgenisBuildDate); @Transactional(readOnly = true) @GetMapping("/{entityTypeId}/{id}") Map<String, Object> retrieveEntity(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("id") String untypedId,
      @RequestParam(value = "attrs", required = false) AttributeFilter attributeFilter,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @PostMapping(value = "/{entityTypeId}/{id}", params = "_method=GET") Map<String, Object> retrieveEntityPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("id") String untypedId,
      @RequestParam(value = "attrs", required = false) AttributeFilter attributeFilter,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional @DeleteMapping("/{entityTypeId:^(?!i18n).+}/{id}") @ResponseStatus(NO_CONTENT) void deleteEntity(
      @PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId); @DeleteMapping("/{entityTypeId}") @ResponseStatus(NO_CONTENT) void deleteEntityCollection(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionDeleteRequestV2 request); @Transactional(readOnly = true) @GetMapping("/{entityTypeId}") EntityCollectionResponseV2 retrieveEntityCollection(
      @PathVariable("entityTypeId") String entityTypeId,
      @Valid EntityCollectionRequestV2 request,
      HttpServletRequest httpRequest,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @PostMapping(value = "/{entityTypeId}", params = "_method=GET") EntityCollectionResponseV2 retrieveEntityCollectionPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @Valid EntityCollectionRequestV2 request,
      HttpServletRequest httpRequest,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @GetMapping(value = "/{entityTypeId}/meta/{attributeName}", produces = APPLICATION_JSON_VALUE) AttributeResponseV2 retrieveEntityAttributeMeta(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName); @Transactional(readOnly = true) @PostMapping( value = "/{entityTypeId}/meta/{attributeName}", params = "_method=GET", produces = APPLICATION_JSON_VALUE) AttributeResponseV2 retrieveEntityAttributeMetaPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) EntityCollectionBatchCreateResponseBodyV2 createEntities(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional @PostMapping(value = "copy/{entityTypeId}", produces = APPLICATION_JSON_VALUE) String copyEntity(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid CopyEntityRequestV2 request,
      HttpServletResponse response); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PutMapping("/{entityTypeId}") synchronized void updateEntities(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional(readOnly = true) @GetMapping(value = "/i18n", produces = APPLICATION_JSON_VALUE) Map<String, String> getL10nStrings(); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PutMapping("/{entityTypeId}/{attributeName}") @ResponseStatus(OK) synchronized void updateAttribute(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional(readOnly = true) @GetMapping( value = "/i18n/{namespace}/{language}", produces = APPLICATION_JSON_VALUE + ";charset=UTF-8") Map<String, String> getL10nStrings(
      @PathVariable String namespace, @PathVariable String language); @Deprecated @Transactional(readOnly = true) @GetMapping( value = "/i18n/{namespace}_{language}.properties", produces = TEXT_PLAIN_VALUE + ";charset=UTF-8 ") String getL10nProperties(@PathVariable String namespace, @PathVariable String language); @Deprecated @Transactional @PostMapping("/i18n/{namespace}") @ResponseStatus(CREATED) void registerMissingResourceStrings(
      @PathVariable String namespace, HttpServletRequest request); @Deprecated @Transactional @DeleteMapping("/i18n/{namespace}") @ResponseStatus(NO_CONTENT) void deleteNamespace(@PathVariable String namespace); @ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleHttpMessageNotReadableException(
      HttpMessageNotReadableException exception); @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleMethodArgumentNotValidException(
      MethodArgumentNotValidException exception); @ExceptionHandler(MolgenisDataException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleMolgenisDataException(MolgenisDataException e); @ExceptionHandler(MolgenisDataAccessException.class) @ResponseStatus(UNAUTHORIZED) @ResponseBody ErrorMessageResponse handleMolgenisDataAccessException(MolgenisDataAccessException e); @ExceptionHandler(ConversionFailedException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleConversionFailedException(ConversionFailedException e); }
RestControllerV2 { @SuppressWarnings("java:S2259") @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) { ServletUriComponentsBuilder uriBuilder = createUriBuilder(); final EntityType meta = dataService.getEntityType(entityTypeId); try { final List<Entity> entities = request.getEntities().stream() .map(e -> this.restService.toEntity(meta, e)) .collect(toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<>(); if (ATTRIBUTE_META_DATA.equals(entityTypeId)) { entities.stream() .map(attribute -> (Attribute) attribute) .forEach(attribute -> dataService.getMeta().addAttribute(attribute)); } else { this.dataService.add(entityTypeId, entities.stream()); } entities.forEach( entity -> { restService.updateMappedByEntities(entity); String id = entity.getIdValue().toString(); ids.add(id); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( UriUtils.createEntityUriPath(uriBuilder, entityTypeId, id))); }); responseBody.setLocation( UriUtils.createEntitiesUriPath( uriBuilder, entityTypeId, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } } RestControllerV2(
      DataService dataService,
      UserPermissionEvaluator permissionService,
      RestService restService,
      LocalizationService localizationService,
      PermissionSystemService permissionSystemService,
      RepositoryCopier repoCopier); @Autowired @Deprecated @Transactional(readOnly = true) @GetMapping("/version") Map<String, String> getVersion(
      @Value("${molgenis.version:@null}") String molgenisVersion,
      @Value("${molgenis.build.date:@null}") String molgenisBuildDate); @Transactional(readOnly = true) @GetMapping("/{entityTypeId}/{id}") Map<String, Object> retrieveEntity(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("id") String untypedId,
      @RequestParam(value = "attrs", required = false) AttributeFilter attributeFilter,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @PostMapping(value = "/{entityTypeId}/{id}", params = "_method=GET") Map<String, Object> retrieveEntityPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("id") String untypedId,
      @RequestParam(value = "attrs", required = false) AttributeFilter attributeFilter,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional @DeleteMapping("/{entityTypeId:^(?!i18n).+}/{id}") @ResponseStatus(NO_CONTENT) void deleteEntity(
      @PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId); @DeleteMapping("/{entityTypeId}") @ResponseStatus(NO_CONTENT) void deleteEntityCollection(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionDeleteRequestV2 request); @Transactional(readOnly = true) @GetMapping("/{entityTypeId}") EntityCollectionResponseV2 retrieveEntityCollection(
      @PathVariable("entityTypeId") String entityTypeId,
      @Valid EntityCollectionRequestV2 request,
      HttpServletRequest httpRequest,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @PostMapping(value = "/{entityTypeId}", params = "_method=GET") EntityCollectionResponseV2 retrieveEntityCollectionPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @Valid EntityCollectionRequestV2 request,
      HttpServletRequest httpRequest,
      @RequestParam(value = "includeCategories", defaultValue = "false")
          boolean includeCategories); @Transactional(readOnly = true) @GetMapping(value = "/{entityTypeId}/meta/{attributeName}", produces = APPLICATION_JSON_VALUE) AttributeResponseV2 retrieveEntityAttributeMeta(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName); @Transactional(readOnly = true) @PostMapping( value = "/{entityTypeId}/meta/{attributeName}", params = "_method=GET", produces = APPLICATION_JSON_VALUE) AttributeResponseV2 retrieveEntityAttributeMetaPost(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE) EntityCollectionBatchCreateResponseBodyV2 createEntities(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional @PostMapping(value = "copy/{entityTypeId}", produces = APPLICATION_JSON_VALUE) String copyEntity(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid CopyEntityRequestV2 request,
      HttpServletResponse response); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PutMapping("/{entityTypeId}") synchronized void updateEntities(
      @PathVariable("entityTypeId") String entityTypeId,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional(readOnly = true) @GetMapping(value = "/i18n", produces = APPLICATION_JSON_VALUE) Map<String, String> getL10nStrings(); @SuppressWarnings("java:S2259") // getEntities is guaranteed to be not empty @Transactional @PutMapping("/{entityTypeId}/{attributeName}") @ResponseStatus(OK) synchronized void updateAttribute(
      @PathVariable("entityTypeId") String entityTypeId,
      @PathVariable("attributeName") String attributeName,
      @RequestBody @Valid EntityCollectionBatchRequestV2 request,
      HttpServletResponse response); @Deprecated @Transactional(readOnly = true) @GetMapping( value = "/i18n/{namespace}/{language}", produces = APPLICATION_JSON_VALUE + ";charset=UTF-8") Map<String, String> getL10nStrings(
      @PathVariable String namespace, @PathVariable String language); @Deprecated @Transactional(readOnly = true) @GetMapping( value = "/i18n/{namespace}_{language}.properties", produces = TEXT_PLAIN_VALUE + ";charset=UTF-8 ") String getL10nProperties(@PathVariable String namespace, @PathVariable String language); @Deprecated @Transactional @PostMapping("/i18n/{namespace}") @ResponseStatus(CREATED) void registerMissingResourceStrings(
      @PathVariable String namespace, HttpServletRequest request); @Deprecated @Transactional @DeleteMapping("/i18n/{namespace}") @ResponseStatus(NO_CONTENT) void deleteNamespace(@PathVariable String namespace); @ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleHttpMessageNotReadableException(
      HttpMessageNotReadableException exception); @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleMethodArgumentNotValidException(
      MethodArgumentNotValidException exception); @ExceptionHandler(MolgenisDataException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleMolgenisDataException(MolgenisDataException e); @ExceptionHandler(MolgenisDataAccessException.class) @ResponseStatus(UNAUTHORIZED) @ResponseBody ErrorMessageResponse handleMolgenisDataAccessException(MolgenisDataAccessException e); @ExceptionHandler(ConversionFailedException.class) @ResponseStatus(BAD_REQUEST) @ResponseBody ErrorMessageResponse handleConversionFailedException(ConversionFailedException e); static final String TIME_PARAM_NAME; }