Skip to content

Commit

Permalink
New test cases (#207)
Browse files Browse the repository at this point in the history
* Update ProblemTypeServiceTest.java

* Update LearningMethodControllerTest.java

* Update AlgorithmControllerTest.java

* improve parentProblemType test

Co-authored-by: salmma <marie.salm@iaas.uni-stuttgart.de>
  • Loading branch information
emymaria and salmma authored Jan 17, 2022
1 parent accdf50 commit a2d1685
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.data.domain.Sort.Direction.ASC;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
Expand All @@ -37,7 +40,9 @@
import org.planqk.atlas.core.model.ProblemType;
import org.planqk.atlas.core.util.AtlasDatabaseTestBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -50,6 +55,8 @@ public class ProblemTypeServiceTest extends AtlasDatabaseTestBase {
@Autowired
private AlgorithmService algorithmService;



@Test
void createProblemType() {
ProblemType problemType = getFullProblemType("problemTypeName");
Expand All @@ -67,12 +74,41 @@ void findAllProblemTypes() {
problemTypeService.create(problemType1);
ProblemType problemType2 = getFullProblemType("problemTypeName2");
problemTypeService.create(problemType2);

List<ProblemType> problemTypes = problemTypeService.findAll(Pageable.unpaged(), "").getContent();

assertThat(problemTypes.size()).isEqualTo(2);
}

@Test
void findAllSortedByParent(){

int page = 0;
int size = 10;
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(ASC,"parentProblemTypeName"));
Sort sort = Sort.by(orders);
Pageable pageable = PageRequest.of(page,size,sort);

ProblemType grandParentProblemType = problemTypeService.create(
getFullProblemType("aParentParentProblemTypeName")
);

ProblemType parentProblemType = getFullProblemType("bParentProblemTypeName");
parentProblemType.setParentProblemType(grandParentProblemType.getId());
parentProblemType = problemTypeService.create(parentProblemType);

ProblemType problemType = getFullProblemType("cProblemTypeName");
problemType.setParentProblemType(parentProblemType.getId());
problemType = problemTypeService.create(problemType);

List<ProblemType> problemTypes = problemTypeService.findAll(pageable, "").getContent();
assertThat(problemTypes.get(0).getId()).isEqualTo(grandParentProblemType.getId());
assertNull(problemTypes.get(0).getParentProblemType());
assertThat(problemTypes.get(1).getId()).isEqualTo(parentProblemType.getId());
assertThat(problemTypes.get(1).getParentProblemType()).isEqualTo(grandParentProblemType.getId());
assertThat(problemTypes.get(2).getId()).isEqualTo(problemType.getId());
assertThat(problemTypes.get(2).getParentProblemType()).isEqualTo(parentProblemType.getId());
}

@Test
void findProblemTypeById_ElementNotFound() {
assertThrows(NoSuchElementException.class, () -> problemTypeService.findById(UUID.randomUUID()));
Expand Down Expand Up @@ -195,7 +231,6 @@ private ProblemType getFullProblemType(String name) {
ProblemType problemType = new ProblemType();

problemType.setName(name);
problemType.setParentProblemType(UUID.randomUUID());

return problemType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import org.planqk.atlas.web.dtos.ComputeResourcePropertyTypeDto;
import org.planqk.atlas.web.dtos.DiscussionCommentDto;
import org.planqk.atlas.web.dtos.DiscussionTopicDto;
import org.planqk.atlas.web.dtos.LearningMethodDto;
import org.planqk.atlas.web.dtos.PatternRelationDto;
import org.planqk.atlas.web.dtos.PatternRelationTypeDto;
import org.planqk.atlas.web.dtos.ProblemTypeDto;
Expand Down Expand Up @@ -2379,4 +2380,39 @@ void deleteDiscussionCommentAndFail() {
// call
mockMvc.perform(delete(path)).andExpect(status().isNotFound());
}

@Test
public void getLearningMethodsOfAlgorithm_returnOk() throws Exception {
LearningMethod learningMethod = new LearningMethod();
learningMethod.setId(UUID.randomUUID());
learningMethod.setName("supervised");
when(algorithmService.getLearningMethodOfAlgorithm(any(), any())).thenReturn(learningMethod);

var url = linkBuilderService.urlStringTo(methodOn(AlgorithmController.class).
getLearningMethodOfAlgorithm(UUID.randomUUID(), learningMethod.getId()));
MvcResult mvcResult = mockMvc.perform(get(url).accept(MediaType.APPLICATION_JSON)).andReturn();

assertEquals(mvcResult.getResponse().getStatus(), 200);
}

@Test
public void linkAlgorithmAndLearningMethod_returnNoContent() throws Exception{
LearningMethod learningMethod = new LearningMethod();
learningMethod.setId(UUID.randomUUID());
learningMethod.setName("supervised");
LearningMethodDto learningMethodDto = ModelMapperUtils.convert(learningMethod,LearningMethodDto.class);
doNothing().when(linkingService).linkAlgorithmAndLearningMethod(any(),any());
var url = linkBuilderService.urlStringTo(methodOn(AlgorithmController.class).
linkAlgorithmAndLearningMethod(UUID.randomUUID(), learningMethodDto));
mockMvc.perform(post(url).accept(MediaType.APPLICATION_JSON).contentType(APPLICATION_JSON).content(mapper.writeValueAsString(learningMethodDto))).andExpect(status().isNoContent());
}


@Test
public void unlinkAlgorithmAndLearningMethod_returnNoContent() throws Exception{
doNothing().when(linkingService).unlinkAlgorithmAndLearningMethod(any(),any());
var url = linkBuilderService.urlStringTo(methodOn(AlgorithmController.class).
unlinkAlgorithmAndLearningMethod(UUID.randomUUID(), UUID.randomUUID()));
mockMvc.perform(delete(url).accept(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
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;

Expand Down Expand Up @@ -208,4 +211,33 @@ public void getLearningMethodsForAlgorithm_Empty() throws Exception {
getLearningMethodsOfAlgorithm(algorithm.getId(), new ListParameters(pageable, null)));
mockMvc.perform(get(url).accept(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}

@Test
public void updateLearningMethod_returnOk() throws Exception {
LearningMethod testLearningMethod = getTestLearningMethod();
when(learningMethodService.update(testLearningMethod)).thenReturn(testLearningMethod);
LearningMethodDto learningMethodDto = ModelMapperUtils.convert(testLearningMethod, LearningMethodDto.class);
var url = linkBuilderService.urlStringTo(methodOn(LearningMethodController.class)
.updateLearningMethod(testLearningMethod.getId(),learningMethodDto));
MvcResult result = mockMvc
.perform(put(url).content(mapper.writeValueAsString(learningMethodDto))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
LearningMethodDto response = ObjectMapperUtils.mapMvcResultToDto(result, LearningMethodDto.class);
assertEquals(response.getName(), testLearningMethod.getName());
assertEquals(response.getId(), testLearningMethod.getId());
}

@Test
public void deleteLearningMethod_returnNoContent() throws Exception {
LearningMethod testLearningMethod = getTestLearningMethod();
doNothing().when(learningMethodService).delete(testLearningMethod.getId());
LearningMethodDto learningMethodDto = ModelMapperUtils.convert(testLearningMethod, LearningMethodDto.class);
var url = linkBuilderService.urlStringTo(methodOn(LearningMethodController.class)
.deleteLearningMethod(testLearningMethod.getId()));
MvcResult result = mockMvc
.perform(delete(url).content(mapper.writeValueAsString(learningMethodDto))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent()).andReturn();
}
}

0 comments on commit a2d1685

Please sign in to comment.