@Test public void changeTransactionState() { withPaymentTransaction(client(), (Payment payment, Transaction transaction) -> { assertThat(transaction.getState()).isEqualTo(TransactionState.INITIAL); final TransactionState transactionState = TransactionState.SUCCESS; final Payment updatedPayment = client().executeBlocking(PaymentUpdateCommand.of(payment, ChangeTransactionState.of(transactionState, transaction.getId()))); final Transaction updatedTransaction = updatedPayment.getTransactions().get(0); assertThat(updatedTransaction.getState()).isEqualTo(transactionState); //check messages assertEventually(() -> { final PagedQueryResult<PaymentTransactionStateChangedMessage> messageQueryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(payment)) .forMessageType(PaymentTransactionStateChangedMessage.MESSAGE_HINT)); assertThat(messageQueryResult.head()).isPresent(); final PaymentTransactionStateChangedMessage message = messageQueryResult.head().get(); assertThat(message.getState()).isEqualTo(transactionState); assertThat(message.getTransactionId()).isEqualTo(transaction.getId()); }); return updatedPayment; }); }
@Test public void orderCreatedMessage() throws Exception { withOrderAndReturnInfo(client(), ((order, returnInfo) -> { final Query<OrderCreatedMessage> query = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(OrderCreatedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<OrderCreatedMessage> pagedQueryResult = client().executeBlocking(query); final Optional<OrderCreatedMessage> message = pagedQueryResult.head(); assertThat(message).isPresent(); assertThat(message.get().getOrder().getId()).isEqualTo(order.getId()); assertThat(message.get().getResource().getObj()).isNotNull(); assertThat(message.get().getResource().getId()).isEqualTo(order.getId()); }); return order; })); }
@Test public void changeSlug() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString newSlug = LocalizedString.ofEnglish("new-slug-" + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeSlug.of(newSlug))); assertThat(updatedProduct.getMasterData().getStaged().getSlug()).isEqualTo(newSlug); //query message assertEventually(() -> { final Query<ProductSlugChangedMessage> query = MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .forMessageType(ProductSlugChangedMessage.MESSAGE_HINT); final List<ProductSlugChangedMessage> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); final ProductSlugChangedMessage message = results.get(0); assertThat(message.getSlug()).isEqualTo(newSlug); }); return updatedProduct; }); }
@Test public void messageGetPayload() throws Exception { withOrderAndReturnInfo(client(), ((order, returnInfo) -> { final Query<Message> query = MessageQuery.of() .withPredicates(m -> m.type().is(ReturnInfoAddedMessage.MESSAGE_HINT).and(m.resource().is(order))) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L); assertEventually(() -> { final PagedQueryResult<Message> pagedQueryResult = client().executeBlocking(query); final Optional<Message> message = pagedQueryResult.head(); assertThat(message).isPresent(); final String fetchedItemId = message.get().getPayload().get("returnInfo").get("items").get(0).get("id").asText(); final String actualItemId = returnInfo.getItems().get(0).getId(); assertThat(fetchedItemId).isEqualTo(actualItemId).isNotNull(); }); return order; })); }
@Test public void queryForASpecificMessage() throws Exception { withOrderAndReturnInfo(client(), ((order, returnInfo) -> { final Query<ReturnInfoAddedMessage> query = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ReturnInfoAddedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ReturnInfoAddedMessage> pagedQueryResult = client().executeBlocking(query); final Optional<ReturnInfoAddedMessage> message = pagedQueryResult.head(); assertThat(message).isPresent(); assertThat(message.get().getReturnInfo()).isEqualTo(returnInfo); assertThat(message.get().getResource().getObj()).isNotNull(); assertThat(message.get().getResource().getId()).isEqualTo(order.getId()); }); return order; })); }
@Test public void addInterfaceInteraction() { withUpdateableType(client(), type -> { withPayment(client(), payment -> { final AddInterfaceInteraction addInterfaceInteraction = AddInterfaceInteraction.ofTypeIdAndObjects(type.getId(), singletonMap(STRING_FIELD_NAME, "some id")); final Payment updatedPayment = client().executeBlocking(PaymentUpdateCommand.of(payment, addInterfaceInteraction)); assertThat(updatedPayment.getInterfaceInteractions().get(0).getFieldAsString(STRING_FIELD_NAME)).isEqualTo("some id"); assertEventually(() -> { final PagedQueryResult<PaymentInteractionAddedMessage> pagedQueryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(payment)) .forMessageType(PaymentInteractionAddedMessage.MESSAGE_HINT)); assertThat(pagedQueryResult.head()).isPresent(); assertThat(pagedQueryResult.head().get().getInteraction().getFieldAsString(STRING_FIELD_NAME)).isEqualTo("some id"); }); return updatedPayment; }); return type; }); }
@Test public void setCustomerEmail() throws Exception { withOrder(client(), order -> { assertThat(order.getCustomerEmail()).isNotEmpty(); final String email = "info@commercetools.de"; final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, SetCustomerEmail.of(email))); assertThat(updatedOrder.getCustomerEmail()).contains(email); //there is also a message final Query<OrderCustomerEmailSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .forMessageType(OrderCustomerEmailSetMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderCustomerEmailSetMessage> customerEmailSetMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(customerEmailSetMessageOptional).isPresent(); final OrderCustomerEmailSetMessage orderCustomerEmailSetMessage = customerEmailSetMessageOptional.get(); assertThat(orderCustomerEmailSetMessage.getEmail()).isEqualTo(email); }); return updatedOrder; }); }
@Test public void queryForAllMessages() throws Exception { withOrderAndReturnInfo(client(), ((order, returnInfo) -> { final MessageQuery query = MessageQuery.of() //example predicate to fetch for a specific message type .withPredicates(m -> m.type().is("ReturnInfoAdded").and(m.resource().is(order))) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L); assertEventually(() -> { final Optional<Message> message = client().executeBlocking(query).head(); assertThat(message).isPresent(); assertThat(message.get().getResource().getObj()).isNotNull(); assertThat(message.get().getResource()).isEqualTo(order.toReference()); assertThat(message.get().getResource().getId()).isEqualTo(order.getId()); }); return order; })); } }
@Test public void setBillingAddress() { withOrder(client(), order -> { assertThat(order.getBillingAddress()).isNull(); final Address newAddress = Address.of(DE).withStreetNumber("5"); final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, SetBillingAddress.of(newAddress))); assertThat(updatedOrder.getBillingAddress().getStreetNumber()).isEqualTo("5"); //there is also a message final Query<OrderBillingAddressSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .forMessageType(OrderBillingAddressSetMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderBillingAddressSetMessage> billingAddressSetMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(billingAddressSetMessageOptional).isPresent(); final OrderBillingAddressSetMessage orderBillingAddressSetMessage = billingAddressSetMessageOptional.get(); assertThat(orderBillingAddressSetMessage.getAddress()).isEqualTo(newAddress); }); return updatedOrder; }); }
@Test public void changeShipmentState() throws Exception { withOrder(client(), order -> { final ShipmentState newState = ShipmentState.SHIPPED; assertThat(order.getShipmentState()).isNotEqualTo(newState); final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, ChangeShipmentState.of(newState))); assertThat(updatedOrder.getShipmentState()).isEqualTo(newState); //you can observe a message final Query<OrderShipmentStateChangedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .forMessageType(OrderShipmentStateChangedMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderShipmentStateChangedMessage> orderShipmentStateChangedMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(orderShipmentStateChangedMessageOptional).isPresent(); final OrderShipmentStateChangedMessage orderShipmentStateChangedMessage = orderShipmentStateChangedMessageOptional.get(); assertThat(orderShipmentStateChangedMessage.getShipmentState()).isNotNull(); }); return updatedOrder; }); }
@Test public void setShippingAddress() { withOrder(client(), order -> { assertThat(order.getShippingAddress().getStreetNumber()).isNull(); final Address newAddress = order.getShippingAddress().withStreetNumber("5"); final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, SetShippingAddress.of(newAddress))); assertThat(updatedOrder.getShippingAddress().getStreetNumber()).isEqualTo("5"); //there is also a message final Query<OrderShippingAddressSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .forMessageType(OrderShippingAddressSetMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderShippingAddressSetMessage> shippingAddressSetMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(shippingAddressSetMessageOptional).isPresent(); final OrderShippingAddressSetMessage orderShippingAddressSetMessage = shippingAddressSetMessageOptional.get(); assertThat(orderShippingAddressSetMessage.getAddress()).isEqualTo(newAddress); }); return updatedOrder; }); }
@Test public void transitionState() { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(PAYMENT_STATE), validNextStateForPaymentStatus -> { withPayment(client(), payment -> { final Payment updatedPayment = client().executeBlocking(PaymentUpdateCommand.of(payment, TransitionState.of(validNextStateForPaymentStatus))); assertThat(updatedPayment.getPaymentStatus().getState()).isEqualTo(validNextStateForPaymentStatus.toReference()); assertEventually(() -> { final PagedQueryResult<PaymentStatusStateTransitionMessage> messageQueryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(payment)) .forMessageType(PaymentStatusStateTransitionMessage.MESSAGE_HINT)); assertThat(messageQueryResult.head()).isPresent(); assertThat(messageQueryResult.head().get().getState()).isEqualTo(validNextStateForPaymentStatus.toReference()); }); return updatedPayment; }); }); }
@Test public void removeAddress() throws Exception { withCustomerWithOneAddress(client(), customer -> { final Address oldAddress = customer.getAddresses().get(0); assertThat(oldAddress.getId()) .overridingErrorMessage("only fetched address contains an ID") .isNotNull(); final RemoveAddress action = RemoveAddress.of(oldAddress); final Customer customerWithoutAddresses = client().executeBlocking(CustomerUpdateCommand.of(customer, action)); assertThat(customerWithoutAddresses.getAddresses()).isEmpty(); Query<CustomerAddressRemovedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerAddressRemovedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerAddressRemovedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerAddressRemovedMessage message = queryResult.head().get(); assertThat(message.getAddress()).isEqualTo(oldAddress); }); }); }
@Test public void setCompanyName() throws Exception { withCustomer(client(), customer -> { assertThat(customer.getCompanyName()).isNull(); final String companyName = "Big coorp"; final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, SetCompanyName.of(companyName))); assertThat(updatedCustomer.getCompanyName()).isEqualTo(companyName); Query<CustomerCompanyNameSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerCompanyNameSetMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerCompanyNameSetMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerCompanyNameSetMessage message = queryResult.head().get(); assertThat(message.getCompanyName()).isEqualTo(companyName); }); }); }
@Test public void customerCreatedMessage() { final String customerNumber = randomString(); final CustomerDraft customerDraft = newCustomerDraft().withCustomerNumber(customerNumber); CustomerFixtures.withCustomer(client(),customerDraft, customer -> { Query<CustomerCreatedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerCreatedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerCreatedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerCreatedMessage message = queryResult.head().get(); assertThat(message.getCustomer()).isEqualTo(customer); assertThat(message.getResourceUserProvidedIdentifiers().getCustomerNumber()).isEqualTo(customerNumber); }); }); } }
@Test public void changeEmail() throws Exception { withCustomer(client(), customer -> { final String newEmail = randomEmail(CustomerUpdateCommandIntegrationTest.class); assertThat(customer.getEmail()).isNotEqualTo(newEmail); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, ChangeEmail.of(newEmail))); assertThat(updatedCustomer.getEmail()).isEqualTo(newEmail); Query<CustomerEmailChangedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerEmailChangedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerEmailChangedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerEmailChangedMessage message = queryResult.head().get(); assertThat(message.getEmail()).isEqualTo(newEmail); }); }); }
@Test public void orderDeletedMessage() throws Exception { withOrder(client(), order -> { final Order orderWithOrderNumber = client().executeBlocking(OrderUpdateCommand.of(order, SetOrderNumber.of(randomString()))); final Order deletedOrder = client().executeBlocking(OrderDeleteCommand.of(orderWithOrderNumber)); final Query<OrderDeletedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(deletedOrder)) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(OrderDeletedMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderDeletedMessage> orderDeletedMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(orderDeletedMessageOptional).isPresent(); final OrderDeletedMessage orderDeletedMessage = orderDeletedMessageOptional.get(); final Order orderFromMessage = orderDeletedMessage.getOrder(); assertThat(orderDeletedMessage.getResourceUserProvidedIdentifiers()).isNotNull(); assertThat(orderDeletedMessage.getResourceUserProvidedIdentifiers().getOrderNumber()).isEqualTo(orderWithOrderNumber.getOrderNumber()); assertThat(orderFromMessage.getId()).isEqualTo(deletedOrder.getId()); }); }); }
@Test public void setDateOfBirth() throws Exception { withCustomer(client(), customer -> { assertThat(customer.getDateOfBirth()).isNull(); final LocalDate dateOfBirth = LocalDate.now(); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, SetDateOfBirth.of(dateOfBirth))); assertThat(updatedCustomer.getDateOfBirth()).isEqualTo(dateOfBirth); Query<CustomerDateOfBirthSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerDateOfBirthSetMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerDateOfBirthSetMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerDateOfBirthSetMessage message = queryResult.head().get(); assertThat(message.getDateOfBirth()).isEqualTo(dateOfBirth); }); }); }
@Test public void categorySlugChangedMessage() { CategoryFixtures.withCategory(client(), category -> { LocalizedString newSlug = randomSlug(); client().executeBlocking(CategoryUpdateCommand.of(category, ChangeSlug.of(newSlug))); Query<CategorySlugChangedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(category)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CategorySlugChangedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CategorySlugChangedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CategorySlugChangedMessage message = queryResult.head().get(); assertThat(message.getSlug()).isEqualTo(newSlug); assertThat(message.getResource().getId()).isEqualTo(category.getId()); }); }); } }
@Test public void setCustomerGroup() throws Exception { withB2cCustomerGroup(client(), customerGroup -> { withCustomer(client(), customer -> { assertThat(customer.getCustomerGroup()).isNull(); final Customer updateCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, SetCustomerGroup.of(customerGroup))); assertThat(updateCustomer.getCustomerGroup()).isEqualTo(customerGroup.toReference()); Query<CustomerGroupSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(customer)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .forMessageType(CustomerGroupSetMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<CustomerGroupSetMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final CustomerGroupSetMessage message = queryResult.head().get(); assertThat(message.getCustomerGroup()).isEqualTo(customerGroup.toReference()); }); }); }); }