aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2020-06-29 19:35:00 +0200
committerDiederick de Vries <diederick@diederickdevries.net>2020-06-29 20:14:22 +0200
commitd9caa021af064207095798ae94eb5f52b93411b2 (patch)
tree9078c80e63465577da3707ef514d330f28a03be2
parent787029324d65ec9791d73d1431ceb28917fbba35 (diff)
downloadboodschapi-d9caa021af064207095798ae94eb5f52b93411b2.tar.gz
boodschapi-d9caa021af064207095798ae94eb5f52b93411b2.tar.bz2
boodschapi-d9caa021af064207095798ae94eb5f52b93411b2.zip
fix: when copied item's groceries are already on the list, don't copy them.
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java212
-rw-r--r--src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java35
2 files changed, 135 insertions, 112 deletions
diff --git a/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java b/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
index e26ce5d..9829338 100644
--- a/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
+++ b/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
@@ -22,6 +22,7 @@ import java.net.URISyntaxException;
22import java.util.ArrayList; 22import java.util.ArrayList;
23import java.util.List; 23import java.util.List;
24import java.util.Optional; 24import java.util.Optional;
25import java.util.function.Consumer;
25import net.diederickdevries.boodschapi.model.Grocery; 26import net.diederickdevries.boodschapi.model.Grocery;
26import net.diederickdevries.boodschapi.model.Item; 27import net.diederickdevries.boodschapi.model.Item;
27import net.diederickdevries.boodschapi.model.Shop; 28import net.diederickdevries.boodschapi.model.Shop;
@@ -48,118 +49,113 @@ import org.springframework.web.bind.annotation.RestController;
48 */ 49 */
49@RestController 50@RestController
50@RequestMapping("/item") 51@RequestMapping("/item")
51public class ItemController 52public class ItemController {
52{ 53
53 public static final String MSG_400_EXISTS = "% staat al in de lijst"; 54 public static final String MSG_400_EXISTS = "% staat al in de lijst";
54 public static final String MSG_404_GROCERY = "De boodschap met id % bestaat niet"; 55 public static final String MSG_404_GROCERY = "De boodschap met id % bestaat niet";
55 public static final String MSG_404_SHOP = "De winkel met id % bestaat niet"; 56 public static final String MSG_404_SHOP = "De winkel met id % bestaat niet";
56 public static final String MSG_404_ITEM = "Het item met id % bestaat niet"; 57 public static final String MSG_404_ITEM = "Het item met id % bestaat niet";
57 58
58 @Autowired private ItemRepository itemRepository; 59 @Autowired
59 @Autowired private ShopRepository shopRepository; 60 private ItemRepository itemRepository;
60 @Autowired private GroceryRepository groceryRepository; 61 @Autowired
61 @Autowired private IngredientRepository ingredientRepository; 62 private ShopRepository shopRepository;
62 63 @Autowired
64 private GroceryRepository groceryRepository;
65 @Autowired
66 private IngredientRepository ingredientRepository;
67
63 /** 68 /**
64 * Creates a new Item on the shopping list of a week. If the item represents an ingredient of a 69 * Creates a new Item on the shopping list of a week. If the item represents
65 * meal, its showalways flag is set to true. If the item already exists in the list and is already 70 * an ingredient of a meal, its showalways flag is set to true. If the item
66 * bought, it is set to not bought. If the existing item was not bought, a 400 Illegal Request is 71 * already exists in the list and is already bought, it is set to not
72 * bought. If the existing item was not bought, a 400 Illegal Request is
67 * returned. 73 * returned.
68 * 74 *
69 * @param groceryid The id of the grocery that the item is. 75 * @param groceryid The id of the grocery that the item is.
70 * @param shopid The id of the shop to add the item to. 76 * @param shopid The id of the shop to add the item to.
71 * @param weeknr The weeknr of the week in whose shopping list the item should appear. 77 * @param weeknr The weeknr of the week in whose shopping list the item
78 * should appear.
72 * @return The added item. 79 * @return The added item.
73 * @throws java.net.URISyntaxException 80 * @throws java.net.URISyntaxException
74 */ 81 */
75 @RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 82 @RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
76 public ResponseEntity<Object> add(final Integer groceryid, final Integer shopid, final String weeknr) throws URISyntaxException 83 public ResponseEntity<Object> add(final Integer groceryid, final Integer shopid, final String weeknr) throws URISyntaxException {
77 {
78 Optional<Grocery> optionalGrocery = groceryRepository.findById(groceryid); 84 Optional<Grocery> optionalGrocery = groceryRepository.findById(groceryid);
79 if (!optionalGrocery.isPresent()) 85 if (!optionalGrocery.isPresent()) {
80 {
81 return ResponseEntity 86 return ResponseEntity
82 .status(HttpStatus.NOT_FOUND) 87 .status(HttpStatus.NOT_FOUND)
83 .body(MessageUtil.parse(MSG_404_GROCERY, groceryid + "")); 88 .body(MessageUtil.parse(MSG_404_GROCERY, groceryid + ""));
84 } 89 }
85 final Grocery grocery = optionalGrocery.get(); 90 final Grocery grocery = optionalGrocery.get();
86 Optional<Shop> optionalShop = shopRepository.findById(shopid); 91 Optional<Shop> optionalShop = shopRepository.findById(shopid);
87 if (!optionalShop.isPresent()) 92 if (!optionalShop.isPresent()) {
88 {
89 return ResponseEntity 93 return ResponseEntity
90 .status(HttpStatus.NOT_FOUND) 94 .status(HttpStatus.NOT_FOUND)
91 .body(MessageUtil.parse(MSG_404_SHOP, groceryid + "")); 95 .body(MessageUtil.parse(MSG_404_SHOP, groceryid + ""));
92 } 96 }
93 final Shop shop = optionalShop.get(); 97 final Shop shop = optionalShop.get();
94 98
95 Item item = new Item(); 99 Item item = new Item();
96 item.setGrocery(grocery); 100 item.setGrocery(grocery);
97 item.setShop(shop); 101 item.setShop(shop);
98 item.setWeek(new Week(weeknr)); 102 item.setWeek(new Week(weeknr));
99 103
100 Item existing = itemRepository.findByWeekAndGrocery(weeknr, item.getGrocery()); 104 Item existing = itemRepository.findByWeekAndGrocery(weeknr, item.getGrocery());
101 if (null != existing) 105 if (null != existing) {
102 { 106 if (existing.isBought()) {
103 if (existing.isBought())
104 {
105 unBuy(existing); 107 unBuy(existing);
106 return ResponseEntity.ok(existing); 108 return ResponseEntity.ok(existing);
107 } 109 } else {
108 else 110 return ResponseEntity
109 { 111 .badRequest()
110 return ResponseEntity 112 .body(MessageUtil.parse(MSG_400_EXISTS, item.getGrocery().getName()));
111 .badRequest()
112 .body(MessageUtil.parse(MSG_400_EXISTS, item.getGrocery().getName()));
113 } 113 }
114 } 114 }
115 115
116 if (0 < ingredientRepository.findByGrocery(item.getGrocery()).size()) 116 if (0 < ingredientRepository.findByGrocery(item.getGrocery()).size()) {
117 {
118 item.getGrocery().setShowAlways(true); 117 item.getGrocery().setShowAlways(true);
119 groceryRepository.saveAndFlush(item.getGrocery()); 118 groceryRepository.saveAndFlush(item.getGrocery());
120 } 119 }
121 120
122 itemRepository.saveAndFlush(item); 121 itemRepository.saveAndFlush(item);
123 122
124 return ResponseEntity 123 return ResponseEntity
125 .created(RequestUtil.getResourceURI(item.getId())) 124 .created(RequestUtil.getResourceURI(item.getId()))
126 .body(item); 125 .body(item);
127 } 126 }
128 127
129 /** 128 /**
130 * Returns the Item with the id given. 129 * Returns the Item with the id given.
131 * 130 *
132 * @param id The id of the Item to get. 131 * @param id The id of the Item to get.
133 * @return 132 * @return
134 */ 133 */
135 @RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 134 @RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
136 public ResponseEntity<Object> get(@PathVariable final Integer id) 135 public ResponseEntity<Object> get(@PathVariable final Integer id) {
137 {
138 Optional<Item> optionalItem = itemRepository.findById(id); 136 Optional<Item> optionalItem = itemRepository.findById(id);
139 if (!optionalItem.isPresent()) 137 if (!optionalItem.isPresent()) {
140 {
141 return ResponseEntity 138 return ResponseEntity
142 .status(HttpStatus.NOT_FOUND) 139 .status(HttpStatus.NOT_FOUND)
143 .body(MessageUtil.parse(MSG_404_ITEM, id + "")); 140 .body(MessageUtil.parse(MSG_404_ITEM, id + ""));
144 } 141 }
145 return ResponseEntity.ok(optionalItem.get()); 142 return ResponseEntity.ok(optionalItem.get());
146 } 143 }
147 144
148 /** 145 /**
149 * Alters the name, amount and unit of the Item given in the shopping list. Expects request in 146 * Alters the name, amount and unit of the Item given in the shopping list.
150 * the form of MediaType.APPLICATION_FORM_URLENCODED_VALUE since that's what jQuery's 'editable' 147 * Expects request in the form of
151 * returns. 148 * MediaType.APPLICATION_FORM_URLENCODED_VALUE since that's what jQuery's
149 * 'editable' returns.
152 * 150 *
153 * @param id 151 * @param id
154 * @param value 152 * @param value
155 * @return 153 * @return
156 */ 154 */
157 @RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 155 @RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
158 public ResponseEntity<Object> edit(final Integer id, final String value) 156 public ResponseEntity<Object> edit(final Integer id, final String value) {
159 {
160 Optional<Item> optionalItem = itemRepository.findById(id); 157 Optional<Item> optionalItem = itemRepository.findById(id);
161 if (!optionalItem.isPresent()) 158 if (!optionalItem.isPresent()) {
162 {
163 return ResponseEntity 159 return ResponseEntity
164 .status(HttpStatus.NOT_FOUND) 160 .status(HttpStatus.NOT_FOUND)
165 .body(MessageUtil.parse(MSG_404_ITEM, id + "")); 161 .body(MessageUtil.parse(MSG_404_ITEM, id + ""));
@@ -169,65 +165,60 @@ public class ItemController
169 item.setAmount(changed.getAmount()); 165 item.setAmount(changed.getAmount());
170 item.getGrocery().setName(changed.getGrocery().getName()); 166 item.getGrocery().setName(changed.getGrocery().getName());
171 String unit = changed.getGrocery().getUnit(); 167 String unit = changed.getGrocery().getUnit();
172 if (null == unit || unit.isEmpty()) 168 if (null == unit || unit.isEmpty()) {
173 {
174 unit = Grocery.DEFAULT_UNIT; 169 unit = Grocery.DEFAULT_UNIT;
175 } 170 }
176 item.getGrocery().setUnit(unit); 171 item.getGrocery().setUnit(unit);
177 groceryRepository.saveAndFlush(item.getGrocery()); 172 groceryRepository.saveAndFlush(item.getGrocery());
178 itemRepository.saveAndFlush(item); 173 itemRepository.saveAndFlush(item);
179 174
180 return ResponseEntity.ok(item.getFormatted()); 175 return ResponseEntity.ok(item.getFormatted());
181 } 176 }
182 177
183 /** 178 /**
184 * Deletes an Item, which in effect removes a Grocery from a shopping list. 179 * Deletes an Item, which in effect removes a Grocery from a shopping list.
185 * 180 *
186 * @param id The id of the Item to remove. 181 * @param id The id of the Item to remove.
187 * @return The grocery represented by the item that was removed from the list. 182 * @return The grocery represented by the item that was removed from the
183 * list.
188 */ 184 */
189 @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") 185 @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
190 public ResponseEntity<Object> remove(@PathVariable final Integer id) 186 public ResponseEntity<Object> remove(@PathVariable final Integer id) {
191 {
192 Optional<Item> optionalItem = itemRepository.findById(id); 187 Optional<Item> optionalItem = itemRepository.findById(id);
193 if (!optionalItem.isPresent()) 188 if (!optionalItem.isPresent()) {
194 {
195 return ResponseEntity 189 return ResponseEntity
196 .status(HttpStatus.NOT_FOUND) 190 .status(HttpStatus.NOT_FOUND)
197 .body(MessageUtil.parse(MSG_404_ITEM, id + "")); 191 .body(MessageUtil.parse(MSG_404_ITEM, id + ""));
198 } 192 }
199 itemRepository.deleteById(id); 193 itemRepository.deleteById(id);
200 itemRepository.flush(); 194 itemRepository.flush();
201 195
202 Item item = optionalItem.get(); 196 Item item = optionalItem.get();
203 197
204 if (item.getGrocery().alwaysShown() || ingredientRepository.findByGrocery(item.getGrocery()).isEmpty()) { 198 if (item.getGrocery().alwaysShown() || ingredientRepository.findByGrocery(item.getGrocery()).isEmpty()) {
205 return ResponseEntity.ok(item.getGrocery()); 199 return ResponseEntity.ok(item.getGrocery());
206 } 200 }
207 return ResponseEntity.noContent().build(); 201 return ResponseEntity.noContent().build();
208 } 202 }
209 203
210 /** 204 /**
211 * Moves an Item on the shopping list from one Shop to another Shop. 205 * Moves an Item on the shopping list from one Shop to another Shop.
212 * 206 *
213 * @param itemid The id of the item to move. 207 * @param itemid The id of the item to move.
214 * @param shopid The id of the shop to move the item to. 208 * @param shopid The id of the shop to move the item to.
215 * @return The moved Item. 209 * @return The moved Item.
216 */ 210 */
217 @RequestMapping(value ="/move", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 211 @RequestMapping(value = "/move", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
218 public ResponseEntity<Object> move(final Integer itemid, final Integer shopid) 212 public ResponseEntity<Object> move(final Integer itemid, final Integer shopid) {
219 {
220 Optional<Item> optionalItem = itemRepository.findById(itemid); 213 Optional<Item> optionalItem = itemRepository.findById(itemid);
221 if (!optionalItem.isPresent()) 214 if (!optionalItem.isPresent()) {
222 {
223 return ResponseEntity 215 return ResponseEntity
224 .status(HttpStatus.NOT_FOUND) 216 .status(HttpStatus.NOT_FOUND)
225 .body(MessageUtil.parse(MSG_404_ITEM, itemid + "")); 217 .body(MessageUtil.parse(MSG_404_ITEM, itemid + ""));
226 } 218 }
227 final Item item = optionalItem.get(); 219 final Item item = optionalItem.get();
228 Optional<Shop> optionalShop = shopRepository.findById(shopid); 220 Optional<Shop> optionalShop = shopRepository.findById(shopid);
229 if (!optionalShop.isPresent()) 221 if (!optionalShop.isPresent()) {
230 {
231 return ResponseEntity 222 return ResponseEntity
232 .status(HttpStatus.NOT_FOUND) 223 .status(HttpStatus.NOT_FOUND)
233 .body(MessageUtil.parse(MSG_404_SHOP, itemid + "")); 224 .body(MessageUtil.parse(MSG_404_SHOP, itemid + ""));
@@ -235,86 +226,87 @@ public class ItemController
235 final Shop shop = optionalShop.get(); 226 final Shop shop = optionalShop.get();
236 item.setShop(shop); 227 item.setShop(shop);
237 itemRepository.saveAndFlush(item); 228 itemRepository.saveAndFlush(item);
238 229
239 item.getGrocery().setShop(shop); 230 item.getGrocery().setShop(shop);
240 groceryRepository.saveAndFlush(item.getGrocery()); 231 groceryRepository.saveAndFlush(item.getGrocery());
241 return ResponseEntity.ok(item); 232 return ResponseEntity.ok(item);
242 } 233 }
243 234
244 /** 235 /**
245 * Updates the shopping list item and the referred grocery to either indicate when the item was 236 * Updates the shopping list item and the referred grocery to either
246 * bought or, if a time was already set, removes that time to indicate that it wasn't bought. It 237 * indicate when the item was bought or, if a time was already set, removes
247 * should only appear on the shopping list when it is not bought and said list can order their 238 * that time to indicate that it wasn't bought. It should only appear on the
248 * items based on when they were bought in relation to other items. 239 * shopping list when it is not bought and said list can order their items
240 * based on when they were bought in relation to other items.
249 * 241 *
250 * @param time 242 * @param time
251 * @param itemid 243 * @param itemid
252 * @return true if the item was bought, false if it was unbought. 244 * @return true if the item was bought, false if it was unbought.
253 */ 245 */
254 @RequestMapping(value = "/buy", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 246 @RequestMapping(value = "/buy", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
255 public ResponseEntity<Object> buy(final Integer itemid, final Long time) 247 public ResponseEntity<Object> buy(final Integer itemid, final Long time) {
256 {
257 Optional<Item> optionalItem = itemRepository.findById(itemid); 248 Optional<Item> optionalItem = itemRepository.findById(itemid);
258 if (!optionalItem.isPresent()) 249 if (!optionalItem.isPresent()) {
259 {
260 return ResponseEntity 250 return ResponseEntity
261 .status(HttpStatus.NOT_FOUND) 251 .status(HttpStatus.NOT_FOUND)
262 .body(MessageUtil.parse(MSG_404_ITEM, itemid + "")); 252 .body(MessageUtil.parse(MSG_404_ITEM, itemid + ""));
263 } 253 }
264 final Item item = optionalItem.get(); 254 final Item item = optionalItem.get();
265 if (false == item.isBought()) 255 if (false == item.isBought()) {
266 {
267 buy(item, time); 256 buy(item, time);
268 } 257 } else {
269 else
270 {
271 unBuy(item); 258 unBuy(item);
272 } 259 }
273 return ResponseEntity.ok(item); 260 return ResponseEntity.ok(item);
274 } 261 }
275 262
276 private void buy(final Item item, final long bought) 263 private void buy(final Item item, final long bought) {
277 {
278 item.setBought(bought); 264 item.setBought(bought);
279 itemRepository.saveAndFlush(item); 265 itemRepository.saveAndFlush(item);
280 item.getGrocery().setBought(bought); 266 item.getGrocery().setBought(bought);
281 groceryRepository.saveAndFlush(item.getGrocery()); 267 groceryRepository.saveAndFlush(item.getGrocery());
282 } 268 }
283 269
284 private void unBuy(final Item item) 270 private void unBuy(final Item item) {
285 {
286 item.setBought(null); 271 item.setBought(null);
287 itemRepository.saveAndFlush(item); 272 itemRepository.saveAndFlush(item);
288 item.getGrocery().setBought(null); 273 item.getGrocery().setBought(null);
289 groceryRepository.saveAndFlush(item.getGrocery()); 274 groceryRepository.saveAndFlush(item.getGrocery());
290 } 275 }
291 276
292 /** 277 /**
293 * copyWeeklyBoughtItems will make a copy in the current list of any item 278 * copyWeeklyBoughtItems will make a copy in the current list of any item
294 * that was on the lists of the three last weeks. The copy will have the 279 * that was on the lists of the three last weeks but whose grocery was not
295 * amount of last week. 280 * already on this week's list. The copy will have the amount of last week.
296 * 281 *
282 * @param weeknr The week to copy the items to
297 * @return The copied items 283 * @return The copied items
298 */ 284 */
299 @RequestMapping(value = "/copyWeeklyBoughtItems", method = RequestMethod.GET, 285 @RequestMapping(value = "/copyWeeklyBoughtItemsTo/{weeknr}", method = RequestMethod.GET,
300 produces = "application/json;charset=UTF-8") 286 produces = "application/json;charset=UTF-8")
301 public ResponseEntity<Object> copyWeeklyBoughtItems() 287 public ResponseEntity<Object> copyWeeklyBoughtItemsTo(@PathVariable final String weeknr) {
302 { 288 final Week week = new Week(weeknr);
303 final Week week = new Week();
304 final Week lastWeek = week.getLastWeek(); 289 final Week lastWeek = week.getLastWeek();
305 final Week twoWeeksAgo = lastWeek.getLastWeek(); 290 final Week twoWeeksAgo = lastWeek.getLastWeek();
306 final Week threeWeeksAgo = twoWeeksAgo.getLastWeek(); 291 final Week threeWeeksAgo = twoWeeksAgo.getLastWeek();
307 292
308 final List<Item> weeklyBoughts = itemRepository.findByWeeks(lastWeek.toString(), 293 final List<Item> weeklyBoughts = itemRepository.findByWeeks(lastWeek.toString(),
309 twoWeeksAgo.toString(), threeWeeksAgo.toString()); 294 twoWeeksAgo.toString(), threeWeeksAgo.toString());
310 295
311 List<Item> copies = new ArrayList<>(); 296 List<Grocery> groceries = new ArrayList<>();
312 297 itemRepository.findList(week.toString()).forEach(item -> {
313 weeklyBoughts.forEach(item -> { 298 groceries.add(item.getGrocery());
314 copies.add(new Item(item.getGrocery(), item.getShop(), item.getAmount(), week));
315 }); 299 });
300
301 List<Item> copies = new ArrayList<>();
302 weeklyBoughts
303 .stream()
304 .filter(item -> !(groceries.contains(item.getGrocery())))
305 .forEach(item -> {
306 copies.add(new Item(item.getGrocery(), item.getShop(), item.getAmount(), week));
307 });
316 itemRepository.saveAll(copies); 308 itemRepository.saveAll(copies);
317 309
318 return ResponseEntity.ok(copies); 310 return ResponseEntity.ok(copies);
319 } 311 }
320} 312}
diff --git a/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java b/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
index 154b627..30dd1a6 100644
--- a/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
+++ b/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
@@ -118,7 +118,7 @@ public class ItemControllerIT
118 } 118 }
119 119
120 /** 120 /**
121 * Test that any item on each of the lists of the last three weeks is copied 121 * Test that the item on each of the lists of the last three weeks is copied
122 * to the current list. 122 * to the current list.
123 */ 123 */
124 @Test 124 @Test
@@ -139,11 +139,42 @@ public class ItemControllerIT
139 itemRepository.saveAndFlush(new Item(grocery, shop, 2, twoWeeksAgo)); 139 itemRepository.saveAndFlush(new Item(grocery, shop, 2, twoWeeksAgo));
140 itemRepository.saveAndFlush(new Item(grocery, shop, 2, threeWeeksAgo)); 140 itemRepository.saveAndFlush(new Item(grocery, shop, 2, threeWeeksAgo));
141 141
142 controller.copyWeeklyBoughtItems(); 142 controller.copyWeeklyBoughtItemsTo(week.toString());
143 143
144 Item found = itemRepository.findByWeekAndGrocery(week.toString(), grocery); 144 Item found = itemRepository.findByWeekAndGrocery(week.toString(), grocery);
145 assertEquals("The grocery should be on the lists of the three last weeks", 145 assertEquals("The grocery should be on the lists of the three last weeks",
146 grocery, found.getGrocery()); 146 grocery, found.getGrocery());
147 assertTrue("The amount should be of the last week's list", 1 == found.getAmount()); 147 assertTrue("The amount should be of the last week's list", 1 == found.getAmount());
148 } 148 }
149
150 /**
151 * Test that any item on each of the lists of the last three weeks is not
152 * copied to the current list since it already contains that item's grocery.
153 */
154 @Test
155 public void testCopyWeeklyBoughtItemsWithExistingGrocery()
156 {
157 final Shop shop = new Shop("shop");
158 final Grocery grocery = new Grocery("grocery",
159 categoryRepository.getOne(Category.NO_CATEGORY));
160 shopRepository.saveAndFlush(shop);
161 groceryRepository.saveAndFlush(grocery);
162
163 final Week week = new Week();
164 final Week lastWeek = week.getLastWeek();
165 final Week twoWeeksAgo = lastWeek.getLastWeek();
166 final Week threeWeeksAgo = twoWeeksAgo.getLastWeek();
167
168 // The one from last week (with amount 2) would be the one copied, so
169 // We'll give the already existing one amount 1.
170 itemRepository.saveAndFlush(new Item(grocery, shop, 1, week));
171 itemRepository.saveAndFlush(new Item(grocery, shop, 2, lastWeek));
172 itemRepository.saveAndFlush(new Item(grocery, shop, 2, twoWeeksAgo));
173 itemRepository.saveAndFlush(new Item(grocery, shop, 2, threeWeeksAgo));
174
175 controller.copyWeeklyBoughtItemsTo(week.toString());
176
177 Item found = itemRepository.findByWeekAndGrocery(week.toString(), grocery);
178 assertTrue("The amount should be of the last week's list", 1 == found.getAmount());
179 }
149} \ No newline at end of file 180} \ No newline at end of file