@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 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()); }
@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 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()); }
@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 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 testUpdateNotFound() { Exception exception = null; Greeting entity = new Greeting(); entity.setId(Long.MAX_VALUE); entity.setText("test"); try { service.update(entity); } catch (NoResultException e) { exception = e; } Assert.assertNotNull("failure - expected exception", exception); Assert.assertTrue("failure - expected NoResultException", exception instanceof NoResultException); }
@Override @Transactional( propagation = Propagation.REQUIRED, readOnly = false) @CachePut( value = "greetings", key = "#result.id") public Greeting create(Greeting greeting) { logger.info("> create"); counterService.increment("method.invoked.greetingServiceBean.create"); // Ensure the entity object to be created does NOT exist in the // repository. Prevent the default behavior of save() which will update // an existing entity if the entity matching the supplied id exists. if (greeting.getId() != null) { // Cannot create Greeting with specified ID value logger.error( "Attempted to create a Greeting, but id attribute was not null."); throw new EntityExistsException( "The id attribute must be null to persist a new entity."); } Greeting savedGreeting = greetingRepository.save(greeting); logger.info("< create"); return savedGreeting; }
@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()); }
@Override @Transactional( propagation = Propagation.REQUIRED, readOnly = false) @CachePut( value = "greetings", key = "#result.id") public Greeting create(Greeting greeting) { logger.info("> create"); counterService.increment("method.invoked.greetingServiceBean.create"); // Ensure the entity object to be created does NOT exist in the // repository. Prevent the default behavior of save() which will update // an existing entity if the entity matching the supplied id exists. if (greeting.getId() != null) { // Cannot create Greeting with specified ID value logger.error( "Attempted to create a Greeting, but id attribute was not null."); throw new EntityExistsException( "The id attribute must be null to persist a new entity."); } Greeting savedGreeting = greetingRepository.save(greeting); logger.info("< create"); return savedGreeting; }
@Test public void testFindOne() { Long id = new Long(1); Greeting entity = service.findOne(id); Assert.assertNotNull("failure - expected not null", entity); Assert.assertEquals("failure - expected id attribute match", id, entity.getId()); }