From 5e0f5914e07288349133919ecd0501372b161fbc Mon Sep 17 00:00:00 2001
From: Michael Breu <Michael.Breu@arctis.at>
Date: Wed, 15 Nov 2023 17:36:54 +0100
Subject: [PATCH 1/3] More tests

---
 .../gitlab_events/GitlabEventService.java     |  3 +
 .../gitsearch/web/rest/GitlabResource.java    |  3 +-
 .../gitlab_events/GitlabEventServiceIT.java   |  4 +-
 .../gitsearch/web/rest/GitlabResourceIT.java  | 67 +++++++++++++++++++
 4 files changed, 73 insertions(+), 4 deletions(-)
 create mode 100644 src/test/java/at/ac/uibk/gitsearch/web/rest/GitlabResourceIT.java

diff --git a/src/main/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventService.java b/src/main/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventService.java
index ae56cd2dd..1290fe4e7 100644
--- a/src/main/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventService.java
+++ b/src/main/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventService.java
@@ -50,6 +50,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.codeability.sharing.plugins.api.search.SearchResultDTO;
 import org.codeability.sharing.plugins.api.search.SearchResultDTO.GitProject;
 import org.codeability.sharing.plugins.api.search.SearchResultDTO.MetadataFile;
+import org.codeability.sharing.plugins.api.search.SearchResultDTO.PluginActionInfo;
 import org.codeability.sharing.plugins.api.search.UserProvidedMetadataDTO;
 import org.codeability.sharing.plugins.api.search.UserProvidedMetadataDTO.InteractivityTypeXXX;
 import org.codeability.sharing.plugins.api.search.UserProvidedMetadataDTO.PublicVisibility;
