/** * Web service endpoint to create a single Greeting entity. The HTTP request * body is expected to contain a Greeting object in JSON format. The * Greeting is persisted in the data repository. * * If created successfully, the persisted Greeting is returned as JSON with * HTTP status 201. * * If not created successfully, the service returns an empty response body * with HTTP status 500. * * @param greeting The Greeting object to be created. * @return A ResponseEntity containing a single Greeting object, if created * successfully, and a HTTP status code as described in the method * comment. */ @RequestMapping( value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Greeting> createGreeting( @RequestBody Greeting greeting) { logger.info("> createGreeting"); Greeting savedGreeting = greetingService.create(greeting); logger.info("< createGreeting"); return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED); }
/** * Web service endpoint to create a single Greeting entity. The HTTP request * body is expected to contain a Greeting object in JSON format. The * Greeting is persisted in the data repository. * * If created successfully, the persisted Greeting is returned as JSON with * HTTP status 201. * * If not created successfully, the service returns an empty response body * with HTTP status 500. * * @param greeting The Greeting object to be created. * @return A ResponseEntity containing a single Greeting object, if created * successfully, and a HTTP status code as described in the method * comment. */ @RequestMapping( value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Greeting> createGreeting( @RequestBody Greeting greeting) { logger.info("> createGreeting"); Greeting savedGreeting = greetingService.create(greeting); logger.info("< createGreeting"); return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED); }
@Test public void testCreateWithId() { Exception exception = null; Greeting entity = new Greeting(); entity.setId(Long.MAX_VALUE); entity.setText("test"); try { service.create(entity); } catch (EntityExistsException e) { exception = e; } Assert.assertNotNull("failure - expected exception", exception); Assert.assertTrue("failure - expected EntityExistsException", exception instanceof EntityExistsException); }
@Test public void testCreate() { Greeting entity = new Greeting(); entity.setText("test"); Greeting createdEntity = service.create(entity); Assert.assertNotNull("failure - expected not null", createdEntity); Assert.assertNotNull("failure - expected id attribute not null", createdEntity.getId()); Assert.assertEquals("failure - expected text attribute match", "test", createdEntity.getText()); Collection<Greeting> list = service.findAll(); Assert.assertEquals("failure - expected size", 3, list.size()); }