1 package com.srv4pos.server.api.product;
2
3 import com.srv4pos.server.api.ServiceFactory;
4 import com.srv4pos.server.api.exceptions.NotFoundJsonException;
5 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
7 import com.srv4pos.server.api.infrastructure.http.Transport;
8 import com.srv4pos.server.api.infrastructure.picture.ProductPictureInfo;
9 import com.srv4pos.server.api.seller.SellerService;
10 import com.srv4pos.server.api.tax.TaxInfo;
11 import com.srv4pos.server.api.tax.TaxService;
12 import com.srv4pos.commons.io.IOUtils;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URL;
17 import java.util.List;
18
19 import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertPicture;
20 import static org.junit.Assert.assertEquals;
21
22
23
24
25 public class ProductServiceSample {
26
27 private static final String TAX_INFO_IDENTIFIER = "Tax1";
28 private static final String MAIN_PICTURE_FILENAME = "MAIN.PNG";
29 private final TaxService taxService;
30 private final ProductService productService;
31 private final SellerService sellerService;
32
33 public ProductServiceSample(URL url, Credentials credentials, Transport transport) {
34 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
35 taxService = serviceFactory.getTaxService();
36 productService = serviceFactory.getProductService();
37 sellerService = serviceFactory.getSellerService();
38 }
39
40 public void testPictures() throws IOException {
41
42 Integer currentSellerVersion = sellerService.getThisVersion().getVersion();
43
44 TaxInfo taxInfo = new TaxInfo();
45 taxInfo.setIdentifier(TAX_INFO_IDENTIFIER);
46 taxInfo.setName("Tax 1");
47 taxInfo.setVat(25);
48
49 taxService.put(currentSellerVersion, taxInfo);
50
51 currentSellerVersion++;
52
53 final String product1 = "PRODUCT1";
54 productService.put(currentSellerVersion, createProduct(product1, "Product 1", TAX_INFO_IDENTIFIER));
55 currentSellerVersion++;
56
57 productService.put(currentSellerVersion, createProduct("PRODUCT2", "Product 2", TAX_INFO_IDENTIFIER));
58 currentSellerVersion++;
59
60
61 URL pictureUrl = super.getClass().getResource("test.png");
62 InputStream pictureStream = pictureUrl.openStream();
63 try {
64 productService.putPicture(currentSellerVersion, product1, MAIN_PICTURE_FILENAME, pictureStream, HttpHelper.CONTENT_TYPE_PNG);
65 currentSellerVersion++;
66 } finally {
67 IOUtils.closeQuietly(pictureStream);
68 }
69
70 int uploadedPictureVersion = currentSellerVersion;
71
72 InputStream actualPicture = productService.getPicture(null, product1, MAIN_PICTURE_FILENAME);
73
74 assertPicture("Make sure uploaded picture matches to expected picture",
75 pictureUrl.openStream(),
76 actualPicture);
77
78 List<String> paths = productService.getPicturePath(null, product1);
79 assertEquals(paths.size(), 1);
80
81 List<ProductPictureInfo> pictures = productService.listDiffPicture(currentSellerVersion, currentSellerVersion);
82 assertEquals("Check amount of changed pictures", 1, pictures.size());
83
84 ProductPictureInfo picture = pictures.get(0);
85
86 assertEquals("Make sure can recognize not-deleted pictures", picture.isDeleted(), false);
87
88 InputStream pictureFromHistory = productService.getPictureFromHistory(picture.getVersion(),
89 product1, picture.getFilename());
90
91 assertPicture("Verify server stores history for the pictures",
92 pictureUrl.openStream(),
93 pictureFromHistory);
94
95 productService.deletePicture(uploadedPictureVersion, product1, MAIN_PICTURE_FILENAME);
96 currentSellerVersion++;
97
98 pictures = productService.listDiffPicture(uploadedPictureVersion, currentSellerVersion);
99 assertEquals("Make sure can recognize deleted pictures", 2, pictures.size());
100
101 ProductPictureInfo deletedPicture = pictures.get(0);
102
103 assertEquals("Verify 'deleted' flag for deleted picture", deletedPicture.isDeleted(), true);
104 assertEquals("Verify version of deleted picture is correct", deletedPicture.getVersion(), currentSellerVersion);
105 assertEquals("Verify filename matches to expected ", deletedPicture.getFilename(), MAIN_PICTURE_FILENAME);
106 assertEquals("Verify picture belongs to correct product", deletedPicture.getEntityIdentifier(), product1);
107
108 try {
109 productService.getPicture(currentSellerVersion - 1, deletedPicture.getEntityIdentifier(), MAIN_PICTURE_FILENAME);
110 org.junit.Assert.assertTrue("Server must respond 404 because picture was deleted!", false);
111
112 } catch (NotFoundJsonException e) {
113
114 }
115
116 }
117
118 public static ProductInfo createProduct(String identifier, String name, String tax) {
119 ProductInfo product = new ProductInfo();
120 product.setIdentifier(identifier);
121 product.setName(name);
122 product.setNetto(10000);
123 product.setTax(tax);
124 product.setCountedInPrice(true);
125 product.setBarcode("012345678912");
126 return product;
127 }
128 }