feat: Add CategoryServiceTest and CategoryControllerTest
The code changes include adding new test classes CategoryServiceTest and CategoryControllerTest. These test classes are used to test the functionality of the CategoryService and CategoryController classes respectively. The CategoryServiceTest class contains tests for the addCategory, getAllCategories, and getCategoryById methods of the CategoryService class. The CategoryControllerTest class contains tests for the getAllCategories, fetchCategoryById, and createCategory methods of the CategoryController class. Recent user commits: - Merge branch '36-refactor-dataset-class' into '22-integrate-api-and-frontend' - Merge branch '22-integrate-api-and-frontend' into '36-refactor-dataset-class' - Refactor Dataset class - Merge branch '11-add-api-for-getting-home-page-data' into '22-integrate-api-and-frontend' - Merge branch '37-restructure-backend-according-to-seperation-by-concerns' into '11-add-api-for-getting-home-page-data' - refactor: finilase project structure - Merge branch '11-add-api-for-getting-home-page-data' into '22-integrate-api-and-frontend' - Merge branch '25-consider-moving-non-templated-html-into-static' into '11-add-api-for-getting-home-page-data' - refactor: move static sites into static, Remove unused PageController and update main.js for page navigation - fixed bugs: - suppressed display of search results if nothing was found but there were previous searches - improved display timing of search results/initial pages, which led to false displays
This commit is contained in:
parent
baa8349110
commit
bbf4f93279
8
pom.xml
8
pom.xml
@ -56,6 +56,14 @@
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>4.5.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,305 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.uni_passau.fim.PADAS.group3.DataDash.Dataset.DatasetController;
|
||||
import de.uni_passau.fim.PADAS.group3.DataDash.Dataset.DatasetService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(DatasetController.class)
|
||||
public class DatasetControllerTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private DatasetService datasetService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private Dataset d = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
@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
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/id/" + id.toString()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDatasetById_whenNotExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(datasetService.getDatasetById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/id/" + id))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDataset() throws Exception {
|
||||
Dataset dataset = d;
|
||||
given(datasetService.addDataset(any(Dataset.class))).willAnswer(invocation -> invocation.getArgument(0));
|
||||
mockMvc.perform(post("/api/v1/datasets")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(dataset)))
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDataset_whenExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(delete("/api/v1/datasets/id/" + id.toString()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDataset_whenNotExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(datasetService.getDatasetById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(delete("/api/v1/datasets/id/" + id))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void upvote_whenExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
doAnswer(invocation -> {
|
||||
dataset.upvote(); // Directly modify the dataset
|
||||
return null;
|
||||
}).when(datasetService).upvoteDataset(id);
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id.toString() + "/upvote"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(objectMapper.writeValueAsString(dataset)));
|
||||
|
||||
assertEquals(1, dataset.getUpvotes()); // Assuming getUpvotes() method exists and initial upvotes are 0
|
||||
}
|
||||
|
||||
@Test
|
||||
void upvote_whenNotExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(datasetService.getDatasetById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/upvote"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void downvote_whenExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
doAnswer(invocation -> {
|
||||
dataset.downvote(); // Directly modify the dataset
|
||||
return null;
|
||||
}).when(datasetService).downvoteDataset(id);
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id.toString() + "/downvote"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(objectMapper.writeValueAsString(dataset)));
|
||||
|
||||
assertEquals(-1, dataset.getUpvotes()); // Assuming getDownvotes() method exists and initial downvotes are 0
|
||||
}
|
||||
|
||||
@Test
|
||||
void downvote_whenNotExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(datasetService.getDatasetById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/downvote"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
doAnswer(invocation -> {
|
||||
dataset.vote(3); // Directly modify the dataset
|
||||
return null;
|
||||
}).when(datasetService).voteDataset(id, 3);
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id.toString() + "/stars?stars=3"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(objectMapper.writeValueAsString(dataset)));
|
||||
|
||||
assertEquals(3, dataset.getRaiting()); // Assuming getVotes() method exists and initial votes are 0
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenNotExists() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(datasetService.getDatasetById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=3"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenInvalidStars() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=7"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenInvalidStars2() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=0"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenInvalidStars3() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=-1"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenInvalidStars4() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=6"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postMethodName_whenInvalidStars5() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT");
|
||||
|
||||
given(datasetService.getDatasetById(id)).willReturn(dataset);
|
||||
|
||||
mockMvc.perform(put("/api/v1/datasets/id/" + id + "/stars?stars=100"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDatasets_whenExists() throws Exception {
|
||||
String keyword = "data";
|
||||
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));
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword))
|
||||
.andExpect(status().isOk());
|
||||
//TODO: Check output
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?seaarch=" + 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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + 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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + 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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + 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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + 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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?searach=" + keyword + "&size=0"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import de.uni_passau.fim.PADAS.group3.DataDash.category.Category;
|
||||
import de.uni_passau.fim.PADAS.group3.DataDash.category.CategoryRepository;
|
||||
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
class DatasetServiceTest {
|
||||
|
||||
@Mock
|
||||
private dataRepository datasetRepository;
|
||||
@Mock
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
@InjectMocks
|
||||
private DatasetService datasetService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDatasetById() { // Test for getDatasetById method
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.getDatasetById(id)).thenReturn(dataset);
|
||||
assertEquals(dataset, datasetService.getDatasetById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddDataset() { // Test for addDataset method
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.save(dataset)).thenReturn(dataset);
|
||||
assertEquals(dataset, datasetService.addDataset(dataset));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVoteDataset() { // Test for voteDataset method
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.getDatasetById(id)).thenReturn(dataset);
|
||||
datasetService.voteDataset(id, 5);
|
||||
verify(datasetRepository).save(dataset);
|
||||
assertEquals(1, dataset.getVotes());
|
||||
assertEquals(5, dataset.getRaiting());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteDataset() { // Test for deleteDataset method
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.getDatasetById(id)).thenReturn(dataset);
|
||||
datasetService.deleteDataset(id);
|
||||
verify(datasetRepository).delete(dataset);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpvoteDataset() { // Test for upvoteDataset method
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.getDatasetById(id)).thenReturn(dataset);
|
||||
datasetService.upvoteDataset(id);
|
||||
verify(datasetRepository).save(dataset);
|
||||
assertEquals(1, dataset.getUpvotes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDownvoteDataset() { // Test for downvoteDataset method
|
||||
UUID id = UUID.randomUUID();
|
||||
Dataset dataset = new Dataset();
|
||||
when(datasetRepository.getDatasetById(id)).thenReturn(dataset);
|
||||
datasetService.downvoteDataset(id);
|
||||
verify(datasetRepository).save(dataset);
|
||||
assertEquals(-1, dataset.getUpvotes());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testSearchByOptionalCriteria() { // Test for searchByOptionalCriteria method
|
||||
String search = "search";
|
||||
Type type = Type.DATASET;
|
||||
Pageable pageable = PageRequest.of(0, 10);
|
||||
List<Dataset> datasets = Arrays.asList(new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT"), new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT"));
|
||||
PageImpl<Dataset> s = new PageImpl<Dataset>(datasets);
|
||||
given(datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), Optional.ofNullable(type),pageable)).willReturn(s);
|
||||
assertEquals(s.getContent(), datasetService.searchByOptionalCriteria(search, "%",type.toString(), pageable).getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchByOptionalCriteriaWithCategory() { // Test for searchByOptionalCriteria method
|
||||
String search = "search";
|
||||
Type type = Type.DATASET;
|
||||
String category = UUID.randomUUID().toString();
|
||||
Category cat = new Category();
|
||||
Pageable pageable = PageRequest.of(0, 10);
|
||||
List<Dataset> datasets = Arrays.asList(new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT"), new Dataset("Title", "abst", "desc", "auth", null, null, Type.API, "MIT"));
|
||||
PageImpl<Dataset> s = new PageImpl<Dataset>(datasets);
|
||||
given(categoryRepository.getCategoryById(UUID.fromString(category))).willReturn(cat);
|
||||
given(datasetRepository.searchByOptionalCriteriaWithCategory(Optional.ofNullable(search), cat ,Optional.ofNullable(type),pageable)).willReturn(s);
|
||||
assertEquals(s.getContent(), datasetService.searchByOptionalCriteria(search, category,type.toString(), pageable).getContent());
|
||||
}
|
||||
|
||||
// Additional tests for other methods can be added following the same pattern.
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.category;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@WebMvcTest(CategoryController.class)
|
||||
public class CategoryControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
Category category = new Category("yeet");
|
||||
|
||||
@Test
|
||||
public void testGetAllCategories() throws Exception {
|
||||
CategoryDto category1 = CategoryDtoMapper.toDto(category);
|
||||
CategoryDto category2 = CategoryDtoMapper.toDto(category);
|
||||
given(categoryService.getAllCategories()).willReturn(Arrays.asList(category1, category2));
|
||||
|
||||
mockMvc.perform(get("/api/v1/categories"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(objectMapper.writeValueAsString(Arrays.asList(category1, category2))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchCategoryByIdFound() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
CategoryDto category = CategoryDtoMapper.toDto(this.category);
|
||||
given(categoryService.getCategoryById(id)).willReturn(category);
|
||||
|
||||
mockMvc.perform(get("/api/v1/categories/id/{id}", id))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(objectMapper.writeValueAsString(category)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchCategoryByIdNotFound() throws Exception {
|
||||
UUID id = UUID.randomUUID();
|
||||
given(categoryService.getCategoryById(id)).willReturn(null);
|
||||
|
||||
mockMvc.perform(get("/api/v1/categories/id/{id}", id))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateCategory() throws Exception {
|
||||
CategoryDto categoryDto = CategoryDtoMapper.toDto(category);
|
||||
mockMvc.perform(post("/api/v1/categories")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(categoryDto)))
|
||||
.andExpect(status().isCreated());
|
||||
// Verify that the service method was called once
|
||||
verify(categoryService).addCategory(any(CategoryDto.class));
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.category;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
public class CategoryServiceTest {
|
||||
|
||||
@Mock
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
@InjectMocks
|
||||
private CategoryService categoryService;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCategory() {
|
||||
CategoryDto categoryDto = new CategoryDto("Test Category", UUID.randomUUID());
|
||||
categoryService.addCategory(categoryDto);
|
||||
|
||||
verify(categoryRepository, times(1)).save(any(Category.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllCategories() {
|
||||
when(categoryRepository.findAll()).thenReturn(Arrays.asList(new Category("Category 1"), new Category("Category 2")));
|
||||
|
||||
List<CategoryDto> categories = categoryService.getAllCategories();
|
||||
|
||||
assertNotNull(categories);
|
||||
assertEquals(2, categories.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCategoryByIdFound() {
|
||||
UUID id = UUID.randomUUID();
|
||||
Category category = new Category("Test Category");
|
||||
when(categoryRepository.getCategoryById(id)).thenReturn(category);
|
||||
|
||||
CategoryDto result = categoryService.getCategoryById(id);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("Test Category", result.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCategoryByIdNotFound() {
|
||||
UUID id = UUID.randomUUID();
|
||||
when(categoryRepository.getCategoryById(id)).thenReturn(null);
|
||||
|
||||
CategoryDto result = categoryService.getCategoryById(id);
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user