Search in sources :

Example 1 with CustomerWasCreated

use of com.github.wenqiglantz.service.customerservice.data.event.CustomerWasCreated in project customer-service by wenqiglantz.

the class CustomerServiceImpl method saveCustomer.

@Override
public CustomerInfo saveCustomer(CustomerInfo customerInfo) throws Exception {
    customerInfo.setCustomerId(Strings.isBlank(customerInfo.getCustomerId()) ? UUID.randomUUID().toString() : customerInfo.getCustomerId());
    Customer customer = Customer.builder().customerId(customerInfo.getCustomerId()).firstName(customerInfo.getFirstName()).lastName(customerInfo.getLastName()).build();
    customerRepository.save(customer);
    CustomerWasCreated customerWasCreated = CustomerWasCreated.builder().customerId(customerInfo.getCustomerId()).firstName(customerInfo.getFirstName()).lastName(customerInfo.getLastName()).status(CustomerStatus.CREATED).build();
    log.info("publish event {} ", customerWasCreated);
    // initialize dapr client in try-with-resource block to properly close the client at the end
    try (DaprClient client = new DaprClientBuilder().build()) {
        client.publishEvent(PUBSUB_NAME, TOPIC_ORDER_SERVICE, customerWasCreated, singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS)).block();
    }
    return customerInfo;
}
Also used : Customer(com.github.wenqiglantz.service.customerservice.persistence.entity.Customer) DaprClientBuilder(io.dapr.client.DaprClientBuilder) DaprClient(io.dapr.client.DaprClient) CustomerWasCreated(com.github.wenqiglantz.service.customerservice.data.event.CustomerWasCreated)

Example 2 with CustomerWasCreated

use of com.github.wenqiglantz.service.customerservice.data.event.CustomerWasCreated in project customer-service by wenqiglantz.

the class CustomerEventsPublishPactVerificationTest method publishCustomerWasCreated.

@PactVerifyProvider("valid CustomerWasCreated from provider")
public String publishCustomerWasCreated() throws Exception {
    CustomerInfo customerInfo = CustomerInfo.builder().customerId("595eed0c-eff5-4278-90ad-b952f18dbee8").firstName("test").lastName("last").build();
    CustomerWasCreated event = CustomerWasCreated.builder().customerId("595eed0c-eff5-4278-90ad-b952f18dbee8").firstName("test").lastName("last").status(CustomerStatus.CREATED).build();
    context.checking(new Expectations() {

        {
            oneOf(customerService).saveCustomer(customerInfo);
        }
    });
    customerService.saveCustomer(customerInfo);
    String eventString = objectMapper.writeValueAsString(event);
    return eventString;
}
Also used : Expectations(org.jmock.Expectations) CustomerWasCreated(com.github.wenqiglantz.service.customerservice.data.event.CustomerWasCreated) CustomerInfo(com.github.wenqiglantz.service.customerservice.data.CustomerInfo) PactVerifyProvider(au.com.dius.pact.provider.PactVerifyProvider)

Aggregations

CustomerWasCreated (com.github.wenqiglantz.service.customerservice.data.event.CustomerWasCreated)2 PactVerifyProvider (au.com.dius.pact.provider.PactVerifyProvider)1 CustomerInfo (com.github.wenqiglantz.service.customerservice.data.CustomerInfo)1 Customer (com.github.wenqiglantz.service.customerservice.persistence.entity.Customer)1 DaprClient (io.dapr.client.DaprClient)1 DaprClientBuilder (io.dapr.client.DaprClientBuilder)1 Expectations (org.jmock.Expectations)1