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

added test for AllergyRepo

parent c4d1ae3a
No related branches found
No related tags found
No related merge requests found
......@@ -10,17 +10,9 @@ import org.springframework.data.jpa.repository.JpaRepository;
* Repository for allergies
*
* @author Stian Lyng
* @version 1.0
* @since 04.04.2023
* @version 1.2
* @since 19.04.2023
*/
public interface AllergyRepository extends JpaRepository<Allergy, String> {
/**
* Gets an allergy by name
*
* @param name the name of the allergy
* @return an optional containing the Allergy if it exists
*/
List<Allergy> findByName(String name);
}
package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.entity.Allergy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.List;
import java.util.Optional;
@DataJpaTest
@DisplayName("AllergyRepositoryTest")
class AllergyRepositoryTest {
@Autowired
private AllergyRepository allergyRepository;
@Test
@DisplayName("Test findByName")
void testFindByName() {
Allergy allergy = Allergy.builder()
.name("testName")
.description("testDescription")
.build();
allergyRepository.save(allergy);
Assertions.assertEquals(1, allergyRepository.findAll().size());
Optional<Allergy> allergies = allergyRepository.findById("testName");
Assertions.assertTrue(allergies.isPresent());
Assertions.assertEquals("testName", allergies.get().getName());
}
@Test
@DisplayName("Test save")
void testSave() {
Allergy allergy = Allergy.builder()
.name("testName")
.description("testDescription")
.build();
Allergy savedAllergy = allergyRepository.save(allergy);
Assertions.assertEquals(allergy, savedAllergy);
Assertions.assertEquals(1, allergyRepository.findAll().size());
}
@Test
@DisplayName("Test delete")
void testDelete() {
Allergy allergy = Allergy.builder()
.name("testName")
.description("testDescription")
.build();
allergyRepository.save(allergy);
allergyRepository.deleteById("testName");
Optional<Allergy> deletedAllergy = allergyRepository.findById("testName");
Assertions.assertTrue(deletedAllergy.isEmpty());
}
@Test
@DisplayName("Test findAll")
void testFindAll() {
// Arrange
Allergy allergy1 = Allergy.builder()
.name("testName")
.description("testDescription")
.build();
Allergy allergy2 = Allergy.builder()
.name("testName2")
.description("testDescription2")
.build();
allergyRepository.save(allergy1);
allergyRepository.save(allergy2);
// Act
List<Allergy> allergies = allergyRepository.findAll();
// Assert
Assertions.assertEquals(2, allergies.size());
Assertions.assertTrue(allergies.contains(allergy1));
Assertions.assertTrue(allergies.contains(allergy2));
}
}
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