From c22ceedab6587cbcc287d04f23e1cbb426f56d10 Mon Sep 17 00:00:00 2001
From: Joze RIHTARSIC <joze.RIHTARSIC@ext.ec.europa.eu>
Date: Wed, 25 May 2022 08:42:09 +0200
Subject: [PATCH] Fix unit tests

---
 .../ApplicationResourceIntegrationTest.java   |  77 ++++++++++-
 ...plicationAdminResourceIntegrationTest.java | 120 ------------------
 2 files changed, 76 insertions(+), 121 deletions(-)
 delete mode 100644 smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/internal/ApplicationAdminResourceIntegrationTest.java

diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/external/ApplicationResourceIntegrationTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/external/ApplicationResourceIntegrationTest.java
index d8ddf8d7c..eaef5ec19 100644
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/external/ApplicationResourceIntegrationTest.java
+++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/external/ApplicationResourceIntegrationTest.java
@@ -1,13 +1,16 @@
 package eu.europa.ec.edelivery.smp.ui.external;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import eu.europa.ec.edelivery.smp.data.ui.SmpConfigRO;
 import eu.europa.ec.edelivery.smp.data.ui.SmpInfoRO;
+import eu.europa.ec.edelivery.smp.data.ui.enums.SMPPropertyEnum;
 import eu.europa.ec.edelivery.smp.test.SmpTestWebAppConfig;
 import eu.europa.ec.edelivery.smp.ui.ResourceConstants;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpSession;
 import org.springframework.mock.web.MockServletContext;
 import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
 import org.springframework.test.context.ContextConfiguration;
@@ -23,7 +26,11 @@ import org.springframework.web.context.WebApplicationContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
-import static org.junit.Assert.assertEquals;
+import static eu.europa.ec.edelivery.smp.test.testutils.MockMvcUtils.*;
+import static eu.europa.ec.edelivery.smp.test.testutils.MockMvcUtils.loginWithSMPAdmin;
+import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
 import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -96,4 +103,72 @@ public class ApplicationResourceIntegrationTest {
         assertEquals("TestApplicationSmpName Version [TestApplicationVersion] Build-Time [2018-11-27 00:00:00|Central European Time]", info.getVersion());
         assertEquals("/", info.getContextPath());
     }
+
+    @Test
+    public void testGetApplicationConfigNotAuthorized() throws Exception {
+        // when
+        mvc.perform(get(PATH + "/config")
+                .with(csrf()))
+                .andExpect(status().isUnauthorized())
+                .andReturn()
+                .getResponse();
+    }
+
+    @Test
+    public void testGetApplicationConfigAuthorized() throws Exception {
+        //  SMP admin
+        MockHttpSession session = loginWithSMPAdmin(mvc);
+        String val = mvc.perform(get(PATH + "/config")
+                .session(session)
+                .with(csrf()))
+                .andExpect(status().isOk())
+                .andReturn()
+                .getResponse()
+                .getContentAsString();
+        assertNotNull(val);
+        //  service group
+        MockHttpSession sessionUser = loginWithServiceGroupUser(mvc);
+        val = mvc.perform(get(PATH + "/config")
+                .session(sessionUser)
+                .with(csrf()))
+                .andExpect(status().isOk())
+                .andReturn()
+                .getResponse()
+                .getContentAsString();
+        assertNotNull(val);
+        // system admin
+        MockHttpSession sessionSystem = loginWithSystemAdmin(mvc);
+        val = mvc.perform(get(PATH + "/config")
+                .session(sessionSystem)
+                .with(csrf()))
+                .andExpect(status().isOk())
+                .andReturn()
+                .getResponse()
+                .getContentAsString();
+        assertNotNull(val);
+    }
+
+    @Test
+    public void testGetApplicationConfigSMPAdmin() throws Exception {
+        // when
+        MockHttpSession session = loginWithSMPAdmin(mvc);
+        String value = mvc.perform(get(PATH + "/config")
+                .session(session)
+                .with(csrf()))
+                .andExpect(status().isOk())
+                .andReturn()
+                .getResponse()
+                .getContentAsString();
+
+        // then
+        ObjectMapper mapper = new ObjectMapper();
+        SmpConfigRO res = mapper.readValue(value, SmpConfigRO.class);
+
+        assertNotNull(res);
+        assertEquals("Participant scheme must start with:urn:oasis:names:tc:ebcore:partyid-type:(iso6523:|unregistered:) OR must be up to 25 characters long with form [domain]-[identifierArea]-[identifierType] (ex.: 'busdox-actorid-upis') and may only contain the following characters: [a-z0-9].", res.getParticipantSchemaRegExpMessage());
+        assertEquals("^$|^(?!^.{26})([a-z0-9]+-[a-z0-9]+-[a-z0-9]+)$|^urn:oasis:names:tc:ebcore:partyid-type:(iso6523|unregistered)(:.+)?$", res.getParticipantSchemaRegExp());
+        assertEquals(SMPPropertyEnum.PARTC_EBCOREPARTYID_CONCATENATE.getDefValue(), res.isConcatEBCorePartyId() + "");
+        assertFalse(res.isSmlIntegrationOn());
+        assertFalse(res.isSmlParticipantMultiDomainOn());
+    }
 }
diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/internal/ApplicationAdminResourceIntegrationTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/internal/ApplicationAdminResourceIntegrationTest.java
deleted file mode 100644
index b2b11b28f..000000000
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/internal/ApplicationAdminResourceIntegrationTest.java
+++ /dev/null
@@ -1,120 +0,0 @@
-package eu.europa.ec.edelivery.smp.ui.internal;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import eu.europa.ec.edelivery.smp.data.ui.SmpConfigRO;
-import eu.europa.ec.edelivery.smp.data.ui.enums.SMPPropertyEnum;
-import eu.europa.ec.edelivery.smp.test.SmpTestWebAppConfig;
-import eu.europa.ec.edelivery.smp.ui.ResourceConstants;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.mock.web.MockHttpSession;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.TestPropertySource;
-import org.springframework.test.context.jdbc.Sql;
-import org.springframework.test.context.junit4.SpringRunner;
-import org.springframework.test.context.web.WebAppConfiguration;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.web.context.WebApplicationContext;
-
-import static eu.europa.ec.edelivery.smp.test.testutils.MockMvcUtils.*;
-import static org.junit.Assert.*;
-import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
-import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
-
-@RunWith(SpringRunner.class)
-@WebAppConfiguration
-@ContextConfiguration(classes = {SmpTestWebAppConfig.class})
-@Sql(scripts = {
-        "classpath:/cleanup-database.sql",
-        "classpath:/webapp_integration_test_data.sql"},
-        executionPhase = BEFORE_TEST_METHOD)
-@TestPropertySource(properties = {
-        "smp.artifact.name=TestApplicationSmpName",
-        "smp.artifact.version=TestApplicationVersion",
-        "smp.artifact.build.time=2018-11-27 00:00:00",
-})
-public class ApplicationAdminResourceIntegrationTest {
-    private static final String PATH = ResourceConstants.CONTEXT_PATH_INTERNAL_APPLICATION;
-
-    @Autowired
-    private WebApplicationContext webAppContext;
-    private MockMvc mvc;
-
-    @Before
-    public void setup() {
-        mvc = initializeMockMvc(webAppContext);
-    }
-
-    @Test
-    public void testGetApplicationConfigNotAuthorized() throws Exception {
-        // when
-        mvc.perform(get(PATH + "/config")
-                .with(csrf()))
-                .andExpect(status().isUnauthorized())
-                .andReturn()
-                .getResponse();
-    }
-
-    @Test
-    public void testGetApplicationConfigAuthorized() throws Exception {
-        //  SMP admin
-        MockHttpSession session = loginWithSMPAdmin(mvc);
-        String val = mvc.perform(get(PATH + "/config")
-                .session(session)
-                .with(csrf()))
-                .andExpect(status().isOk())
-                .andReturn()
-                .getResponse()
-                .getContentAsString();
-        assertNotNull(val);
-        //  service group
-        MockHttpSession sessionUser = loginWithServiceGroupUser(mvc);
-        val = mvc.perform(get(PATH + "/config")
-                .session(sessionUser)
-                .with(csrf()))
-                .andExpect(status().isOk())
-                .andReturn()
-                .getResponse()
-                .getContentAsString();
-        assertNotNull(val);
-        // system admin
-        MockHttpSession sessionSystem = loginWithSystemAdmin(mvc);
-        val = mvc.perform(get(PATH + "/config")
-                .session(sessionSystem)
-                .with(csrf()))
-                .andExpect(status().isOk())
-                .andReturn()
-                .getResponse()
-                .getContentAsString();
-        assertNotNull(val);
-    }
-
-    @Test
-    public void testGetApplicationConfigSMPAdmin() throws Exception {
-        // when
-        MockHttpSession session = loginWithSMPAdmin(mvc);
-        String value = mvc.perform(get(PATH + "/config")
-                .session(session)
-                .with(csrf()))
-                .andExpect(status().isOk())
-                .andReturn()
-                .getResponse()
-                .getContentAsString();
-
-        // then
-        ObjectMapper mapper = new ObjectMapper();
-        SmpConfigRO res = mapper.readValue(value, SmpConfigRO.class);
-
-        assertNotNull(res);
-        assertEquals("Participant scheme must start with:urn:oasis:names:tc:ebcore:partyid-type:(iso6523:|unregistered:) OR must be up to 25 characters long with form [domain]-[identifierArea]-[identifierType] (ex.: 'busdox-actorid-upis') and may only contain the following characters: [a-z0-9].", res.getParticipantSchemaRegExpMessage());
-        assertEquals("^$|^(?!^.{26})([a-z0-9]+-[a-z0-9]+-[a-z0-9]+)$|^urn:oasis:names:tc:ebcore:partyid-type:(iso6523|unregistered)(:.+)?$", res.getParticipantSchemaRegExp());
-        assertEquals(SMPPropertyEnum.PARTC_EBCOREPARTYID_CONCATENATE.getDefValue(), res.isConcatEBCorePartyId() + "");
-        assertFalse(res.isSmlIntegrationOn());
-        assertFalse(res.isSmlParticipantMultiDomainOn());
-    }
-}
-- 
GitLab