1 package com.srv4pos.server.api.treasurer;
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.seller.SellerService;
8
9 import java.net.URL;
10 import java.util.List;
11
12 import static org.junit.Assert.assertEquals;
13
14
15
16
17 public class TreasurerServiceSample {
18
19 private static final String TREASURER_NAME = "John Doe";
20 private static final String TREASURER_SSN = "987-65-4320";
21 private static final String SECOND_TREASURER_ID = "TREASURERNAME1";
22
23 private final TreasurerService treasurerService;
24 private final SellerService sellerService;
25
26 public TreasurerServiceSample(URL url, Credentials credentials, Transport transport) {
27 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
28 treasurerService = serviceFactory.getTreasurerService();
29 sellerService = serviceFactory.getSellerService();
30 }
31
32 private int getSellerVersion() {
33 return sellerService.getThisVersion().getVersion();
34 }
35
36 public void test() {
37 int currentSellerVersion = getSellerVersion();
38
39
40 TreasurerInfo treasurer = createAndSaveTreasurer(currentSellerVersion, TREASURER_NAME, TREASURER_SSN);
41 currentSellerVersion++;
42
43 TreasurerInfo secondTreasurer = new TreasurerInfo();
44 secondTreasurer.setName(TREASURER_NAME);
45 secondTreasurer.setSsn(TREASURER_SSN);
46 secondTreasurer.setIdentifier(SECOND_TREASURER_ID);
47 secondTreasurer.setPin("9999");
48
49
50 treasurerService.put(currentSellerVersion, secondTreasurer);
51 currentSellerVersion++;
52
53
54 TreasurerInfo secondTreasurerFromServer = treasurerService.get(null, SECOND_TREASURER_ID);
55 assertEquals("Identifier mismatch for second treasurer", SECOND_TREASURER_ID, secondTreasurerFromServer.getIdentifier());
56 assertEquals("9999", secondTreasurer.getPin());
57
58
59 List<TreasurerInfo> treasurers = treasurerService.list(null);
60 assertEquals("Make sure server returns right amount of treasurers", 2, treasurers.size());
61 assertEquals("Name mismatch for first treasurer. Make sure server return right data", TREASURER_NAME, treasurers.get(0).getName());
62
63
64 treasurerService.delete(getSellerVersion(), SECOND_TREASURER_ID);
65 currentSellerVersion++;
66
67 treasurers = treasurerService.list(null);
68 assertEquals("Make sure second treasurer is deleted", 1, treasurers.size());
69 }
70
71 private TreasurerInfo createAndSaveTreasurer(final int sellerVersion, final String name, final String ssn) {
72 TreasurerInfo treasurerInfo = new TreasurerInfo();
73 treasurerInfo.setName(name);
74 treasurerInfo.setSsn(ssn);
75
76 IdentifierInfo identifierInfo = treasurerService.create(sellerVersion, treasurerInfo);
77 treasurerInfo.setIdentifier(identifierInfo.getIdentifier());
78 return treasurerInfo;
79 }
80 }