Merge remote-tracking branch 'origin/22-integrate-api-and-frontend' into 45-finalize-details-page
This commit is contained in:
commit
20f3909249
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>
|
||||
|
@ -6,6 +6,7 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
||||
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@ -14,9 +15,9 @@ import java.util.UUID;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
@EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO)
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/datasets")
|
||||
@EnableSpringDataWebSupport
|
||||
public class DatasetController {
|
||||
@Autowired
|
||||
private DatasetService datasetService;
|
||||
|
@ -1,7 +1,6 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.sql.Date;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@ -17,32 +15,6 @@ public interface dataRepository extends JpaRepository<Dataset, UUID> {
|
||||
|
||||
Dataset getDatasetById(UUID id);
|
||||
|
||||
List<Dataset> findByTitle(String title);
|
||||
|
||||
List<Dataset> findByTitleLike(String title);
|
||||
|
||||
List<Dataset> findByAuthorLike(String author);
|
||||
|
||||
List<Dataset> findByType(Type type);
|
||||
|
||||
List<Dataset> findByAuthor(String author);
|
||||
|
||||
List<Dataset> findByAbstLike(String abst);
|
||||
|
||||
List<Dataset> findByDescriptionLike(String description);
|
||||
|
||||
List<Dataset> findByRaitingGreaterThan(float raiting);
|
||||
|
||||
List<Dataset> findByVotesGreaterThan(int votes);
|
||||
|
||||
List<Dataset> findByDateAfter(Date date);
|
||||
|
||||
List<Dataset> findByDateBefore(Date date);
|
||||
|
||||
List<Dataset> findByCategorie(Category categorie);
|
||||
|
||||
List<Dataset> findByDateBetween(Date date1, Date date2);
|
||||
|
||||
@SuppressWarnings("null")
|
||||
Page<Dataset> findAll(Pageable pageable);
|
||||
|
||||
|
@ -17,4 +17,4 @@ public interface CategoryRepository extends JpaRepository<Category, UUID>{
|
||||
@SuppressWarnings("null")
|
||||
Optional<Category> findById(UUID id);
|
||||
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -0,0 +1,308 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.Dataset;
|
||||
|
||||
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.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
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");
|
||||
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 = "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"));
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
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());
|
||||
|
||||
mockMvc.perform(get("/api/v1/datasets/search?search=" + 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,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());
|
||||
}
|
||||
}
|
@ -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