Skip to content
Snippets Groups Projects
Commit 623be50a authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added test for add / remove products from shoppinglist

parent d46aa377
No related branches found
No related tags found
No related merge requests found
......@@ -6,9 +6,12 @@ import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.service.ShoppingListService;
import ntnu.idatt2016.v233.SmartMat.service.group.GroupService;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import ntnu.idatt2016.v233.SmartMat.service.user.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -21,12 +24,13 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class ShoppingListControllerTest {
......@@ -40,6 +44,13 @@ public class ShoppingListControllerTest {
@Mock
private GroupService groupService;
@Mock
private ProductService productService;
@Mock
private UserService userService;
private ShoppingList shoppingList;
private final Authentication regularUser = new Authentication() {
......@@ -199,7 +210,7 @@ public class ShoppingListControllerTest {
}
@Test
public void getAllShoppingListsByGroupId_notFoundReg() {
void getAllShoppingListsByGroupId_notFoundReg() {
long groupId = 1;
when(shoppingListService.getShoppingListByGroupId(groupId)).thenReturn(Optional.empty());
when(groupService.isUserAssociatedWithGroup(regularUser.getName(), groupId)).thenReturn(true);
......@@ -209,4 +220,114 @@ public class ShoppingListControllerTest {
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void addToShoppinglist(){
long groupId = 1;
long shoppingListId = 1;
long ean = 100000000007L;
Product product = Product.builder()
.ean(ean)
.build();
User user = User.builder()
.username(regularUser.getName())
.build();
Group group = Group.builder()
.groupId(groupId)
.build();
UserGroupAsso userGroupAsso = UserGroupAsso.builder()
.id(new UserGroupId(user.getUsername(), group.getGroupId()))
.user(user)
.group(group)
.groupAuthority("USER")
.build();
group.addUser(userGroupAsso);
user.addGroup(userGroupAsso);
ShoppingList.ShoppingListBuilder builder = ShoppingList.builder();
builder.shoppingListID(shoppingListId);
builder.products(new ArrayList<>());
builder.group(group);
ShoppingList shoppingList = builder
.build();
when(shoppingListService.getShoppingListById(shoppingListId)).thenReturn(Optional.of(shoppingList));
when(productService.getProductById(ean)).thenReturn(Optional.of(product));
when(shoppingListService.isUserInShoppinglist(shoppingListId, regularUser.getName())).thenReturn(true);
when(shoppingListService.addProductToShoppingList(ean, shoppingListId))
.thenReturn(Optional.of(shoppingList));
when(userService.getUserFromUsername(regularUser.getName())).thenReturn(Optional.of(user));
ResponseEntity<?> response = shoppingListController.addItemToShoppingList(shoppingListId, String.valueOf(ean), regularUser);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(shoppingList.getProducts(), response.getBody());
}
@Test
void removeFromShoppinglist(){
long groupId = 1;
long shoppingListId = 1;
long ean = 100000000007L;
Product product = Product.builder()
.ean(ean)
.build();
User user = User.builder()
.username(regularUser.getName())
.build();
Group group = Group.builder()
.groupId(groupId)
.build();
UserGroupAsso userGroupAsso = UserGroupAsso.builder()
.id(new UserGroupId(user.getUsername(), group.getGroupId()))
.user(user)
.group(group)
.groupAuthority("USER")
.build();
group.addUser(userGroupAsso);
user.addGroup(userGroupAsso);
ShoppingList.ShoppingListBuilder builder = ShoppingList.builder();
builder.shoppingListID(shoppingListId);
builder.products(List.of(product));
builder.group(group);
ShoppingList shoppingList = builder
.build();
when(shoppingListService.getShoppingListById(shoppingListId)).thenReturn(Optional.of(shoppingList));
when(shoppingListService.isUserInShoppinglist(shoppingListId, regularUser.getName())).thenReturn(true);
when(shoppingListService.removeProductFromShoppingList(ean, shoppingListId))
.thenReturn(Optional.of(shoppingList));
ResponseEntity<?> response = shoppingListController.removeProductFromShoppingList(String.valueOf(shoppingListId),
String.valueOf(ean), regularUser);
System.out.println(response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(shoppingList, response.getBody());
verify(shoppingListService, times(1)).removeProductFromShoppingList(ean, shoppingListId);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment