|
| 1 | +import org.junit.jupiter.api.BeforeEach; |
| 2 | +import org.junit.jupiter.api.DisplayName; |
| 3 | +import org.junit.jupiter.api.Tag; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +public class CaptainsLogTest { |
| 11 | + |
| 12 | + private Random random1; |
| 13 | + private Random random2; |
| 14 | + private Random random3; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + public void setup() { |
| 18 | + random1 = new Random(47); |
| 19 | + random2 = new Random(474747); |
| 20 | + random3 = new Random(474747474747L); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + @Tag("task:1") |
| 25 | + @DisplayName("Generating a random planet class") |
| 26 | + public void testRandomPlanetClass() { |
| 27 | + assertThat(new CaptainsLog(random1).randomPlanetClass()).isEqualTo('T'); |
| 28 | + assertThat(new CaptainsLog(random2).randomPlanetClass()).isEqualTo('K'); |
| 29 | + assertThat(new CaptainsLog(random3).randomPlanetClass()).isEqualTo('J'); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @Tag("task:1") |
| 34 | + @DisplayName("Generated planet classes are valid") |
| 35 | + public void testRandomPlanetClassIsValid() { |
| 36 | + var captainsLog = new CaptainsLog(new Random()); |
| 37 | + |
| 38 | + for (int i = 0; i < 100; i++) { |
| 39 | + assertThat(captainsLog.randomPlanetClass()) |
| 40 | + .isIn('D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @Tag("task:2") |
| 46 | + @DisplayName("Generating a random ship registry number") |
| 47 | + public void testRandomShipRegistryNumber() { |
| 48 | + assertThat(new CaptainsLog(random1).randomShipRegistryNumber()).isEqualTo("NCC-8773"); |
| 49 | + assertThat(new CaptainsLog(random2).randomShipRegistryNumber()).isEqualTo("NCC-2473"); |
| 50 | + assertThat(new CaptainsLog(random3).randomShipRegistryNumber()).isEqualTo("NCC-9576"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @Tag("task:2") |
| 55 | + @DisplayName("Generated ship registry numbers are valid") |
| 56 | + public void testRandomShipRegistryNumberIsValid() { |
| 57 | + var captainsLog = new CaptainsLog(new Random()); |
| 58 | + |
| 59 | + for (int i = 0; i < 100; i++) { |
| 60 | + var shipRegistryNumber = captainsLog.randomShipRegistryNumber(); |
| 61 | + var number = Integer.parseInt(shipRegistryNumber.substring(4)); |
| 62 | + |
| 63 | + assertThat(number).isBetween(1000, 9999); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @Tag("task:3") |
| 69 | + @DisplayName("Generating a random stardate") |
| 70 | + public void testRandomStardate() { |
| 71 | + assertThat(new CaptainsLog(random1).randomStardate()).isEqualTo(41727.115786073); |
| 72 | + assertThat(new CaptainsLog(random2).randomStardate()).isEqualTo(41531.31240225019); |
| 73 | + assertThat(new CaptainsLog(random3).randomStardate()).isEqualTo(41283.50713600276); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @Tag("task:3") |
| 78 | + @DisplayName("Generated stardates are valid") |
| 79 | + public void testRandomStardateIsValid() { |
| 80 | + var captainsLog = new CaptainsLog(new Random()); |
| 81 | + |
| 82 | + for (int i = 0; i < 100; i++) { |
| 83 | + assertThat(captainsLog.randomStardate()).isBetween(41000.0, 42000.0); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments