feat: Add CategoryRepositoryTests and DataRepositoryTests
The code changes include adding new test classes CategoryRepositoryTests and DataRepositoryTests. These test classes are used to test the functionality of the CategoryRepository and DataRepository classes respectively. The CategoryRepositoryTests class contains tests for the findById, findByName, and findAll methods of the CategoryRepository class. The DataRepositoryTests class contains tests for the findByOptionalCriteria and getDatasetById methods of the DataRepository class.
This commit is contained in:
parent
46b11fd638
commit
7e08a507e4
@ -0,0 +1,80 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DataJpaTest
|
||||
public class DataRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
private dataRepository dataRepository;
|
||||
|
||||
@BeforeEach
|
||||
private void clearDatabase() {
|
||||
dataRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByOptionalCriteria() {
|
||||
// Prepare test data
|
||||
Dataset dataset = new Dataset("title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset);
|
||||
|
||||
// Execute the method under test
|
||||
Page<Dataset> result = dataRepository.findByOptionalCriteria(
|
||||
Optional.of("title"), Optional.empty(), Optional.empty(), Optional.empty(),
|
||||
Optional.empty(), Optional.empty(), Optional.empty(), PageRequest.of(0, 10));
|
||||
|
||||
// Assertions
|
||||
assertThat(result.getContent()).hasSize(1);
|
||||
assertThat(result.getContent().get(0).getTitle()).isEqualTo("title");
|
||||
}
|
||||
@Test
|
||||
public void searchByOptionalCriteria() {
|
||||
Dataset dataset1 = new Dataset("water", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset1);
|
||||
Dataset dataset = new Dataset("title1", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset);
|
||||
dataset = new Dataset("title1", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset);
|
||||
|
||||
Page<Dataset> result = dataRepository.searchByOptionalCriteria(
|
||||
Optional.of("water"), Optional.empty(), PageRequest.of(0, 10));
|
||||
|
||||
assertThat(result.getContent()).hasSize(1);
|
||||
assertThat(result.getContent().get(0).getTitle()).isEqualTo("water");
|
||||
}
|
||||
@Test
|
||||
public void searchByOptionalCriteriaMulti() {
|
||||
Dataset dataset1 = new Dataset("title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset1);
|
||||
Dataset dataset = new Dataset("title1", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset);
|
||||
dataset = new Dataset("title1", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataRepository.save(dataset);
|
||||
|
||||
Page<Dataset> result = dataRepository.searchByOptionalCriteria(
|
||||
Optional.of("title%"), Optional.empty(), PageRequest.of(0, 10));
|
||||
|
||||
assertThat(result.getContent()).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDatasetById() {
|
||||
Dataset dataset = new Dataset("title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
dataset = dataRepository.save(dataset);
|
||||
|
||||
Dataset result = dataRepository.getDatasetById(dataset.getId());
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getTitle()).isEqualTo("title");
|
||||
}
|
||||
}
|
@ -45,9 +45,7 @@ public class DatasetControllerTests {
|
||||
@Test
|
||||
void getDatasetById_whenExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT"); // Assuming a
|
||||
// constructor or
|
||||
// builder pattern
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/id/" + id.toString()))
|
||||
@ -233,68 +231,77 @@ public class DatasetControllerTests {
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenExists() throws Exception {
|
||||
String keyword = "data";
|
||||
String keyword = "title%";
|
||||
List<Dataset> datasets = Arrays.asList(
|
||||
new Dataset("Title 1", "abst", "data", "auth", null, null, Type.API, "MIT"),
|
||||
new Dataset("Title 2", "abst", "data", "auth", null, null, Type.API, "MIT"));
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(new PageImpl<>(datasets));
|
||||
new Dataset("Title 1", "abst", "data", "auth", null, null, Type.API, "MIT"));
|
||||
// new Dataset("Title 2", "abst", "data", "auth", null, null, Type.API, "MIT"));
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword))
|
||||
.andExpect(status().isOk());
|
||||
//TODO: Check output
|
||||
Page<Dataset> page = new PageImpl<>(datasets);
|
||||
given(datasetService.searchByOptionalCriteria(keyword, "%", "%",
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(page);
|
||||
String s = objectMapper.writeValueAsString(page);
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenNotExists() throws Exception {
|
||||
String keyword = "nonexistent";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?seaarch=" + keyword))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenInvalidSort() throws Exception {
|
||||
String keyword = "data";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&sort=invalid"))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword + "&sort=invalid"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenInvalidDirection() throws Exception {
|
||||
String keyword = "data";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&direction=invalid"))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword + "&direction=invalid"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenInvalidPage() throws Exception {
|
||||
String keyword = "data";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&page=-1"))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword + "&page=-1"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenInvalidSize() throws Exception {
|
||||
String keyword = "data";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&size=-1"))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword + "&size=-1"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenInvalidSize2() throws Exception {
|
||||
String keyword = "data";
|
||||
given(datasetService.searchByOptionalCriteria(keyword,null,null,PageRequest.of(0,20,Sort.by(Sort.Direction.DESC,"upvotes")))).willReturn(Page.empty());
|
||||
given(datasetService.searchByOptionalCriteria(keyword, null, null,
|
||||
PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "upvotes")))).willReturn(Page.empty());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&size=0"))
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + keyword + "&size=0"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,56 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.category;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DataJpaTest
|
||||
public class CategoryRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
private Category savedCategory;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
// Setup data for each test
|
||||
Category category = new Category();
|
||||
category.setName("Test Category");
|
||||
// Assuming Category class has a setName method and an ID that is auto-generated
|
||||
savedCategory = categoryRepository.save(category);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindById_thenReturnCategory() {
|
||||
Optional<Category> foundCategory = categoryRepository.findById(savedCategory.getId());
|
||||
assertThat(foundCategory).isPresent();
|
||||
assertThat(foundCategory.get().getId()).isEqualTo(savedCategory.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByName_thenReturnCategoryList() {
|
||||
List<Category> categories = categoryRepository.findByName("Test Category");
|
||||
assertThat(categories).hasSize(1);
|
||||
assertThat(categories.get(0).getName()).isEqualTo("Test Category");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindAll_thenReturnAllCategories() {
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
assertThat(categories).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetCategoryById_thenReturnCategory() {
|
||||
Category foundCategory = categoryRepository.getCategoryById(savedCategory.getId());
|
||||
assertThat(foundCategory).isNotNull();
|
||||
assertThat(foundCategory.getId()).isEqualTo(savedCategory.getId());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user