Skip to content
Snippets Groups Projects
Commit 3c8ec65d authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge remote-tracking branch 'origin/main' into feature/finishWasteEndpoints

parents 9b906ee0 29ba1e3b
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.dto.response.AllergyResponse;
import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy;
import ntnu.idatt2016.v233.SmartMat.repository.AllergyRepository;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class AllergyServiceTest {
@InjectMocks
private AllergyService allergyService;
@Mock
private AllergyRepository allergyRepository;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testGetAllAllergies() {
Allergy allergy1 = new Allergy("Allergy1", "Description1", null, null);
Allergy allergy2 = new Allergy("Allergy2", "Description2", null, null);
when(allergyRepository.findAll()).thenReturn(Arrays.asList(allergy1, allergy2));
List<AllergyResponse> result = allergyService.getAllAllergies();
assertEquals(2, result.size());
assertEquals("Allergy1", result.get(0).getName());
assertEquals("Description1", result.get(0).getDescription());
assertEquals("Allergy2", result.get(1).getName());
assertEquals("Description2", result.get(1).getDescription());
}
@Test
void testGetAllergyByName() {
Allergy allergy = new Allergy("Allergy1", "Description1", null, null);
when(allergyRepository.findById("Allergy1")).thenReturn(Optional.of(allergy));
Optional<Allergy> result = allergyService.getAllergyByName("Allergy1");
assertTrue(result.isPresent());
assertEquals("Allergy1", result.get().getName());
assertEquals("Description1", result.get().getDescription());
}
@Test
void testSaveAllergy() {
Allergy allergy = new Allergy("Allergy1", "Description1", null, null);
allergyService.saveAllergy(allergy);
verify(allergyRepository, times(1)).save(allergy);
}
@Test
void testDeleteAllergyByName() {
allergyService.deleteAllergyByName("Allergy1");
verify(allergyRepository, times(1)).deleteById("Allergy1");
}
}
package ntnu.idatt2016.v233.SmartMat.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.dto.request.ShoppingListRequest;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class ShoppingListServiceTest {
@InjectMocks
private ShoppingListService shoppingListService;
@Mock
private ShoppingListRepository shoppingListRepository;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testCreateShoppingList() {
ShoppingListRequest shoppingListRequest = new ShoppingListRequest(1L);
ShoppingList shoppingList = new ShoppingList();
shoppingList.setGroupID(1L);
when(shoppingListRepository.save(any(ShoppingList.class))).thenReturn(shoppingList);
ShoppingList result = shoppingListService.createShoppingList(shoppingListRequest);
assertEquals(1L, result.getGroupID());
}
@Test
void testGetShoppingListById() {
ShoppingList shoppingList = new ShoppingList();
shoppingList.setShoppingListID(1L);
when(shoppingListRepository.findById(1L)).thenReturn(Optional.of(shoppingList));
Optional<ShoppingList> result = shoppingListService.getShoppingListById(1L);
assertTrue(result.isPresent());
assertEquals(1L, result.get().getShoppingListID());
}
@Test
void testGetShoppingListByGroupId() {
ShoppingList shoppingList = new ShoppingList();
shoppingList.setGroupID(1L);
when(shoppingListRepository.getByGroupID(1L)).thenReturn(Optional.of(shoppingList));
Optional<ShoppingList> result = shoppingListService.getShoppingListByGroupId(1L);
assertTrue(result.isPresent());
assertEquals(1L, result.get().getGroupID());
}
@Test
void testGetAllShoppingLists() {
ShoppingList shoppingList1 = new ShoppingList();
ShoppingList shoppingList2 = new ShoppingList();
when(shoppingListRepository.findAll()).thenReturn(Arrays.asList(shoppingList1, shoppingList2));
List<ShoppingList> result = shoppingListService.getAllShoppingLists();
assertEquals(2, result.size());
}
@Test
void testDeleteShoppingListById() {
ShoppingList shoppingList = new ShoppingList();
shoppingList.setShoppingListID(1L);
when(shoppingListRepository.findById(1L)).thenReturn(Optional.of(shoppingList));
shoppingListService.deleteShoppingListById(1L);
verify(shoppingListRepository, times(1)).deleteById(1L);
}
}
package ntnu.idatt2016.v233.SmartMat.service.user;
import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
import java.util.List;
import ntnu.idatt2016.v233.SmartMat.dto.response.WeeklyMenuResponse;
import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class WeeklyMenuServiceTest {
@InjectMocks
private WeeklyMenuService weeklyMenuService;
@Mock
private RecipeRepository recipeRepository;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testGetTop5RecipesWithProducts() {
long groupId = 1L;
Object[] row1 = new Object[] {1, "Recipe 1", 10L, "Product 1", "Description 1", true};
Object[] row2 = new Object[] {2, "Recipe 2", 15L, "Product 2", "Description 2", false};
List<Object[]> rawData = Arrays.asList(row1, row2);
when(recipeRepository.findTop5RecipesWithProductsRaw(groupId)).thenReturn(rawData);
List<WeeklyMenuResponse> result = weeklyMenuService.getTop5RecipesWithProducts(groupId);
assertEquals(2, result.size());
assertEquals("Recipe 1", result.get(0).getRecipeName());
assertEquals(10L, result.get(0).getEan());
assertEquals("Product 1", result.get(0).getItemName());
assertEquals("Recipe 2", result.get(1).getRecipeName());
assertEquals(15L, result.get(1).getEan());
assertEquals("Product 2", result.get(1).getItemName());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment