1 package com.srv4pos.server.api.activation;
2
3 import com.srv4pos.server.api.ServiceFactory;
4 import com.srv4pos.server.api.infrastructure.credentials.ActivationCredentials;
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 org.junit.Test;
9
10 import java.io.IOException;
11 import java.net.URL;
12 import java.nio.charset.Charset;
13 import java.security.InvalidKeyException;
14 import java.security.NoSuchAlgorithmException;
15 import java.security.SignatureException;
16 import java.security.spec.InvalidKeySpecException;
17 import java.util.Calendar;
18 import java.util.Collections;
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.TimeZone;
24
25
26
27
28 public class ActivationServiceSample {
29 public static final String DEVICE_ID = "111AAA222BBB3-022";
30 public static final String CASH_REGISTER_NAME = "Cash22";
31 public static final String THIN_KASSAN_CASH_REGISTER_NAME = "CashTK";
32 public static final String MOBILKASSAN_CASH_REGISTER_NAME = "CashMobilkassan";
33 public static final String CONTROL_UNIT_SERIAL_NUMBER = "PTEST900000000001";
34 public static final String CONTROL_UNIT_DEVICE_SERIAL_NUMBER = "PTEST000000001001";
35 private static final String CONSTANT_COM_POS4MOBILE = "com.pos4mobile";
36 private static final String CONSTANT_COM_SWESS = "se.swess.pos";
37 private static final String CONSTANT_SE_MOBILKASSAN = "se.mobilkassan";
38 private static final String CONSTANT_THINKASSAN = "com.srv4pos.thinkassan";
39
40 public static final GeolocationInfo GEOLOCATION_INFO = new GeolocationInfo();
41 public static final String COMPANY_NAME = "MyExampleCompanyName";
42 public static final String COMMENT = "Please notify me by phone when activation is done. Thanks";
43
44 static {
45 GEOLOCATION_INFO.setAddress("Street, 5");
46 GEOLOCATION_INFO.setCity("Town");
47 GEOLOCATION_INFO.setPostalCode("100100");
48 GEOLOCATION_INFO.setLatitude(4.084659);
49 GEOLOCATION_INFO.setLongitude(46.610195);
50 }
51
52 private final SignatureHelper signatureHelper;
53 private TimeZone timezone;
54
55 private final ActivationService activationService;
56 private final Credentials credentials;
57
58 public ActivationServiceSample(URL url, Credentials credentials, Transport transport) {
59 this.credentials = credentials;
60 ServiceFactory serviceFactory = new ServiceFactory(url, credentials, transport);
61 this.activationService = serviceFactory.getActivationService();
62 signatureHelper = serviceFactory.getSignatureHelper();
63 this.timezone = TimeZone.getTimeZone(serviceFactory.getServerService().getTimezone());
64 }
65
66 public static final String SIGNATURE_ENCODING = "UTF-8";
67 public static final Charset SIGNATURE_CHARSET = Charset.forName(SIGNATURE_ENCODING);
68
69 @Test
70 public ActivationSecurityInfo test() throws InvalidKeySpecException, IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
71
72
73 testPos4mobile();
74
75 testThinKassan();
76
77 return testMobilkassan();
78 }
79
80 private ActivationSecurityInfo testMobilkassan() {
81 Set<Feature> features = new HashSet<Feature>();
82 features.add(Feature.AIRPAY);
83 features.add(Feature.SEQR);
84 features.add(Feature.CONTROL_UNIT);
85
86 final ActivationSecurityInfo activationSecurityInfo =
87 activationService.claim(HttpHelper.getSellerCountry(credentials.getSellerId()), HttpHelper.getSellerCorporateId(credentials.getSellerId()),
88 MOBILKASSAN_CASH_REGISTER_NAME, CONSTANT_SE_MOBILKASSAN, features, getDateValidFrom(timezone), getDateValidTo(timezone),
89 newContactInfo(), COMMENT,
90 CONTROL_UNIT_SERIAL_NUMBER, ControlUnitLocation.REMOTE, GEOLOCATION_INFO, DEVICE_ID, newBuildInfo(), null);
91
92 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo);
93 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getActivationId());
94 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getApiKey());
95 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getProductionNumber());
96
97 return activationSecurityInfo;
98 }
99
100 private ActivationSecurityInfo testThinKassan() {
101 Set<Feature> features = new HashSet<Feature>();
102 features.add(Feature.AIRPAY);
103 features.add(Feature.SEQR);
104
105 final ActivationSecurityInfo activationSecurityInfo =
106 activationService.claim(HttpHelper.getSellerCountry(credentials.getSellerId()), HttpHelper.getSellerCorporateId(credentials.getSellerId()),
107 THIN_KASSAN_CASH_REGISTER_NAME, CONSTANT_THINKASSAN, features, getDateValidFrom(timezone), getDateValidTo(timezone),
108 newContactInfo(), COMMENT,
109 CONTROL_UNIT_SERIAL_NUMBER, ControlUnitLocation.REMOTE, GEOLOCATION_INFO, DEVICE_ID, newBuildInfo(), null);
110
111 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo);
112 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getActivationId());
113 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getApiKey());
114 org.junit.Assert.assertNotNull("Make sure mobilkassan has been ordered", activationSecurityInfo.getProductionNumber());
115
116 return activationSecurityInfo;
117 }
118
119 private void testPos4mobile() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, IOException {
120 final String sellerCountry = HttpHelper.getSellerCountry(credentials.getSellerId());
121 final String sellerCorporateId = HttpHelper.getSellerCorporateId(credentials.getSellerId());
122
123 final ActivationSecurityInfo activationSecurityInfo =
124 activationService.claim(HttpHelper.getSellerCountry(credentials.getSellerId()), HttpHelper.getSellerCorporateId(credentials.getSellerId()),
125 CASH_REGISTER_NAME, CONSTANT_COM_POS4MOBILE, Collections.<Feature>emptySet(), getDateValidFrom(timezone),
126 getDateValidTo(timezone), newContactInfo(), COMMENT,
127 null, ControlUnitLocation.REMOTE, GEOLOCATION_INFO, DEVICE_ID, newBuildInfo(), null);
128
129 org.junit.Assert.assertNotNull("Verify registering production number", activationSecurityInfo.getProductionNumber());
130
131 final ActivationCodeInfo activationCodeInfo = activationService.getActivationCode(new ActivationCredentials(sellerCountry, sellerCorporateId,
132 activationSecurityInfo.getApiKey()));
133 org.junit.Assert.assertEquals("Make sure version of ActivationCode is correct", 2, activationCodeInfo.getActivationInfoVersion());
134 org.junit.Assert.assertTrue("Verify digital signature of ActivationCode", signatureHelper.verifySignature(activationCodeInfo));
135
136 final String activationDescriptorInfoJson = activationCodeInfo.getActivationDescriptorInfo();
137 final ActivationDescriptorInfo activationDescriptorInfo = ActivationDescriptorInfo.fromJsonToActivationDescriptorInfo(activationDescriptorInfoJson);
138 org.junit.Assert.assertEquals("Verify application package for registered application", CONSTANT_COM_POS4MOBILE,
139 activationDescriptorInfo.getApplicationPackage());
140 org.junit.Assert.assertEquals("Verify correctness of device identifier", DEVICE_ID, activationDescriptorInfo.getDeviceId());
141 org.junit.Assert.assertEquals("Make sure seller country was registered correctly", sellerCountry, activationDescriptorInfo.getCountry());
142 org.junit.Assert.assertEquals("Make sure corporate was registered correctly", sellerCorporateId, activationDescriptorInfo.getCorporateId());
143 org.junit.Assert.assertEquals("Make sure cash register name was registered correctly", CASH_REGISTER_NAME,
144 activationDescriptorInfo.getCashRegisterName());
145
146 final ActivationDetailsInfo details = activationDescriptorInfo.getActivationDetailsInfo();
147 org.junit.Assert.assertEquals("Verify licensing start date", getDateValidFrom(timezone), details.getValidFrom());
148 org.junit.Assert.assertEquals("Verify licensing end date", getDateValidTo(timezone), details.getValidTo());
149 org.junit.Assert.assertEquals("Make sure we registered requested set of features", Collections.<Feature>emptySet(), details.getFeatures());
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public static ContactInfo newContactInfo() {
189 final ContactInfo contactInfo = new ContactInfo();
190 contactInfo.setName("John Doe");
191 contactInfo.setEmail("john@example.org");
192 contactInfo.setPhone("+555 55 555 555");
193 return contactInfo;
194 }
195
196 public static HashMap<String, String> newBuildInfo() {
197 final HashMap<String, String> buildInfo = new HashMap<String, String>();
198 buildInfo.put("KEY1", "VALUE1");
199 buildInfo.put("KEY2", "VALUE2");
200 return buildInfo;
201 }
202
203 public static Date create(int year, int month, int day, TimeZone timeZone) {
204 final Calendar instance = Calendar.getInstance(timeZone);
205 instance.set(year, month, day, 0, 0, 0);
206 instance.set(Calendar.MILLISECOND, 0);
207 return instance.getTime();
208 }
209
210 public static Date getDateValidFrom(TimeZone timeZone) {
211 Calendar calendar = Calendar.getInstance(timeZone);
212 return create(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), timeZone);
213 }
214
215 public static Date getDateValidTo(TimeZone timeZone) {
216 Calendar calendar = Calendar.getInstance(timeZone);
217 calendar.add(Calendar.YEAR, 1);
218 return create(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), timeZone);
219 }
220 }