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 : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
            ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", carryingCapacity: " + commons.getCarryingCapacity());
33 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() + ")"));
34
35 1 1. accept : negated conditional → KILLED
            if (numUsers==0) {
36 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("No users in this commons, skipping");
37
                continue;
38
            }
39
40
            int carryingCapacity = commons.getCarryingCapacity();
41
            Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
42
43 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() + ")"));
44
45 2 1. accept : changed conditional boundary → KILLED
2. accept : negated conditional → KILLED
            var isAboveCapacity = totalCows > carryingCapacity;
46 1 1. accept : negated conditional → KILLED
            var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy();
47
48
            for (UserCommons userCommons : allUserCommons) {
49
                User user = userCommons.getUser();
50
                var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commons, userCommons, totalCows);
51 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());
52
53
                double oldHealth = userCommons.getCowHealth();
54 1 1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
                userCommons.setCowHealth(newCowHealth);
55 1 1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED
                calculateCowDeaths(userCommons, ctx);
56
57 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());
58
                userCommonsRepository.save(userCommons);
59
            }
60
        }
61
62 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Cow health has been updated!");
63
    }
64
65
    // exposed for testing
66
    public static double calculateNewCowHealthUsingStrategy(
67
            CowHealthUpdateStrategy strategy,
68
            Commons commons,
69
            UserCommons userCommons,
70
            int totalCows
71
    ) {
72
        var health = strategy.calculateNewCowHealth(commons, userCommons, totalCows);
73 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));
74
    }
75
76
    public static void calculateCowDeaths(UserCommons userCommons, JobContext ctx) {
77 1 1. calculateCowDeaths : negated conditional → KILLED
        if (userCommons.getCowHealth() == 0.0) {
78 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());
79 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
            userCommons.setNumOfCows(0);
80 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
            userCommons.setCowHealth(100.0);
81
82 1 1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
            ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." );
83
        }
84
    }
85
}

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

33

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

35

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

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

43

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

45

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

51

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

54

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

57

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

62

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

73

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

77

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

78

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

79

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

80

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

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_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