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.annotations.Api;
24
import io.swagger.annotations.ApiOperation;
25
import io.swagger.annotations.ApiParam;
26
27
import org.springframework.http.ResponseEntity;
28
import javax.validation.Valid;
29
import org.springframework.web.bind.annotation.RequestBody;
30
import org.springframework.web.bind.annotation.PutMapping;
31
import org.springframework.web.bind.annotation.PostMapping;
32
import org.springframework.web.bind.annotation.RequestBody;
33
34
@Api(description = "User Commons")
35
@RequestMapping("/api/usercommons")
36
@RestController
37
public class UserCommonsController extends ApiController {
38
39
  @Autowired
40
  private UserCommonsRepository userCommonsRepository;
41
42
  @Autowired
43
  private CommonsRepository commonsRepository;
44
45
  @Autowired
46
  ObjectMapper mapper;
47
48
  @ApiOperation(value = "Get a specific user commons (admin only)")
49
  @PreAuthorize("hasRole('ROLE_ADMIN')")
50
  @GetMapping("")
51
  public UserCommons getUserCommonsById(
52
      @ApiParam("userId") @RequestParam Long userId,
53
      @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException {
54
55
    UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
56
        .orElseThrow(
57 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));
58 1 1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED
    return userCommons;
59
  }
60
61
  @ApiOperation(value = "Get a user commons for current user")
62
  @PreAuthorize("hasRole('ROLE_USER')")
63
  @GetMapping("/forcurrentuser")
64
  public UserCommons getUserCommonsById(
65
      @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException {
66
67
    User u = getCurrentUser().getUser();
68
    Long userId = u.getId();
69
    UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
70
        .orElseThrow(
71 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));
72 1 1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED
    return userCommons;
73
  }
74
75
  @ApiOperation(value = "Buy cow(s), totalWealth updated")
76
  @PreAuthorize("hasRole('ROLE_USER')")
77
  @PutMapping("/buy")
78
  public ResponseEntity<String> putUserCommonsByIdBuy(
79
          @ApiParam("commonsId") @RequestParam Long commonsId, 
80
          @ApiParam("numOfCowsToBuy") @RequestParam int numOfCowsToBuy)
81
           throws NotEnoughMoneyException, JsonProcessingException{
82
        User u = getCurrentUser().getUser();
83
        Long userId = u.getId();
84 2 1. putUserCommonsByIdBuy : changed conditional boundary → KILLED
2. putUserCommonsByIdBuy : negated conditional → KILLED
          if (numOfCowsToBuy < 1) {
85
          throw new IllegalArgumentException("Number of cows to buy should be greater than or equal to 1");
86
}
87
        Commons commons = commonsRepository.findById(commonsId).orElseThrow( 
88 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));
89
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
90
        .orElseThrow(
91 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));
92
          
93 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()*numOfCowsToBuy ){
94 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()*numOfCowsToBuy);
95 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() +(numOfCowsToBuy));
96 2 1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED
2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setLifetimeCowsBought → KILLED
          userCommons.setLifetimeCowsBought(userCommons.getLifetimeCowsBought() + (numOfCowsToBuy));
97
        }
98
        else{
99
        throw new NotEnoughMoneyException("You need more money!");        }
100
        userCommonsRepository.save(userCommons);
101
102
        String body = mapper.writeValueAsString(userCommons);
103 1 1. putUserCommonsByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED
        return ResponseEntity.ok().body(body);
104
    }
105
106
  @ApiOperation(value = "Sell a cow, totalWealth updated")
107
  @PreAuthorize("hasRole('ROLE_USER')")
108
  @PutMapping("/sell")
109
  public ResponseEntity<String> putUserCommonsByIdSell(
110
          @ApiParam("commonsId") @RequestParam Long commonsId) throws NoCowsException, JsonProcessingException {
111
        User u = getCurrentUser().getUser();
112
        Long userId = u.getId();
113
114
        Commons commons = commonsRepository.findById(commonsId).orElseThrow( 
115 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));
116
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
117
        .orElseThrow(
118 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));
119
120
121 2 1. putUserCommonsByIdSell : changed conditional boundary → KILLED
2. putUserCommonsByIdSell : negated conditional → KILLED
        if(userCommons.getNumOfCows() >= 1 ){
122 2 1. putUserCommonsByIdSell : Replaced double addition with subtraction → KILLED
2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED
          userCommons.setTotalWealth(userCommons.getTotalWealth() + commons.getCowPrice());
123 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() - 1);
124 2 1. putUserCommonsByIdSell : Replaced integer addition with subtraction → KILLED
2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setLifetimeCowsSold → KILLED
          userCommons.setLifetimeCowsSold(userCommons.getLifetimeCowsSold() + 1);
125
        }
126
        else{
127
          throw new NoCowsException("You have no cows to sell!");
128
        }
129
        userCommonsRepository.save(userCommons);
130
131
        String body = mapper.writeValueAsString(userCommons);
132 1 1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED
        return ResponseEntity.ok().body(body);
133
    }
134
135
    @ApiOperation(value = "Get all user commons for a specific commons")
136
    @GetMapping("/commons/all")
137
    public  ResponseEntity<String> getUsersCommonsByCommonsId(
138
        @ApiParam("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

57

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

58

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

71

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

72

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

84

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

88

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

91

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_commons_for_user_DOES_NOT_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$3 → KILLED

93

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

94

1.1
Location : putUserCommonsByIdBuy
Killed by : edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UserCommonsControllerTests]/[method:test_BuyMultiCow_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

95

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

96

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::setLifetimeCowsBought → KILLED

103

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

115

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

118

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_commons_for_usercommons_DOES_NOT_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$5 → 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()]
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()]
negated conditional → KILLED

122

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 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::setTotalWealth → KILLED

123

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

124

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::setLifetimeCowsSold → KILLED

132

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