View Javadoc
1   package com.srv4pos.server.api.category;
2   
3   import com.srv4pos.server.api.ServiceFactory;
4   import com.srv4pos.server.api.exceptions.NotFoundJsonException;
5   import com.srv4pos.server.api.infrastructure.IdentifierInfo;
6   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
7   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
8   import com.srv4pos.server.api.infrastructure.http.Transport;
9   import com.srv4pos.server.api.infrastructure.picture.CategoryPictureInfo;
10  import com.srv4pos.server.api.seller.SellerService;
11  import com.srv4pos.commons.io.IOUtils;
12  
13  import java.io.InputStream;
14  import java.net.URL;
15  import java.util.List;
16  
17  import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertExceptionThrown;
18  import static com.srv4pos.server.api.infrastructure.DiagnosticsUtil.assertPicture;
19  import static org.junit.Assert.assertEquals;
20  
21  /**
22   * <p>User: Kirill, Date: 11.09.13 16:18</p>.
23   */
24  public class CategoryServiceSample {
25      private static final String MAIN_PICTURE_FILENAME = "MAIN.PNG";
26  
27      private static final String FIRST_CATEGORY_NAME = "Test";
28      private static final String SECOND_CATEGORY_NAME = "Test 2";
29  
30      private final SellerService sellerService;
31      private final CategoryService categoryService;
32  
33      public CategoryServiceSample(URL url, Credentials credentials, Transport transport) {
34          ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
35          categoryService = serviceFactory.getCategoryService();
36          sellerService = serviceFactory.getSellerService();
37      }
38  
39      public void test() throws Exception {
40  
41          // get current version of the seller from server
42          int currentSellerVersion = sellerService.getThisVersion().getVersion();
43  
44          // create test category
45          CategoryInfo firstCategory = createAndSaveCategory(currentSellerVersion, FIRST_CATEGORY_NAME);
46          currentSellerVersion++;
47  
48          final CategoryInfo secondCategory = createAndSaveCategory(currentSellerVersion, SECOND_CATEGORY_NAME);
49          currentSellerVersion++;
50  
51          //Retrieve all categories from the server
52          List<CategoryInfo> categories = categoryService.list(null);
53  
54          assertEquals("Make sure server returns right amount of categories ", 2, categories.size());
55          assertEquals("Name mismatch for first category. Make sure server return right data ", FIRST_CATEGORY_NAME, categories.get(0).getName());
56          assertEquals("Name mismatch for second category. Make sure server return right data", SECOND_CATEGORY_NAME, categories.get(1).getName());
57  
58          //Delete the second category from the server
59          categoryService.delete(currentSellerVersion, secondCategory.getIdentifier());
60  
61          //Assert exception. Category must be deleted, so when we try to retrieve if from server, we must get NotFoundJsonException
62          assertExceptionThrown("Category must be deleted", NotFoundJsonException.class, new Runnable() {
63              public void run() {
64                  categoryService.get(null, secondCategory.getIdentifier());
65              }
66          });
67  
68          //Retrieve all categories from the server
69          categories = categoryService.list(null);
70  
71          assertEquals("Make sure server returns right amount of categories", 1, categories.size());
72          assertEquals("Name mismatch for first category. Make sure server return right data", FIRST_CATEGORY_NAME, categories.get(0).getName());
73      }
74  
75      public void testPictures() throws Exception {
76  
77          Integer currentSellerVersion = sellerService.getThisVersion().getVersion();
78  
79          String categoryId = createAndSaveCategory(currentSellerVersion, "Category 1").getIdentifier();
80          currentSellerVersion++;
81  
82  
83          // pictures
84  
85          URL pictureUrl = super.getClass().getResource("test.png");
86          InputStream pictureStream = pictureUrl.openStream();
87  
88          try {
89              categoryService.putPicture(currentSellerVersion, categoryId, MAIN_PICTURE_FILENAME, pictureStream, HttpHelper.CONTENT_TYPE_PNG);
90              currentSellerVersion++;
91          } finally {
92              IOUtils.closeQuietly(pictureStream);
93          }
94  
95          int uploadedPictureVersion = currentSellerVersion;
96  
97          InputStream pictureFromServer = categoryService.getPicture(null, categoryId, MAIN_PICTURE_FILENAME);
98          assertPicture("Make sure uploaded picture matches to expected picture",
99                  pictureUrl.openStream(),
100                 pictureFromServer);
101 
102         List<String> paths = categoryService.getPicturePath(null, categoryId);
103         assertEquals(paths.size(), 1);
104 
105         List<CategoryPictureInfo> pictures = categoryService.listDiffPicture(currentSellerVersion, currentSellerVersion);
106         assertEquals("Check amount of changed pictures", 1, pictures.size());
107 
108         CategoryPictureInfo picture = pictures.get(0);
109 
110         assertEquals("Make sure can recognize not-deleted pictures", picture.isDeleted(), false);
111 
112         InputStream pictureFromHistory = categoryService.getPictureFromHistory(picture.getVersion(), categoryId, picture.getFilename());
113 
114         assertPicture("Verify server stores history for the pictures",
115                 pictureUrl.openStream(),
116                 pictureFromHistory);
117 
118         categoryService.deletePicture(uploadedPictureVersion, categoryId, MAIN_PICTURE_FILENAME);
119         currentSellerVersion++;
120 
121         pictures = categoryService.listDiffPicture(uploadedPictureVersion, currentSellerVersion);
122         assertEquals("Make sure can recognize deleted pictures", 2, pictures.size());
123 
124         final CategoryPictureInfo deletedPicture = pictures.get(0);
125 
126         assertEquals("Verify 'deleted' flag for deleted picture", deletedPicture.isDeleted(), true);
127         assertEquals("Verify version of deleted picture is correct", deletedPicture.getVersion(), currentSellerVersion);
128         assertEquals("Verify filename matches to expected ", deletedPicture.getFilename(), MAIN_PICTURE_FILENAME);
129         assertEquals("Verify picture belongs to correct category", deletedPicture.getEntityIdentifier(), categoryId);
130 
131         // null because it was not modified since uploadedPictureVersion
132         assertEquals("Make sure server detects when picture was not modified after deletion",
133                 null,
134                 categoryService.getPicture(currentSellerVersion, deletedPicture.getEntityIdentifier(), MAIN_PICTURE_FILENAME));
135 
136 
137         assertExceptionThrown("Server must respond 404 because picture was deleted!", NotFoundJsonException.class, new Runnable() {
138             public void run() {
139                 categoryService.getPicture(sellerService.getThisVersion().getVersion() - 1, deletedPicture.getEntityIdentifier(), MAIN_PICTURE_FILENAME);
140             }
141         });
142     }
143 
144 
145     private CategoryInfo createAndSaveCategory(final int sellerVersion, final String categoryName) {
146         CategoryInfo categoryInfo = new CategoryInfo();
147         categoryInfo.setName(categoryName);
148 
149         IdentifierInfo identifierInfo = categoryService.create(sellerVersion, categoryInfo);
150 
151         categoryInfo.setIdentifier(identifierInfo.getIdentifier());
152         return categoryInfo;
153     }
154 }