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
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
31
32
33
34
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
45
46
47 public void test() throws IOException {
48 int initialSellerVersion = sellerService.getThisVersion().getVersion();
49 int currentSellerVersion = initialSellerVersion;
50
51
52
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
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
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 }