Skip to content

Commit

Permalink
code review comments and test improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-lippert committed Nov 20, 2023
1 parent 5fc19ca commit cec9057
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public interface BuildPlanRepository extends JpaRepository<BuildPlan, Long> {
""")
Optional<BuildPlan> findByProgrammingExercises_IdWithProgrammingExercises(@Param("exerciseId") long exerciseId);

@Query("""
SELECT buildPlan
FROM BuildPlan buildPlan
JOIN buildPlan.programmingExercises programmingExercises
WHERE programmingExercises.id = :exerciseId
""")
Optional<BuildPlan> findByProgrammingExercises_Id(long exerciseId);

Check warning on line 33 in src/main/java/de/tum/in/www1/artemis/repository/BuildPlanRepository.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/repository/BuildPlanRepository.java#L33

[New] In this file 5 interface comments are missing. Consider adding explanatory comments or restricting the visibility. View in Teamscale: Line 16: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=3F125A21B421303140918D76DB7D9C70 Line 24: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=A5F325B126160D97605D46F8C12FEA9F Line 33: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=F675B276DA5D5254E1951ACAE0B0EC4F Line 35: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=1042BB037E1E05E1F330D7657586FCD2 Line 41: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=0CBCC7454A53CBA2C35FCFA340016EE5

Check warning on line 33 in src/main/java/de/tum/in/www1/artemis/repository/BuildPlanRepository.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/repository/BuildPlanRepository.java#L33

[New] Method `findByProgrammingExercises_Id` violates naming convention. Should be one of `[a-z][a-zA-Z0-9]*` https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=901024BFBC661B0CCE934F21506A4BAE

default BuildPlan findByProgrammingExercises_IdWithProgrammingExercisesElseThrow(final long exerciseId) {
return findByProgrammingExercises_IdWithProgrammingExercises(exerciseId)
.orElseThrow(() -> new EntityNotFoundException("Could not find a build plan for exercise " + exerciseId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

Check warning on line 14 in src/main/java/de/tum/in/www1/artemis/service/export/ProgrammingExerciseExportService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/export/ProgrammingExerciseExportService.java#L14

[New] Star import of `java.util.*` should not be used https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=D49C666A8FF0E304AA3CBED989594407
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
Expand All @@ -31,10 +27,7 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.*;

Check warning on line 30 in src/main/java/de/tum/in/www1/artemis/service/export/ProgrammingExerciseExportService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/export/ProgrammingExerciseExportService.java#L30

[New] Star import of `javax.xml.xpath.*` should not be used https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=enhancement%2Fexport-import-buildplan%3AHEAD&id=60D6063D0AF46F06EF4D0BA1F4C8BDAF

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand All @@ -48,8 +41,6 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.common.base.Charsets;

import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.domain.enumeration.ProgrammingLanguage;
import de.tum.in.www1.artemis.domain.enumeration.RepositoryType;
Expand Down Expand Up @@ -149,10 +140,10 @@ private Path exportProgrammingExerciseMaterialWithStudentReposOptional(Programmi
});

// Export the build plan of a programming exercise, if one exists. Only relevant for Gitlab/Jenkins or Gitlab/GitlabCI setups.
var buildPlan = buildPlanRepository.findByProgrammingExercises_IdWithProgrammingExercises(exercise.getId());
var buildPlan = buildPlanRepository.findByProgrammingExercises_Id(exercise.getId());
if (buildPlan.isPresent()) {
Path buildPlanPath = exportDir.orElseThrow().resolve(BUILD_PLAN_FILE_NAME);
FileUtils.writeStringToFile(buildPlanPath.toFile(), buildPlan.orElseThrow().getBuildPlan(), Charsets.UTF_8);
FileUtils.writeStringToFile(buildPlanPath.toFile(), buildPlan.orElseThrow().getBuildPlan(), StandardCharsets.UTF_8);
pathsToBeZipped.add(buildPlanPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -22,20 +23,18 @@

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;

import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.domain.enumeration.RepositoryType;
import de.tum.in.www1.artemis.repository.BuildPlanRepository;
import de.tum.in.www1.artemis.service.*;
import de.tum.in.www1.artemis.service.connectors.GitService;
import de.tum.in.www1.artemis.service.export.ProgrammingExerciseExportService;
import de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException;

@Service
public class ProgrammingExerciseImportFromFileService {

private final Logger log = LoggerFactory.getLogger(ProgrammingExerciseExportService.class);
private final Logger log = LoggerFactory.getLogger(ProgrammingExerciseImportFromFileService.class);

private final ProgrammingExerciseService programmingExerciseService;

Expand Down Expand Up @@ -126,7 +125,7 @@ private void importBuildPlanIfExisting(ProgrammingExercise programmingExercise,
Path buildPlanPath = importExerciseDir.resolve(BUILD_PLAN_FILE_NAME);
if (Files.exists(buildPlanPath)) {
try {
buildPlanRepository.setBuildPlanForExercise(FileUtils.readFileToString(buildPlanPath.toFile(), Charsets.UTF_8), programmingExercise);
buildPlanRepository.setBuildPlanForExercise(FileUtils.readFileToString(buildPlanPath.toFile(), StandardCharsets.UTF_8), programmingExercise);
}
catch (IOException e) {
log.warn("Could not read build plan file. Continue importing the exercise but skipping the build plan.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void exportInstructorRepositories_forbidden() throws Exception {
@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void exportProgrammingExerciseInstructorMaterial() throws Exception {
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFile(true);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFileWithoutBuildplan(true);
// we have a working directory and one directory for each repository
verify(fileService, times(4)).scheduleDirectoryPathForRecursiveDeletion(any(Path.class), eq(5L));
verify(fileService).schedulePathForDeletion(any(Path.class), eq(5L));
Expand Down Expand Up @@ -435,29 +435,29 @@ void testArchiveCourseWithProgrammingExercise() throws Exception {
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testExportProgrammingExerciseInstructorMaterial_failToCreateZip() throws Exception {
doThrow(IOException.class).when(zipFileService).createZipFile(any(Path.class), any());
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, true, true);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, true, true, false);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testExportProgrammingExerciseInstructorMaterial_failToCreateTempDir() throws Exception {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.createTempDirectory(any(Path.class), any(String.class))).thenThrow(IOException.class);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, false, false);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, false, false, false);
}
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testExportProgrammingExerciseInstructorMaterial_embeddedFilesDontExist() throws Exception {
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFile(false);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFileWithoutBuildplan(false);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testExportProgrammingExerciseInstructorMaterial_failToExportRepository() throws Exception {
doThrow(GitException.class).when(fileService).getTemporaryUniquePathWithoutPathCreation(any(Path.class), anyLong());
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, false, true, true);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, false, true, true, false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void exportInstructorRepositories_forbidden() throws Exception {
@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void exportProgrammingExerciseInstructorMaterial() throws Exception {
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFile(true);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFileWithBuildplan();
// we have a working directory and one directory for each repository
verify(fileService, times(4)).scheduleDirectoryPathForRecursiveDeletion(any(Path.class), eq(5L));
verify(fileService).schedulePathForDeletion(any(Path.class), eq(5L));
Expand All @@ -427,14 +427,14 @@ void exportProgrammingExerciseInstructorMaterial_problemStatementShouldContainTe
void testExportProgrammingExerciseInstructorMaterial_failToCreateTempDir() throws Exception {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.createTempDirectory(any(Path.class), any(String.class))).thenThrow(IOException.class);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, false, false);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial(HttpStatus.INTERNAL_SERVER_ERROR, true, false, false, false);
}
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testExportProgrammingExerciseInstructorMaterial_embeddedFilesDontExist() throws Exception {
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFile(false);
programmingExerciseTestService.exportProgrammingExerciseInstructorMaterial_shouldReturnFile(false, false);
}

@Test
Expand Down
Loading

0 comments on commit cec9057

Please sign in to comment.