diff --git a/smp-server-library/src/main/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizer.java b/smp-server-library/src/main/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizer.java index 5498fa11241727a14ad6243fa423d11da911a1c3..192ae66095561e80f16775476fba29fa2cdd58dd 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizer.java +++ b/smp-server-library/src/main/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizer.java @@ -20,7 +20,6 @@ import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; import java.util.List; import java.util.ListIterator; @@ -33,16 +32,19 @@ import static eu.europa.ec.smp.api.Identifiers.asString; @Component public class CaseSensitivityNormalizer { - @Value("#{'${identifiersBehaviour.caseSensitive.ParticipantIdentifierSchemes}'.split('\\|')}") private List<String> caseSensitiveParticipantSchemes; + private List<String> caseSensitiveDocumentSchemes; @Value("#{'${identifiersBehaviour.caseSensitive.DocumentIdentifierSchemes}'.split('\\|')}") - private List<String> caseSensitiveDocumentSchemes; + public void setCaseSensitiveDocumentSchemes(List<String> caseSensitiveDocumentSchemes) { + this.caseSensitiveDocumentSchemes = caseSensitiveDocumentSchemes; + toLowerCaseStringList(this.caseSensitiveDocumentSchemes); + } - @PostConstruct - public void init() { - toLowerCaseStringList(caseSensitiveParticipantSchemes); - toLowerCaseStringList(caseSensitiveDocumentSchemes); + @Value("#{'${identifiersBehaviour.caseSensitive.DocumentIdentifierSchemes}'.split('\\|')}") + public void setCaseSensitiveParticipantSchemes(List<String> caseSensitiveParticipantSchemes) { + this.caseSensitiveParticipantSchemes = caseSensitiveParticipantSchemes; + toLowerCaseStringList(this.caseSensitiveParticipantSchemes); } public ParticipantIdentifierType normalize(final ParticipantIdentifierType participantIdentifier) { diff --git a/smp-server-library/src/test/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizerTest.java b/smp-server-library/src/test/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizerTest.java index c0955b23d3b85018fcc983b33c47a80371ee965d..2935a1887358f36a2de9d0691e7e6f3e1b8ed2e4 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizerTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/cipa/smp/server/conversion/CaseSensitivityNormalizerTest.java @@ -15,31 +15,36 @@ package eu.europa.ec.cipa.smp.server.conversion; -import eu.europa.ec.edelivery.smp.config.SmpServicesTestConfig; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.oasis_open.docs.bdxr.ns.smp._2016._05.DocumentIdentifier; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; /** * Created by gutowpa on 06/03/2017. */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {SmpServicesTestConfig.class}) +@RunWith(JUnitParamsRunner.class) public class CaseSensitivityNormalizerTest { - @Autowired private CaseSensitivityNormalizer normalizer; + @Before + public void init() { + normalizer = new CaseSensitivityNormalizer(); + normalizer.setCaseSensitiveDocumentSchemes(asList(new String[]{"case-SENSITIVE-scheme-1", "Case-SENSITIVE-Scheme-2"})); + normalizer.setCaseSensitiveParticipantSchemes(asList(new String[]{"case-sensitive-scheme-1", "Case-SENSITIVE-Scheme-2"})); + } + @SuppressWarnings("unused") - private static String[][] testCases() { - return new String[][]{ + private static Object[] testCases() { + return new Object[][]{ {"scheme", "value", "scheme", "value"}, {"SCHEME", "VALUE", "scheme", "value"}, {"SchemE", "ValuE", "scheme", "value"}, @@ -51,53 +56,40 @@ public class CaseSensitivityNormalizerTest { } @Test - public void testParticipantIdsCaseNormalization() { - for (int i = 0; i < testCases().length; i++) { - String inputScheme = testCases()[i][0]; - String inputValue = testCases()[i][1]; - String expectedScheme = testCases()[i][2]; - String expectedValue = testCases()[i][3]; - - //given - ParticipantIdentifierType inputParticpantId = new ParticipantIdentifierType(inputValue, inputScheme); - - //when - ParticipantIdentifierType outputParticipantId = normalizer.normalize(inputParticpantId); - - //then - assertEquals(expectedScheme, outputParticipantId.getScheme()); - assertEquals(expectedValue, outputParticipantId.getValue()); - - //input stays untouched - assertFalse(inputParticpantId == outputParticipantId); - assertEquals(inputScheme, inputParticpantId.getScheme()); - assertEquals(inputValue, inputParticpantId.getValue()); - } + @Parameters(method = "testCases") + public void testParticipantIdsCaseNormalization(String inputScheme, String inputValue, String expectedScheme, String expectedValue) { + //given + ParticipantIdentifierType inputParticpantId = new ParticipantIdentifierType(inputValue, inputScheme); + + //when + ParticipantIdentifierType outputParticipantId = normalizer.normalize(inputParticpantId); + + //then + assertEquals(expectedScheme, outputParticipantId.getScheme()); + assertEquals(expectedValue, outputParticipantId.getValue()); + + //input stays untouched + assertFalse(inputParticpantId == outputParticipantId); + assertEquals(inputScheme, inputParticpantId.getScheme()); + assertEquals(inputValue, inputParticpantId.getValue()); } @Test - public void testDocumentIdsCaseNormalization() { - for (int i = 0; i < testCases().length; i++) { - String inputScheme = testCases()[i][0]; - String inputValue = testCases()[i][1]; - String expectedScheme = testCases()[i][2]; - String expectedValue = testCases()[i][3]; - - //given - DocumentIdentifier inputDocId = new DocumentIdentifier(inputValue, inputScheme); - - //when - DocumentIdentifier outputDocId = normalizer.normalize(inputDocId); - - //then - assertEquals(expectedScheme, outputDocId.getScheme()); - assertEquals(expectedValue, outputDocId.getValue()); - - //input stays untouched - assertFalse(inputDocId == outputDocId); - assertEquals(inputScheme, inputDocId.getScheme()); - assertEquals(inputValue, inputDocId.getValue()); - } + @Parameters(method = "testCases") + public void testDocumentIdsCaseNormalization(String inputScheme, String inputValue, String expectedScheme, String expectedValue) { + //given + DocumentIdentifier inputDocId = new DocumentIdentifier(inputValue, inputScheme); + + //when + DocumentIdentifier outputDocId = normalizer.normalize(inputDocId); + + //then + assertEquals(expectedScheme, outputDocId.getScheme()); + assertEquals(expectedValue, outputDocId.getValue()); + + //input stays untouched + assertFalse(inputDocId == outputDocId); + assertEquals(inputScheme, inputDocId.getScheme()); + assertEquals(inputValue, inputDocId.getValue()); } - -} +} \ No newline at end of file diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java index 3ab1600185a97eb9d75e1cb21c3ba784801d9fa5..d0a8b7780114c1ce81b7d19b5ea226f2e65701c5 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java @@ -34,11 +34,11 @@ import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; /** - * Created by Flavio Santos + * Created by gutowpa on 21/09/2017. */ @Configuration @ComponentScan(basePackages = { - "eu.europa.ec"}) + "eu.europa.ec.cipa.smp.server"}) @PropertySource(value = "classpath:config.properties") public class SmpServicesTestConfig { diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/DatabaseConfig.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/DatabaseConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..d4f19c2b802c537211f1cc31df892973c388c991 --- /dev/null +++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/DatabaseConfig.java @@ -0,0 +1,93 @@ +/* + * Copyright 2017 European Commission | CEF eDelivery + * + * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * + * You may obtain a copy of the Licence at: + * https://joinup.ec.europa.eu/software/page/eupl + * or file: LICENCE-EUPL-v1.1.pdf + * + * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and limitations under the Licence. + */ + +package eu.europa.ec.edelivery.smp.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +/** + * Created by gutowpa on 12/07/2017. + */ + +@Configuration +@EnableTransactionManagement +@ComponentScan(basePackages = { + "eu.europa.ec.cipa.smp.server.data.dbms", + "eu.europa.ec.cipa.smp.server.services", + "eu.europa.ec.cipa.smp.server.hook"}) +public class DatabaseConfig { + + @Value("${jdbc.driver}") + private String driver; + + @Value("${jdbc.user}") + private String username; + + @Value("${jdbc.password}") + private String password; + + @Value("${jdbc.url}") + private String url; + + @Bean + public DataSource dataSource() { + DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); + driverManagerDataSource.setDriverClassName(driver); + driverManagerDataSource.setUrl(url); + driverManagerDataSource.setUsername(username); + driverManagerDataSource.setPassword(password); + + return driverManagerDataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean smpEntityManagerFactory() { + LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); + lef.setDataSource(dataSource()); + lef.setJpaVendorAdapter(jpaVendorAdapter()); + lef.setPackagesToScan("eu.europa.ec.cipa.smp.server.data.dbms.model"); + lef.setPersistenceXmlLocation("classpath:META-INF/smp-persistence.xml"); + return lef; + } + + @Bean + public JpaVendorAdapter jpaVendorAdapter() { + HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); + + return hibernateJpaVendorAdapter; + } + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + + return transactionManager; + } +} diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/SmpAppConfig.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/SmpAppConfig.java index c132f0f61729b71b81dd95eb3b36f3a859e66d3a..eb6291284d64f33b179be944d6a0e00e8cefd43f 100644 --- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/SmpAppConfig.java +++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/config/SmpAppConfig.java @@ -15,26 +15,14 @@ package eu.europa.ec.edelivery.smp.config; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.*; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.JpaVendorAdapter; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; /** * Created by gutowpa on 12/07/2017. */ @Configuration -@EnableTransactionManagement @ComponentScan(basePackages = { "eu.europa.ec.edelivery.smp.validation", "eu.europa.ec.cipa.smp.server.data.dbms", @@ -46,58 +34,11 @@ import javax.sql.DataSource; @PropertySource(value = "classpath:config.properties", ignoreResourceNotFound = true), @PropertySource(value = "classpath:smp.config.properties", ignoreResourceNotFound = true) }) +@Import(DatabaseConfig.class) public class SmpAppConfig { - @Value("${jdbc.driver}") - private String driver; - - @Value("${jdbc.user}") - private String username; - - @Value("${jdbc.password}") - private String password; - - @Value("${jdbc.url}") - private String url; - @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } - - @Bean - public DataSource dataSource() { - DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); - driverManagerDataSource.setDriverClassName(driver); - driverManagerDataSource.setUrl(url); - driverManagerDataSource.setUsername(username); - driverManagerDataSource.setPassword(password); - - return driverManagerDataSource; - } - - @Bean - public LocalContainerEntityManagerFactoryBean smpEntityManagerFactory() { - LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); - lef.setDataSource(dataSource()); - lef.setJpaVendorAdapter(jpaVendorAdapter()); - lef.setPackagesToScan("eu.europa.ec.cipa.smp.server.data.dbms.model"); - lef.setPersistenceXmlLocation("classpath:META-INF/smp-persistence.xml"); - return lef; - } - - @Bean - public JpaVendorAdapter jpaVendorAdapter() { - HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); - - return hibernateJpaVendorAdapter; - } - - @Bean - public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(emf); - - return transactionManager; - } } diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidator.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidator.java index 9121b719381ec59b6080d3640f701102f35074b0..e91964bff15720bd9e47f6a84426ec9bc04eafae 100644 --- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidator.java +++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidator.java @@ -38,7 +38,6 @@ import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceGroup; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -51,13 +50,10 @@ import static eu.europa.ec.edelivery.smp.error.ErrorBusinessCode.WRONG_FIELD; @Component public class ServiceGroupValidator { - @Value("${identifiersBehaviour.ParticipantIdentifierScheme.validationRegex}") - private String regex; - private Pattern schemaPattern; - @PostConstruct - public void init() { + @Value("${identifiersBehaviour.ParticipantIdentifierScheme.validationRegex}") + public void setRegexPattern(String regex) { try { schemaPattern = Pattern.compile(regex); } catch (PatternSyntaxException | NullPointerException e) { diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidatorTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidatorTest.java index 876d85b1986bd4e95b172b6e9a25d018ac2b7d15..6e7734e55b41964672f91bdacbdcdfe0fc05fed9 100644 --- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidatorTest.java +++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/validation/ServiceGroupValidatorTest.java @@ -45,28 +45,31 @@ package eu.europa.ec.edelivery.smp.validation; -import eu.europa.ec.edelivery.smp.config.SmpAppConfig; import eu.europa.ec.edelivery.smp.error.exceptions.BadRequestException; +import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceGroup; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static eu.europa.ec.smp.api.Identifiers.asString; /** * Created by gutowpa on 02/08/2017. */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {SmpAppConfig.class}) public class ServiceGroupValidatorTest { + private static final String ALLOWED_SCHEME_REGEXP = "^(?!^.{26})([a-z0-9]+-[a-z0-9]+-[a-z0-9]+)"; + @Autowired private ServiceGroupValidator validator; + @Before + public void init() { + validator = new ServiceGroupValidator(); + validator.setRegexPattern(ALLOWED_SCHEME_REGEXP); + } + @Test public void testPositiveGoodScheme() throws Throwable { validateBadScheme("good6-scheme4-ok");