UserCommonsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
6
import org.springframework.beans.factory.annotation.Autowired;
7
8
import org.springframework.security.access.prepost.PreAuthorize;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.RequestParam;
12
import org.springframework.web.bind.annotation.RestController;
13
14
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
15
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
16
import edu.ucsb.cs156.happiercows.entities.User;
17
import edu.ucsb.cs156.happiercows.entities.UserCommons;
18
import edu.ucsb.cs156.happiercows.entities.Commons;
19
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
20
import edu.ucsb.cs156.happiercows.errors.NoCowsException;
21
import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException;
22
23
import io.swagger.v3.oas.annotations.tags.Tag;
24
import io.swagger.v3.oas.annotations.Operation;
25
import io.swagger.v3.oas.annotations.Parameter;
26
27
import org.springframework.http.ResponseEntity;
28
import org.springframework.web.bind.annotation.PutMapping;
29
30
@Tag(name = "User Commons")
31
@RequestMapping("/api/usercommons")
32
@RestController
33
public class UserCommonsController extends ApiController {
34
35
  @Autowired
36
  private UserCommonsRepository userCommonsRepository;
37
38
  @Autowired
39
  private CommonsRepository commonsRepository;
40
41
  @Autowired
42
  ObjectMapper mapper;
43
44
  @Operation(summary = "Get a specific user commons (admin only)")
45
  @PreAuthorize("hasRole('ROLE_ADMIN')")
46
  @GetMapping("")
47
  public UserCommons getUserCommonsById(
48
      @Parameter(name="userId") @RequestParam Long userId,
49
      @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException {
50
51
    UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
52
        .orElseThrow(
53 1 1. lambda$getUserCommonsById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$0 → KILLED
            () -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
54 1 1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED
    return userCommons;
55
  }
56
57
  @Operation(summary = "Get a user commons for current user")
58
  @PreAuthorize("hasRole('ROLE_USER')")
59
  @GetMapping("/forcurrentuser")
60
  public UserCommons getUserCommonsById(
61
      @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException {
62
63
    User u = getCurrentUser().getUser();
64
    Long userId = u.getId();
65
    UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
66
        .orElseThrow(
67 1 1. lambda$getUserCommonsById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$1 → KILLED
            () -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
68 1 1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED
    return userCommons;
69
  }
70
71
  @Operation(summary = "Buy a cow, totalWealth updated")
72
  @PreAuthorize("hasRole('ROLE_USER')")
73
  @PutMapping("/buy")
74
  public ResponseEntity<String> putUserCommonsByIdBuy(
75
          @Parameter(name="commonsId") @RequestParam Long commonsId,
76
          @Parameter(name="numCows") @RequestParam int numCows) throws NotEnoughMoneyException, JsonProcessingException{
77
78
        User u = getCurrentUser().getUser();
79
        Long userId = u.getId();
80
81
        Commons commons = commonsRepository.findById(commonsId).orElseThrow( 
82 1 1. lambda$putUserCommonsByIdBuy$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$2 → KILLED
          ()->new EntityNotFoundException(Commons.class, commonsId));
83
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
84
        .orElseThrow(
85 1 1. lambda$putUserCommonsByIdBuy$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$3 → KILLED
            () -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
86
87 3 1. putUserCommonsByIdBuy : changed conditional boundary → KILLED
2. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED
3. putUserCommonsByIdBuy : negated conditional → KILLED
        if(userCommons.getTotalWealth() >= (commons.getCowPrice() * numCows)){
88 3 1. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED
2. putUserCommonsByIdBuy : Replaced double subtraction with addition → KILLED
3. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED
          userCommons.setTotalWealth(userCommons.getTotalWealth() - (commons.getCowPrice() * numCows));
89 2 1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED
2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
          userCommons.setNumOfCows(userCommons.getNumOfCows() + numCows);
90 2 1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED
2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsBought → KILLED
          userCommons.setCowsBought(userCommons.getCowsBought() + numCows);
91
        }
92
        else{
93
          throw new NotEnoughMoneyException("You need more money!");
94
        }
95
        userCommonsRepository.save(userCommons);
96
97
        String body = mapper.writeValueAsString(userCommons);
98 1 1. putUserCommonsByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED
        return ResponseEntity.ok().body(body);
99
    }
100
101
  @Operation(summary = "Sell a cow, totalWealth updated")
102
  @PreAuthorize("hasRole('ROLE_USER')")
103
  @PutMapping("/sell")
104
  public ResponseEntity<String> putUserCommonsByIdSell(
105
          @Parameter(name="commonsId") @RequestParam Long commonsId,
106
          @Parameter(name="numCows") @RequestParam int numCows) throws NoCowsException, JsonProcessingException {
107
        User u = getCurrentUser().getUser();
108
        Long userId = u.getId();
109
110
        Commons commons = commonsRepository.findById(commonsId).orElseThrow( 
111 1 1. lambda$putUserCommonsByIdSell$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$4 → KILLED
          ()->new EntityNotFoundException(Commons.class, commonsId));
112
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
113
        .orElseThrow(
114 1 1. lambda$putUserCommonsByIdSell$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$5 → KILLED
            () -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
115
116
117 2 1. putUserCommonsByIdSell : changed conditional boundary → KILLED
2. putUserCommonsByIdSell : negated conditional → KILLED
        if(userCommons.getNumOfCows() >= numCows ){
118 2 1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED
2. putUserCommonsByIdSell : Replaced double division with multiplication → KILLED
          double cowValue = commons.getCowPrice() * userCommons.getCowHealth() / 100;
119 3 1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED
2. putUserCommonsByIdSell : Replaced double addition with subtraction → KILLED
3. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED
          userCommons.setTotalWealth(userCommons.getTotalWealth() + (cowValue * numCows));
120 2 1. putUserCommonsByIdSell : Replaced integer subtraction with addition → KILLED
2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED
          userCommons.setNumOfCows(userCommons.getNumOfCows() - numCows);
121 2 1. putUserCommonsByIdSell : Replaced integer addition with subtraction → KILLED
2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsSold → KILLED
          userCommons.setCowsSold(userCommons.getCowsSold() + numCows);
122
        }
123
        else{
124
          throw new NoCowsException("You do not have enough cows to sell!");
125
        }
126
        userCommonsRepository.save(userCommons);
127
128
        String body = mapper.writeValueAsString(userCommons);
129 1 1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED
        return ResponseEntity.ok().body(body);
130
    }
131
132
    
133
134
    @Operation(summary = "Get all user commons for a specific commons")
135
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
136
    @GetMapping("/commons/all")
137
    public  ResponseEntity<String> getUsersCommonsByCommonsId(
138
        @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException {
139
      Iterable<UserCommons> uc = userCommonsRepository.findByCommonsId(commonsId);
140
      
141
   
142
    String body = mapper.writeValueAsString(uc);
143 1 1. getUsersCommonsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED
    return ResponseEntity.ok().body(body);
144
  }
145
146
}

Mutations

53

1.1
Location : lambda$getUserCommonsById$0
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_getUserCommonsById_nonexists_admin()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$0 → KILLED

54

1.1
Location : getUserCommonsById
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_getUserCommonsById_exists_admin()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED

67

1.1
Location : lambda$getUserCommonsById$1
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_getUserCommonsById_nonexists()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$1 → KILLED

68

1.1
Location : getUserCommonsById
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_getUserCommonsById_exists()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED

82

1.1
Location : lambda$putUserCommonsByIdBuy$2
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_buyCow_commons_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$2 → KILLED

85

1.1
Location : lambda$putUserCommonsByIdBuy$3
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_buyCow_for_user_not_in_commons()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$3 → KILLED

87

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
changed conditional boundary → KILLED

2.2
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_not_enough_money()]
Replaced double multiplication with division → KILLED

3.3
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_not_enough_money()]
negated conditional → KILLED

88

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists()]
Replaced double multiplication with division → KILLED

2.2
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
Replaced double subtraction with addition → KILLED

3.3
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED

89

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
Replaced integer addition with subtraction → KILLED

2.2
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED

90

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
Replaced integer addition with subtraction → KILLED

2.2
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsBought → KILLED

98

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyCow_commons_exists_user_has_exact_amount_needed()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED

111

1.1
Location : lambda$putUserCommonsByIdSell$4
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_sellCow_commons_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$4 → KILLED

114

1.1
Location : lambda$putUserCommonsByIdSell$5
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_sellCow_for_user_not_in_commons()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$5 → KILLED

117

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
changed conditional boundary → KILLED

2.2
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists_no_cow_to_sell()]
negated conditional → KILLED

118

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced double multiplication with division → KILLED

2.2
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced double division with multiplication → KILLED

119

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced double multiplication with division → KILLED

2.2
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced double addition with subtraction → KILLED

3.3
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED

120

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced integer subtraction with addition → KILLED

2.2
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED

121

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
Replaced integer addition with subtraction → KILLED

2.2
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsSold → KILLED

129

1.1
Location : putUserCommonsByIdSell
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_SellCow_commons_exists()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED

143

1.1
Location : getUsersCommonsByCommonsId
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_getAllUserCommonsById_exists()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3