aboutsummaryrefslogtreecommitdiffstats
path: root/test/eu/equalparts/cardbase/CardbaseTest.java
blob: 82b05babb7ff4bfa0746ba63ef51504ad75649f6 (plain)
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
package eu.equalparts.cardbase;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import eu.equalparts.cardbase.Cardbase;
import eu.equalparts.cardbase.cards.Card;

/**
 * TODO deck functionality needs to be built into these.
 * 
 * @author Eduardo Pedroni
 *
 */
public class CardbaseTest {

	private Cardbase uut;
	private static Card testCard;
	
	@Rule
	public final ExpectedException exception = ExpectedException.none();
	
	@Rule
	public final TemporaryFolder tempFolder = new TemporaryFolder();
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		uut = new Cardbase();
	}
	
	/***********************************************************************************
	 * Constructor tests, happy path
	 ***********************************************************************************/
	@Test
	public void cleanCardbaseIsInitialised() throws Exception {
		assertEquals("Card collection is not empty.", 0, uut.getCards().size());
	}
	
	@Test
	public void fileCardbaseIsInitialised() throws Exception {
		uut = new Cardbase(new File(getClass().getResource("/testbase.cb").getFile()));
		
		assertEquals("Card collection contains the wrong number of card entries.", 6, uut.getCards().size());
		
		class CardInfo {
			String setCode, number;
			Integer count;
			public CardInfo(String setCode, String number, Integer count) {
				this.setCode = setCode;
				this.number = number;
				this.count = count;
			}
		}
		CardInfo[] testCards = new CardInfo[] {
				new CardInfo("M12", "34", 2),
				new CardInfo("FRF", "129", 8),
				new CardInfo("M12", "26", 1),
				new CardInfo("FRF", "127", 1),
				new CardInfo("FRF", "128", 1),
				new CardInfo("M12", "152", 1)};
		
		for (CardInfo ci : testCards) {
			Card card = uut.getCard(ci.setCode, ci.number);
			assertNotNull("Missing card, set " + ci.setCode + ", " + ci.number, card);
			assertEquals("Wrong card count, set " + ci.setCode + ", " + ci.number, ci.count, card.count);
		}
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void loadFileIsNull() throws Exception {
		exception.expect(NullPointerException.class);
		uut = new Cardbase(null);
	}
	
	@Test
	public void loadFileDoesNotExist() throws Exception {
		File notAFile = tempFolder.newFile();
		tempFolder.delete();
		
		exception.expect(IOException.class);
		uut = new Cardbase(notAFile);
	}
	
	@Test
	public void loadFileHasWrongStructure() throws Exception {
		File wrongStructure = tempFolder.newFile("wrongStructure.json");
		try (FileWriter writer = new FileWriter(wrongStructure)) {
			writer.write("{\"field1\":\"content\",\"field2\":50,\"list\":[10,20,30]}");
		}
		
		exception.expect(JsonMappingException.class);
		uut = new Cardbase(wrongStructure);
	}
	
	@Test
	public void loadFileIsNotJson() throws Exception {
		File notJson = tempFolder.newFile("wrongStructure.json");
		try (FileWriter writer = new FileWriter(notJson)) {
			writer.write("This is a file which does not contain valid JSON.");
		}
		
		exception.expect(JsonParseException.class);
		uut = new Cardbase(notJson);
	}
	
	/***********************************************************************************
	 * Saving cardbase tests, happy path
	 ***********************************************************************************/
	@Test
	public void cardbaseIsSaved() throws Exception {
		File testFile = tempFolder.newFile("saveTest.cb");
		uut.writeCollection(testFile);
		uut = new Cardbase(testFile);
		assertEquals("Cardbase should contain no cards.", 0, uut.getCards().size());

		uut.addCard(testCard);

		uut.writeCollection(testFile);
		uut = new Cardbase(testFile);
		assertEquals("Cardbase should contain one card.", 1, uut.getCards().size());
		Card card = uut.getCard("M15", "281");
		assertNotNull("Cardbase should contain a Shivan Dragon.", card);
		assertEquals("Cardbase should contain only one Shivan Dragon.", new Integer(1), card.count);
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void saveFileCannotBeWrittenTo() throws Exception {
		File testFile = tempFolder.newFile("saveTest.cb");
		testFile.setWritable(false);
		exception.expect(IOException.class);
		uut.writeCollection(testFile);
	}
	
	@Test
	public void saveFileIsNull() throws Exception {
		exception.expect(NullPointerException.class);
		uut = new Cardbase(null);
	}
	
	/***********************************************************************************
	 * Adding card tests, happy path
	 ***********************************************************************************/
	@Test
	public void newCardIsAdded() throws Exception {
		uut.addCard(testCard);
		Card addedCard = uut.getCard("M15", "281");
		
		assertNotNull("Card was not found in cardbase.", addedCard);
		assertEquals(testCard, addedCard);
	}
	
	@Test
	public void existingCardIsIncremented() throws Exception {
		Card shivanClone = testCard.clone();
		uut.addCard(shivanClone);
		Card shivanClone2 = testCard.clone();
		shivanClone2.count = 2;
		uut.addCard(shivanClone2);
		
		Card addedCard = uut.getCard("M15", "281");
		assertNotNull("Card was not found in cardbase.", addedCard);
		assertEquals("Card count was not updated correctly.", new Integer(3), addedCard.count);
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void cardAddedIsNull() throws Exception {
		exception.expect(NullPointerException.class);
		uut.addCard(null);
	}
	
	/***********************************************************************************
	 * Removing card tests, happy path
	 ***********************************************************************************/
	@Test
	public void cardRemoveCountIsLessThanCardCount() throws Exception {
		Card shivanClone = testCard.clone();
		shivanClone.count = 5;
		uut.addCard(shivanClone);
		
		int removed = uut.removeCard(testCard);
		
		Card removedCard = uut.getCard("M15", "281");
		assertNotNull("Card was not found in cardbase.", removedCard);
		assertEquals("Card count was not updated correctly.", new Integer(4), removedCard.count);
		assertEquals("Cardbase reports wrong removed count.", 1, removed);
	}
	
	@Test
	public void cardRemoveCountIsEqualToCardCount() throws Exception {
		Card shivanClone = testCard.clone();
		uut.addCard(shivanClone);
		
		int removed = uut.removeCard(testCard);
		
		Card removedCard = uut.getCard("M15", "281");
		assertNull("Card was not removed from cardbase.", removedCard);
		assertEquals("Cardbase reports wrong removed count.", 1, removed);
	}
	
	@Test
	public void cardRemoveCountIsGreaterThanCardCount() throws Exception {
		Card shivanClone = testCard.clone();
		shivanClone.count = 3;
		uut.addCard(shivanClone);
		
		Card shivanClone2 = testCard.clone();
		shivanClone2.count = 5;
		int removed = uut.removeCard(shivanClone2);
		
		Card removedCard = uut.getCard("M15", "281");
		assertNull("Card was not removed from cardbase.", removedCard);
		assertEquals("Cardbase reports wrong removed count.", 3, removed);
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void removedCardIsNull() throws Exception {
		exception.expect(NullPointerException.class);
		uut.removeCard(null);
	}
	
	@Test
	public void removedCardIsNotInCardbase() throws Exception {
		int removed = uut.removeCard(testCard);
		
		assertEquals("Removed count should be 0.", 0, removed);
	}

	/***********************************************************************************
	 * Card getter tests, happy path
	 ***********************************************************************************/
	@Test
	public void correctCardIsReturnedByGetter() throws Exception {
		uut.addCard(testCard.clone());
		
		Card card = uut.getCard("M15", "281");
		
		for (Field field : Card.class.getFields()) {
			assertEquals("Field " + field.getName(), field.get(testCard), field.get(card));
		}
	}
	
	@Test
	public void correctCardCollectionIsReturnedByGetter() throws Exception {
		uut = new Cardbase(new File(getClass().getResource("/testbase.cb").getFile()));
		Map<Integer, Card> cards = new ObjectMapper().readValue(getClass().getResourceAsStream("/testbase.cb"), new TypeReference<Map<Integer, Card>>() {});
		
		assertTrue("Not all cards were returned by the getter.", uut.getCards().containsAll(cards.values()));
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void getCardIsNotInCardbase() throws Exception {
		assertNull("Method should have returned null", uut.getCard("M15", "281"));
	}
	
	@Test
	public void cardCollectionWhenCardbaseIsEmpty() throws Exception {
		assertEquals("Returned collection size should have been 0.", 0, uut.getCards().size());
	}
}