1 package com.srv4pos.server.api.order;
2
3 import com.srv4pos.server.api.ServiceFactory;
4 import com.srv4pos.server.api.activation.ContactInfo;
5 import com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException;
6 import com.srv4pos.server.api.infrastructure.DiagnosticsUtil;
7 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
8 import com.srv4pos.server.api.infrastructure.http.Transport;
9 import com.srv4pos.server.api.seller.SellerInfo;
10 import com.srv4pos.server.api.seller.SellerService;
11
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.List;
16 import java.util.Map;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22
23
24
25 public class OrderServiceSample {
26 private final SellerService sellerService;
27 private final OrderService orderService;
28
29 public OrderServiceSample(URL url, Credentials credentials, Transport transport) {
30 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
31 sellerService = serviceFactory.getSellerService();
32 orderService = serviceFactory.getOrderService();
33 }
34
35 public void testSendOrder() {
36 int currentSellerVersion = sellerService.getThisVersion().getVersion();
37
38
39
40 List<OrderItemInfo> items = new ArrayList<OrderItemInfo>();
41 items.add(createOrderItem("Product1", "Product customization 1", 100));
42 items.add(createOrderItem("Product2", 200));
43
44 DeliveryInfo delivery = new DeliveryInfo();
45 delivery.setAddress("Address, building 1");
46
47 ContactInfo contact = new ContactInfo();
48 contact.setName("Mike");
49 contact.setPhone("+987123555");
50 contact.setEmail("test@example.com");
51 delivery.setContact(contact);
52 delivery.setDate(new Date());
53
54 OrderInfo order = createOrder("Advandare", items, null, delivery, "GcmRegId");
55
56 SellerInfo selectedSeller = sellerService.get(null);
57
58
59 try {
60 orderService.create(currentSellerVersion, order);
61 } catch (ReferenceNotFoundJsonException e) {
62
63
64 Map<String, String> details = e.getDetails();
65 fail(String.format("Entity %s with id %s is not found!", details.get("entityName"), details.get("identifier")));
66 }
67 }
68
69 public void testGetOrder() throws InterruptedException {
70 testGetOrder(new Stopper() {
71 private int times;
72
73 public boolean stop() {
74 times++;
75 return times > 2;
76 }
77
78 public int getTimes() {
79 return times;
80 }
81 });
82 }
83
84 public void testPutOrder() {
85 int currentSellerVersion = sellerService.getThisVersion().getVersion();
86
87 List<OrderItemInfo> items = new ArrayList<OrderItemInfo>();
88 items.add(createOrderItem("Product1", "Product customization 1", 100));
89 items.add(createOrderItem("Product2", 200));
90
91 OrderInfo orderInfoCreate = createOrder("Advandare", items, null, null, "GcmRegId");
92 orderService.putOrder("ORD1", orderInfoCreate, currentSellerVersion);
93
94 OrderInfo orderInfoGet = orderService.get("ORD1", null);
95 assertEquals(2, orderInfoGet.getOrderItems().size());
96
97 orderInfoGet.getOrderItems().remove(0);
98 orderService.putOrder("ORD1", orderInfoGet, sellerService.getThisVersion().getVersion());
99
100 assertEquals(1, orderService.get("ORD1", null).getOrderItems().size());
101 }
102
103
104
105
106
107
108 public interface Stopper {
109 boolean stop();
110 }
111
112 private void testGetOrder(Stopper stopper) throws InterruptedException {
113
114 int version = 0;
115 int pageSize = 1000;
116 int afterWhatTimeCheckAgainInMillis = 100;
117 while (!stopper.stop()) {
118
119 final int latestVersion = sellerService.getThisVersion().getVersion();
120
121 if (latestVersion < version) {
122
123 Thread.sleep(afterWhatTimeCheckAgainInMillis);
124 continue;
125 }
126
127
128 final int versionFrom = version;
129 final int versionTo = Math.min(version + pageSize, latestVersion);
130
131
132 final List<OrderInfo> orders = orderService.listDiff(versionFrom, versionTo);
133
134
135 DeliveryInfo deliveryInfo = orders.get(0).getDelivery();
136 assertEquals("Mike", deliveryInfo.getContact().getName());
137 assertEquals("Address, building 1", deliveryInfo.getAddress());
138
139
140 for (OrderInfo orderInfo : orders) {
141 createSaleByOrder(orderInfo);
142 }
143
144
145 version = versionTo + 1;
146 }
147 }
148
149 public static final DiagnosticsUtil.Indexer<String, OrderItemInfo> ORDER_ITEM_INFO_BY_PRODUCT_INDEXER =
150 new DiagnosticsUtil.Indexer<String, OrderItemInfo>() {
151 public String index(OrderItemInfo orderItemInfo) {
152 return orderItemInfo.getProductIdentifier();
153 }
154 };
155
156 private static void assertCustmozation(Map<String, OrderItemInfo> orderItemsByProductName, String product1, String expectedCustomization) {
157 org.junit.Assert.assertEquals(
158 String.format("Make sure order item for test product with name %s has correct customization", product1),
159 expectedCustomization,
160 orderItemsByProductName.get(product1).getProductCustomization()
161 );
162 }
163
164 private static void createSaleByOrder(OrderInfo orderInfo) {
165 final List<OrderItemInfo> orderItems = orderInfo.getOrderItems();
166 org.junit.Assert.assertEquals("Make sure amount of order items is as expected", 2, orderItems.size());
167 final Map<String, OrderItemInfo> orderItemsByProductIdentifier = DiagnosticsUtil.index(orderItems, ORDER_ITEM_INFO_BY_PRODUCT_INDEXER);
168 final String product1 = "PRODUCT1";
169 final String product2 = "PRODUCT2";
170 containsProduct(orderItemsByProductIdentifier, product1);
171 containsProduct(orderItemsByProductIdentifier, product2);
172 assertDelta(orderItemsByProductIdentifier, product1, 100);
173 assertCustmozation(orderItemsByProductIdentifier, product1, "Product customization 1");
174 assertDelta(orderItemsByProductIdentifier, product2, 200);
175 }
176
177 private static void assertDelta(Map<String, OrderItemInfo> orderItemsByProductIdentifier, String productID, int delata) {
178 org.junit.Assert.assertEquals(
179 String.format("Make sure order item fir test product with id %s has correct delta", productID),
180 delata,
181 orderItemsByProductIdentifier.get(productID).getDelta()
182 );
183 }
184
185 private static void containsProduct(Map<String, OrderItemInfo> orderItemsByProductIdentifier, String productId) {
186 assertTrue(
187 String.format("Make sure order has test product with id %s", productId),
188 orderItemsByProductIdentifier.containsKey(productId)
189 );
190 }
191
192
193 public OrderItemInfo createOrderItem(final String productIdentifier, final int delta) {
194 return createOrderItem(productIdentifier, null, delta);
195 }
196
197 public OrderItemInfo createOrderItem(final String productIdentifier, final String customization, final int delta) {
198 OrderItemInfo orderItemInfo = new OrderItemInfo();
199 orderItemInfo.setDelta(delta);
200 orderItemInfo.setProductIdentifier(productIdentifier);
201 orderItemInfo.setProductCustomization(customization);
202 return orderItemInfo;
203 }
204
205 public OrderInfo createOrder(final String treasurer, final List<OrderItemInfo> items, String bookingIdentifier,
206 DeliveryInfo delivery, String gcmRegistrationId) {
207 OrderInfo orderInfo = new OrderInfo();
208 orderInfo.setTreasurerName(treasurer);
209 orderInfo.setOrderItems(items);
210 orderInfo.setBooking(bookingIdentifier);
211 orderInfo.setDelivery(delivery);
212 orderInfo.setGcmRegistrationId(gcmRegistrationId);
213 return orderInfo;
214 }
215
216 }