UpdateCowHealthJob.java

1
package edu.ucsb.cs156.happiercows.jobs;
2
3
import edu.ucsb.cs156.happiercows.entities.Commons;
4
import edu.ucsb.cs156.happiercows.entities.CommonsPlus;
5
import edu.ucsb.cs156.happiercows.entities.User;
6
import edu.ucsb.cs156.happiercows.entities.UserCommons;
7
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
8
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
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.CommonsPlusBuilderService;
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 CommonsRepository commonsRepository;
22
    @Getter
23
    private UserCommonsRepository userCommonsRepository;
24
    @Getter
25
    private UserRepository userRepository;
26
    @Getter
27
    private CommonsPlusBuilderService commonsPlusBuilderService;
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<Commons> allCommons = commonsRepository.findAll();
35
        Iterable<CommonsPlus> allCommonsPlus = commonsPlusBuilderService.convertToCommonsPlus(allCommons);
36
37
        for (CommonsPlus commonsPlus : allCommonsPlus) {
38
39
40
            Commons commons = commonsPlus.getCommons();
41
42 1 1. accept : negated conditional → KILLED
            if (!CommonsGate.shouldProcess(commons, commonsRepository, ctx)) {
43
                continue;
44
            }
45
46 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInCommons → KILLED
            runUpdateJobInCommons(commons, commonsPlus, commonsPlusBuilderService, commonsRepository, userCommonsRepository, 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
            CommonsPlus commonsPlus,
57
            UserCommons userCommons,
58
            int totalCows
59
    ) {
60
        var health = strategy.calculateNewCowHealth(commonsPlus, userCommons, 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(UserCommons userCommons, JobContext ctx) {
65 1 1. calculateCowDeaths : negated conditional → KILLED
        if (userCommons.getCowHealth() == 0.0) {
66 2 1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED
2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowDeaths → KILLED
            userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows());
67 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
            userCommons.setNumOfCows(0);
68 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
            userCommons.setCowHealth(100.0);
69
70 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
            ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." );
71
        }
72
    }
73
74
    public static void runUpdateJobInCommons(Commons commons, CommonsPlus commonsPlus, CommonsPlusBuilderService commonsPlusBuilderService, CommonsRepository commonsRepository, UserCommonsRepository userCommonsRepository, JobContext ctx){
75 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", effectiveCapacity: " + commonsPlus.getEffectiveCapacity());
76
77 1 1. lambda$runUpdateJobInCommons$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$0 → KILLED
            int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")"));
78
79 1 1. runUpdateJobInCommons : negated conditional → KILLED
            if (numUsers==0) {
80 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log("No users in this commons, skipping");
81
                return;
82
            }
83
84
            int carryingCapacity = commonsPlus.getEffectiveCapacity();
85
            Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
86
87 1 1. lambda$runUpdateJobInCommons$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$1 → KILLED
            Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")"));
88
89 2 1. runUpdateJobInCommons : negated conditional → KILLED
2. runUpdateJobInCommons : changed conditional boundary → KILLED
            var isAboveCapacity = totalCows > carryingCapacity;
90 1 1. runUpdateJobInCommons : negated conditional → KILLED
            var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy();
91
92
            for (UserCommons userCommons : allUserCommons) {
93
                User user = userCommons.getUser();
94
95
                var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commonsPlusBuilderService.toCommonsPlus(commons), userCommons, totalCows);
96 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth());
97
98
                double oldHealth = userCommons.getCowHealth();
99 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
                userCommons.setCowHealth(newCowHealth);
100 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED
                calculateCowDeaths(userCommons, ctx);
101
102 1 1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
                ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth());
103
                userCommonsRepository.save(userCommons);
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_commons()]
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_commons_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_commons_has_zero_users()]
removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInCommons → 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_commons()]
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/UserCommons::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/UserCommons::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/UserCommons::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 : runUpdateJobInCommons
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_skipping_job_when_commons_has_zero_users()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

77

1.1
Location : lambda$runUpdateJobInCommons$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$runUpdateJobInCommons$0 → KILLED

79

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

80

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

87

1.1
Location : lambda$runUpdateJobInCommons$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$runUpdateJobInCommons$1 → KILLED

89

1.1
Location : runUpdateJobInCommons
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

2.2
Location : runUpdateJobInCommons
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

90

1.1
Location : runUpdateJobInCommons
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 : runUpdateJobInCommons
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 : runUpdateJobInCommons
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/UserCommons::setCowHealth → KILLED

100

1.1
Location : runUpdateJobInCommons
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 : runUpdateJobInCommons
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.17.0