generated from runelite/example-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarDatabaseScenarioTest.java
237 lines (193 loc) · 10.1 KB
/
StarDatabaseScenarioTest.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
package com.janboerman.f2pstarassist.web;
import com.janboerman.f2pstarassist.common.*;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
/**
* Simulates a few scenarios of players calling out star updates to the star database.
*/
public class StarDatabaseScenarioTest {
/**
* Scenario 0:
* Player a from group A calls a star
* Player b from group A requests the lists of known stars
* expected result: player b gets the star back in the response
*/
@Test
public void testScenario0() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_1, StarLocation.PVP_ARENA, 565, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
starDatabase.add(Set.of(groupA), crashedStar);
final Set<CrashedStar> starsKnownToGroupA = starDatabase.getStars(Set.of(groupA));
assertTrue(starsKnownToGroupA.contains(crashedStar));
}
/**
* Scenario 1:
* Player a from group A calls a star
* Player b from group B requests the list of known stars
* expected result: b shouldn't get the star found by player a because player b is not in group A.
*/
@Test
public void testScenario1() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_9, StarLocation.VARROCK_AUBURY, 301, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(Set.of(groupA), crashedStar);
final Set<CrashedStar> starsKnownToGroupB = starDatabase.getStars(Set.of(groupB));
assertFalse(starsKnownToGroupB.contains(crashedStar));
}
/**
* Scenario 2:
* Player a from group A calls a star
* Player b from group B also finds this star
* Player b2 from group B requests the list of known stars
* expected result: b2 shouldn't get the star found by player a because player b2 is not in group A.
*/
@Test
public void testScenario2() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_9, StarLocation.VARROCK_AUBURY, 301, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(Set.of(groupA), crashedStar);
starDatabase.add(Set.of(groupB), crashedStar);
final Set<CrashedStar> starsKnownToGroupB = starDatabase.getStars(Set.of(groupB));
assertFalse(starsKnownToGroupB.contains(crashedStar));
}
/**
* Scenario 3:
* Player x is in both group A and B and calls a star
* Player a from group A requests stars
* Player b from group B requests stars
* expected result: both player a and b should get the found star.
*/
@Test
public void testScenario3() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_5, StarLocation.LUMBRIDGE_SWAMP_SOUTH_EAST_MINE, 382, Instant.now(), new RunescapeUser("x"));
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(Set.of(groupA, groupB), crashedStar);
final Set<CrashedStar> starsKnownToGroupA = starDatabase.getStars(Set.of(groupA));
final Set<CrashedStar> starsKnownToGroupB = starDatabase.getStars(Set.of(groupB));
assertTrue(starsKnownToGroupA.contains(crashedStar));
assertTrue(starsKnownToGroupB.contains(crashedStar));
}
/**
* Scenario 4:
* Player a from group A calls a star
* Player a witnesses it degrading to a lower tier
* Player a2 from group A requests the star
* expected result: a2 gets the updated star back
*/
@Test
public void testScenario4() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final Instant detectedAt = Instant.now();
final User detectedBy = new RunescapeUser("a");
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_5, StarLocation.AL_KHARID_BANK, 553, detectedAt, detectedBy);
final GroupKey groupA = new GroupKey("A");
starDatabase.add(Set.of(groupA), crashedStar);
starDatabase.update(groupA, new StarUpdate(StarTier.SIZE_4, StarLocation.AL_KHARID_BANK, 553));
final CrashedStar updatedStar = new CrashedStar(StarTier.SIZE_4, StarLocation.AL_KHARID_BANK, 553, detectedAt, detectedBy);
assertTrue(starDatabase.getStars(Set.of(groupA)).contains(updatedStar));
}
/**
* Scenario 5:
* Player a from group A calls a star
* Player b from group B notices this star degrading
* Player a2 from group A requests the list of stars
* Player b2 from group B requests the list of stars
* expected result: b gets the updated star back
* expected result: b2 doesn't get the star
* expected result a2 still gets the old star info
*/
@Test
public void testScenario5() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final Instant detectedAt = Instant.now();
final User detectedBy = new RunescapeUser("a");
final CrashedStar crashedStar = new CrashedStar(StarTier.SIZE_8, StarLocation.MINING_GUILD, 385, detectedAt, detectedBy);
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(Set.of(groupA), crashedStar);
CrashedStar resultb = starDatabase.update(groupB, new StarUpdate(StarTier.SIZE_7, StarLocation.MINING_GUILD, 385));
CrashedStar expectedb = new CrashedStar(StarTier.SIZE_7, StarLocation.MINING_GUILD, 385, detectedAt, detectedBy);
assertEquals(expectedb, resultb);
StarKey starKey = new StarKey(StarLocation.MINING_GUILD, 385);
assertNull(starDatabase.get(groupB, starKey));
assertTrue(starDatabase.getStars(Set.of(groupB)).stream().map(CrashedStar::getKey).noneMatch(starKey::equals));
CrashedStar expecteda = new CrashedStar(StarTier.SIZE_8, StarLocation.MINING_GUILD, 385, detectedAt, detectedBy);
assertEquals(expecteda, starDatabase.get(groupA, starKey));
assertTrue(starDatabase.getStars(groupA).contains(expecteda));
}
/**
* Scenario 6:
* Player a from group A finds a star
* Player a detects that the star depletes or poofs
* Player a2 from group A requests the list of stars
* expected result: player a2 doesn't get the star
*/
@Test
public void testScenario6() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final StarKey starKey = new StarKey(StarLocation.CORSAIR_COVE_RESOURCE_AREA, 565);
final CrashedStar crashedStar = new CrashedStar(starKey, StarTier.SIZE_2, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
starDatabase.add(new HashSet<>() { { add(groupA); } }, crashedStar);
starDatabase.remove(groupA, starKey);
assertFalse(starDatabase.getStars(groupA).stream().map(CrashedStar::getKey).anyMatch(starKey::equals));
}
/**
* Scenario 7:
* Player a from group A finds a star
* Player b from group B finds the same star a bit later
* Player a logs out
* The star depletes, only player b notices this
* Player a2 from group A requests the star list
* Player b2 from group B requests the star list
* expected result: player a2 still gets the star
* expected result: player b2 doesn't get the star
*/
@Test
public void testScenario7() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final StarKey starKey = new StarKey(StarLocation.CORSAIR_COVE_BANK, 417);
final CrashedStar crashedStar = new CrashedStar(starKey, StarTier.SIZE_3, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(new HashSet<>() { { add(groupA); } }, crashedStar);
starDatabase.add(new HashSet<>() { { add(groupB); } }, crashedStar);
starDatabase.remove(new HashSet<>() { { add(groupB); } }, starKey);
assertEquals(crashedStar, starDatabase.get(groupA, starKey));
assertNull(starDatabase.get(groupB, starKey));
}
/**
* Scenario 8:
* Player a who is in both group A and B finds a star
* Player b from group B finds the same star a bit later
* Player a logs out
* The star depletes, only player b notices this
* Player a2 from group A requests the star list
* Player b2 from group B requests the star list
* expected result: player a2 still gets the star
* expected result: player b2 doesn't get the star
*/
@Test
public void testScenario8() {
final StarDatabase starDatabase = new StarDatabase(NoOpStarListener.INSTANCE);
final StarKey starKey = new StarKey(StarLocation.CORSAIR_COVE_BANK, 418);
final CrashedStar crashedStar = new CrashedStar(starKey, StarTier.SIZE_3, Instant.now(), new RunescapeUser("a"));
final GroupKey groupA = new GroupKey("A");
final GroupKey groupB = new GroupKey("B");
starDatabase.add(new HashSet<>() { { add(groupA); add(groupB); } }, crashedStar);
starDatabase.add(new HashSet<>() { { add(groupB); } }, crashedStar);
starDatabase.remove(new HashSet<>() { { add(groupB); } }, starKey);
assertTrue(starDatabase.getStars(groupA).contains(crashedStar));
assertFalse(starDatabase.getStars(groupB).contains(crashedStar));
}
}