UpdateCowHealthJob.java

1
package edu.ucsb.cs156.happiercows.jobs;
2
3
import java.util.Optional;
4
5
6
import java.util.Iterator;
7
import edu.ucsb.cs156.happiercows.services.jobs.JobContext;
8
import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer;
9
import edu.ucsb.cs156.happiercows.entities.Commons;
10
import edu.ucsb.cs156.happiercows.entities.UserCommons;
11
import edu.ucsb.cs156.happiercows.entities.CommonsPlus;
12
import edu.ucsb.cs156.happiercows.entities.User;
13
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
14
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
15
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
16
import lombok.Getter;
17
import lombok.extern.slf4j.Slf4j;
18
import lombok.AllArgsConstructor;
19
20
@AllArgsConstructor
21
public class UpdateCowHealthJob implements JobContextConsumer {
22
23
    @Getter
24
    private CommonsRepository commonsRepository;
25
    @Getter
26
    private UserCommonsRepository userCommonsRepository;
27
    @Getter
28
    private UserRepository userRepository;
29
30
    @Override
31
    public void accept(JobContext ctx) throws Exception {
32 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Updating cow health...");
33
34
        Iterable<Commons> allCommons = commonsRepository.findAll();
35
36
        for (Commons commons : allCommons) {
37 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());
38
39
            int carryingCapacity = commons.getCarryingCapacity();
40
            double degradationRate = commons.getDegradationRate();
41
            Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
42
43 1 1. lambda$accept$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED
            Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(()->new RuntimeException("Error calling getNumCows(" + commons.getId() + ")"));
44
45
            for (UserCommons userCommons : allUserCommons) {
46 1 1. lambda$accept$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED
                User user = userRepository.findById(userCommons.getUserId()).orElseThrow(()->new RuntimeException("Error calling userRepository.findById(" + userCommons.getUserId() + ")"));
47 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());
48
49
                double newCowHealth = calculateNewCowHealth(userCommons.getCowHealth(), userCommons.getNumOfCows(), totalCows, carryingCapacity, degradationRate);
50 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log(" old cow health: " + userCommons.getCowHealth() + ", new cow health: " + newCowHealth);
51 1 1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
                userCommons.setCowHealth(newCowHealth);
52
                userCommonsRepository.save(userCommons);
53
            }
54
        }
55
56 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Cow health has been updated!");
57
    }
58
59
    public static double calculateNewCowHealth(
60
            double oldCowHealth,
61
            int numCows,
62
            int totalCows,
63
            int carryingCapacity,
64
            double degradationRate) {
65 2 1. calculateNewCowHealth : changed conditional boundary → KILLED
2. calculateNewCowHealth : negated conditional → KILLED
        if (totalCows <= carryingCapacity) {
66
            // increase cow health but do not exceed 100
67 2 1. calculateNewCowHealth : Replaced double addition with subtraction → KILLED
2. calculateNewCowHealth : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealth → KILLED
            return Math.min(100, oldCowHealth + (degradationRate));
68
        } else {
69
            // decrease cow health, don't go lower than 0
70 4 1. calculateNewCowHealth : Replaced integer subtraction with addition → KILLED
2. calculateNewCowHealth : Replaced double multiplication with division → KILLED
3. calculateNewCowHealth : Replaced double subtraction with addition → KILLED
4. calculateNewCowHealth : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealth → KILLED
            return Math.max(0, oldCowHealth - Math.min((totalCows - carryingCapacity) * degradationRate, 100));
71
        }
72
    }
73
74
}

Mutations

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

37

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

43

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_cows_fails()]
replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED

46

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_getting_user_fails()]
replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED

47

1.1
Location : accept
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_updating_to_new_values_health_lower_than_zero()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → 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_updating_to_new_values_health_lower_than_zero()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → 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_updating_to_new_values_if_equal_to_carrying_capacity()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED

56

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

65

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

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

67

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

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

70

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

2.2
Location : calculateNewCowHealth
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateNewCowHealth__numCows_ten_over_carryingCapacity_old_health_at_80__goes_down_by_degradation_rate_times_10()]
Replaced double multiplication with division → KILLED

3.3
Location : calculateNewCowHealth
Killed by : edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobTests]/[method:test_calculateNewCowHealth__numCows_ten_over_carryingCapacity_old_health_at_80__goes_down_by_degradation_rate_times_10()]
Replaced double subtraction with addition → KILLED

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

Active mutators

Tests examined


Report generated by PIT 1.7.3