forked from carlosmiranda/farm-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeopleTest.java
596 lines (545 loc) · 19.3 KB
/
PeopleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/**
* Copyright (c) 2016-2018 Zerocracy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to read
* the Software only. Permissions is hereby NOT GRANTED to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.zerocracy.pmo;
import com.jcabi.aspects.Tv;
import com.zerocracy.Project;
import com.zerocracy.SoftException;
import com.zerocracy.cash.Cash;
import com.zerocracy.farm.fake.FkFarm;
import com.zerocracy.farm.fake.FkProject;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.iterable.RangeOf;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.And;
import org.cactoos.text.FormattedText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Test case for {@link People}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
* @todo #952:30min Continue replacing old Date classes with Instant.
* Remember also to remove instances of `DateAsText` (Instant.toString should
* be used). There is a lot of classes to change so try to find a good small
* cluster of related classes that can be updated.
* @checkstyle JavadocMethodCheck (1000 lines)
* @checkstyle JavadocVariableCheck (1000 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (3 lines)
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
public final class PeopleTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void addsAndFindsPeople() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "yegor256";
final String rel = "slack";
final String alias = "U67WE3343P";
people.link(uid, rel, alias);
people.link(uid, "jira", "https://www.0crat.com/jira");
MatcherAssert.assertThat(
people.iterate(),
Matchers.hasItem(uid)
);
MatcherAssert.assertThat(
people.find(rel, alias),
Matchers.not(Matchers.emptyIterable())
);
MatcherAssert.assertThat(
people.links(uid),
Matchers.hasItem("slack:U67WE3343P")
);
}
@Test
public void setsUserRate() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "alex-palevsky";
people.wallet(uid, "paypal", "test@example.com");
people.rate(uid, new Cash.S("$35"));
people.rate(uid, new Cash.S("$50"));
MatcherAssert.assertThat(
people.rate(uid),
Matchers.equalTo(new Cash.S("USD 50"))
);
}
@Test
public void readsUnsetRate() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "dmarkov9";
MatcherAssert.assertThat(
people.rate(uid),
Matchers.equalTo(Cash.ZERO)
);
}
@Test
public void setsUserWallet() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "yegor256-1";
people.wallet(uid, "paypal", "yegor256@gmail.com");
MatcherAssert.assertThat(
people.wallet(uid),
Matchers.startsWith("yegor256@")
);
MatcherAssert.assertThat(
people.bank(uid),
Matchers.startsWith("payp")
);
}
@Test
public void failsForIncorrectBankName() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String bank = "test";
this.thrown.expect(SoftException.class);
this.thrown.expectMessage(
new FormattedText("Bank name `%s` is invalid", bank).asString()
);
people.wallet("yegor128", bank, "");
}
@Test
public void setsCorrectPaypalAddress() throws Exception {
PeopleTest.setsWallet("paypal", "john@example.com");
}
@Test
public void setsCorrectBitcoinAddress() throws Exception {
PeopleTest.setsWallet("btc", "3HcEB6bi4TFPdvk31Pwz77DwAzfAZz2fMn");
}
@Test
public void setsCorrectBitcoinCashAddress() throws Exception {
PeopleTest.setsWallet(
"bch", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"
);
}
@Test
public void setsCorrectEthereumAddress() throws Exception {
PeopleTest.setsWallet(
"eth", "e79c3300773E8593fb332487E1EfdD8729b87445"
);
}
@Test
public void setsCorrectPrefixedEthereumAddress() throws Exception {
PeopleTest.setsWallet(
"eth", "0xe79c3300773E8593fb332487E1EfdD8729b87445"
);
}
@Test
public void setsCorrectLitecoinAddress() throws Exception {
PeopleTest.setsWallet("ltc", "LS78aoGtfuGCZ777x3Hmr6tcoW3WaYynx9");
}
@Test
public void setCorrectZoldAddress() throws Exception {
PeopleTest.setsWallet("zld", "g4s8");
}
@Test
public void failsForIncorrectPaypalAddress() throws Exception {
this.failsWallet("paypal");
}
@Test
public void failsForIncorrectBitcoinAddress() throws Exception {
this.failsWallet("btc");
}
@Test
public void failsForIncorrectEthereumAddress() throws Exception {
this.failsWallet("eth");
}
@Test
public void failsForIncorrectBitcoinCashAddress() throws Exception {
this.failsWallet("bch");
}
@Test
public void failsForIncorrectLitecoinAddress() throws Exception {
this.failsWallet("ltc");
}
@Test
public void failsForIncorrectZoldAddress() throws Exception {
this.failsWallet("zld", "!@#$%fgs");
}
@Test
public void upgradesXsdAutomatically() throws Exception {
final Project project = new FkProject();
Files.write(
project.acq("people.xml").path(),
String.join(
"",
"<people xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'",
" xsi:noNamespaceSchemaLocation='",
"http://datum.zerocracy.com/0.7.1",
"/xsd/project/people.xsd'/>"
).getBytes()
);
final FkFarm farm = new FkFarm(project);
final People people = new People(farm).bootstrap();
final String uid = "karato90";
people.wallet(uid, "paypal", "tes1t@example.com");
people.rate(uid, new Cash.S("$27"));
}
@Test
public void invitesFriend() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "jack";
final String friend = "friend";
people.invite(friend, uid);
people.invite("another-friend", uid);
MatcherAssert.assertThat(
people.hasMentor(friend),
Matchers.is(true)
);
}
@Test
public void vacationTest() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "g4s8";
MatcherAssert.assertThat(
people.vacation(uid),
Matchers.is(false)
);
people.vacation(uid, true);
MatcherAssert.assertThat(
people.vacation(uid),
Matchers.is(true)
);
people.vacation(uid, false);
MatcherAssert.assertThat(
people.vacation(uid),
Matchers.is(false)
);
}
@Test
public void breakupTest() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "john";
final String friend = "jimmy";
people.invite(friend, uid);
people.breakup(friend);
MatcherAssert.assertThat(
people.hasMentor(friend),
Matchers.is(false)
);
}
@Test
public void mentorTest() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "datum";
final String mentor = "0crat";
people.invite(uid, mentor);
MatcherAssert.assertThat(
people.mentor(uid),
Matchers.equalTo(mentor)
);
}
@Test
public void studentsTest() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People ppl = new People(farm).bootstrap();
final String mentor = "mentor";
final List<String> students = new ListOf<>(
"student1",
"student2",
"student3"
);
new And((String std) -> ppl.invite(std, mentor), students).value();
MatcherAssert.assertThat(
ppl.students(mentor),
Matchers.allOf(
Matchers.iterableWithSize(students.size()),
Matchers.hasItems(
students.toArray(new String[students.size()])
)
)
);
}
@Test(expected = SoftException.class)
public void inviteSixteen() throws Exception {
final String mentor = "mnt";
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
new And(
(String std) -> people.invite(std, mentor),
new Mapped<>(
(Integer num) -> String.format("student%d", num),
// @checkstyle MagicNumber (1 line)
new RangeOf<>(0, 16, x -> x + 1)
)
).value();
}
@Test
public void graduate() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "yegor11";
people.invite(uid, "the-mentor");
people.graduate(uid);
MatcherAssert.assertThat(
people.mentor(uid),
Matchers.equalTo("0crat")
);
}
@Test
public void reputation() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "user2345";
people.invite(uid, uid);
final int rep = 1024;
people.reputation(uid, rep);
MatcherAssert.assertThat(
people.reputation(uid),
Matchers.equalTo(rep)
);
}
@Test
public void remove() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "remove";
people.invite(uid, "mentor11");
people.remove(uid);
MatcherAssert.assertThat(
people.iterate(),
Matchers.emptyIterable()
);
}
@Test
public void getSingleLink() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "linker";
people.invite(uid, uid);
final String rel = "some-rel11";
final String href = "some-href22";
people.link(uid, rel, href);
MatcherAssert.assertThat(
people.link(uid, rel),
Matchers.equalTo(href)
);
}
@Test
public void canApply() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "user3236";
final Date when = new Date(0L);
people.invite(uid, uid);
people.apply(uid, when);
MatcherAssert.assertThat(
"applied",
people.applied(uid),
Matchers.is(true)
);
MatcherAssert.assertThat(
"applied time",
people.appliedTime(uid),
Matchers.equalTo(when)
);
}
@Test(expected = IllegalArgumentException.class)
public void throwIfApplyButDoesntExist() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
new People(farm).bootstrap()
.apply("user124", new Date(0L));
}
@Test(expected = IllegalArgumentException.class)
public void throwIfgetAppliedDateIfNotApplied() throws Exception {
final FkProject project = new FkProject();
final FkFarm farm = new FkFarm(project);
final People people = new People(farm).bootstrap();
final String uid = "user3236";
people.invite(uid, uid);
people.appliedTime(uid);
}
@Test
public void readsEmptyDetails() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "read-empty-details";
people.invite(uid, uid);
MatcherAssert.assertThat(
people.details(uid),
Matchers.isEmptyString()
);
}
@Test
public void setsDetails() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "detailed";
people.invite(uid, uid);
final String details = "dtls";
people.details(uid, details);
MatcherAssert.assertThat(
people.details(uid),
Matchers.equalTo(details)
);
}
@Test
public void setsJobs() throws Exception {
final People people =
new People(new FkFarm(new FkProject())).bootstrap();
final String uid = "jobs";
people.invite(uid, uid);
final int jobs = Tv.TEN;
people.jobs(uid, jobs);
MatcherAssert.assertThat(
people.jobs(uid),
Matchers.is(jobs)
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsIfFetchingJobsOfNonExistentUser() throws Exception {
new People(new FkFarm(new FkProject())).bootstrap()
.jobs("jobs1");
}
@Test(expected = IllegalArgumentException.class)
public void throwsIfSettingJobsOfNonExistentUser() throws Exception {
new People(new FkFarm(new FkProject())).bootstrap()
.jobs("jobs2", 1);
}
@Test
public void setsSpeed() throws Exception {
final People people =
new People(new FkFarm(new FkProject())).bootstrap();
final String uid = "speed";
people.invite(uid, uid);
final double speed = 5.5;
people.speed(uid, speed);
MatcherAssert.assertThat(
people.speed(uid),
Matchers.is(speed)
);
}
@Test(expected = IllegalArgumentException.class)
public void throwsIfFetchingSpeedOfNonExistentUser() throws Exception {
new People(new FkFarm(new FkProject())).bootstrap()
.speed("speed1");
}
@Test(expected = IllegalArgumentException.class)
public void throwsIfSettingSpeedOfNonExistentUser() throws Exception {
new People(new FkFarm(new FkProject())).bootstrap()
.speed("speed2", 1.0);
}
@Test
public void throwIfTryToSetDetailsButNotApplied() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "not-applied";
this.thrown.expect(SoftException.class);
this.thrown.expectMessage(
Matchers.allOf(
Matchers.containsString(uid),
Matchers.containsString("is not with us yet")
)
);
people.details(uid, "ignored");
}
@Test
public void throwIfTryToSetEmptyDetails() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "set-empty-details";
this.thrown.expect(SoftException.class);
this.thrown.expectMessage(
Matchers.allOf(
Matchers.containsString(uid),
Matchers.containsString("details can't be empty")
)
);
people.invite(uid, uid);
people.details(uid, "");
}
@Test
public void setsLinks() throws Exception {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "yegor256";
final String srel = "slack";
final String jrel = "jira";
final String salias = "U67WE3343P";
final String jalias = "https://www.0crat.com/jira";
people.link(uid, srel, salias);
people.link(uid, jrel, jalias);
final String format = "%s:%s";
MatcherAssert.assertThat(
people.links(uid),
Matchers.containsInAnyOrder(
String.format(format, srel, salias),
String.format(format, jrel, jalias),
String.format("github:%s", uid)
)
);
MatcherAssert.assertThat(
people.links(uid, jrel),
Matchers.contains(jalias)
);
}
@Test
public void setsSkills() throws IOException {
final People people =
new People(new FkFarm(new FkProject())).bootstrap();
final String uid = "user";
people.invite(uid, "0crat");
final Iterable<String> skills = new IterableOf<>("c", "cobol");
people.skills(uid, skills);
MatcherAssert.assertThat(
new ArrayList<>(new ListOf<>(people.skills(uid))),
Matchers.equalTo(new ArrayList<>(new ListOf<>(skills)))
);
}
private void failsWallet(final String bank, final String wallet)
throws IOException {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
this.thrown.expect(SoftException.class);
this.thrown.expectMessage(
new FormattedText(" not valid: `%s`", wallet).asString()
);
people.wallet("yegor512", bank, wallet);
}
private void failsWallet(final String bank)
throws IOException {
this.failsWallet(bank, "123456");
}
private static void setsWallet(final String bank, final String wallet)
throws IOException {
final FkFarm farm = new FkFarm(new FkProject());
final People people = new People(farm).bootstrap();
final String uid = "yegor64";
people.wallet(uid, bank, wallet);
MatcherAssert.assertThat(
people.wallet(uid),
Matchers.is(wallet)
);
}
}