1 package com.srv4pos.server.api.commodity;
2
3 import com.srv4pos.server.api.ServiceFactory;
4 import com.srv4pos.server.api.category.CategoryInfo;
5 import com.srv4pos.server.api.category.CategoryService;
6 import com.srv4pos.server.api.exceptions.NotFoundJsonException;
7 import com.srv4pos.server.api.infrastructure.IdentifierInfo;
8 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
9 import com.srv4pos.server.api.infrastructure.http.Transport;
10 import com.srv4pos.server.api.seller.SellerService;
11
12 import java.net.URL;
13 import java.util.List;
14
15 import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertExceptionThrown;
16 import static org.junit.Assert.assertEquals;
17
18
19
20
21 public class CommodityServiceSample {
22 private static final String PRODUCT_NAME_1 = "PRODUCT1";
23 private static final String PRODUCT_NAME_2 = "PRODUCT2";
24 private final CategoryService categoryService;
25 private final CommodityService commodityService;
26 private final SellerService sellerService;
27
28 public CommodityServiceSample(URL url, Credentials credentials, Transport transport) {
29 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
30 categoryService = serviceFactory.getCategoryService();
31 commodityService = serviceFactory.getCommodityService();
32 sellerService = serviceFactory.getSellerService();
33 }
34
35 public void test() throws Exception {
36
37 int sellerVersion = sellerService.getThisVersion().getVersion();
38
39
40
41 final List<CategoryInfo> categories = categoryService.list(null);
42 org.junit.Assert.assertEquals("Make sure server returns right amount of categories", 1, categories.size());
43 final String categoryIdentifier = categories.get(0).getIdentifier();
44
45
46 CommodityInfo firstCommodity = createAndSaveCommodity(sellerVersion, PRODUCT_NAME_1, categoryIdentifier);
47 sellerVersion++;
48
49 CommodityInfo secondCommodity = createAndSaveCommodity(sellerVersion, PRODUCT_NAME_2, categoryIdentifier);
50 sellerVersion++;
51
52
53
54 List<CommodityInfo> commodities = commodityService.list(null);
55
56 assertEquals("Make sure server returns right amount of commodities ", 2, commodities.size());
57
58 assertEquals("Category mismatch for first commodity. Make sure server return right data", categoryIdentifier, commodities.get(0).getCategory());
59 assertEquals("Category mismatch for second commodity. Make sure server return right data", categoryIdentifier, commodities.get(1).getCategory());
60
61 assertEquals("Product mismatch for first commodity. Make sure server return right data", PRODUCT_NAME_1, commodities.get(0).getProduct());
62 assertEquals("Product mismatch for second commodity. Make sure server return right data", PRODUCT_NAME_2, commodities.get(1).getProduct());
63
64
65 final CommodityInfo third = createAndSaveCommodity(sellerVersion, PRODUCT_NAME_2, categoryIdentifier);
66 sellerVersion++;
67
68 commodities = commodityService.list(null);
69 assertEquals("Make sure server returns right amount of commodities ", 3, commodities.size());
70
71
72 commodityService.delete(sellerVersion, third.getIdentifier());
73
74
75 assertExceptionThrown("Commodity must be deleted", NotFoundJsonException.class, new Runnable() {
76 public void run() {
77 commodityService.get(null, third.getIdentifier());
78 }
79 });
80
81
82 commodities = commodityService.list(null);
83 assertEquals("Make sure server returns right amount of commodities", 2, commodities.size());
84 }
85
86 private CommodityInfo createAndSaveCommodity(final int sellerVersion, final String product, final String category) {
87 CommodityInfo commodityInfo = new CommodityInfo();
88 commodityInfo.setProduct(product);
89 commodityInfo.setCategory(category);
90
91 IdentifierInfo identifierInfo = commodityService.create(sellerVersion, commodityInfo);
92 commodityInfo.setIdentifier(identifierInfo.getIdentifier());
93
94 return commodityInfo;
95 }
96 }