@@ -957,6 +958,8 @@ public class GitlabEventService {
             } else {
                 if ("repository_update".equals(gitLabEvent.getEventName())) {
                     logger.debug("Ignoring event {}", gitLabEvent.getEventName());
+                } else if ("user_add_to_group".equals(gitLabEvent.getEventName())) {
+                    logger.debug("Ignoring event {}", gitLabEvent.getEventName());
                 } else {
                     logger.error("Cannot handle event {}", gitLabEvent.getEventName());
                     throw new MethodNotFoundException(gitLabEvent.getEventName(), null);
diff --git a/src/main/java/at/ac/uibk/gitsearch/web/rest/GitlabResource.java b/src/main/java/at/ac/uibk/gitsearch/web/rest/GitlabResource.java
index d3823e3b1..bc270b218 100644
--- a/src/main/java/at/ac/uibk/gitsearch/web/rest/GitlabResource.java
+++ b/src/main/java/at/ac/uibk/gitsearch/web/rest/GitlabResource.java
@@ -28,7 +28,6 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/api/gitlab")
 public class GitlabResource {
 
-    @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(GitlabResource.class);
 
     private final GitlabService gitlabService;
@@ -85,7 +84,7 @@ public class GitlabResource {
     @PostMapping("/eventListener")
     public void gitEventListener(@Valid @RequestBody String gitLabEventString) throws JsonProcessingException {
         // there may be events that cannot be processed
-        if (gitLabEventString.contains("event_name")) {
+        if (!gitLabEventString.contains("event_name")) {
             log.info("\"event_name\" missing: Will not process {}", gitLabEventString);
             return;
         }
diff --git a/src/test/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventServiceIT.java b/src/test/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventServiceIT.java
index 837f92a0c..52409e4a0 100644
--- a/src/test/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventServiceIT.java
+++ b/src/test/java/at/ac/uibk/gitsearch/service/gitlab_events/GitlabEventServiceIT.java
@@ -55,7 +55,7 @@ import org.springframework.security.test.context.support.WithMockUser;
 
 @SpringBootTest(classes = GitsearchApp.class)
 @WithMockUser(value = "test", authorities = "sharing")
-class GitlabEventServiceIT {
+public class GitlabEventServiceIT {
 
     private static final String TEST_META_DATA = "testmetadata";
 
@@ -121,7 +121,7 @@ class GitlabEventServiceIT {
             );
     }
 
-    static Stream<String> allTestSystemHooks() throws IOException {
+    public static Stream<String> allTestSystemHooks() throws IOException {
         return getResourceFiles("testData/gitlabEvents").stream().map(a -> a);
     }
 
diff --git a/src/test/java/at/ac/uibk/gitsearch/web/rest/GitlabResourceIT.java b/src/test/java/at/ac/uibk/gitsearch/web/rest/GitlabResourceIT.java
new file mode 100644
index 000000000..8d3f56d8f
--- /dev/null
+++ b/src/test/java/at/ac/uibk/gitsearch/web/rest/GitlabResourceIT.java
@@ -0,0 +1,67 @@
+package at.ac.uibk.gitsearch.web.rest;
+
+import static org.junit.Assert.fail;
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import at.ac.uibk.gitsearch.GitsearchApp;
+import at.ac.uibk.gitsearch.service.gitlab_events.GitlabEventServiceIT;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.nio.charset.Charset;
+import java.util.stream.Stream;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Named;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.transaction.annotation.Transactional;
+
+/**
+ * Integration tests for the {@link LikesResource} REST controller.
+ */
+@SpringBootTest(classes = GitsearchApp.class)
+@ExtendWith(MockitoExtension.class)
+@AutoConfigureMockMvc
+//@WithMockUser(value = "user", authorities = { "sharing", AuthoritiesConstants.ADMIN })
+class GitlabResourceIT {
+
+    @Autowired
+    private MockMvc gitLabEventMockMvc;
+
+    public static Stream<Object> allTestSystemHooks() throws IOException {
+        return GitlabEventServiceIT.allTestSystemHooks().map(name -> Arguments.of(Named.of(name, getTestSystemHookEventAsString(name))));
+    }
+
+    private static String getTestSystemHookEventAsString(final String testEvent) {
+        InputStream collectionCommit = GitlabEventServiceIT.class.getResourceAsStream(testEvent);
+        StringWriter sw = new StringWriter();
+        try {
+            IOUtils.copy(collectionCommit, sw, Charset.forName("UTF-8"));
+        } catch (IOException e) {
+            fail("Cannot parse " + testEvent);
+        }
+        return sw.toString();
+    }
+
+    @ParameterizedTest
+    @MethodSource(value = "allTestSystemHooks")
+    @Transactional
+    void testVariousEvents(String systemHookEvent) throws Exception {
+        // Create the Likes
+        gitLabEventMockMvc
+            .perform(
+                post("/api/gitlab/eventListener").with(csrf().asHeader()).contentType(MediaType.APPLICATION_JSON).content(systemHookEvent)
+            )
+            .andExpect(status().isOk());
+    }
+}
-- 
GitLab


From 33519dc689ac3cc90f0608e2ec0ad91d943405fa Mon Sep 17 00:00:00 2001
From: Michael Breu <Michael.Breu@arctis.at>
Date: Thu, 14 Dec 2023 12:15:15 +0100
Subject: [PATCH 2/3] Fixing Permission Problem #455

---
 .../at/ac/uibk/gitsearch/service/FileService.java   |  4 +++-
 .../gitsearch/service/ZipRepackagingService.java    |  5 +----
 .../gitsearch/service/ZipRepackagingServiceIT.java  | 13 +++++++++----
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/main/java/at/ac/uibk/gitsearch/service/FileService.java b/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
index e8143f75c..c2219650b 100644
--- a/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
+++ b/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
@@ -205,6 +205,8 @@ public class FileService implements DisposableBean {
                 return null;
             }
 
+            String path = exerciseId.getPath();
+            if (path == null) path = "/";
             InputStream project = zipRepackagingService.rePackageGitLabProjectZip(
                 new ZipInputStream(
                     gitLabRepository
@@ -214,7 +216,7 @@ public class FileService implements DisposableBean {
                 ),
                 "from project " + exerciseId.getProjectId(),
                 filter,
-                exerciseId.getPath()
+                path
             );
 
             if (recursive == ExtractionDepth.JUST_PROJECT) {
diff --git a/src/main/java/at/ac/uibk/gitsearch/service/ZipRepackagingService.java b/src/main/java/at/ac/uibk/gitsearch/service/ZipRepackagingService.java
index 58f8f0be9..d06343210 100644
--- a/src/main/java/at/ac/uibk/gitsearch/service/ZipRepackagingService.java
+++ b/src/main/java/at/ac/uibk/gitsearch/service/ZipRepackagingService.java
@@ -246,10 +246,7 @@ public class ZipRepackagingService {
 
     @SuppressWarnings({ "PMD.CognitiveComplexity", "PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength" })
     protected static List<String> getApplicableFilters(final String exercisePathF, TreeNode<SearchResultDTO> currentTreeNode) {
-        if (exercisePathF == null) {
-            return new ArrayList<>();
-        }
-        final String exercisePath = normalizeAbsolutePath(exercisePathF);
+        final String exercisePath = exercisePathF == null ? "/" : normalizeAbsolutePath(exercisePathF);
         // String relativeToRootExercisePath =
         // path.substring(rootExercisePath.length())
 
diff --git a/src/test/java/at/ac/uibk/gitsearch/service/ZipRepackagingServiceIT.java b/src/test/java/at/ac/uibk/gitsearch/service/ZipRepackagingServiceIT.java
index 23c600777..756a43ee2 100644
--- a/src/test/java/at/ac/uibk/gitsearch/service/ZipRepackagingServiceIT.java
+++ b/src/test/java/at/ac/uibk/gitsearch/service/ZipRepackagingServiceIT.java
@@ -20,6 +20,8 @@ import org.junit.jupiter.api.Timeout;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -66,8 +68,10 @@ class ZipRepackagingServiceIT {
         testRepackagedZip.close();
     }
 
-    @Test
-    void testFilteredRepackage() throws IOException {
+    @ParameterizedTest(name = "#{index} - testFilteredRepackage with folderPath ={0}")
+    @NullSource
+    @ValueSource(strings = { "", "/" })
+    void testFilteredRepackage(String folderPath) throws IOException {
         final MockedSingletonTreeNodeForFiltering filterOut = new MockedSingletonTreeNodeForFiltering("solution", "tests");
 
         // first check whether this is good test data
@@ -89,7 +93,7 @@ class ZipRepackagingServiceIT {
             testZip,
             "from " + TEST_ZIP_LOCATION,
             Optional.of(filterOut),
-            ""
+            folderPath
         );
 
         final ZipInputStream testRepackagedZip = new ZipInputStream(testRepackagedZipIS);
@@ -237,6 +241,7 @@ class ZipRepackagingServiceIT {
 
     static Stream<Arguments> checkExplicitMethodSourceArgs() {
         return Stream.of(
+            Arguments.of(null, expectedEntriesAll),
             Arguments.of("", expectedEntriesAll),
             Arguments.of("SS21_Assignment1", expectedEntriesAssignment1),
             Arguments.of("SS21_Assignment1/SimplePrograms_HelloWorld", expectedEntriesHelloWorld)
@@ -278,7 +283,7 @@ class ZipRepackagingServiceIT {
             );
     }
 
-    @ParameterizedTest
+    @ParameterizedTest(name = "#{index} - testComplexFilteredRepackage with rootPath = {0}, expected = {1}")
     @Timeout(value = 10 * 60 * 1000L)
     @MethodSource("checkExplicitMethodSourceArgs")
     void testComplexFilteredRepackage(String rootPath, List<String> expected) throws IOException {
-- 
GitLab


From ede19351e87f6b097f49828be616e6b40daeaa4b Mon Sep 17 00:00:00 2001
From: Michael Breu <Michael.Breu@arctis.at>
Date: Thu, 14 Dec 2023 12:36:32 +0100
Subject: [PATCH 3/3] Fixing FileService

---
 src/main/java/at/ac/uibk/gitsearch/service/FileService.java | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/main/java/at/ac/uibk/gitsearch/service/FileService.java b/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
index c2219650b..61488fb16 100644
--- a/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
+++ b/src/main/java/at/ac/uibk/gitsearch/service/FileService.java
@@ -206,7 +206,9 @@ public class FileService implements DisposableBean {
             }
 
             String path = exerciseId.getPath();
-            if (path == null) path = "/";
+            if (path == null) {
+                path = "/";
+            }
             InputStream project = zipRepackagingService.rePackageGitLabProjectZip(
                 new ZipInputStream(
                     gitLabRepository
-- 
GitLab