View Javadoc
1   package com.srv4pos.server.api.keyboardentry;
2   
3   import com.srv4pos.server.api.ServiceFactory;
4   import com.srv4pos.server.api.infrastructure.IdentifierInfo;
5   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6   import com.srv4pos.server.api.infrastructure.http.Transport;
7   import com.srv4pos.server.api.keyboard.KeyboardInfo;
8   import com.srv4pos.server.api.keyboard.KeyboardService;
9   import com.srv4pos.server.api.seller.SellerService;
10  
11  import java.io.IOException;
12  import java.net.URL;
13  import java.util.List;
14  
15  import static org.junit.Assert.assertEquals;
16  
17  /**
18   * <p>User: Sergey, Date: 29.03.14 20:25</p>.
19   */
20  public class KeyboardEntryServiceSample {
21  
22      private static final String PRODUCT_NAME_1 = "PRODUCT1";
23      private static final String PRODUCT_NAME_2 = "PRODUCT2";
24      private static final String THIRD_KEYBOARD_ENTRY_IDENTIFIER = "TESTKEYBOARDENTRY3";
25      private final KeyboardEntryService keyboardEntryService;
26      private final KeyboardService keyboardService;
27      private final SellerService sellerService;
28  
29      /**
30       * Constructs the service.
31       *
32       * @param url of the server
33       * @param credentials to work with the server
34       * @param transport to know the method to reach the server
35       */
36      public KeyboardEntryServiceSample(URL url, Credentials credentials, Transport transport) {
37          ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
38          keyboardEntryService = serviceFactory.getKeyboardEntryService();
39          keyboardService = serviceFactory.getKeyboardService();
40          sellerService = serviceFactory.getSellerService();
41      }
42  
43      /**
44       * The example.
45       * @throws IOException when something is wrong.
46       */
47      public void test() throws IOException {
48          int initialSellerVersion = sellerService.getThisVersion().getVersion();
49          int currentSellerVersion = initialSellerVersion;
50  
51          //To create keyboardEntry we need a keyboard. At least one keyboard must exists.
52          //So retrieve all keyboards from server and get first one
53          final List<KeyboardInfo> keyboards = keyboardService.list(null);
54          assertEquals("Make sure server returns right amount of keyboards", 1, keyboards.size());
55          final String keyboardIdentifier = keyboards.get(0).getIdentifier();
56  
57  
58          KeyboardEntryInfo first = createAndSaveKeyboardEntry(currentSellerVersion, PRODUCT_NAME_1, keyboardIdentifier);
59          currentSellerVersion++;
60  
61          KeyboardEntryInfo second = createAndSaveKeyboardEntry(currentSellerVersion, PRODUCT_NAME_2, keyboardIdentifier);
62          currentSellerVersion++;
63  
64          //Get list of entries by specified seller's version
65          List<KeyboardEntryInfo> keyboardEntries = keyboardEntryService.list(initialSellerVersion);
66          assertEquals("Make sure server returns right amount of keyboard entries ", 2, keyboardEntries.size());
67          assertEquals(PRODUCT_NAME_1, keyboardEntries.get(0).getProduct());
68          assertEquals(PRODUCT_NAME_2, keyboardEntries.get(1).getProduct());
69  
70  
71          KeyboardEntryInfo third = new KeyboardEntryInfo();
72          third.setProduct(PRODUCT_NAME_2);
73          third.setKeyboard(keyboardIdentifier);
74          third.setKeyAction(KeyAction.Product);
75          third.setLabel(PRODUCT_NAME_2);
76          third.setIdentifier(THIRD_KEYBOARD_ENTRY_IDENTIFIER);
77  
78          //Put entry to the server
79          keyboardEntryService.put(currentSellerVersion, third);
80          currentSellerVersion++;
81  
82          KeyboardEntryInfo thirdEntryFromServer = keyboardEntryService.get(null, third.getIdentifier());
83          assertEquals(PRODUCT_NAME_2, thirdEntryFromServer.getProduct());
84          assertEquals(keyboardIdentifier, thirdEntryFromServer.getKeyboard());
85  
86          keyboardEntries = keyboardEntryService.list(null);
87          assertEquals("Make sure server returns right amount of keyboard entries", 3, keyboardEntries.size());
88  
89          keyboardEntryService.delete(currentSellerVersion, third.getIdentifier());
90  
91  
92          keyboardEntries = keyboardEntryService.list(null);
93          assertEquals("Make sure third keyboard entry is deleted", 2, keyboardEntries.size());
94      }
95  
96      private KeyboardEntryInfo createAndSaveKeyboardEntry(final int sellerVersion, final String product, final String keyboard) {
97          KeyboardEntryInfo keyboardEntryInfo = new KeyboardEntryInfo();
98          keyboardEntryInfo.setProduct(product);
99          keyboardEntryInfo.setKeyboard(keyboard);
100         keyboardEntryInfo.setKeyAction(KeyAction.Product);
101         keyboardEntryInfo.setLabel(product);
102 
103         IdentifierInfo identifierInfo = keyboardEntryService.create(sellerVersion, keyboardEntryInfo);
104         keyboardEntryInfo.setIdentifier(identifierInfo.getIdentifier());
105         return keyboardEntryInfo;
106     }
107 }