From dfb5640631be53073b0e89828fd8b21f14005918 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Fri, 7 Mar 2025 10:49:38 +0100
Subject: [PATCH] clean tests

---
 .../api/plugin/NewPluginControllerTest.java   | 63 +++++++++++--------
 1 file changed, 36 insertions(+), 27 deletions(-)

diff --git a/web/src/test/java/lcsb/mapviewer/web/api/plugin/NewPluginControllerTest.java b/web/src/test/java/lcsb/mapviewer/web/api/plugin/NewPluginControllerTest.java
index 368ca16ad..77f5de1e1 100644
--- a/web/src/test/java/lcsb/mapviewer/web/api/plugin/NewPluginControllerTest.java
+++ b/web/src/test/java/lcsb/mapviewer/web/api/plugin/NewPluginControllerTest.java
@@ -21,6 +21,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.web.servlet.RequestBuilder;
 
 import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.List;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
@@ -51,23 +53,27 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
 
   private ObjectMapper objectMapper;
 
+  List<String> pluginHashes = new ArrayList<>();
+
   @Before
   public void setUp() throws Exception {
     objectMapper = newApiResponseSerializer.getObjectMapper();
+    pluginHashes = new ArrayList<>();
   }
 
   @After
   public void tearDown() throws Exception {
-    Plugin plugin = pluginService.getByHash(PLUGIN_HASH);
-    if (plugin != null) {
-      pluginService.delete(plugin);
+    for (String hash : pluginHashes) {
+      Plugin plugin = pluginService.getByHash(hash);
+      if (plugin != null) {
+        pluginService.delete(plugin);
+      }
     }
   }
 
   @Test
   public void testDocsGetPlugin() throws Exception {
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    Plugin plugin = createAndPersistPlugin(PLUGIN_HASH);
 
     final RequestBuilder request = get("/minerva/new_api/plugins/{hash}", plugin.getHash());
 
@@ -79,6 +85,12 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
         .andExpect(status().is2xxSuccessful());
   }
 
+  private Plugin createAndPersistPlugin(final String hash) {
+    Plugin plugin = createPlugin(hash);
+    persistPlugin(plugin);
+    return plugin;
+  }
+
   private static Plugin createPlugin(final String hash) {
     Plugin plugin = new Plugin();
     plugin.setHash(hash);
@@ -129,8 +141,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
 
   @Test
   public void testCreatePluginExisting() throws Exception {
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final NewPluginDTO data = createPluginDTO(PLUGIN_HASH);
 
@@ -147,8 +158,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final NewPluginDTO data = createPluginDTO(PLUGIN_HASH);
 
@@ -162,7 +172,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
         .andReturn().getResponse();
     assertNotNull(response.getHeader("ETag"));
 
-    plugin = pluginService.getByHash(PLUGIN_HASH);
+    Plugin plugin = pluginService.getByHash(PLUGIN_HASH);
     assertEquals(data.getVersion(), plugin.getVersion());
   }
 
@@ -171,8 +181,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testUpdatePluginWithMissingData() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final NewPluginDTO data = createPluginDTO(PLUGIN_HASH);
     data.setName(null);
@@ -190,8 +199,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testUpdatePluginWithOldVersion() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final NewPluginDTO data = createPluginDTO(PLUGIN_HASH);
 
@@ -209,8 +217,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testUpdatePluginWithGoodVersion() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    Plugin plugin = createAndPersistPlugin(PLUGIN_HASH);
 
     String originalVersion = plugin.getEntityVersion() + "";
 
@@ -247,8 +254,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testDeletePlugin() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final RequestBuilder request = delete("/minerva/new_api/plugins/{pluginId}/", PLUGIN_HASH)
         .session(session);
@@ -273,8 +279,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
 
   @Test
   public void testDeleteNoAccessPlugin() throws Exception {
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final RequestBuilder request = delete("/minerva/new_api/plugins/{pluginId}/", PLUGIN_HASH);
 
@@ -286,8 +291,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testDeletePluginWithGoodVersion() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    Plugin plugin = createAndPersistPlugin(PLUGIN_HASH);
 
     final String originalVersion = plugin.getEntityVersion() + "";
 
@@ -303,8 +307,7 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testDeletePluginWithWrongVersion() throws Exception {
     final MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
 
-    Plugin plugin = createPlugin(PLUGIN_HASH);
-    pluginService.add(plugin);
+    createAndPersistPlugin(PLUGIN_HASH);
 
     final RequestBuilder request = delete("/minerva/new_api/plugins/{pluginId}/", PLUGIN_HASH)
         .header("If-Match", "-1")
@@ -318,15 +321,16 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
   public void testDocsListPlugins() throws Exception {
     Plugin plugin = createPlugin(PLUGIN_HASH);
     plugin.setPublic(true);
-    pluginService.add(plugin);
+    persistPlugin(plugin);
+
 
     plugin = createPlugin(faker.hashing().md5());
     plugin.addUrl(PLUGIN_URL);
-    pluginService.add(plugin);
+    persistPlugin(plugin);
 
     plugin = createPlugin(faker.hashing().md5());
     plugin.addUrl(PLUGIN_URL);
-    pluginService.add(plugin);
+    persistPlugin(plugin);
 
     final RequestBuilder request = get("/minerva/new_api/plugins/");
 
@@ -344,4 +348,9 @@ public class NewPluginControllerTest extends ControllerIntegrationTest {
     assertEquals(1, page.getNumberOfElements());
 
   }
+
+  private void persistPlugin(final Plugin plugin) {
+    pluginService.add(plugin);
+    pluginHashes.add(plugin.getHash());
+  }
 }
-- 
GitLab