UpdateCowHealthJob.java

1
package edu.ucsb.cs156.happiercows.jobs;
2
3
import edu.ucsb.cs156.happiercows.entities.Game;
4
import edu.ucsb.cs156.happiercows.entities.GamePlus;
5
import edu.ucsb.cs156.happiercows.entities.User;
6
import edu.ucsb.cs156.happiercows.entities.Farmer;
7
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
8
import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
9
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
10
import edu.ucsb.cs156.jobs.services.JobContext;
11
import edu.ucsb.cs156.jobs.services.JobContextConsumer;
12
import edu.ucsb.cs156.happiercows.services.GamePlusBuilderService;
13
import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy;
14
import lombok.AllArgsConstructor;
15
import lombok.Getter;
16
17
@AllArgsConstructor
18
public class UpdateCowHealthJob implements JobContextConsumer {
19
20
    @Getter
21
    private GameRepository gameRepository;
22
    @Getter
23
    private FarmerRepository farmerRepository;
24
    @Getter
25
    private UserRepository userRepository;
26
    @Getter
27
    private GamePlusBuilderService gamePlusBuilderService;
28
29
    @Override
30
    public void accept(JobContext ctx) throws Exception {
31 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Updating cow health...");
32
33
34
        Iterable<Game> allGame = gameRepository.findAll();
35
        Iterable<GamePlus> allGamePlus = gamePlusBuilderService.convertToGamePlus(allGame);
36
37
        for (GamePlus gamePlus : allGamePlus) {
38
39
40
            Game game = gamePlus.getGame();
41
42 1 1. accept : negated conditional → KILLED
            if (!GameGate.shouldProcess(game, gameRepository, ctx)) {
43
                continue;
44
            }
45
46 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInGame → KILLED
            runUpdateJobInGame(game, gamePlus, gamePlusBuilderService, gameRepository, farmerRepository, ctx);
47
48
        }
49
50 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Cow health has been updated!");
51
    }
52
53
    // exposed for testing
54
    public static double calculateNewCowHealthUsingStrategy(
55
            CowHealthUpdateStrategy strategy,
56
            GamePlus gamePlus,
57
            Farmer farmer,
58
            int totalCows
59
    ) {
60
        var health = strategy.calculateNewCowHealth(gamePlus, farmer, totalCows);
61 1 1. calculateNewCowHealthUsingStrategy : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED
        return Math.max(0, Math.min(health, 100));
62
    }
63
64
    public static void calculateCowDeaths(Farmer farmer, JobContext ctx) {
65 1 1. calculateCowDeaths : negated conditional → KILLED
        if (farmer.getCowHealth() == 0.0) {
66 2 1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED
2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED
            farmer.setCowDeaths(farmer.getCowDeaths() + farmer.getNumOfCows());
67 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED
            farmer.setNumOfCows(0);
68 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED
            farmer.setCowHealth(100.0);
69
70 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log(" " + farmer.getCowDeaths() + " cows for this user died." );
71
        }
72
    }
73
74
    public static void runUpdateJobInGame(Game game, GamePlus gamePlus, GamePlusBuilderService gamePlusBuilderService, GameRepository gameRepository, FarmerRepository farmerRepository, JobContext ctx){
75 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Game " + game.getName() + ", degradationRate: " + game.getDegradationRate() + ", effectiveCapacity: " + gamePlus.getEffectiveCapacity());
76
77 1 1. lambda$runUpdateJobInGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$0 → KILLED
            int numUsers = gameRepository.getNumUsers(game.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + game.getId() + ")"));
78
79 1 1. runUpdateJobInGame : negated conditional → KILLED
            if (numUsers==0) {
80 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log("No users in this game, skipping");
81
                return;
82
            }
83
84
            int carryingCapacity = gamePlus.getEffectiveCapacity();
85
            Iterable<Farmer> allFarmer = farmerRepository.findByGameId(game.getId());
86
87 1 1. lambda$runUpdateJobInGame$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$1 → KILLED
            Integer totalCows = gameRepository.getNumCows(game.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + game.getId() + ")"));
88
89 2 1. runUpdateJobInGame : changed conditional boundary → KILLED
2. runUpdateJobInGame : negated conditional → KILLED
            var isAboveCapacity = totalCows > carryingCapacity;
90 1 1. runUpdateJobInGame : negated conditional → KILLED
            var cowHealthUpdateStrategy = isAboveCapacity ? game.getAboveCapacityHealthUpdateStrategy() : game.getBelowCapacityHealthUpdateStrategy();
91
92
            for (Farmer farmer : allFarmer) {
93
                User user = farmer.getUser();
94
95
                var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, gamePlusBuilderService.toGamePlus(game), farmer, totalCows);
96 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log("User: " + user.getFullName() + ", numCows: " + farmer.getNumOfCows() + ", cowHealth: " + farmer.getCowHealth());
97
98
                double oldHealth = farmer.getCowHealth();
99 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED
                farmer.setCowHealth(newCowHealth);
100 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED
                calculateCowDeaths(farmer, ctx);
101
102 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log(" old cow health: " + oldHealth + ", new cow health: " + farmer.getCowHealth());
103
                farmerRepository.save(farmer);
104
            }
105
106
    }
107
}

Mutations

31

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_log_output_with_no_game()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

42

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skips_game_when_game_not_in_progress()]
negated conditional → KILLED

46

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skipping_job_when_game_has_zero_users()]
removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInGame → KILLED

50

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_log_output_with_no_game()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

61

1.1
Location : calculateNewCowHealthUsingStrategy
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED

65

1.1
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateCowDeaths_health_nonZero()]
negated conditional → KILLED

66

1.1
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateCowDeaths_health_zero()]
Replaced integer addition with subtraction → KILLED

2.2
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateCowDeaths_health_zero()]
removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED

67

1.1
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateCowDeaths_health_zero()]
removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED

68

1.1
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateCowDeaths_health_zero()]
removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED

70

1.1
Location : calculateCowDeaths
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_cowDeaths_in_job_context()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

75

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skipping_job_when_game_has_zero_users()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

77

1.1
Location : lambda$runUpdateJobInGame$0
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_throws_exception_when_get_num_users_fails()]
replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$0 → KILLED

79

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skipping_job_when_game_has_zero_users()]
negated conditional → KILLED

80

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skipping_job_when_game_has_zero_users()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

87

1.1
Location : lambda$runUpdateJobInGame$1
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_throws_exception_when_get_num_cows_fails()]
replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$1 → KILLED

89

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_uses_below_capacity_update_strategy_if_equal_to_carrying_capacity()]
changed conditional boundary → KILLED

2.2
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
negated conditional → KILLED

90

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
negated conditional → KILLED

96

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

99

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED

100

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_cowDeaths_in_job_context()]
removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED

102

1.1
Location : runUpdateJobInGame
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_values_for_multiple_users()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1