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.CowLot;
13
import edu.ucsb.cs156.happiercows.entities.User;
14
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
15
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
16
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
17
import edu.ucsb.cs156.happiercows.repositories.CowLotRepository;
18
import lombok.Getter;
19
import lombok.extern.slf4j.Slf4j;
20
import lombok.AllArgsConstructor;
21
22
@AllArgsConstructor
23
public class UpdateCowHealthJob implements JobContextConsumer {
24
25
    @Getter
26
    private CommonsRepository commonsRepository;
27
    @Getter
28
    private UserCommonsRepository userCommonsRepository;
29
    @Getter
30
    private CowLotRepository cowLotRepository;
31
    @Getter
32
    private UserRepository userRepository;
33
34
    @Override
35
    public void accept(JobContext ctx) throws Exception {
36 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Starting Update Cow Health job:");
37
        Iterable<Commons> allCommons = commonsRepository.findAll();
38
        
39
        for (Commons commons : allCommons) {
40
            String name = commons.getName();
41 1 1. accept : negated conditional → KILLED
            if(commons.gameInProgress()){
42 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());
43
                int carryingCapacity = commons.getCarryingCapacity();
44
                double degradationRate = commons.getDegradationRate();
45
                Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());
46
47 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() + ")"));
48
49
                for (UserCommons userCommons : allUserCommons) {
50
                    double totalHealths = 0d;
51 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() + ")"));
52 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());
53
                    for(CowLot cowLot: cowLotRepository.findAllByUserCommonsId(userCommons.getId())){
54
                        double newCowHealth = calculateNewCowHealth(cowLot.getHealth(), userCommons.getNumOfCows(), totalCows, carryingCapacity, degradationRate);
55 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                        ctx.log("old cow health: " + cowLot.getHealth() + ", new cow health: " + newCowHealth);
56 1 1. accept : removed call to edu/ucsb/cs156/happiercows/entities/CowLot::setHealth → KILLED
                        cowLot.setHealth(newCowHealth);
57 2 1. accept : changed conditional boundary → KILLED
2. accept : negated conditional → KILLED
                        if(newCowHealth > 0){
58
                            cowLotRepository.save(cowLot);
59
                        } else {
60 1 1. accept : removed call to edu/ucsb/cs156/happiercows/repositories/CowLotRepository::delete → KILLED
                            cowLotRepository.delete(cowLot);
61 2 1. accept : Replaced integer subtraction with addition → KILLED
2. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
                            userCommons.setNumOfCows(userCommons.getNumOfCows()-cowLot.getNumCows());
62
                        }
63 2 1. accept : Replaced double multiplication with division → KILLED
2. accept : Replaced double addition with subtraction → KILLED
                        totalHealths += newCowHealth * cowLot.getNumCows();
64
                    }
65 2 1. accept : Replaced double division with multiplication → KILLED
2. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED
                    userCommons.setCowHealth(totalHealths / userCommons.getNumOfCows());
66
                    userCommonsRepository.save(userCommons);
67
                }
68
            } else{
69 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
                ctx.log("Game " + name + " is not currently in progress, cow health will not be updated for this commons.");
70
            }
71
        }
72 1 1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED
        ctx.log("Update Cow Health job complete!");
73
    }
74
75
    public static double calculateNewCowHealth(
76
        double oldCowHealth,
77
        int numCows,
78
        int totalCows,
79
        int carryingCapacity,
80
        double degradationRate) {
81 2 1. calculateNewCowHealth : changed conditional boundary → KILLED
2. calculateNewCowHealth : negated conditional → KILLED
        if (totalCows <= carryingCapacity) {
82
            // increase cow health but do not exceed 100
83 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));
84
        } else {
85
            // decrease cow health, don't go lower than 0
86 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));
87
        }
88
    }
89
90
}

Mutations

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

41

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

47

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

51

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

52

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_greater_than_100()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → 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_updating_to_new_values_health_greater_than_100()]
removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → 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_updating_to_new_values_for_multiple()]
removed call to edu/ucsb/cs156/happiercows/entities/CowLot::setHealth → 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_updating_to_new_values_health_lower_than_zero()]
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_updating_to_new_values_health_greater_than_100()]
negated conditional → KILLED

60

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/repositories/CowLotRepository::delete → 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_updating_to_new_values_health_lower_than_zero()]
Replaced integer subtraction with addition → 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_updating_to_new_values_health_lower_than_zero()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED

63

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_greater_than_carrying_capacity()]
Replaced double multiplication with division → 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_updating_to_new_values_health_greater_than_100()]
Replaced double addition with subtraction → KILLED

65

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_greater_than_carrying_capacity()]
Replaced double division with multiplication → 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_updating_to_new_values_if_less_than_carrying_capacity()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED

69

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

72

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

81

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

83

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

86

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