1 package com.srv4pos.server.api.seller;
2
3 import com.srv4pos.commons.io.IOUtils;
4 import com.srv4pos.server.api.ServiceFactory;
5 import com.srv4pos.server.api.auth.AuthService;
6 import com.srv4pos.server.api.category.CategoryInfo;
7 import com.srv4pos.server.api.commodity.CommodityInfo;
8 import com.srv4pos.server.api.exceptions.ForbiddenJsonException;
9 import com.srv4pos.server.api.exceptions.NotFoundJsonException;
10 import com.srv4pos.server.api.exceptions.VersionConflictJsonException;
11 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
12 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
13 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
14 import com.srv4pos.server.api.infrastructure.http.Transport;
15 import com.srv4pos.server.api.infrastructure.picture.CategoryPictureInfo;
16 import com.srv4pos.server.api.infrastructure.picture.ProductPictureInfo;
17 import com.srv4pos.server.api.infrastructure.picture.SellerPictureInfo;
18 import com.srv4pos.server.api.keyboard.KeyboardInfo;
19 import com.srv4pos.server.api.keyboardentry.KeyboardEntryInfo;
20 import com.srv4pos.server.api.order.OrderInfo;
21 import com.srv4pos.server.api.product.ProductInfo;
22 import com.srv4pos.server.api.sale.SaleSlotInfo;
23 import com.srv4pos.server.api.tax.TaxInfo;
24 import com.srv4pos.server.api.treasurer.TreasurerInfo;
25 import org.junit.Assert;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36 import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertExceptionThrown;
37 import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertPicture;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertTrue;
41 import static org.junit.Assert.fail;
42
43
44
45
46 public class SellerServiceSample {
47 private final SellerService sellerService;
48 private final AuthService authService;
49 private final Credentials credentials;
50
51 private static final String MAIN_PICTURE_FILENAME = "MAIN.PNG";
52
53 public SellerServiceSample(URL url, Credentials credentials, Transport transport) {
54 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
55 authService = serviceFactory.getAuthService();
56 sellerService = serviceFactory.getSellerService();
57
58 this.credentials = credentials;
59 }
60
61 public void test() {
62
63
64
65 try {
66 authService.handshake();
67 } catch (ForbiddenJsonException e) {
68 assertEquals(ForbiddenErrorType.SELLER_NOT_FOUND, e.getCode());
69 Assert.fail("Seller not found");
70 }
71
72
73
74 final SellerInfo sellerInfo = sellerService.get(null);
75 final int version = sellerService.getThisVersion().getVersion();
76
77
78 assertEquals(0, sellerInfo.getWorkHoursWeek().size());
79 putWorkHoursMapInSeller(sellerInfo);
80
81
82 try {
83 sellerService.put(version, sellerInfo);
84 } catch (VersionConflictJsonException e) {
85 Assert.fail("There is no parallel actors for this test, this couldn't happen");
86 }
87
88
89 final SellerInfo updatedSellerInfo = sellerService.get(version);
90 final int updatedSellerVersion = sellerService.getThisVersion().getVersion();
91 assertNotNull("Make sure server respond after test update", updatedSellerInfo);
92 assertEquals("Make sure version changed as expected", version + 1, updatedSellerVersion);
93
94 checkWorkHoursMapInSeller(updatedSellerInfo);
95
96
97 SellerInfo anotherUpdatedSellerInfo = sellerService.get(updatedSellerVersion);
98
99
100 Assert.assertNull("Make sure server can determine the seller data was not changed", anotherUpdatedSellerInfo);
101
102
103 final SellerInfo publicSellerInfo = sellerService.get(version);
104 publicSellerInfo.setPublicAccess(true);
105 sellerService.put(updatedSellerVersion, publicSellerInfo);
106
107 checkRemoveWorkHours(updatedSellerInfo);
108 }
109
110 private void putWorkHoursMapInSeller(SellerInfo sellerInfo) {
111 final Map<DayOfWeek, WorkHoursInfo> workHoursInfoMap = new HashMap<DayOfWeek, WorkHoursInfo>();
112
113 WorkHoursInfo workHoursInfo = new WorkHoursInfo();
114 workHoursInfo.setDayOfWeek(DayOfWeek.TUESDAY);
115 workHoursInfo.setOpenTime(9 * 60);
116 workHoursInfo.setCloseTime(18 * 60);
117 workHoursInfoMap.put(DayOfWeek.TUESDAY, workHoursInfo);
118
119 workHoursInfo = new WorkHoursInfo();
120 workHoursInfo.setDayOfWeek(DayOfWeek.FRIDAY);
121 workHoursInfo.setOpenTime(10 * 60);
122 workHoursInfo.setCloseTime(20 * 60);
123 workHoursInfoMap.put(DayOfWeek.FRIDAY, workHoursInfo);
124
125 sellerInfo.setWorkHoursWeek(workHoursInfoMap);
126 }
127
128 private void checkWorkHoursMapInSeller(SellerInfo sellerInfo) {
129 final Map<DayOfWeek, WorkHoursInfo> workHoursInfoMap = sellerInfo.getWorkHoursWeek();
130 assertEquals(2, workHoursInfoMap.size());
131
132 WorkHoursInfo workHours = workHoursInfoMap.get(DayOfWeek.TUESDAY);
133 assertEquals(DayOfWeek.TUESDAY, workHours.getDayOfWeek());
134 assertEquals(9 * 60, workHours.getOpenTime());
135 assertEquals(18 * 60, workHours.getCloseTime());
136
137 workHours = workHoursInfoMap.get(DayOfWeek.FRIDAY);
138 assertEquals(DayOfWeek.FRIDAY, workHours.getDayOfWeek());
139 assertEquals(10 * 60, workHours.getOpenTime());
140 assertEquals(20 * 60, workHours.getCloseTime());
141 }
142
143 private void checkRemoveWorkHours(SellerInfo sellerInfo) {
144 sellerInfo.getWorkHoursWeek().remove(DayOfWeek.TUESDAY);
145 assertEquals(1, sellerInfo.getWorkHoursWeek().size());
146 sellerService.put(sellerService.getThisVersion().getVersion(), sellerInfo);
147
148 SellerInfo updatedSellerInfo = sellerService.get(null);
149 assertEquals(1, updatedSellerInfo.getWorkHoursWeek().size());
150 assertNotNull(updatedSellerInfo.getWorkHoursWeek().get(DayOfWeek.FRIDAY));
151 }
152
153 public void testPictures() throws IOException {
154
155 Integer currentSellerVersion = sellerService.getThisVersion().getVersion();
156
157
158
159 URL testPng = super.getClass().getResource("test.png");
160
161 InputStream pictureStream = testPng.openStream();
162
163 try {
164 sellerService.putPicture(currentSellerVersion, MAIN_PICTURE_FILENAME, pictureStream, HttpHelper.CONTENT_TYPE_PNG);
165 currentSellerVersion++;
166 } finally {
167 IOUtils.closeQuietly(pictureStream);
168 }
169
170 int uploadedPictureVersion = currentSellerVersion;
171
172 InputStream pictureFromServer = sellerService.getPicture(null, MAIN_PICTURE_FILENAME);
173 assertPicture("Make sure uploaded picture matches to expected picture",
174 testPng.openStream(),
175 pictureFromServer);
176
177 List<String> paths = sellerService.getPicturePath(null);
178 assertEquals(paths.size(), 1);
179
180 List<SellerPictureInfo> pictures = sellerService.listDiffPicture(currentSellerVersion, currentSellerVersion);
181
182 assertEquals("Check amount of changed pictures", 1, pictures.size());
183
184 SellerPictureInfo picture = pictures.get(0);
185
186 assertEquals("Make sure can recognize not-deleted pictures", picture.isDeleted(), false);
187
188 InputStream pictureFromHistory = sellerService.getPictureFromHistory(picture.getVersion(), picture.getFilename());
189 assertPicture("Verify server stores history for the pictures",
190 testPng.openStream(),
191 pictureFromHistory);
192
193 sellerService.deletePicture(uploadedPictureVersion, MAIN_PICTURE_FILENAME);
194 currentSellerVersion++;
195
196 pictures = sellerService.listDiffPicture(uploadedPictureVersion, currentSellerVersion);
197 assertEquals("Make sure can recognize deleted pictures", 2, pictures.size());
198
199 SellerPictureInfo deletedPicture = pictures.get(0);
200
201 assertEquals("Verify 'deleted' flag for deleted picture", deletedPicture.isDeleted(), true);
202 assertEquals("Verify version of deleted picture is correct", deletedPicture.getVersion(), currentSellerVersion);
203 assertEquals("Verify filename matches to expected ", deletedPicture.getFilename(), MAIN_PICTURE_FILENAME);
204 assertEquals("Verify picture belongs to correct seller", deletedPicture.getEntityIdentifier(), credentials.getSellerId());
205
206
207 assertEquals("Make sure server detects when picture was not modified after deletion",
208 null,
209 sellerService.getPicture(currentSellerVersion, MAIN_PICTURE_FILENAME));
210
211 try {
212 sellerService.getPicture(currentSellerVersion - 1, MAIN_PICTURE_FILENAME);
213 fail("Server must respond 404 because picture was deleted!");
214 } catch (NotFoundJsonException e) {
215
216 assertTrue(true);
217 }
218 }
219
220 public void removeSeller(final String sellerId) throws Exception {
221 sellerService.remove(sellerId);
222
223 assertExceptionThrown("Seller has not been removed from database!", ForbiddenJsonException.class, new Runnable() {
224 public void run() {
225 sellerService.get(null);
226 }
227 });
228 }
229
230 public void testSecurityEnabled() throws Exception {
231 assertExceptionThrown("The security should restrict the access to the seller's stats by ROLE_ACTIVATION.", ForbiddenJsonException.class,
232 new Runnable() {
233 public void run() {
234 sellerService.getStats();
235 }
236 });
237 }
238
239 public void testSellerModifications() {
240 Class[] classes = {CategoryInfo.class,
241 CommodityInfo.class,
242 KeyboardInfo.class,
243 KeyboardEntryInfo.class,
244 ProductInfo.class,
245 SellerInfo.class,
246 ProductPictureInfo.class,
247 CategoryPictureInfo.class,
248 SellerPictureInfo.class,
249 SaleSlotInfo.class,
250 TaxInfo.class,
251 TreasurerInfo.class,
252 OrderInfo.class};
253 Set<Class> modificationsClasses = new HashSet<Class>();
254 List<SellerModificationInfo> modifications = sellerService.getModifications(0, 1000, false);
255 for (SellerModificationInfo modificationInfo: modifications) {
256 modificationsClasses.add(modificationInfo.getEntityType());
257 }
258
259 for (Class clazz: classes) {
260 Assert.assertTrue(String.format("Seller modifications don't contain entity %s", clazz.getSimpleName()),
261 modificationsClasses.contains(clazz));
262 }
263
264 for (Class clazz: classes) {
265 int firstVersion = Integer.MAX_VALUE;
266 int lastVersion = -1;
267 for (SellerModificationInfo modificationInfo: modifications) {
268 if (modificationInfo.getEntityType() == clazz) {
269 firstVersion = Math.min(firstVersion, modificationInfo.getVersion());
270 lastVersion = Math.max(lastVersion, modificationInfo.getVersion());
271 }
272 }
273 boolean found = false;
274 for (SellerModificationInfo modificationInfo: sellerService.getModifications(0, firstVersion, false)) {
275 if (modificationInfo.getEntityType() == clazz) {
276 found = true;
277 }
278 }
279 Assert.assertTrue(String.format("Entity %s must be in seller modification list of range [%d, %d]",
280 clazz.getSimpleName(), 0, firstVersion), found);
281
282 found = false;
283 for (SellerModificationInfo modificationInfo: sellerService.getModifications(0, firstVersion - 1, false)) {
284 if (modificationInfo.getVersion() == firstVersion && modificationInfo.getEntityType() == clazz) {
285 found = true;
286 }
287 }
288 Assert.assertFalse(String.format("Entity %s must not be in seller modification list of range [%d, %d]",
289 clazz.getSimpleName(), 0, firstVersion - 1), found);
290
291 found = false;
292 for (SellerModificationInfo modificationInfo: sellerService.getModifications(lastVersion, 1000, false)) {
293 if (modificationInfo.getEntityType() == clazz) {
294 found = true;
295 }
296 }
297 Assert.assertTrue(String.format("Entity %s must be in seller modification list of range [%d, %d]",
298 clazz.getSimpleName(), lastVersion, 1000), found);
299
300 found = false;
301 for (SellerModificationInfo modificationInfo: sellerService.getModifications(lastVersion + 1, 1000, false)) {
302 if (modificationInfo.getEntityType() == clazz) {
303 found = true;
304 }
305 }
306 Assert.assertFalse(String.format("Entity %s must not be in seller modification list of range [%d, %d]",
307 clazz.getSimpleName(), lastVersion + 1, 1000), found);
308
309 }
310 }
311 }