From 1b33445193c4ee9aa2bdedab5e1b79a882ec453f Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 23 Jul 2024 19:34:24 +0530 Subject: [PATCH] feat: update order service --- src/ordering/adapters/output/db/errors.rs | 1 - src/ordering/adapters/output/db/mod.rs | 2 +- .../adapters/output/db/order_id_exists.rs | 10 +- src/ordering/adapters/output/db/order_view.rs | 8 +- .../application/port/output/db/errors.rs | 1 - .../port/output/db/order_id_exists.rs | 8 +- .../application/services/add_order_service.rs | 6 +- src/ordering/application/services/mod.rs | 9 +- .../services/update_order_service.rs | 119 ++++++++++++++++++ src/ordering/domain/commands.rs | 3 +- src/ordering/domain/events.rs | 3 + src/ordering/domain/line_item_aggregate.rs | 1 + src/ordering/domain/mod.rs | 2 + src/ordering/domain/order_aggregate.rs | 106 ++++++++-------- src/ordering/domain/order_updated_event.rs | 48 +++++++ src/ordering/domain/update_order_command.rs | 117 +++++++++++++++++ 16 files changed, 359 insertions(+), 85 deletions(-) create mode 100644 src/ordering/application/services/update_order_service.rs create mode 100644 src/ordering/domain/order_updated_event.rs create mode 100644 src/ordering/domain/update_order_command.rs diff --git a/src/ordering/adapters/output/db/errors.rs b/src/ordering/adapters/output/db/errors.rs index 5a6789a..0c45084 100644 --- a/src/ordering/adapters/output/db/errors.rs +++ b/src/ordering/adapters/output/db/errors.rs @@ -24,7 +24,6 @@ impl From for OrderingDBError { return Self::DuplicateLineItemID; } else if msg.contains("cqrs_ordering_store_query_order_id_key") { return Self::DuplicateOrderID; - } else { println!("{msg}"); } diff --git a/src/ordering/adapters/output/db/mod.rs b/src/ordering/adapters/output/db/mod.rs index 0f1a601..dca55ef 100644 --- a/src/ordering/adapters/output/db/mod.rs +++ b/src/ordering/adapters/output/db/mod.rs @@ -10,8 +10,8 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres}; mod errors; mod line_item_id_exists; -mod order_id_exists; mod line_item_view; +mod order_id_exists; mod order_view; #[derive(Clone)] diff --git a/src/ordering/adapters/output/db/order_id_exists.rs b/src/ordering/adapters/output/db/order_id_exists.rs index 9510030..5b5a99c 100644 --- a/src/ordering/adapters/output/db/order_id_exists.rs +++ b/src/ordering/adapters/output/db/order_id_exists.rs @@ -71,18 +71,12 @@ pub mod tests { let order = Order::default(); // state doesn't exist - assert!(!db - .order_id_exists(order.order_id()) - .await - .unwrap()); + assert!(!db.order_id_exists(order.order_id()).await.unwrap()); create_dummy_order(&order, &db).await; // state exists - assert!(db - .order_id_exists(order.order_id()) - .await - .unwrap()); + assert!(db.order_id_exists(order.order_id()).await.unwrap()); settings.drop_db().await; } diff --git a/src/ordering/adapters/output/db/order_view.rs b/src/ordering/adapters/output/db/order_view.rs index 242713c..313aec7 100644 --- a/src/ordering/adapters/output/db/order_view.rs +++ b/src/ordering/adapters/output/db/order_view.rs @@ -43,7 +43,6 @@ impl Default for OrderView { impl From for Order { fn from(v: OrderView) -> Self { - OrderBuilder::default() .customer_name(v.customer_name) .order_id(v.order_id) @@ -212,12 +211,7 @@ impl Query for OrderingDBPostgresAdapter { let res = self .load_with_context(order_id) .await - .unwrap_or_else(|_| { - Some(( - OrderView::default(), - ViewContext::new(order_id.into(), 0), - )) - }); + .unwrap_or_else(|_| Some((OrderView::default(), ViewContext::new(order_id.into(), 0)))); let (mut view, view_context): (OrderView, ViewContext) = res.unwrap(); for event in events { view.update(event); diff --git a/src/ordering/application/port/output/db/errors.rs b/src/ordering/application/port/output/db/errors.rs index b383b4c..43f5eb1 100644 --- a/src/ordering/application/port/output/db/errors.rs +++ b/src/ordering/application/port/output/db/errors.rs @@ -14,5 +14,4 @@ pub enum OrderingDBError { InternalError, DuplicateOrderID, OrderIDNotFound, - } diff --git a/src/ordering/application/port/output/db/order_id_exists.rs b/src/ordering/application/port/output/db/order_id_exists.rs index 97e7951..349209c 100644 --- a/src/ordering/application/port/output/db/order_id_exists.rs +++ b/src/ordering/application/port/output/db/order_id_exists.rs @@ -25,9 +25,7 @@ pub mod tests { use std::sync::Arc; - pub fn mock_order_id_exists_db_port_false( - times: Option, - ) -> OrderIDExistsDBPortObj { + pub fn mock_order_id_exists_db_port_false(times: Option) -> OrderIDExistsDBPortObj { let mut m = MockOrderIDExistsDBPort::new(); if let Some(times) = times { m.expect_order_id_exists() @@ -40,9 +38,7 @@ pub mod tests { Arc::new(m) } - pub fn mock_order_id_exists_db_port_true( - times: Option, - ) -> OrderIDExistsDBPortObj { + pub fn mock_order_id_exists_db_port_true(times: Option) -> OrderIDExistsDBPortObj { let mut m = MockOrderIDExistsDBPort::new(); if let Some(times) = times { m.expect_order_id_exists() diff --git a/src/ordering/application/services/add_order_service.rs b/src/ordering/application/services/add_order_service.rs index d66b45e..e9893ee 100644 --- a/src/ordering/application/services/add_order_service.rs +++ b/src/ordering/application/services/add_order_service.rs @@ -36,11 +36,7 @@ impl AddOrderUseCase for AddOrderService { let mut order_id = self.get_uuid.get_uuid(); loop { - if self - .db_order_id_exists - .order_id_exists(&order_id) - .await? - { + if self.db_order_id_exists.order_id_exists(&order_id).await? { order_id = self.get_uuid.get_uuid(); continue; } else { diff --git a/src/ordering/application/services/mod.rs b/src/ordering/application/services/mod.rs index 170139c..d723801 100644 --- a/src/ordering/application/services/mod.rs +++ b/src/ordering/application/services/mod.rs @@ -10,9 +10,10 @@ pub mod errors; //services pub mod add_line_item_service; +pub mod add_order_service; pub mod delete_line_item_service; pub mod update_line_item_service; -pub mod add_order_service; +pub mod update_order_service; #[automock] pub trait OrderingServicesInterface: Send + Sync { @@ -20,6 +21,7 @@ pub trait OrderingServicesInterface: Send + Sync { fn update_line_item(&self) -> update_line_item_service::UpdateLineItemServiceObj; fn delete_line_item(&self) -> delete_line_item_service::DeleteLineItemServiceObj; fn add_order(&self) -> add_order_service::AddOrderServiceObj; + fn update_order(&self) -> update_order_service::UpdateOrderServiceObj; } #[derive(Clone, Builder)] @@ -28,6 +30,7 @@ pub struct OrderingServices { update_line_item: update_line_item_service::UpdateLineItemServiceObj, delete_line_item: delete_line_item_service::DeleteLineItemServiceObj, add_order: add_order_service::AddOrderServiceObj, + update_order: update_order_service::UpdateOrderServiceObj, } impl OrderingServicesInterface for OrderingServices { @@ -45,4 +48,8 @@ impl OrderingServicesInterface for OrderingServices { fn add_order(&self) -> add_order_service::AddOrderServiceObj { self.add_order.clone() } + + fn update_order(&self) -> update_order_service::UpdateOrderServiceObj { + self.update_order.clone() + } } diff --git a/src/ordering/application/services/update_order_service.rs b/src/ordering/application/services/update_order_service.rs new file mode 100644 index 0000000..667628e --- /dev/null +++ b/src/ordering/application/services/update_order_service.rs @@ -0,0 +1,119 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::sync::Arc; + +use derive_builder::Builder; +use mockall::predicate::*; +use mockall::*; +use time::OffsetDateTime; + +use super::errors::*; +use crate::ordering::{ + application::port::output::db::order_id_exists::*, + domain::{order_aggregate::*, order_updated_event::*, update_order_command::*}, +}; +use crate::utils::uuid::*; + +#[automock] +#[async_trait::async_trait] +pub trait UpdateOrderUseCase: Send + Sync { + async fn update_order(&self, cmd: UpdateOrderCommand) -> OrderingResult; +} + +pub type UpdateOrderServiceObj = Arc; + +#[derive(Clone, Builder)] +pub struct UpdateOrderService { + db_order_id_exists: OrderIDExistsDBPortObj, +} + +#[async_trait::async_trait] +impl UpdateOrderUseCase for UpdateOrderService { + async fn update_order(&self, cmd: UpdateOrderCommand) -> OrderingResult { + if !self + .db_order_id_exists + .order_id_exists(cmd.old_order().order_id()) + .await? + { + return Err(OrderingError::OrderIDNotFound); + } + + let new_order = OrderBuilder::default() + .created_time(cmd.created_time().clone()) + .customer_name(cmd.customer_name().into()) + .order_id(*cmd.old_order().order_id()) + .deleted(false) + .build() + .unwrap(); + + Ok(OrderUpdatedEventBuilder::default() + .added_by_user(*cmd.adding_by()) + .new_order(new_order) + .old_order(cmd.old_order().clone()) + .build() + .unwrap()) + } +} + +#[cfg(test)] +pub mod tests { + use super::*; + + use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command; + use crate::utils::uuid::tests::UUID; + use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid}; + + pub fn mock_update_order_service( + times: Option, + cmd: UpdateOrderCommand, + ) -> UpdateOrderServiceObj { + let mut m = MockUpdateOrderUseCase::new(); + + let res = get_updated_order_event_from_command(&cmd); + if let Some(times) = times { + m.expect_update_order() + .times(times) + .returning(move |_| Ok(res.clone())); + } else { + m.expect_update_order().returning(move |_| Ok(res.clone())); + } + + Arc::new(m) + } + + #[actix_rt::test] + async fn test_service() { + let cmd = UpdateOrderCommand::get_cmd(); + + let s = UpdateOrderServiceBuilder::default() + .db_order_id_exists(mock_order_id_exists_db_port_true(IS_CALLED_ONLY_ONCE)) + .build() + .unwrap(); + + let res = s.update_order(cmd.clone()).await.unwrap(); + assert_eq!(res.new_order().customer_name(), cmd.customer_name()); + assert_eq!(res.new_order().order_id(), cmd.old_order().order_id()); + assert!(!res.new_order().deleted()); + + assert_eq!(res.old_order(), cmd.old_order()); + + assert_eq!(res.added_by_user(), cmd.adding_by()); + } + + #[actix_rt::test] + async fn test_service_order_id_doesnt_exist() { + let cmd = UpdateOrderCommand::get_cmd(); + + let s = UpdateOrderServiceBuilder::default() + .db_order_id_exists(mock_order_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) + .build() + .unwrap(); + + assert_eq!( + s.update_order(cmd.clone()).await, + Err(OrderingError::OrderIDNotFound) + ); + } +} diff --git a/src/ordering/domain/commands.rs b/src/ordering/domain/commands.rs index d68ec89..13983a3 100644 --- a/src/ordering/domain/commands.rs +++ b/src/ordering/domain/commands.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; use super::{ add_line_item_command::AddLineItemCommand, add_order_command::AddOrderCommand, delete_line_item_command::DeleteLineItemCommand, - update_line_item_command::UpdateLineItemCommand, + update_line_item_command::UpdateLineItemCommand, update_order_command::UpdateOrderCommand, }; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] @@ -17,4 +17,5 @@ pub enum OrderingCommand { UpdateLineItem(UpdateLineItemCommand), DeleteLineItem(DeleteLineItemCommand), AddOrder(AddOrderCommand), + UpdateOrder(UpdateOrderCommand), } diff --git a/src/ordering/domain/events.rs b/src/ordering/domain/events.rs index 0d6a6ef..969456d 100644 --- a/src/ordering/domain/events.rs +++ b/src/ordering/domain/events.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; use super::{ line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent, line_item_updated_event::LineItemUpdatedEvent, order_added_event::OrderAddedEvent, + order_updated_event::OrderUpdatedEvent, }; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] @@ -16,6 +17,7 @@ pub enum OrderingEvent { LineItemUpdated(LineItemUpdatedEvent), LineItemDeleted(LineItemDeletedEvent), OrderAdded(OrderAddedEvent), + OrderUpdated(OrderUpdatedEvent), } impl DomainEvent for OrderingEvent { @@ -29,6 +31,7 @@ impl DomainEvent for OrderingEvent { OrderingEvent::LineItemUpdated { .. } => "OrderingLineItemUpdated", OrderingEvent::LineItemDeleted { .. } => "OrderingLineItemDeleted", OrderingEvent::OrderAdded { .. } => "OrderingOrderAdded", + OrderingEvent::OrderUpdated { .. } => "OrderingOrderUpdated", }; e.to_string() diff --git a/src/ordering/domain/line_item_aggregate.rs b/src/ordering/domain/line_item_aggregate.rs index 0483d33..d1859ea 100644 --- a/src/ordering/domain/line_item_aggregate.rs +++ b/src/ordering/domain/line_item_aggregate.rs @@ -25,6 +25,7 @@ pub struct LineItem { product_name: String, product_id: Uuid, line_item_id: Uuid, + // TODO: add kot_id quantity: Quantity, #[builder(default = "false")] deleted: bool, diff --git a/src/ordering/domain/mod.rs b/src/ordering/domain/mod.rs index 1a806b2..907a767 100644 --- a/src/ordering/domain/mod.rs +++ b/src/ordering/domain/mod.rs @@ -13,6 +13,7 @@ pub mod add_order_command; pub mod commands; pub mod delete_line_item_command; pub mod update_line_item_command; +pub mod update_order_command; // events pub mod events; @@ -20,3 +21,4 @@ pub mod line_item_added_event; pub mod line_item_deleted_event; pub mod line_item_updated_event; pub mod order_added_event; +pub mod order_updated_event; diff --git a/src/ordering/domain/order_aggregate.rs b/src/ordering/domain/order_aggregate.rs index 2768f7c..bbf8a1d 100644 --- a/src/ordering/domain/order_aggregate.rs +++ b/src/ordering/domain/order_aggregate.rs @@ -15,7 +15,6 @@ use crate::ordering::{ domain::{commands::*, events::*}, }; - #[derive( Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters, )] @@ -60,7 +59,6 @@ mod tests { } } - #[async_trait] impl Aggregate for Order { type Command = OrderingCommand; @@ -85,6 +83,10 @@ impl Aggregate for Order { let res = services.add_order().add_order(cmd).await?; Ok(vec![OrderingEvent::OrderAdded(res)]) } + OrderingCommand::UpdateOrder(cmd) => { + let res = services.update_order().update_order(cmd).await?; + Ok(vec![OrderingEvent::OrderUpdated(res)]) + } _ => Ok(Vec::default()), } } @@ -92,8 +94,8 @@ impl Aggregate for Order { fn apply(&mut self, event: Self::Event) { match event { OrderingEvent::OrderAdded(e) => *self = e.order().clone(), - // OrderingEvent::OrderUpdated(e) => *self = e.new_order().clone(), - // OrderingEvent::OrderDeleted(e) => *self = e.order().clone(), + OrderingEvent::OrderUpdated(e) => *self = e.new_order().clone(), + // OrderingEvent::OrderDeleted(e) => *self = e.order().clone(), _ => (), } } @@ -105,20 +107,19 @@ mod aggregate_tests { use add_order_service::tests::mock_add_order_service; use cqrs_es::test::TestFramework; - // use delete_order_service::tests::mock_delete_order_service; - // use update_order_service::tests::mock_update_order_service; + // use delete_order_service::tests::mock_delete_order_service; + use update_order_service::tests::mock_update_order_service; use super::*; -// use crate::ordering::domain::delete_order_command::DeleteOrderCommand; -// use crate::ordering::domain::order_deleted_event::tests::get_deleted_order_event_from_command; -// use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command; -// use crate::ordering::domain::update_order_command::UpdateOrderCommand; + // use crate::ordering::domain::delete_order_command::DeleteOrderCommand; + // use crate::ordering::domain::order_deleted_event::tests::get_deleted_order_event_from_command; + use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command; + use crate::ordering::domain::update_order_command::UpdateOrderCommand; use crate::tests::bdd::*; use crate::ordering::domain::{ - add_order_command::*, - order_added_event::tests::get_added_order_event_from_command, + add_order_command::*, order_added_event::tests::get_added_order_event_from_command, }; type OrderTestFramework = TestFramework; @@ -140,46 +141,43 @@ mod aggregate_tests { .when(OrderingCommand::AddOrder(cmd)) .then_expect_events(vec![expected]); } -// -// #[test] -// fn test_update_order() { -// let cmd = UpdateOrderCommand::get_cmd(); -// let expected = get_updated_order_event_from_command(&cmd); -// let expected = OrderingEvent::OrderUpdated(expected); -// -// let mut services = MockOrderingServicesInterface::new(); -// services -// .expect_update_order() -// .times(IS_CALLED_ONLY_ONCE.unwrap()) -// .return_const(mock_update_order_service( -// IS_CALLED_ONLY_ONCE, -// cmd.clone(), -// )); -// -// OrderTestFramework::with(Arc::new(services)) -// .given_no_previous_events() -// .when(OrderingCommand::UpdateOrder(cmd)) -// .then_expect_events(vec![expected]); -// } -// -// #[test] -// fn test_delete_order() { -// let cmd = DeleteOrderCommand::get_cmd(); -// let expected = get_deleted_order_event_from_command(&cmd); -// let expected = OrderingEvent::OrderDeleted(expected); -// -// let mut services = MockOrderingServicesInterface::new(); -// services -// .expect_delete_order() -// .times(IS_CALLED_ONLY_ONCE.unwrap()) -// .return_const(mock_delete_order_service( -// IS_CALLED_ONLY_ONCE, -// cmd.clone(), -// )); -// -// OrderTestFramework::with(Arc::new(services)) -// .given_no_previous_events() -// .when(OrderingCommand::DeleteOrder(cmd)) -// .then_expect_events(vec![expected]); -// } + + #[test] + fn test_update_order() { + let cmd = UpdateOrderCommand::get_cmd(); + let expected = get_updated_order_event_from_command(&cmd); + let expected = OrderingEvent::OrderUpdated(expected); + + let mut services = MockOrderingServicesInterface::new(); + services + .expect_update_order() + .times(IS_CALLED_ONLY_ONCE.unwrap()) + .return_const(mock_update_order_service(IS_CALLED_ONLY_ONCE, cmd.clone())); + + OrderTestFramework::with(Arc::new(services)) + .given_no_previous_events() + .when(OrderingCommand::UpdateOrder(cmd)) + .then_expect_events(vec![expected]); + } + // + // #[test] + // fn test_delete_order() { + // let cmd = DeleteOrderCommand::get_cmd(); + // let expected = get_deleted_order_event_from_command(&cmd); + // let expected = OrderingEvent::OrderDeleted(expected); + // + // let mut services = MockOrderingServicesInterface::new(); + // services + // .expect_delete_order() + // .times(IS_CALLED_ONLY_ONCE.unwrap()) + // .return_const(mock_delete_order_service( + // IS_CALLED_ONLY_ONCE, + // cmd.clone(), + // )); + // + // OrderTestFramework::with(Arc::new(services)) + // .given_no_previous_events() + // .when(OrderingCommand::DeleteOrder(cmd)) + // .then_expect_events(vec![expected]); + // } } diff --git a/src/ordering/domain/order_updated_event.rs b/src/ordering/domain/order_updated_event.rs new file mode 100644 index 0000000..594a5ba --- /dev/null +++ b/src/ordering/domain/order_updated_event.rs @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_builder::Builder; +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use super::order_aggregate::*; + +#[derive( + Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd, +)] +pub struct OrderUpdatedEvent { + added_by_user: Uuid, + + new_order: Order, + old_order: Order, +} + +#[cfg(test)] +pub mod tests { + use crate::ordering::domain::update_order_command::UpdateOrderCommand; + + use super::*; + + pub fn get_updated_order_event_from_command(cmd: &UpdateOrderCommand) -> OrderUpdatedEvent { + let new_order = OrderBuilder::default() + .created_time(cmd.created_time().clone()) + .customer_name(cmd.customer_name().into()) + .order_id(*cmd.old_order().order_id()) + .build() + .unwrap(); + + OrderUpdatedEventBuilder::default() + .added_by_user(cmd.adding_by().clone()) + .new_order(new_order) + .old_order(cmd.old_order().clone()) + .build() + .unwrap() + } + + #[test] + fn test_event() { + get_updated_order_event_from_command(&UpdateOrderCommand::get_cmd()); + } +} diff --git a/src/ordering/domain/update_order_command.rs b/src/ordering/domain/update_order_command.rs new file mode 100644 index 0000000..3029043 --- /dev/null +++ b/src/ordering/domain/update_order_command.rs @@ -0,0 +1,117 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_builder::Builder; +use derive_getters::Getters; +use derive_more::{Display, Error}; +use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; +use uuid::Uuid; + +use super::order_aggregate::*; +use crate::utils::string::empty_string_err; + +#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub enum UpdateOrderCommandError { + CustomerNameIsEmpty, +} + +#[derive( + Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, +)] +pub struct UnvalidatedUpdateOrderCommand { + adding_by: Uuid, + + #[builder(default = "OffsetDateTime::now_utc()")] + created_time: OffsetDateTime, + customer_name: String, + old_order: Order, +} + +impl UnvalidatedUpdateOrderCommand { + pub fn validate(self) -> Result { + let customer_name = empty_string_err( + self.customer_name, + UpdateOrderCommandError::CustomerNameIsEmpty, + )?; + + Ok(UpdateOrderCommand { + created_time: self.created_time, + customer_name, + adding_by: self.adding_by, + old_order: self.old_order, + }) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct UpdateOrderCommand { + created_time: OffsetDateTime, + customer_name: String, + adding_by: Uuid, + old_order: Order, +} + +#[cfg(test)] +mod tests { + use time::macros::datetime; + + use crate::utils::uuid::tests::UUID; + + use super::*; + + impl UpdateOrderCommand { + pub fn get_cmd() -> Self { + let customer_name = "foo-new-name"; + let adding_by = UUID; + + UnvalidatedUpdateOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .created_time(datetime!(1970-01-01 0:00 UTC)) + .old_order(Order::default()) + .build() + .unwrap() + .validate() + .unwrap() + } + } + + #[test] + fn test_cmd() { + let customer_name = "foo"; + let adding_by = UUID; + let old_order = Order::default(); + + let cmd = UnvalidatedUpdateOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .old_order(old_order.clone()) + .build() + .unwrap() + .validate() + .unwrap(); + + assert_eq!(*cmd.adding_by(), adding_by); + assert_eq!(cmd.customer_name(), customer_name); + assert_eq!(cmd.old_order(), &old_order); + } + + #[test] + fn test_cmd_customer_name_empty() { + let customer_name = ""; + let adding_by = UUID; + + assert_eq!( + UnvalidatedUpdateOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .old_order(Order::default()) + .build() + .unwrap() + .validate(), + Err(UpdateOrderCommandError::CustomerNameIsEmpty) + ); + } +}