|
| 1 | +package io.snabble.sdk; |
| 2 | + |
| 3 | +import android.support.test.annotation.UiThreadTest; |
| 4 | +import android.support.test.filters.LargeTest; |
| 5 | +import android.support.test.runner.AndroidJUnit4; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.ArrayList; |
| 14 | + |
| 15 | +import io.snabble.sdk.codes.ScannableCode; |
| 16 | +import io.snabble.sdk.encodedcodes.EncodedCodesGenerator; |
| 17 | +import io.snabble.sdk.encodedcodes.EncodedCodesOptions; |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4.class) |
| 20 | +@LargeTest |
| 21 | +public class EncodedCodesGeneratorTest extends SnabbleSdkTest { |
| 22 | + @Test |
| 23 | + @UiThreadTest |
| 24 | + public void testGeneration() { |
| 25 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 26 | + .prefix("+") |
| 27 | + .separator(";") |
| 28 | + .suffix("-") |
| 29 | + .maxChars(1000) |
| 30 | + .build(); |
| 31 | + |
| 32 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 33 | + generator.add("foo"); |
| 34 | + generator.add("bar"); |
| 35 | + generator.add("baz"); |
| 36 | + |
| 37 | + ArrayList<String> codes = generator.generate(); |
| 38 | + Assert.assertEquals(1, codes.size()); |
| 39 | + Assert.assertEquals("+foo;bar;baz-", codes.get(0)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @UiThreadTest |
| 44 | + public void testSplitAtMaxChars() { |
| 45 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 46 | + .prefix("+") |
| 47 | + .separator(";") |
| 48 | + .suffix("-") |
| 49 | + .maxChars(13) |
| 50 | + .maxCodes(9999) |
| 51 | + .build(); |
| 52 | + |
| 53 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 54 | + generator.add("foo"); |
| 55 | + generator.add("bar"); |
| 56 | + generator.add("baz"); |
| 57 | + generator.add("asdf"); |
| 58 | + |
| 59 | + ArrayList<String> codes = generator.generate(); |
| 60 | + Assert.assertEquals(2, codes.size()); |
| 61 | + Assert.assertEquals("+foo;bar;baz-", codes.get(0)); |
| 62 | + Assert.assertEquals("+asdf-", codes.get(1)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @UiThreadTest |
| 67 | + public void testSplitAtMaxCodes() { |
| 68 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 69 | + .prefix("+") |
| 70 | + .separator(";") |
| 71 | + .suffix("-") |
| 72 | + .maxCodes(3) |
| 73 | + .maxChars(2000) |
| 74 | + .build(); |
| 75 | + |
| 76 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 77 | + generator.add("foo"); |
| 78 | + generator.add("bar"); |
| 79 | + generator.add("baz"); |
| 80 | + generator.add("asdf"); |
| 81 | + |
| 82 | + ArrayList<String> codes = generator.generate(); |
| 83 | + Assert.assertEquals(2, codes.size()); |
| 84 | + Assert.assertEquals("+foo;bar;baz-", codes.get(0)); |
| 85 | + Assert.assertEquals("+asdf-", codes.get(1)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @UiThreadTest |
| 90 | + public void testSplitAgeRestrictedProducts() throws IOException, Snabble.SnabbleException { |
| 91 | + withDb("demoDb_1_11.sqlite3"); |
| 92 | + |
| 93 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 94 | + .prefix("+") |
| 95 | + .separator(";") |
| 96 | + .suffix("-") |
| 97 | + .nextCode("****") |
| 98 | + .nextCodeWithCheck("####") |
| 99 | + .finalCode("%%%%") |
| 100 | + .maxChars(1000) |
| 101 | + .build(); |
| 102 | + |
| 103 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 104 | + Product duplo = project.getProductDatabase().findBySku("49"); |
| 105 | + Product krombacherPils = project.getProductDatabase().findBySku("37"); |
| 106 | + |
| 107 | + project.getShoppingCart().add(duplo, 5, ScannableCode.parse(project, "4008400301020")); |
| 108 | + project.getShoppingCart().add(krombacherPils, 2, ScannableCode.parse(project, "4008287051124")); |
| 109 | + |
| 110 | + generator.add(project.getShoppingCart()); |
| 111 | + |
| 112 | + ArrayList<String> codes = generator.generate(); |
| 113 | + Assert.assertEquals(2, codes.size()); |
| 114 | + Assert.assertEquals("+4008400301020;4008400301020;4008400301020;4008400301020;4008400301020;####-", codes.get(0)); |
| 115 | + Assert.assertEquals("+4008287051124;4008287051124;%%%%-", codes.get(1)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @UiThreadTest |
| 120 | + public void testSplitAgeRestrictedProductsInMultipleCodes() throws IOException, Snabble.SnabbleException { |
| 121 | + withDb("demoDb_1_11.sqlite3"); |
| 122 | + |
| 123 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 124 | + .prefix("+") |
| 125 | + .separator(";") |
| 126 | + .suffix("-") |
| 127 | + .nextCode("****") |
| 128 | + .nextCodeWithCheck("####") |
| 129 | + .finalCode("%%%%") |
| 130 | + .maxChars(40) |
| 131 | + .build(); |
| 132 | + |
| 133 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 134 | + Product duplo = project.getProductDatabase().findBySku("49"); |
| 135 | + Product krombacherPils = project.getProductDatabase().findBySku("37"); |
| 136 | + |
| 137 | + project.getShoppingCart().add(duplo, 3, ScannableCode.parse(project, "4008400301020")); |
| 138 | + project.getShoppingCart().add(krombacherPils, 3, ScannableCode.parse(project, "4008287051124")); |
| 139 | + |
| 140 | + generator.add(project.getShoppingCart()); |
| 141 | + |
| 142 | + ArrayList<String> codes = generator.generate(); |
| 143 | + Assert.assertEquals(4, codes.size()); |
| 144 | + Assert.assertEquals("+4008400301020;4008400301020;****-", codes.get(0)); |
| 145 | + Assert.assertEquals("+4008400301020;####-", codes.get(1)); |
| 146 | + Assert.assertEquals("+4008287051124;4008287051124;****-", codes.get(2)); |
| 147 | + Assert.assertEquals("+4008287051124;%%%%-", codes.get(3)); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + @UiThreadTest |
| 152 | + public void testSplitAgeRestrictedProductsInMultipleCodes2() throws IOException, Snabble.SnabbleException { |
| 153 | + withDb("demoDb_1_11.sqlite3"); |
| 154 | + |
| 155 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 156 | + .prefix("+") |
| 157 | + .separator(";") |
| 158 | + .suffix("-") |
| 159 | + .nextCode("****") |
| 160 | + .nextCodeWithCheck("####") |
| 161 | + .finalCode("%%%%") |
| 162 | + .maxChars(33) |
| 163 | + .build(); |
| 164 | + |
| 165 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 166 | + Product duplo = project.getProductDatabase().findBySku("49"); |
| 167 | + Product krombacherPils = project.getProductDatabase().findBySku("37"); |
| 168 | + |
| 169 | + project.getShoppingCart().add(duplo, 3, ScannableCode.parse(project, "4008400301020")); |
| 170 | + project.getShoppingCart().add(krombacherPils, 3, ScannableCode.parse(project, "4008287051124")); |
| 171 | + |
| 172 | + generator.add(project.getShoppingCart()); |
| 173 | + |
| 174 | + ArrayList<String> codes = generator.generate(); |
| 175 | + Assert.assertEquals(6, codes.size()); |
| 176 | + Assert.assertEquals("+4008400301020;****-", codes.get(0)); |
| 177 | + Assert.assertEquals("+4008400301020;****-", codes.get(1)); |
| 178 | + Assert.assertEquals("+4008400301020;####-", codes.get(2)); |
| 179 | + Assert.assertEquals("+4008287051124;****-", codes.get(3)); |
| 180 | + Assert.assertEquals("+4008287051124;****-", codes.get(4)); |
| 181 | + Assert.assertEquals("+4008287051124;%%%%-", codes.get(5)); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + @UiThreadTest |
| 186 | + public void testSplitWithOnlyOneRestrictedProduct() throws IOException, Snabble.SnabbleException { |
| 187 | + withDb("demoDb_1_11.sqlite3"); |
| 188 | + |
| 189 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 190 | + .prefix("+") |
| 191 | + .separator(";") |
| 192 | + .suffix("-") |
| 193 | + .nextCode("****") |
| 194 | + .nextCodeWithCheck("####") |
| 195 | + .finalCode("%%%%") |
| 196 | + .maxChars(1000) |
| 197 | + .build(); |
| 198 | + |
| 199 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 200 | + Product krombacherPils = project.getProductDatabase().findBySku("37"); |
| 201 | + |
| 202 | + project.getShoppingCart().add(krombacherPils, 1, ScannableCode.parse(project, "4008287051124")); |
| 203 | + |
| 204 | + generator.add(project.getShoppingCart()); |
| 205 | + |
| 206 | + ArrayList<String> codes = generator.generate(); |
| 207 | + Assert.assertEquals(2, codes.size()); |
| 208 | + Assert.assertEquals("+####-", codes.get(0)); |
| 209 | + Assert.assertEquals("+4008287051124;%%%%-", codes.get(1)); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + @UiThreadTest |
| 214 | + public void testKnauber() throws IOException, Snabble.SnabbleException { |
| 215 | + withDb("demoDb_1_11.sqlite3"); |
| 216 | + |
| 217 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 218 | + .prefix("") |
| 219 | + .separator("\n") |
| 220 | + .suffix("") |
| 221 | + .nextCode("") |
| 222 | + .nextCodeWithCheck("") |
| 223 | + .finalCode("2030801009061") |
| 224 | + .maxChars(1000) |
| 225 | + .build(); |
| 226 | + |
| 227 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 228 | + |
| 229 | + generator.add("voucher1"); |
| 230 | + generator.add("voucher2"); |
| 231 | + |
| 232 | + Product duplo = project.getProductDatabase().findBySku("49"); |
| 233 | + project.getShoppingCart().add(duplo, 3, ScannableCode.parse(project, "4008400301020")); |
| 234 | + |
| 235 | + Product heinz = project.getProductDatabase().findBySku("42"); |
| 236 | + project.getShoppingCart().add(heinz, 2, ScannableCode.parse(project, "8715700421698")); |
| 237 | + generator.add(project.getShoppingCart()); |
| 238 | + |
| 239 | + ArrayList<String> codes = generator.generate(); |
| 240 | + Assert.assertEquals(1, codes.size()); |
| 241 | + Assert.assertEquals("voucher1\nvoucher2\n4008400301020\n4008400301020\n4008400301020\n8715700421698\n8715700421698\n2030801009061", codes.get(0)); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + @UiThreadTest |
| 246 | + public void testEdeka() throws IOException, Snabble.SnabbleException { |
| 247 | + withDb("demoDb_1_11.sqlite3"); |
| 248 | + |
| 249 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 250 | + .prefix("XE") |
| 251 | + .separator("XE") |
| 252 | + .suffix("XZ") |
| 253 | + .nextCode("0000000001234") |
| 254 | + .nextCodeWithCheck("0000000012345") |
| 255 | + .finalCode("0000000013444") |
| 256 | + .maxChars(1000) |
| 257 | + .build(); |
| 258 | + |
| 259 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 260 | + |
| 261 | + Product duplo = project.getProductDatabase().findBySku("48"); |
| 262 | + project.getShoppingCart().add(duplo, 3, ScannableCode.parse(project, "42276630")); |
| 263 | + |
| 264 | + generator.add(project.getShoppingCart()); |
| 265 | + |
| 266 | + ArrayList<String> codes = generator.generate(); |
| 267 | + Assert.assertEquals(1, codes.size()); |
| 268 | + Assert.assertEquals("XE0000042276630XE0000042276630XE0000042276630XE0000000013444XZ", codes.get(0)); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + @UiThreadTest |
| 273 | + public void testEdekaWithOverflow() throws IOException, Snabble.SnabbleException { |
| 274 | + withDb("demoDb_1_11.sqlite3"); |
| 275 | + |
| 276 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 277 | + .prefix("XE") |
| 278 | + .separator("XE") |
| 279 | + .suffix("XZ") |
| 280 | + .nextCode("0000000001234") |
| 281 | + .nextCodeWithCheck("0000000012345") |
| 282 | + .finalCode("0000000013444") |
| 283 | + .maxChars(677) |
| 284 | + .build(); |
| 285 | + |
| 286 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 287 | + |
| 288 | + Product duplo = project.getProductDatabase().findBySku("48"); |
| 289 | + project.getShoppingCart().add(duplo, 45, ScannableCode.parse(project, "42276630")); |
| 290 | + |
| 291 | + generator.add(project.getShoppingCart()); |
| 292 | + |
| 293 | + ArrayList<String> codes = generator.generate(); |
| 294 | + Assert.assertEquals(2, codes.size()); |
| 295 | + Assert.assertEquals(StringUtils.repeat("XE0000042276630", 44) + "XE0000000001234XZ", codes.get(0)); |
| 296 | + Assert.assertEquals("XE0000042276630XE0000000013444XZ", codes.get(1)); |
| 297 | + } |
| 298 | + |
| 299 | + @Test |
| 300 | + @UiThreadTest |
| 301 | + public void testEdekaWithRestrictedProduct() throws IOException, Snabble.SnabbleException { |
| 302 | + withDb("demoDb_1_11.sqlite3"); |
| 303 | + |
| 304 | + EncodedCodesOptions options = new EncodedCodesOptions.Builder() |
| 305 | + .prefix("XE") |
| 306 | + .separator("XE") |
| 307 | + .suffix("XZ") |
| 308 | + .nextCode("0000000001234") |
| 309 | + .nextCodeWithCheck("0000000012345") |
| 310 | + .finalCode("0000000013444") |
| 311 | + .maxChars(675) |
| 312 | + .build(); |
| 313 | + |
| 314 | + EncodedCodesGenerator generator = new EncodedCodesGenerator(options); |
| 315 | + |
| 316 | + Product duplo = project.getProductDatabase().findBySku("48"); |
| 317 | + project.getShoppingCart().add(duplo, 3, ScannableCode.parse(project, "42276630")); |
| 318 | + |
| 319 | + Product krombacherPils = project.getProductDatabase().findBySku("37"); |
| 320 | + project.getShoppingCart().add(krombacherPils, 1, ScannableCode.parse(project, "4008287051124")); |
| 321 | + |
| 322 | + generator.add(project.getShoppingCart()); |
| 323 | + |
| 324 | + ArrayList<String> codes = generator.generate(); |
| 325 | + Assert.assertEquals(2, codes.size()); |
| 326 | + Assert.assertEquals(StringUtils.repeat("XE0000042276630", 3) + "XE0000000012345XZ", codes.get(0)); |
| 327 | + Assert.assertEquals("XE4008287051124XE0000000013444XZ", codes.get(1)); |
| 328 | + } |
| 329 | +} |
| 330 | + |
0 commit comments