1+ /**
2+ * This Source Code Form is subject to the terms of the Mozilla Public License,
3+ * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+ * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+ * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+ *
7+ * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+ * graphic logo is a trademark of OpenMRS Inc.
9+ */
10+ package org .openmrs .module .initializer .api .billing ;
11+
12+ import static org .junit .Assert .assertEquals ;
13+ import static org .junit .Assert .assertNull ;
14+ import static org .junit .Assert .assertSame ;
15+ import static org .mockito .Mockito .when ;
16+
17+ import java .math .BigDecimal ;
18+
19+ import org .junit .Before ;
20+ import org .junit .Test ;
21+ import org .mockito .Mock ;
22+ import org .mockito .MockitoAnnotations ;
23+ import org .openmrs .module .billing .api .BillableServiceService ;
24+ import org .openmrs .module .billing .api .PaymentModeService ;
25+ import org .openmrs .module .billing .api .model .BillableService ;
26+ import org .openmrs .module .billing .api .model .CashierItemPrice ;
27+ import org .openmrs .module .billing .api .model .PaymentMode ;
28+ import org .openmrs .module .initializer .api .CsvLine ;
29+ import org .openmrs .module .stockmanagement .api .StockManagementService ;
30+ import org .openmrs .module .stockmanagement .api .model .StockItem ;
31+
32+ public class CashierItemPriceLineProcessorTest {
33+
34+ private static final String PAYMENT_MODE_UUID = "pm-0000-0000-0000-000000000001" ;
35+
36+ private static final String STOCK_ITEM_UUID = "si-0000-0000-0000-000000000001" ;
37+
38+ private static final String BILLABLE_SERVICE_UUID = "bs-0000-0000-0000-000000000001" ;
39+
40+ private static final String [] HEADERS = new String [] { "Uuid" , "name" , "price" , "payment mode" , "stock item" ,
41+ "billable service" };
42+
43+ @ Mock
44+ private PaymentModeService paymentModeService ;
45+
46+ @ Mock
47+ private StockManagementService stockManagementService ;
48+
49+ @ Mock
50+ private BillableServiceService billableServiceService ;
51+
52+ private CashierItemPriceLineProcessor processor ;
53+
54+ private PaymentMode paymentMode ;
55+
56+ @ Before
57+ public void setUp () {
58+ MockitoAnnotations .initMocks (this );
59+ processor = new CashierItemPriceLineProcessor (paymentModeService , stockManagementService , billableServiceService );
60+ paymentMode = new PaymentMode ();
61+ paymentMode .setUuid (PAYMENT_MODE_UUID );
62+ when (paymentModeService .getPaymentModeByUuid (PAYMENT_MODE_UUID )).thenReturn (paymentMode );
63+ }
64+
65+ @ Test
66+ public void fill_shouldClearBillableServiceWhenSwitchingToStockItem () {
67+ CashierItemPrice instance = new CashierItemPrice ();
68+ BillableService existing = new BillableService ();
69+ existing .setUuid (BILLABLE_SERVICE_UUID );
70+ instance .setBillableService (existing );
71+
72+ StockItem stockItem = new StockItem ();
73+ stockItem .setUuid (STOCK_ITEM_UUID );
74+ when (stockManagementService .getStockItemByUuid (STOCK_ITEM_UUID )).thenReturn (stockItem );
75+
76+ CsvLine line = new CsvLine (HEADERS ,
77+ new String [] { "" , "Switched Price" , "100.00" , PAYMENT_MODE_UUID , STOCK_ITEM_UUID , "" });
78+
79+ processor .fill (instance , line );
80+
81+ assertSame (stockItem , instance .getItem ());
82+ assertNull (instance .getBillableService ());
83+ }
84+
85+ @ Test
86+ public void fill_shouldClearItemWhenSwitchingToBillableService () {
87+ CashierItemPrice instance = new CashierItemPrice ();
88+ StockItem existing = new StockItem ();
89+ existing .setUuid (STOCK_ITEM_UUID );
90+ instance .setItem (existing );
91+
92+ BillableService billableService = new BillableService ();
93+ billableService .setUuid (BILLABLE_SERVICE_UUID );
94+ when (billableServiceService .getBillableServiceByUuid (BILLABLE_SERVICE_UUID )).thenReturn (billableService );
95+
96+ CsvLine line = new CsvLine (HEADERS ,
97+ new String [] { "" , "Switched Price" , "100.00" , PAYMENT_MODE_UUID , "" , BILLABLE_SERVICE_UUID });
98+
99+ processor .fill (instance , line );
100+
101+ assertSame (billableService , instance .getBillableService ());
102+ assertNull (instance .getItem ());
103+ }
104+
105+ @ Test
106+ public void fill_shouldSetBasicFields () {
107+ CashierItemPrice instance = new CashierItemPrice ();
108+ BillableService billableService = new BillableService ();
109+ billableService .setUuid (BILLABLE_SERVICE_UUID );
110+ when (billableServiceService .getBillableServiceByUuid (BILLABLE_SERVICE_UUID )).thenReturn (billableService );
111+
112+ CsvLine line = new CsvLine (HEADERS ,
113+ new String [] { "" , "ANC Service Price" , "150.00" , PAYMENT_MODE_UUID , "" , BILLABLE_SERVICE_UUID });
114+
115+ processor .fill (instance , line );
116+
117+ assertEquals ("ANC Service Price" , instance .getName ());
118+ assertEquals (new BigDecimal ("150.00" ), instance .getPrice ());
119+ assertSame (paymentMode , instance .getPaymentMode ());
120+ }
121+
122+ @ Test (expected = IllegalArgumentException .class )
123+ public void fill_shouldRejectWhenBothAssociationsSet () {
124+ CashierItemPrice instance = new CashierItemPrice ();
125+ CsvLine line = new CsvLine (HEADERS , new String [] { "" , "Price" , "100.00" , PAYMENT_MODE_UUID , STOCK_ITEM_UUID ,
126+ BILLABLE_SERVICE_UUID });
127+ processor .fill (instance , line );
128+ }
129+
130+ @ Test (expected = IllegalArgumentException .class )
131+ public void fill_shouldRejectWhenNeitherAssociationSet () {
132+ CashierItemPrice instance = new CashierItemPrice ();
133+ CsvLine line = new CsvLine (HEADERS , new String [] { "" , "Price" , "100.00" , PAYMENT_MODE_UUID , "" , "" });
134+ processor .fill (instance , line );
135+ }
136+ }
0 commit comments