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

adde test for shopping list, and deleted getallbyid

parent b44076aa
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
* This interface defines the methods for the shopping list repository
*
* @author Stian Lyng
* @version 1.1
* @version 1.2
* @since 19.04.2023
*
*/
public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long> {
......@@ -22,13 +23,6 @@ public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long
* @return an optional containing the shopping list if it exists
*/
Optional<ShoppingList> getByGroupID(long id);
/**
* Gets all shopping lists by group ID
*
* @param id the ID of the group
* @return an optional containing a list of all shopping lists in the group
*/
List<ShoppingList> getAllByGroupID(long id);
}
......@@ -61,16 +61,7 @@ public class ShoppingListService {
public List<ShoppingList> getAllShoppingLists() {
return shoppingListRepository.findAll();
}
/**
* Gets all shopping lists by group ID
*
* @param id the ID of the group
* @return an optional containing a list of all shopping lists if they exist
*/
public List<ShoppingList> getAllShoppingListsByGroupId(long id) {
return shoppingListRepository.getAllByGroupID(id);
}
/**
* Deletes a shopping list by its ID
......
package ntnu.idatt2016.v233.SmartMat.repository;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
@DataJpaTest
public class ShoppingListRepositoryTest {
@Autowired
private ShoppingListRepository shoppingListRepository;
@Test
public void testGetByGroupID() {
ShoppingList shoppingList = ShoppingList.builder()
.groupID(1L)
.build();
shoppingListRepository.save(shoppingList);
Optional<ShoppingList> shoppingListOptional = shoppingListRepository.getByGroupID(1L);
assertTrue(shoppingListOptional.isPresent());
ShoppingList tempshoppingList = shoppingListOptional.get();
assertEquals(1L, tempshoppingList.getGroupID());
}
@Test
public void testSaveShoppingList() {
ShoppingList shoppingList = ShoppingList.builder()
.groupID(1L)
.build();
shoppingListRepository.save(shoppingList);
List<ShoppingList> shoppingLists = shoppingListRepository.findAll();
assertEquals(1, shoppingLists.size());
}
@Test
public void testDeleteShoppingList() {
ShoppingList shoppingList = ShoppingList.builder()
.groupID(1L)
.build();
shoppingListRepository.save(shoppingList);
shoppingListRepository.deleteById(shoppingList.getShoppingListID());
List<ShoppingList> shoppingLists = shoppingListRepository.findAll();
assertEquals(0, shoppingLists.size());
}
@Test
public void testFindAll() {
ShoppingList shoppingList1 = ShoppingList.builder()
.groupID(1L)
.build();
ShoppingList shoppingList2 = ShoppingList.builder()
.groupID(2L)
.build();
shoppingListRepository.save(shoppingList1);
shoppingListRepository.save(shoppingList2);
List<ShoppingList> shoppingLists = shoppingListRepository.findAll();
assertEquals(2, shoppingLists.size());
}
}
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