Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c22ceeda authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Fix unit tests

parent 6bfe2e62
No related branches found
No related tags found
No related merge requests found
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());
}
}
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment