summaryrefslogtreecommitdiff
path: root/src/test/java/design/persistence/JSONLeagueDatabaseTest.java
blob: b27555c92ecfa0e6579e3e00f44d8a9e28d0ad49 (plain) (blame)
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
package design.persistence;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import design.model.League;
import design.model.StrokeLeague;
import design.model.Course;
import design.model.Golfer;
import design.model.Hole;
import design.model.Match;

/** Unit Tests for the JSON Personal Database Singleton
 * @author Willem Dalton
 **/
@Tag("Persistence-tier")
public class JSONLeagueDatabaseTest {
    Path tempDB;

    @BeforeEach
    void clearDB() throws IOException
    {
        tempDB = Files.createTempFile("testleaguedb", ".json");
        Files.writeString(tempDB, "[]");
        JSONLeagueDatabase.testInstance(tempDB.toString());
    }
    
    @Test 
    void testInstance()
    {
        JSONLeagueDatabase instance = JSONLeagueDatabase.instance(); // makes new instance
        assertNotNull(instance);
        JSONLeagueDatabase instance2 = JSONLeagueDatabase.instance(); // instance already exists
        assertNotNull(instance2);
    }

    @Test
    void testAddRemove() throws IOException
    {
        JSONLeagueDatabase instance = JSONLeagueDatabase.testInstance(tempDB.toString()); // makes new instance
        Golfer testOwner = new Golfer("Jamie Doe", "joe_cool", "12345");
        League testLeague = new StrokeLeague("The A Team", new Date(1234), new Date(123), new Date(12345), testOwner);
        instance.addLeague(testLeague);
        assertEquals(1, instance.getLeagues().length);
        assertEquals(testLeague, instance.getLeagues()[0]);
        instance.removeLeague(testLeague);
        assertEquals(0, instance.getLeagues().length);
    }

    @Test
    void testUpdateGolfer() throws IOException
    {
        JSONLeagueDatabase instance = JSONLeagueDatabase.testInstance(tempDB.toString()); // makes new instance
        Golfer testOwner = new Golfer("Jamie Doe", "joe_cool", "12345");
        League testLeague = new StrokeLeague("The A Team", new Date(1234), new Date(123), new Date(12345), testOwner);
        instance.addLeague(testLeague);
        Course testCourse = new Course(0, "Rolling Waves", 62, "Rochester, NY", 9, 20, new ArrayList<Hole>());
        Match testMatch = new Match(testCourse, new Date(123), LocalDateTime.now(), 3);
        testLeague.addMatchToSchedule(testMatch);
        instance.updateLeague(testLeague);
        assertEquals(testLeague, instance.getLeague(testLeague.getId()));
    }
}