package design.model.statistics; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import design.model.Course; import design.model.Golfer; import design.model.Hole; import design.model.Round; import design.model.Club.ClubType; import design.model.Play; import design.model.Club; import design.model.Swing; /** Unit Tests for the Statistics Subsystem * @author Willem Dalton **/ @Tag("Model-tier") public class StatisticsTest { /* base stats wrapper for testing purposes */ private static class StubStatistics implements Statistics { private final Golfer golfer; public StubStatistics(Golfer golfer) { this.golfer = golfer; } @Override public Round[] getRounds() { return golfer.getRounds(); } @Override public int get_score() { return Arrays.stream(getRounds()) .mapToInt(Round::getTotalSwings) .sum(); } @Override public double get_distance() { return Arrays.stream(getRounds()) .mapToDouble(Round::getTotalDistance) .sum(); } } @Test void testLifeTime() { Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList()); Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); LifetimeStats stats = new LifetimeStats(testGolfer); assertNotNull(stats); assertEquals(0, stats.get_score()); assertEquals(0, stats.get_distance()); ArrayList testSwings = new ArrayList(); testSwings.add(testSwing); Play testPlay = new Play(0, testSwings); testRound.addPlay(testPlay); testGolfer.addRound(testRound); assertEquals(1, stats.get_score()); assertEquals(100, stats.get_distance()); } @Test void testYearly() { Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList()); Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); Statistics wrapper = new StubStatistics(testGolfer); YearlyStats stats = new YearlyStats(wrapper, 2025); assertNotNull(stats); assertEquals(0, stats.get_score()); assertEquals(0, stats.get_distance()); ArrayList testSwings = new ArrayList(); testSwings.add(testSwing); Play testPlay = new Play(0, testSwings); testRound.addPlay(testPlay); testGolfer.addRound(testRound); assertEquals(1, stats.get_score()); assertEquals(100, stats.get_distance()); } @Test void testCourse() { Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList()); Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); Statistics wrapper = new StubStatistics(testGolfer); CourseStats stats = new CourseStats(wrapper, testCourse); assertNotNull(stats); assertEquals(0, stats.get_score()); assertEquals(0, stats.get_distance()); ArrayList testSwings = new ArrayList(); testSwings.add(testSwing); Play testPlay = new Play(0, testSwings); testRound.addPlay(testPlay); testGolfer.addRound(testRound); assertEquals(1, stats.get_score()); assertEquals(100, stats.get_distance()); } @Test void testRound() { Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList()); Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); Statistics wrapper = new StubStatistics(testGolfer); RoundStats stats = new RoundStats(wrapper, testRound); assertNotNull(stats); assertEquals(0, stats.get_score()); assertEquals(0, stats.get_distance()); ArrayList testSwings = new ArrayList(); testSwings.add(testSwing); Play testPlay = new Play(0, testSwings); testRound.addPlay(testPlay); testGolfer.addRound(testRound); assertEquals(1, stats.get_score()); assertEquals(100, stats.get_distance()); } @Test void testHole() { Golfer testGolfer = new Golfer("John Doe", "j_doe", "weback"); Course testCourse = new Course(0, "Rolling Waves", 67, "Rochester, NY", 0, 0, new ArrayList()); Round testRound = new Round(testCourse, LocalDateTime.now(), new Hole(0, 10)); Swing testSwing = new Swing(100, new Club("John Doe Inc", "The Slammer", ClubType.DRIVER)); Hole testHole = new Hole(0, 10); Statistics wrapper = new StubStatistics(testGolfer); HoleStats stats = new HoleStats(wrapper, testHole); assertNotNull(stats); assertEquals(0, stats.get_score()); assertEquals(0, stats.get_distance()); ArrayList testSwings = new ArrayList(); testSwings.add(testSwing); Play testPlay = new Play(0, testSwings); testRound.addPlay(testPlay); testGolfer.addRound(testRound); assertEquals(1, stats.get_score()); assertEquals(100, stats.get_distance()); assertEquals(0, stats.getRounds().length); } }