This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 7cce7bcb authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Intermediate commit, extending Tests

parent 6b78affd
2 merge requests!188Merging Peer Reviewing et. al to Master,!164211 peer reviewing functionality
......@@ -15,6 +15,7 @@ import java.util.zip.ZipInputStream;
import org.codeability.sharing.plugins.api.search.SearchInputDTO;
import org.codeability.sharing.plugins.api.search.SearchResultDTO;
import org.codeability.sharing.plugins.api.search.SearchResultsDTO;
import org.codeability.sharing.plugins.api.search.util.ExerciseId;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.slf4j.Logger;
......@@ -238,5 +239,15 @@ public class SearchService {
return file;
}
}
public Optional<SearchResultDTO> findExerciseById(ExerciseId exerciseId) {
try {
SearchResultDTO result = metaDataRepository.getExerciseById(exerciseId.toString());
return Optional.ofNullable(result);
} catch (javax.ws.rs.NotFoundException e) {
log.error("exercise with id {} not found?", exerciseId, e);
return Optional.empty();
}
}
}
......@@ -3,9 +3,11 @@ package at.ac.uibk.gitsearch.web.rest;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.codeability.sharing.plugins.api.search.SearchResultDTO;
import org.codeability.sharing.plugins.api.search.util.ExerciseId;
import org.gitlab4j.api.GitLabApiException;
import org.slf4j.Logger;
......@@ -24,6 +26,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import at.ac.uibk.gitsearch.service.GitlabService;
import at.ac.uibk.gitsearch.service.SearchService;
import at.ac.uibk.gitsearch.web.util.HeaderUtil;
/**
......@@ -39,16 +42,18 @@ public class ExerciseResource {
@Autowired
private GitlabService gitLabService;
@Autowired
private SearchService searchService;
@Value("${jhipster.clientApp.name}")
private String applicationName;
/**
* GET readMe : get problem statement or any other file.
* GET some file of an exercise.
*
* @param basketToken the basket
* @return the ResponseEntity with status 200 (OK) and with body the exercise, or with status 404 (Not Found)
* @param filePath the file path
* @return the ResponseEntity with status 200 (OK) and with body the exercise file, or with status 404 (Not Found)
*/
@GetMapping("/exerciseFile/**")
public ResponseEntity<Resource> getRepositoryFile(HttpServletRequest request, @RequestParam("filePath") String filePath) throws IOException {
......@@ -77,5 +82,35 @@ public class ExerciseResource {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).header("filename", filePath).body(resource);
}
/**
* GET the exercise by Id
*
* @param request the complete request, in order to parse the exerciseId
* @return the ResponseEntity with status 200 (OK) and with the searchresult, or with status 404 (Not Found)
*/
@GetMapping("/exercise/**")
public ResponseEntity<SearchResultDTO> getExerciseById(HttpServletRequest request) throws IOException {
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String exerciseId = new AntPathMatcher().extractPathWithinPattern(pattern, request.getRequestURI());
ExerciseId parsedId;
try {
parsedId = ExerciseId.fromString(exerciseId);
} catch (ParseException e) {
return ResponseEntity.notFound().headers(HeaderUtil.createFailureAlert(applicationName, true, "RepositoryFile", "not found",
"There was an error with your requested id " + exerciseId + ".")).build();
}
final Optional<SearchResultDTO> result = searchService.findExerciseById(parsedId);
if(result.isPresent()) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result.get());
} else {
return ResponseEntity.notFound().headers(HeaderUtil.createFailureAlert(applicationName, true, "RepositoryFile", "not found",
"There was an error finding your exercise " + exerciseId + ".")).build();
}
}
}
......@@ -150,7 +150,7 @@ public class SearchResource {
/**
* POST /programming-exercises/:exerciseId/export-programmingExercise : sends all repositories and details of the programming exercise as zip
* TODO projectId and ExerciseId are mixed up here. Please clarify!
* TODO move to Exercise Resource, and export is not only for programming-exercises!
*
* @param exerciseId the id of the exercise to get the repos from
* ResponseEntity with status
......
......@@ -3,6 +3,8 @@ package at.ac.uibk.gitsearch.web.rest;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
......@@ -121,8 +123,44 @@ public class ExerciseResourceIT {
}
@Test
public void getExerciseById() {
public void getExerciseById1() throws Exception {
String exerciseId = "1"; // warning: this depends on the sequence in the current search index :-(
restExerciseMockMvc.perform(get("/api/exercise/"+exerciseId)
.with(csrf().asHeader())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.project.project_id").value(exerciseId));
}
@Test
public void getExerciseBySubId() throws Exception {
String exerciseId = "272:solution/src/at"; // warning: this depends on the sequence in the current search index :-(
restExerciseMockMvc.perform(get("/api/exercise/"+exerciseId)
.with(csrf().asHeader())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.project.project_id").value("272"));
}
@Test
public void getExerciseByNotFound1() throws Exception {
String exerciseId = "272:solution/src/atX"; // warning: this depends on the sequence in the current search index :-(
restExerciseMockMvc.perform(get("/api/exercise/"+exerciseId)
.with(csrf().asHeader())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
public void getExerciseByNotFound2() throws Exception {
String exerciseId = "xxx272:solution/src/atX"; // warning: this depends on the sequence in the current search index :-(
restExerciseMockMvc.perform(get("/api/exercise/"+exerciseId)
.with(csrf().asHeader())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNotFound());
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment