org.example.ws.model.Greeting Java Examples

The following examples show how to use org.example.ws.model.Greeting. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GreetingServiceTest.java    From spring-security-fundamentals with Apache License 2.0 8 votes vote down vote up
@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);

}
 
Example #2
Source File: GreetingController.java    From spring-data-fundamentals with Apache License 2.0 6 votes vote down vote up
/**
 * Web service endpoint to fetch a single Greeting entity by primary key
 * identifier.
 * 
 * If found, the Greeting is returned as JSON with HTTP status 200.
 * 
 * If not found, the service returns an empty response body with HTTP status
 * 404.
 * 
 * @param id A Long URL path variable containing the Greeting primary key
 *        identifier.
 * @return A ResponseEntity containing a single Greeting object, if found,
 *         and a HTTP status code as described in the method comment.
 */
@RequestMapping(
        value = "/api/greetings/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> getGreeting(@PathVariable("id") Long id) {
    logger.info("> getGreeting id:{}", id);

    Greeting greeting = greetingService.findOne(id);
    if (greeting == null) {
        return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
    }

    logger.info("< getGreeting id:{}", id);
    return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
 
Example #3
Source File: EmailServiceBean.java    From spring-security-fundamentals with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean send(Greeting greeting) {
    logger.info("> send");

    Boolean success = Boolean.FALSE;

    // Simulate method execution time
    long pause = 5000;
    try {
        Thread.sleep(pause);
    } catch (Exception e) {
        // do nothing
    }
    logger.info("Processing time was {} seconds.", pause / 1000);

    success = Boolean.TRUE;

    logger.info("< send");
    return success;
}
 
Example #4
Source File: GreetingServiceTest.java    From spring-data-fundamentals with Apache License 2.0 6 votes vote down vote up
@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);

}
 
Example #5
Source File: GreetingServiceTest.java    From spring-data-fundamentals with Apache License 2.0 6 votes vote down vote up
@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());

}
 
Example #6
Source File: EmailServiceBean.java    From spring-data-fundamentals with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean send(Greeting greeting) {
    logger.info("> send");

    Boolean success = Boolean.FALSE;

    // Simulate method execution time
    long pause = 5000;
    try {
        Thread.sleep(pause);
    } catch (Exception e) {
        // do nothing
    }
    logger.info("Processing time was {} seconds.", pause / 1000);

    success = Boolean.TRUE;

    logger.info("< send");
    return success;
}
 
Example #7
Source File: GreetingServiceTest.java    From spring-boot-fundamentals with Apache License 2.0 6 votes vote down vote up
@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);

}
 
Example #8
Source File: EmailServiceBean.java    From spring-data-fundamentals with Apache License 2.0 6 votes vote down vote up
@Async
@Override
public Future<Boolean> sendAsyncWithResult(Greeting greeting) {
    logger.info("> sendAsyncWithResult");

    AsyncResponse<Boolean> response = new AsyncResponse<Boolean>();

    try {
        Boolean success = send(greeting);
        response.complete(success);
    } catch (Exception e) {
        logger.warn("Exception caught sending asynchronous mail.", e);
        response.completeExceptionally(e);
    }

    logger.info("< sendAsyncWithResult");
    return response;
}
 
Example #9
Source File: GreetingServiceTest.java    From spring-security-fundamentals with Apache License 2.0 6 votes vote down vote up
@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());

}
 
Example #10
Source File: GreetingServiceTest.java    From spring-boot-fundamentals with Apache License 2.0 6 votes vote down vote up
@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);

}
 
Example #11
Source File: GreetingController.java    From spring-boot-fundamentals with Apache License 2.0 6 votes vote down vote up
/**
 * Web service endpoint to fetch a single Greeting entity by primary key
 * identifier.
 * 
 * If found, the Greeting is returned as JSON with HTTP status 200.
 * 
 * If not found, the service returns an empty response body with HTTP status
 * 404.
 * 
 * @param id A Long URL path variable containing the Greeting primary key
 *        identifier.
 * @return A ResponseEntity containing a single Greeting object, if found,
 *         and a HTTP status code as described in the method comment.
 */
