1 package com.srv4pos.server.api.precept;
2
3 import com.srv4pos.server.api.ServiceFactory;
4 import com.srv4pos.server.api.activation.ActivationServiceSample;
5 import com.srv4pos.server.api.exceptions.ReferenceNotFoundJsonException;
6 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
7 import com.srv4pos.server.api.infrastructure.http.Transport;
8 import com.srv4pos.server.api.order.OrderInfo;
9 import com.srv4pos.server.api.order.OrderItemInfo;
10 import com.srv4pos.server.api.order.OrderService;
11 import com.srv4pos.server.api.seller.SellerInfo;
12 import com.srv4pos.server.api.seller.SellerService;
13
14 import java.net.URL;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 import static java.lang.String.format;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.fail;
23
24 public class PreceptServiceSample {
25 private final SellerService sellerService;
26 private final PreceptService preceptService;
27 private final OrderService orderService;
28
29 public PreceptServiceSample(URL url, Credentials credentials, Transport transport) {
30 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
31 sellerService = serviceFactory.getSellerService();
32 orderService = serviceFactory.getOrderService();
33 preceptService = serviceFactory.getPreceptService();
34 }
35
36 public void test() {
37
38 int currentSellerVersion = sellerService.getThisVersion().getVersion();
39 String orderIdentifier = postOrder(currentSellerVersion++);
40
41 List<PreceptItemInfo> items = new ArrayList<PreceptItemInfo>();
42 items.add(createPreceptItem("Product1", "Product customization 1", 100));
43 items.add(createPreceptItem("Product2", 200));
44
45 PreceptInfo preceptInfo = new PreceptInfo();
46 preceptInfo.setTreasurerName("Advandare");
47 preceptInfo.setPreceptItems(items);
48 preceptInfo.setIdentifier(format("%s-%s", ActivationServiceSample.MOBILKASSAN_CASH_REGISTER_NAME, "1-01"));
49 preceptInfo.setTable("table near the entrance");
50 preceptInfo.setOrder(orderIdentifier);
51
52 SellerInfo selectedSeller = sellerService.get(null);
53
54 String preceptIdentifier = null;
55 try {
56
57 preceptIdentifier = preceptService.create(selectedSeller.getCountry(), selectedSeller.getCorporateId(), preceptInfo);
58 currentSellerVersion++;
59 } catch (ReferenceNotFoundJsonException e) {
60
61
62 Map<String, String> details = e.getDetails();
63 fail(format("Entity %s with id %s is not found!", details.get("entityName"), details.get("identifier")));
64 }
65
66 testOrderPrecept(orderIdentifier, preceptIdentifier);
67 currentSellerVersion = sellerService.getThisVersion().getVersion();
68
69 preceptInfo.setIdentifier(format("%s-%s", ActivationServiceSample.MOBILKASSAN_CASH_REGISTER_NAME, "1-02"));
70 orderIdentifier = postOrder(currentSellerVersion++);
71 preceptInfo.setOrder(orderIdentifier);
72 String identifier = preceptService.create(selectedSeller.getCountry(), selectedSeller.getCorporateId(), preceptInfo);
73 currentSellerVersion++;
74
75 preceptInfo.setIdentifier(format("%s-%s", ActivationServiceSample.MOBILKASSAN_CASH_REGISTER_NAME, "1-03"));
76 orderIdentifier = postOrder(currentSellerVersion++);
77 preceptInfo.setOrder(orderIdentifier);
78 preceptService.create(selectedSeller.getCountry(), selectedSeller.getCorporateId(), preceptInfo);
79 currentSellerVersion++;
80
81
82 preceptInfo = preceptService.get(identifier);
83 assertEquals("Make sure server returns right amount of precept items", 2, preceptInfo.getPreceptItems().size());
84
85
86 assertEquals("Check current precept status", PreceptStatus.NEW, preceptInfo.getStatus());
87 preceptInfo.setStatus(PreceptStatus.PRINTED);
88 preceptService.put(identifier, preceptInfo);
89 preceptInfo = preceptService.get(identifier);
90 assertEquals("Make sure that precept status has changed", PreceptStatus.PRINTED, preceptInfo.getStatus());
91
92
93 List<PreceptInfo> preceptInfoList = preceptService.list(null, null, "NEW");
94 assertEquals("Make sure server returns only precepts that comply with the filter", PreceptStatus.NEW, preceptInfoList.get(0).getStatus());
95
96
97 preceptInfoList = preceptService.listDiff(currentSellerVersion - 2, currentSellerVersion);
98 assertEquals("Make sure server returns list of precepts of specified version interval", 2, preceptInfoList.size());
99 }
100
101 private void testOrderPrecept(String orderIdentifier, String preceptIdentifier) {
102 PreceptInfo preceptInfo = preceptService.get(preceptIdentifier);
103
104 assertEquals(orderIdentifier, preceptInfo.getOrder());
105
106 OrderInfo orderInfo1 = orderService.get(orderIdentifier, null);
107
108 assertEquals(orderIdentifier, orderInfo1.getIdentifier());
109
110
111 String orderIdentifierNew = postOrder(sellerService.getThisVersion().getVersion());
112 assertNotEquals(orderIdentifier, orderIdentifierNew);
113 preceptInfo.setOrder(orderIdentifierNew);
114
115 preceptService.put(preceptIdentifier, preceptInfo);
116
117 preceptInfo = preceptService.get(preceptIdentifier);
118
119 assertEquals(orderIdentifier, preceptInfo.getOrder());
120 }
121
122 private String postOrder(int currentSellerVersion) {
123 List<OrderItemInfo> orderItems = new ArrayList<OrderItemInfo>();
124 OrderItemInfo orderItemInfo = new OrderItemInfo();
125 orderItemInfo.setDelta(100);
126 orderItemInfo.setProductIdentifier("Product1");
127 orderItemInfo.setProductCustomization("Product customization 1");
128 orderItems.add(orderItemInfo);
129 orderItemInfo = new OrderItemInfo();
130 orderItemInfo.setDelta(200);
131 orderItemInfo.setProductIdentifier("Product2");
132 orderItems.add(orderItemInfo);
133
134 OrderInfo orderInfo = new OrderInfo();
135 orderInfo.setTreasurerName("Advandare");
136 orderInfo.setOrderItems(orderItems);
137 return orderService.create(currentSellerVersion, orderInfo).getIdentifier();
138 }
139
140 private PreceptItemInfo createPreceptItem(final String productIdentifier, final int delta) {
141 return createPreceptItem(productIdentifier, null, delta);
142 }
143
144 private PreceptItemInfo createPreceptItem(final String productIdentifier, final String customization, final int delta) {
145 PreceptItemInfo preceptItemInfo = new PreceptItemInfo();
146 preceptItemInfo.setDelta(delta);
147 preceptItemInfo.setProductIdentifier(productIdentifier);
148 preceptItemInfo.setProductCustomization(customization);
149 return preceptItemInfo;
150 }
151 }