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.User;
5
import edu.ucsb.cs156.happiercows.entities.UserCommons;
6
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
7
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
8
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
9
import edu.ucsb.cs156.happiercows.services.jobs.JobContext;
10
import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer;
11
import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy;
12
import lombok.AllArgsConstructor;
13
import lombok.Getter;
14
15
@AllArgsConstructor
16
public class UpdateCowHealthJob implements JobContextConsumer {
17
18
    @Getter
19
    private CommonsRepository commonsRepository;
20
    @Getter
21
    private UserCommonsRepository userCommonsRepository;
22
    @Getter
23
    private UserRepository userRepository;
24
25
    @Override
26
    public void accept(JobContext ctx) throws Exception {
27 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Updating cow health...");
28
29
        Iterable<Commons> allCommons = commonsRepository.findAll();
30
31
        for (Commons commons : allCommons) {
32 1 1. accept : negated conditional → KILLED
            if (!commons.gameInProgress()) {
33 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("Skipping Cow Health Update at Commons: " + commons.getName() + " because game is not in progress");
34
                continue;
35
            }
36 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
            ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", carryingCapacity: " + commons.getCarryingCapacity());
37 1 1. lambda$accept$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED
            int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")"));
38
39 1 1. accept : negated conditional → KILLED
            if (numUsers==0) {
40 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("No users in this commons, skipping");
41
                continue;
42
            }
43
44
            int effectiveCapacity = Commons.computeEffectiveCapacity(commons, commonsRepository);
45
            Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
46
47 1 1. lambda$accept$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED
            Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")"));
48
49 2 1. accept : changed conditional boundary → KILLED
2. accept : negated conditional → KILLED
            var isAboveCapacity = totalCows > effectiveCapacity;
50 1 1. accept : negated conditional → KILLED
            var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy();
51
52
            for (UserCommons userCommons : allUserCommons) {
53
                User user = userCommons.getUser();
54
                var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commons, commonsRepository, userCommons, totalCows);
55 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth());
56
57
                double oldHealth = userCommons.getCowHealth();
58 1 1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
                userCommons.setCowHealth(newCowHealth);
59 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED
                calculateCowDeaths(userCommons, ctx);
60
61 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth());
62
                userCommonsRepository.save(userCommons);
63
            }
64
        }
65
66 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Cow health has been updated!");
67
    }
68
69
    // exposed for testing
70
    public static double calculateNewCowHealthUsingStrategy(
71
            CowHealthUpdateStrategy strategy,
72
            Commons commons,
73
            CommonsRepository commonsRepository,
74
            UserCommons userCommons,
75
            int totalCows
76
    ) {
77
        var health = strategy.calculateNewCowHealth(commons, commonsRepository, userCommons, totalCows);
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(UserCommons userCommons, JobContext ctx) {
82 1 1. calculateCowDeaths : negated conditional → KILLED
        if (userCommons.getCowHealth() == 0.0) {
83 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());
84 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
            userCommons.setNumOfCows(0);
85 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
            userCommons.setCowHealth(100.0);
86
87 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
            ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." );
88
        }
89
    }
90
}

Mutations

27

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/happiercows/services/jobs/JobContext::log → KILLED

32

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

33

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

36

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/services/jobs/JobContext::log → KILLED

37

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

39

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

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

47

1.1
Location : lambda$accept$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$accept$1 → 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_uses_below_capacity_update_strategy_if_equal_to_carrying_capacity()]
changed conditional boundary → KILLED

2.2
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_uses_above_capacity_update_strategy()]
negated conditional → 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_uses_above_capacity_update_strategy()]
negated conditional → KILLED

55

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

58

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

59

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

61

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

66

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/happiercows/services/jobs/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_cow_health_maximum_is_100()]
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()]
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

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

85

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

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

Active mutators

Tests examined


Report generated by PIT 1.7.3