@RequestMapping(
        value = "/api/greetings/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> getGreeting(@PathVariable("id") Long id) {
    logger.info("> getGreeting id:{}", id);

    Greeting greeting = greetingService.findOne(id);
    if (greeting == null) {
        return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
    }

    logger.info("< getGreeting id:{}", id);
    return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
 
Example #12
Source File: EmailServiceBean.java    From spring-boot-fundamentals with Apache License 2.0 6 votes vote down vote up
@Async
@Override
public Future<Boolean> sendAsyncWithResult(Greeting greeting) {
    logger.info("> sendAsyncWithResult");

    AsyncResponse<Boolean> response = new AsyncResponse<Boolean>();

    try {
        Boolean success = send(greeting);
        response.complete(success);
    } catch (Exception e) {
        logger.warn("Exception caught sending asynchronous mail.", e);
        response.completeExceptionally(e);
    }

    logger.info("< sendAsyncWithResult");
    return response;
}
 
Example #13
Source File: EmailServiceBean.java    From spring-boot-fundamentals with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean send(Greeting greeting) {
    logger.info("> send");

    Boolean success = Boolean.FALSE;

    // Simulate method execution time
    long pause = 5000;
    try {
        Thread.sleep(pause);
    } catch (Exception e) {
        // do nothing
    }
    logger.info("Processing time was {} seconds.", pause / 1000);

    success = Boolean.TRUE;

    logger.info("< send");
    return success;
}
 
Example #14
Source File: GreetingServiceBean.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #15
Source File: GreetingHealthIndicator.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
@Override
public Health health() {

    // Assess the application's Greeting health. If the application's
    // Greeting components have data to service user requests, the Greeting
    // component is considered 'healthy', otherwise it is not.

    Collection<Greeting> greetings = greetingService.findAll();

    if (greetings == null || greetings.size() == 0) {
        return Health.down().withDetail("count", 0).build();
    }

    return Health.up().withDetail("count", greetings.size()).build();
}
 
Example #16
Source File: EmailServiceBean.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@Async
@Override
public void sendAsync(Greeting greeting) {
    logger.info("> sendAsync");

    try {
        send(greeting);
    } catch (Exception e) {
        logger.warn("Exception caught sending asynchronous mail.", e);
    }

    logger.info("< sendAsync");
}
 
Example #17
Source File: GreetingController.java    From spring-data-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Web service endpoint to fetch all Greeting entities. The service returns
 * the collection of Greeting entities as JSON.
 * 
 * @return A ResponseEntity containing a Collection of Greeting objects.
 */
@RequestMapping(
        value = "/api/greetings",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Greeting>> getGreetings() {
    logger.info("> getGreetings");

    Collection<Greeting> greetings = greetingService.findAll();

    logger.info("< getGreetings");
    return new ResponseEntity<Collection<Greeting>>(greetings,
            HttpStatus.OK);
}
 
Example #18
Source File: GreetingController.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Web service endpoint to fetch all Greeting entities. The service returns
 * the collection of Greeting entities as JSON.
 * 
 * @return A ResponseEntity containing a Collection of Greeting objects.
 */
@RequestMapping(
        value = "/api/greetings",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Greeting>> getGreetings() {
    logger.info("> getGreetings");

    Collection<Greeting> greetings = greetingService.findAll();

    logger.info("< getGreetings");
    return new ResponseEntity<Collection<Greeting>>(greetings,
            HttpStatus.OK);
}
 
Example #19
Source File: GreetingController.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Web service endpoint to fetch a single Greeting entity by primary key
 * identifier and send it as an email.
 * 
 * If found, the Greeting is returned as JSON with HTTP status 200 and sent
 * via Email.
 * 
 * If not found, the service returns an empty response body with HTTP status
 * 404.
 * 
 * @param id A Long URL path variable containing the Greeting primary key
 *        identifier.
 * @param waitForAsyncResult A boolean indicating if the web service should
 *        wait for the asynchronous email transmission.
 * @return A ResponseEntity containing a single Greeting object, if found,
 *         and a HTTP status code as described in the method comment.
 */
@RequestMapping(
        value = "/api/greetings/{id}/send",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> sendGreeting(@PathVariable("id") Long id,
        @RequestParam(
                value = "wait",
                defaultValue = "false") boolean waitForAsyncResult) {

    logger.info("> sendGreeting id:{}", id);

    Greeting greeting = null;

    try {
        greeting = greetingService.findOne(id);
        if (greeting == null) {
            logger.info("< sendGreeting id:{}", id);
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }

        if (waitForAsyncResult) {
            Future<Boolean> asyncResponse = emailService
                    .sendAsyncWithResult(greeting);
            boolean emailSent = asyncResponse.get();
            logger.info("- greeting email sent? {}", emailSent);
        } else {
            emailService.sendAsync(greeting);
        }
    } catch (Exception e) {
        logger.error("A problem occurred sending the Greeting.", e);
        return new ResponseEntity<Greeting>(
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    logger.info("< sendGreeting id:{}", id);
    return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
 
Example #20
Source File: GreetingServiceBean.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #21
Source File: GreetingServiceBean.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source File: GreetingBatchBean.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Use a cron expression to execute logic on a schedule.
 * 
 * Expression: second minute hour day-of-month month weekday
 * 
 * @see http ://docs.spring.io/spring/docs/current/javadoc-api/org/
 *      springframework /scheduling/support/CronSequenceGenerator.html
 */
@Scheduled(
        cron = "${batch.greeting.cron}")
public void cronJob() {
    logger.info("> cronJob");

    // Add scheduled logic here
    Collection<Greeting> greetings = greetingService.findAll();
    logger.info("There are {} greetings in the data store.",
            greetings.size());

    logger.info("< cronJob");
}
 
Example #23
Source File: GreetingHealthIndicator.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@Override
public Health health() {

    // Assess the application's Greeting health. If the application's
    // Greeting components have data to service user requests, the Greeting
    // component is considered 'healthy', otherwise it is not.

    Collection<Greeting> greetings = greetingService.findAll();

    if (greetings == null || greetings.size() == 0) {
        return Health.down().withDetail("count", 0).build();
    }

    return Health.up().withDetail("count", greetings.size()).build();
}
 
Example #24
Source File: GreetingControllerMocksTest.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGreetings() throws Exception {

    // Create some test data
    Collection<Greeting> list = getEntityListStubData();

    // Stub the GreetingService.findAll method return value
    when(greetingService.findAll()).thenReturn(list);

    // Perform the behavior being tested
    String uri = "/api/greetings";

    MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri)
            .accept(MediaType.APPLICATION_JSON)).andReturn();

    // Extract the response status and body
    String content = result.getResponse().getContentAsString();
    int status = result.getResponse().getStatus();

    // Verify the GreetingService.findAll method was invoked once
    verify(greetingService, times(1)).findAll();

    // Perform standard JUnit assertions on the response
    Assert.assertEquals("failure - expected HTTP status 200", 200, status);
    Assert.assertTrue(
            "failure - expected HTTP response body to have a value",
            content.trim().length() > 0);

}
 
Example #25
Source File: GreetingControllerMocksTest.java    From spring-boot-fundamentals with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGreeting() throws Exception {

    // Create some test data
    Long id = new Long(1);
    Greeting entity = getEntityStubData();

    // Stub the GreetingService.findOne method return value
    when(greetingService.findOne(id)).thenReturn(entity);

    // Perform the behavior being tested
    String uri = "/api/greetings/{id}";

    MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri, id)
            .accept(MediaType.APPLICATION_JSON)).andReturn();

    // Extract the response status and body
    String content = result.getResponse().getContentAsString();
    int status = result.getResponse().getStatus();

    // Verify the GreetingService.findOne method was invoked once
    verify(greetingService, times(1)).findOne(id);

    // Perform standard JUnit assertions on the test results
    Assert.assertEquals("failure - expected HTTP status 200", 200, status);
    Assert.assertTrue(
            "failure - expected HTTP response body to have a value",
            content.trim().length() > 0);
}
 
Example #26
Source File: GreetingServiceBean.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(
        value = "greetings",
        key = "#id")
public Greeting findOne(Long id) {
    logger.info("> findOne id:{}", id);

    counterService.increment("method.invoked.greetingServiceBean.findOne");

    Greeting greeting = greetingRepository.findOne(id);

    logger.info("< findOne id:{}", id);
    return greeting;
}
 
Example #27
Source File: GreetingControllerMocksTest.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGreetings() throws Exception {

    // Create some test data
    Collection<Greeting> list = getEntityListStubData();

    // Stub the GreetingService.findAll method return value
    when(greetingService.findAll()).thenReturn(list);

    // Perform the behavior being tested
    String uri = "/api/greetings";

    MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri)
            .accept(MediaType.APPLICATION_JSON)).andReturn();

    // Extract the response status and body
    String content = result.getResponse().getContentAsString();
    int status = result.getResponse().getStatus();

    // Verify the GreetingService.findAll method was invoked once
    verify(greetingService, times(1)).findAll();

    // Perform standard JUnit assertions on the response
    Assert.assertEquals("failure - expected HTTP status 200", 200, status);
    Assert.assertTrue(
            "failure - expected HTTP response body to have a value",
            content.trim().length() > 0);

}
 
Example #28
Source File: GreetingController.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Web service endpoint to fetch all Greeting entities. The service returns
 * the collection of Greeting entities as JSON.
 * 
 * @return A ResponseEntity containing a Collection of Greeting objects.
 */
@RequestMapping(
        value = "/api/greetings",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Greeting>> getGreetings() {
    logger.info("> getGreetings");

    Collection<Greeting> greetings = greetingService.findAll();

    logger.info("< getGreetings");
    return new ResponseEntity<Collection<Greeting>>(greetings,
            HttpStatus.OK);
}
 
Example #29
Source File: GreetingController.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
/**
 * Web service endpoint to fetch a single Greeting entity by primary key
 * identifier and send it as an email.
 * 
 * If found, the Greeting is returned as JSON with HTTP status 200 and sent
 * via Email.
 * 
 * If not found, the service returns an empty response body with HTTP status
 * 404.
 * 
 * @param id A Long URL path variable containing the Greeting primary key
 *        identifier.
 * @param waitForAsyncResult A boolean indicating if the web service should
 *        wait for the asynchronous email transmission.
 * @return A ResponseEntity containing a single Greeting object, if found,
 *         and a HTTP status code as described in the method comment.
 */
@RequestMapping(
        value = "/api/greetings/{id}/send",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> sendGreeting(@PathVariable("id") Long id,
        @RequestParam(
                value = "wait",
                defaultValue = "false") boolean waitForAsyncResult) {

    logger.info("> sendGreeting id:{}", id);

    Greeting greeting = null;

    try {
        greeting = greetingService.findOne(id);
        if (greeting == null) {
            logger.info("< sendGreeting id:{}", id);
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }

        if (waitForAsyncResult) {
            Future<Boolean> asyncResponse = emailService
                    .sendAsyncWithResult(greeting);
            boolean emailSent = asyncResponse.get();
            logger.info("- greeting email sent? {}", emailSent);
        } else {
            emailService.sendAsync(greeting);
        }
    } catch (Exception e) {
        logger.error("A problem occurred sending the Greeting.", e);
        return new ResponseEntity<Greeting>(
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    logger.info("< sendGreeting id:{}", id);
    return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
 
Example #30
Source File: EmailServiceBean.java    From spring-security-fundamentals with Apache License 2.0 5 votes vote down vote up
@Async
@Override
public void sendAsync(Greeting greeting) {
    logger.info("> sendAsync");

    try {
        send(greeting);
    } catch (Exception e) {
        logger.warn("Exception caught sending asynchronous mail.", e);
    }

    logger.info("< sendAsync");
}