@Override @Transactional( propagation = Propagation.REQUIRED, readOnly = false) @CachePut( value = "greetings", key = "#greeting.id") public Greeting update(Greeting greeting) { logger.info("> update id:{}", greeting.getId()); counterService.increment("method.invoked.greetingServiceBean.update"); // Ensure the entity object to be updated exists in the repository to // prevent the default behavior of save() which will persist a new // entity if the entity matching the id does not exist Greeting greetingToUpdate = findOne(greeting.getId()); if (greetingToUpdate == null) { // Cannot update Greeting that hasn't been persisted logger.error( "Attempted to update a Greeting, but the entity does not exist."); throw new NoResultException("Requested entity not found."); } greetingToUpdate.setText(greeting.getText()); Greeting updatedGreeting = greetingRepository.save(greetingToUpdate); logger.info("< update id:{}", greeting.getId()); return updatedGreeting; }
@Override @Transactional( propagation = Propagation.REQUIRED, readOnly = false) @CachePut( value = "greetings", key = "#greeting.id") public Greeting update(Greeting greeting) { logger.info("> update id:{}", greeting.getId()); counterService.increment("method.invoked.greetingServiceBean.update"); // Ensure the entity object to be updated exists in the repository to // prevent the default behavior of save() which will persist a new // entity if the entity matching the id does not exist Greeting greetingToUpdate = findOne(greeting.getId()); if (greetingToUpdate == null) { // Cannot update Greeting that hasn't been persisted logger.error( "Attempted to update a Greeting, but the entity does not exist."); throw new NoResultException("Requested entity not found."); } greetingToUpdate.setText(greeting.getText()); Greeting updatedGreeting = greetingRepository.save(greetingToUpdate); logger.info("< update id:{}", greeting.getId()); return updatedGreeting; }
@Test public void testUpdate() { Long id = new Long(1); Greeting entity = service.findOne(id); Assert.assertNotNull("failure - expected not null", entity); String updatedText = entity.getText() + " test"; entity.setText(updatedText); Greeting updatedEntity = service.update(entity); Assert.assertNotNull("failure - expected not null", updatedEntity); Assert.assertEquals("failure - expected id attribute match", id, updatedEntity.getId()); Assert.assertEquals("failure - expected text attribute match", updatedText, updatedEntity.getText()); }
@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()); }
@Test public void testUpdateGreeting() throws Exception { String uri = "/api/greetings/{id}"; Long id = new Long(1); Greeting greeting = greetingService.findOne(id); String updatedText = greeting.getText() + " test"; greeting.setText(updatedText); String inputJson = super.mapToJson(greeting); MvcResult result = mvc .perform(MockMvcRequestBuilders.put(uri, id) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON).content(inputJson)) .andReturn(); String content = result.getResponse().getContentAsString(); int status = result.getResponse().getStatus(); Assert.assertEquals("failure - expected HTTP status 200", 200, status); Assert.assertTrue( "failure - expected HTTP response body to have a value", content.trim().length() > 0); Greeting updatedGreeting = super.mapFromJson(content, Greeting.class); Assert.assertNotNull("failure - expected greeting not null", updatedGreeting); Assert.assertEquals("failure - expected greeting.id unchanged", greeting.getId(), updatedGreeting.getId()); Assert.assertEquals("failure - expected updated greeting text match", updatedText, updatedGreeting.getText()); }
@Test public void testCreateGreeting() throws Exception { String uri = "/api/greetings"; Greeting greeting = new Greeting(); greeting.setText("test"); String inputJson = super.mapToJson(greeting); MvcResult result = mvc .perform(MockMvcRequestBuilders.post(uri) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON).content(inputJson)) .andReturn(); String content = result.getResponse().getContentAsString(); int status = result.getResponse().getStatus(); Assert.assertEquals("failure - expected HTTP status 201", 201, status); Assert.assertTrue( "failure - expected HTTP response body to have a value", content.trim().length() > 0); Greeting createdGreeting = super.mapFromJson(content, Greeting.class); Assert.assertNotNull("failure - expected greeting not null", createdGreeting); Assert.assertNotNull("failure - expected greeting.id not null", createdGreeting.getId()); Assert.assertEquals("failure - expected greeting.text match", "test", createdGreeting.getText()); }