UpdateCowHealthJob.java

1
2
package edu.ucsb.cs156.happiercows.jobs;
3
4
import edu.ucsb.cs156.happiercows.entities.Game;
5
import edu.ucsb.cs156.happiercows.entities.GamePlus;
6
import edu.ucsb.cs156.happiercows.entities.User;
7
import edu.ucsb.cs156.happiercows.entities.Farmer;
8
import edu.ucsb.cs156.happiercows.entities.CowHerd;
9
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
10
import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
11
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
12
import edu.ucsb.cs156.happiercows.repositories.CowHerdRepository;
13
import edu.ucsb.cs156.jobs.services.JobContext;
14
import edu.ucsb.cs156.jobs.services.JobContextConsumer;
15
import edu.ucsb.cs156.happiercows.services.GamePlusBuilderService;
16
import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy;
17
import lombok.AllArgsConstructor;
18
import lombok.Getter;
19
20
@AllArgsConstructor
21
public class UpdateCowHealthJob implements JobContextConsumer {
22
23
    @Getter
24
    private GameRepository gameRepository;
25
26
    @Getter
27
    private FarmerRepository farmerRepository;
28
29
    @Getter
30
    private UserRepository userRepository;
31
32
    @Getter
33
    private GamePlusBuilderService gamePlusBuilderService;
34
35
    @Getter
36
    private CowHerdRepository cowHerdRepository;
37
38
    @Override
39
    public void accept(JobContext ctx) throws Exception {
40 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Updating cow health...");
41
42
        Iterable<Game> allGame = gameRepository.findAll();
43
        Iterable<GamePlus> allGamePlus =
44
            gamePlusBuilderService.convertToGamePlus(allGame);
45
46
        for (GamePlus gamePlus : allGamePlus) {
47
            Game game = gamePlus.getGame();
48
49 1 1. accept : negated conditional → KILLED
            if (!GameGate.shouldProcess(game, gameRepository, ctx)) {
50
                continue;
51
            }
52
53 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInGame → KILLED
            runUpdateJobInGame(
54
                game,
55
                gamePlus,
56
                gamePlusBuilderService,
57
                gameRepository,
58
                farmerRepository,
59
                cowHerdRepository,
60
                ctx
61
            );
62
        }
63
64 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Cow health has been updated!");
65
    }
66
67
    // exposed for testing
68
    public static double calculateNewCowHealthUsingStrategy(
69
            CowHealthUpdateStrategy strategy,
70
            GamePlus gamePlus,
71
            Farmer farmer,
72
            int totalCows
73
    ) {
74
        var health = strategy.calculateNewCowHealth(
75
            gamePlus, farmer, totalCows
76
        );
77
78 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));
79
    }
80
81
    public static void calculateCowDeaths(Farmer farmer, JobContext ctx) {
82 1 1. calculateCowDeaths : negated conditional → KILLED
        if (farmer.getCowHealth() == 0.0) {
83 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED
            farmer.setCowDeaths(
84 1 1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED
                farmer.getCowDeaths() + farmer.getNumOfCows()
85
            );
86
87 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED
            farmer.setNumOfCows(0);
88 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED
            farmer.setCowHealth(100.0);
89
90 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log(
91
                " " + farmer.getCowDeaths() + " cows for this user died."
92
            );
93
        }
94
    }
95
96
    public static void runUpdateJobInGame(
97
            Game game,
98
            GamePlus gamePlus,
99
            GamePlusBuilderService gamePlusBuilderService,
100
            GameRepository gameRepository,
101
            FarmerRepository farmerRepository,
102
            CowHerdRepository cowHerdRepository,
103
            JobContext ctx
104
    ) {
105 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log(
106
            "Game " + game.getName()
107
            + ", degradationRate: " + game.getDegradationRate()
108
            + ", effectiveCapacity: " + gamePlus.getEffectiveCapacity()
109
        );
110
111
        int numUsers = gameRepository.getNumUsers(game.getId())
112 1 1. lambda$runUpdateJobInGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$0 → KILLED
            .orElseThrow(() -> new RuntimeException(
113
                "Error calling getNumUsers(" + game.getId() + ")"
114
            ));
115
116 1 1. runUpdateJobInGame : negated conditional → KILLED
        if (numUsers == 0) {
117 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log("No users in this game, skipping");
118
            return;
119
        }
120
121
        int carryingCapacity = gamePlus.getEffectiveCapacity();
122
123
        Iterable<Farmer> allFarmer =
124
            farmerRepository.findByGameId(game.getId());
125
126
        Integer totalCows = gameRepository.getNumCows(game.getId())
127 1 1. lambda$runUpdateJobInGame$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$1 → KILLED
            .orElseThrow(() -> new RuntimeException(
128
                "Error calling getNumCows(" + game.getId() + ")"
129
            ));
130
131 2 1. runUpdateJobInGame : changed conditional boundary → KILLED
2. runUpdateJobInGame : negated conditional → KILLED
        var isAboveCapacity = totalCows > carryingCapacity;
132
133 1 1. runUpdateJobInGame : negated conditional → KILLED
        var cowHealthUpdateStrategy = isAboveCapacity
134
            ? game.getAboveCapacityHealthUpdateStrategy()
135
            : game.getBelowCapacityHealthUpdateStrategy();
136
137
        for (Farmer farmer : allFarmer) {
138
            User user = farmer.getUser();
139
140
            // Reuse the existing GamePlus for farmer health.
141
            var newCowHealth = calculateNewCowHealthUsingStrategy(
142
                cowHealthUpdateStrategy,
143
                gamePlus,
144
                farmer,
145
                totalCows
146
            );
147
148 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log(
149
                "User: " + user.getFullName()
150
                + ", numCows: " + farmer.getNumOfCows()
151
                + ", cowHealth: " + farmer.getCowHealth()
152
            );
153
154
            double oldHealth = farmer.getCowHealth();
155
156 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED
            farmer.setCowHealth(newCowHealth);
157 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED
            calculateCowDeaths(farmer, ctx);
158
159 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log(
160
                " old cow health: " + oldHealth
161
                + ", new cow health: " + farmer.getCowHealth()
162
            );
163
164
            farmerRepository.save(farmer);
165
166
            for (CowHerd herd :
167
                cowHerdRepository.findByFarmer_Id_UserIdAndFarmer_Id_GameId(
168
                    farmer.getUserId(),
169
                    game.getId()
170
                )
171
            ) {
172
                Farmer herdFarmer = Farmer.builder()
173
                    .cowHealth(herd.getCowHealth())
174
                    .build();
175
176
                // Reuse GamePlus instead of rebuilding it per herd.
177
                double newHerdHealth =
178
                    calculateNewCowHealthUsingStrategy(
179
                        cowHealthUpdateStrategy,
180
                        gamePlus,
181
                        herdFarmer,
182
                        totalCows
183
                    );
184
185 1 1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/CowHerd::setCowHealth → KILLED
                herd.setCowHealth(newHerdHealth);
186
                cowHerdRepository.save(herd);
187
            }
188
        }
189
    }
190
}

Mutations

40

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

49

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

53

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

64

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

78

1.1
Location : calculateNewCowHealthUsingStrategy
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()]
replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED

82

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

83

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::setCowDeaths → KILLED

84

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

87

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

88

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

90

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

105

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

112

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

116

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

117

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

127

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

131

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_uses_below_capacity_update_strategy()]
negated conditional → KILLED

133

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()]
negated conditional → KILLED

148

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()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

156

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()]
removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED

157

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

159

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()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

185

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

Active mutators

Tests examined


Report generated by PIT 1.22.1