Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit dba0880

Browse files
committed
Add JUnit Test Classes
1 parent 6dd2f9c commit dba0880

File tree

6 files changed

+1083
-0
lines changed

6 files changed

+1083
-0
lines changed

.idea/libraries/junit_jupiter.xml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rykerzhu.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
78
</content>
89
<orderEntry type="inheritedJdk" />
910
<orderEntry type="sourceFolder" forTests="false" />
1011
<orderEntry type="library" name="thoughtworks.xstream" level="project" />
12+
<orderEntry type="library" name="junit.jupiter" level="project" />
1113
</component>
1214
</module>

test/AppStoreAPITest.java

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
import controllers.AppStoreAPI;
2+
import models.*;
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class AppStoreAPITest {
11+
12+
private EducationApp edAppBelowBoundary, edAppOnBoundary, edAppAboveBoundary, edAppInvalidData;
13+
private ProductivityApp prodAppBelowBoundary, prodAppOnBoundary, prodAppAboveBoundary, prodAppInvalidData;
14+
private GameApp gameAppBelowBoundary, gameAppOnBoundary, gameAppAboveBoundary, gameAppInvalidData;
15+
16+
private Developer developerLego = new Developer("Lego", "www.lego.com");
17+
private Developer developerSphero = new Developer("Sphero", "www.sphero.com");
18+
private Developer developerEAGames = new Developer("EA Games", "www.eagames.com");
19+
private Developer developerKoolGames = new Developer("Kool Games", "www.koolgames.com");
20+
private Developer developerApple = new Developer("Apple", "www.apple.com");
21+
private Developer developerMicrosoft = new Developer("Microsoft", "www.microsoft.com");
22+
23+
private AppStoreAPI appStore = new AppStoreAPI();
24+
private AppStoreAPI emptyAppStore = new AppStoreAPI();
25+
26+
@BeforeEach
27+
void setUp() {
28+
29+
//Validation: appSize(1-1000), appVersion(>=1.0), ageRating (0-18), appCost(>=0), level(1-10).
30+
edAppBelowBoundary = new EducationApp(developerLego, "WeDo", 1, 1.0, 0, 1);
31+
32+
edAppOnBoundary = new EducationApp(developerLego, "Spike", 1000, 2.0,
33+
1.99, 10);
34+
35+
edAppAboveBoundary = new EducationApp(developerLego, "EV3", 1001, 3.5, 2.99, 11);
36+
37+
edAppInvalidData = new EducationApp(developerLego, "", -1, 0, -1.00, 0);
38+
39+
40+
//Validation: appSize(1-1000), appVersion(>=1.0), ageRating (0-18), appCost(>=0),
41+
prodAppBelowBoundary = new ProductivityApp(developerApple, "NoteKeeper", 1, 1.0, 0.0);
42+
43+
prodAppOnBoundary = new ProductivityApp(developerMicrosoft, "Outlook", 1000, 2.0, 1.99);
44+
45+
prodAppAboveBoundary = new ProductivityApp(developerApple, "Pages", 1001, 3.5, 2.99);
46+
47+
prodAppInvalidData = new ProductivityApp(developerMicrosoft, "", -1, 0, -1.00);
48+
49+
50+
//Validation: appSize(1-1000), appVersion(>=1.0), ageRating (0-18), appCost(>=0),
51+
gameAppBelowBoundary = new GameApp(developerEAGames, "Tetris", 1, 1.0, 0.0, false);
52+
53+
gameAppOnBoundary = new GameApp(developerKoolGames, "CookOff", 1000, 2.0, 1.99, true);
54+
55+
gameAppAboveBoundary = new GameApp(developerEAGames, "Empires", 1001, 3.5, 2.99, false);
56+
57+
gameAppInvalidData = new GameApp(developerKoolGames, "", -1, 0, -1.00, true);
58+
59+
// Re-write the code
60+
61+
// EducationApp
62+
appStore.addApp(edAppOnBoundary); // index 0
63+
appStore.addApp(edAppBelowBoundary); // index 1
64+
appStore.addApp(edAppAboveBoundary); // index 2
65+
appStore.addApp(edAppInvalidData); // index 3
66+
67+
// ProductivityApp
68+
appStore.addApp(prodAppOnBoundary); // index 4
69+
appStore.addApp(prodAppBelowBoundary); // index 5
70+
appStore.addApp(prodAppAboveBoundary); // index 6
71+
appStore.addApp(prodAppInvalidData); // index 7
72+
73+
// GameApp
74+
appStore.addApp(gameAppOnBoundary); // index 8
75+
appStore.addApp(gameAppBelowBoundary); // index 9
76+
appStore.addApp(gameAppAboveBoundary); // index 10
77+
appStore.addApp(gameAppInvalidData); // index 11
78+
}
79+
80+
@AfterEach
81+
void tearDown() {
82+
edAppBelowBoundary = edAppOnBoundary = edAppAboveBoundary = edAppInvalidData = null;
83+
gameAppBelowBoundary = gameAppOnBoundary = gameAppAboveBoundary = gameAppInvalidData = null;
84+
prodAppBelowBoundary = prodAppOnBoundary = prodAppAboveBoundary = prodAppInvalidData = null;
85+
developerApple = developerEAGames = developerKoolGames = developerLego = developerMicrosoft = null;
86+
appStore = emptyAppStore = null;
87+
}
88+
89+
@Nested
90+
class GettersAndSetters {
91+
92+
}
93+
94+
// Written by Ryker Zhu
95+
@Nested
96+
class CRUDMethods {
97+
@Test
98+
void addingAnAppToArrayList() {
99+
App app = setupProductivityAppWithRating(2,4); // index 12
100+
assertEquals(12, appStore.numberOfApps());
101+
assertTrue(appStore.addApp(app));
102+
assertTrue(appStore.getAppByName("Evernote").getAppName().contains("Evernote"));
103+
assertEquals(13, appStore.numberOfApps());
104+
appStore.deleteAppByIndex(12);
105+
}
106+
107+
@Test
108+
void gettingAppByName() {
109+
App app = setupProductivityAppWithRating(2,4); // index 12
110+
appStore.addApp(app);
111+
assertNull(appStore.getAppByName("AppThatCannotExist"));
112+
assertEquals("Evernote", appStore.getAppByName("Evernote").getAppName());
113+
assertEquals("Evernote", appStore.getAppByName("eVeRnoTe").getAppName());
114+
appStore.deleteAppByIndex(12);
115+
}
116+
117+
@Test
118+
void gettingAppByIndex() {
119+
assertNull(appStore.getAppByIndex(-1));
120+
assertNull(appStore.getAppByIndex(666));
121+
assertEquals(edAppOnBoundary,appStore.getAppByIndex(0));
122+
assertEquals(prodAppBelowBoundary,appStore.getAppByIndex(5));
123+
}
124+
125+
@Test
126+
void removingAnAppThatDoesNotExistReturnsNull() {
127+
assertNull(appStore.deleteAppByIndex(666));
128+
assertNull(appStore.deleteAppByIndex(-200));
129+
assertNull(appStore.deleteAppByIndex(13));
130+
}
131+
132+
@Test
133+
void removingAnAppFromArrayList() {
134+
App app = setupProductivityAppWithRating(2,4); // index 12
135+
appStore.addApp(app);
136+
assertEquals(13, appStore.numberOfApps());
137+
assertEquals(app, appStore.deleteAppByIndex(12));
138+
assertEquals(12, appStore.numberOfApps());
139+
}
140+
}
141+
142+
@Nested
143+
class ListingMethods {
144+
145+
@Test
146+
void listAllAppsReturnsNoAppsStoredWhenArrayListIsEmpty() {
147+
assertEquals(0, emptyAppStore.numberOfApps());
148+
assertTrue(emptyAppStore.listAllApps().toLowerCase().contains("no apps"));
149+
}
150+
151+
@Test
152+
void listAllAppsReturnsAppsStoredWhenArrayListHasAppsStored() {
153+
assertEquals(12, appStore.numberOfApps());
154+
String apps = appStore.listAllApps();
155+
//checks for objects in the string
156+
assertTrue(apps.contains("WeDo"));
157+
assertTrue(apps.contains("Outlook"));
158+
assertTrue(apps.contains("Empires"));
159+
assertTrue(apps.contains("NoteKeeper"));
160+
assertTrue(apps.contains("EV3"));
161+
assertTrue(apps.contains("CookOff"));
162+
}
163+
164+
@Test
165+
void listRecommendedAppsReturnsNoAppsWhenRecommendedAppsDoNotExist() {
166+
assertEquals(12, appStore.numberOfApps());
167+
168+
String apps = appStore.listAllRecommendedApps();
169+
//checks for the three objects in the string
170+
assertTrue(apps.contains("No recommended apps"));
171+
}
172+
173+
@Test
174+
void listRecommendedAppsReturnsRecommendedAppsWhenTheyExist() {
175+
assertEquals(12, appStore.numberOfApps());
176+
177+
//adding recommended apps to the list
178+
appStore.addApp(setupGameAppWithRating(5,4));
179+
appStore.addApp(setupEducationAppWithRating(3,4));
180+
appStore.addApp(setupProductivityAppWithRating(3,4));
181+
assertEquals(15, appStore.numberOfApps());
182+
183+
String apps = appStore.listAllRecommendedApps();
184+
System.out.println(apps);
185+
//checks for the three objects in the string
186+
assertTrue(apps.contains("MazeRunner"));
187+
assertTrue(apps.contains("Evernote"));
188+
assertTrue(apps.contains("WeDo"));
189+
}
190+
191+
192+
}
193+
194+
@Nested
195+
class ReportingMethods {
196+
197+
}
198+
199+
@Nested
200+
class SearchingMethods {
201+
202+
}
203+
204+
@Nested
205+
class SortingMethods {
206+
207+
@Test
208+
void sortByNameAscendingReOrdersList() {
209+
assertEquals(12, appStore.numberOfApps());
210+
//checks the order of the objects in the list
211+
assertEquals(edAppOnBoundary, appStore.getAppByIndex(0));
212+
assertEquals(edAppBelowBoundary, appStore.getAppByIndex(1));
213+
assertEquals(edAppAboveBoundary, appStore.getAppByIndex(2));
214+
assertEquals(edAppInvalidData, appStore.getAppByIndex(3));
215+
assertEquals(prodAppOnBoundary, appStore.getAppByIndex(4));
216+
assertEquals(prodAppBelowBoundary, appStore.getAppByIndex(5));
217+
assertEquals(prodAppAboveBoundary, appStore.getAppByIndex(6));
218+
219+
appStore.sortAppsByNameAscending();
220+
221+
assertEquals(gameAppOnBoundary, appStore.getAppByIndex(3));
222+
assertEquals(edAppAboveBoundary, appStore.getAppByIndex(4));
223+
assertEquals(gameAppAboveBoundary, appStore.getAppByIndex(5));
224+
assertEquals(prodAppBelowBoundary, appStore.getAppByIndex(6));
225+
assertEquals(prodAppOnBoundary, appStore.getAppByIndex(7));
226+
}
227+
228+
@Test
229+
void sortByNameAscendingDoesntCrashWhenListIsEmpty() {
230+
assertEquals(0,emptyAppStore.numberOfApps());
231+
emptyAppStore.sortAppsByNameAscending();
232+
}
233+
234+
}
235+
236+
//--------------------------------------------
237+
// Helper Methods
238+
//--------------------------------------------
239+
EducationApp setupEducationAppWithRating(int rating1, int rating2) {
240+
//setting all conditions to true
241+
EducationApp edApp = new EducationApp(developerLego, "WeDo", 1,
242+
1.0, 1.00, 3);
243+
edApp.addRating(new Rating(rating1, "John Doe", "Very Good"));
244+
edApp.addRating(new Rating(rating2, "Jane Doe", "Excellent"));
245+
246+
return edApp;
247+
}
248+
249+
GameApp setupGameAppWithRating(int rating1, int rating2) {
250+
GameApp gameApp = new GameApp(developerEAGames, "MazeRunner", 1,
251+
1.0, 1.00, true);
252+
gameApp.addRating(new Rating(rating1, "John Soap", "Exciting Game"));
253+
gameApp.addRating(new Rating(rating2, "Jane Soap", "Nice Game"));
254+
return gameApp;
255+
}
256+
257+
ProductivityApp setupProductivityAppWithRating(int rating1, int rating2) {
258+
ProductivityApp productivityApp = new ProductivityApp(developerApple, "Evernote", 1,
259+
1.0, 1.99);
260+
261+
productivityApp.addRating(new Rating(rating1, "John101", "So easy to add a note"));
262+
productivityApp.addRating(new Rating(rating2, "Jane202", "So useful"));
263+
return productivityApp;
264+
}
265+
266+
}

0 commit comments

Comments
 (0)