diff --git a/smp-parent-pom/pom.xml b/smp-parent-pom/pom.xml index 5c12286fb608a464f5d32dc38b4229d47e547057..12071f76ea88b9b03f7a4895fe7e0856a6a18cfe 100644 --- a/smp-parent-pom/pom.xml +++ b/smp-parent-pom/pom.xml @@ -57,6 +57,7 @@ <slf4j.version>1.7.10</slf4j.version> <spring.version>5.0.6.RELEASE</spring.version> <spring.security.version>4.2.6.RELEASE</spring.security.version> + <spring.boot.version>1.5.2.RELEASE</spring.boot.version> <bdmsl.client.version>3.0.0</bdmsl.client.version> <cxf.version>3.2.1</cxf.version> <cxf-xjc-runtime.version>3.2.0</cxf-xjc-runtime.version> @@ -82,6 +83,7 @@ <hibernate.version>5.2.12.Final</hibernate.version> <hibernate-jpa.version>1.0.0.Final</hibernate-jpa.version> <hibernate.validator>5.2.12.Final</hibernate.validator> + <h2.version>1.4.187</h2.version> <oracle.version>12.1.0.1</oracle.version> <mysql.version>5.1.45</mysql.version> <soapui.plugin.version>5.1.2</soapui.plugin.version> @@ -269,6 +271,11 @@ <artifactId>hibernate-envers</artifactId> <version>${hibernate.version}</version> </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>${h2.version}</version> + </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> @@ -486,11 +493,6 @@ <artifactId>dnsjava</artifactId> <version>${dnsjava.version}</version> </dependency> - <dependency> - <groupId>com.h2database</groupId> - <artifactId>h2</artifactId> - <version>${h2.version}</version> - </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> @@ -548,6 +550,22 @@ <version>${hamcrest.version}</version> <scope>test</scope> </dependency> + <!-- Test dependencies --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + <version>${spring.boot.version}</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>${spring.boot.version}</version> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>${h2.version}</version> + </dependency> </dependencies> </dependencyManagement> diff --git a/smp-server-library/pom.xml b/smp-server-library/pom.xml index 77538b61197dcd55563fa8a5f9c73c4149c01d55..a9a0c59df71c7c41c865c8b57551cee286124d15 100644 --- a/smp-server-library/pom.xml +++ b/smp-server-library/pom.xml @@ -162,6 +162,35 @@ <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> + + <!-- Test dependencies --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-logging</artifactId> + </exclusion> + </exclusions> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-logging</artifactId> + </exclusion> + </exclusions> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceGroupConverter.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceGroupConverter.java index 7a3f18f9acce3dff6c17217be9e466e1a04b2624..996694b11d89c2fc5a314d9d2f33fc5c46dcf535 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceGroupConverter.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceGroupConverter.java @@ -15,6 +15,7 @@ package eu.europa.ec.edelivery.smp.conversion; import eu.europa.ec.edelivery.smp.data.model.DBServiceGroupId; import eu.europa.ec.edelivery.smp.exceptions.ConversionException; +import eu.europa.ec.edelivery.smp.exceptions.SMPInitializationException; import eu.europa.ec.edelivery.smp.exceptions.XmlParsingException; import eu.europa.ec.edelivery.smp.data.model.DBServiceGroup; import org.oasis_open.docs.bdxr.ns.smp._2016._05.*; @@ -56,7 +57,7 @@ public class ServiceGroupConverter { JAXBContext jaxbContext = JAXBContext.newInstance(ServiceGroup.class); return jaxbContext.createUnmarshaller(); }catch(JAXBException ex) { - throw new RuntimeException("Could not create ServiceGroup Unmarshaller!", ex); + throw new SMPInitializationException("Could not create ServiceGroup Unmarshaller!", ex); } } ); diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceMetadataConverter.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceMetadataConverter.java index 226f3693f5cc0a854462eb6e7ecfb00d228370bb..3045b1f0338978ba93805a10aa238f33a8478e1f 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceMetadataConverter.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/ServiceMetadataConverter.java @@ -14,6 +14,7 @@ package eu.europa.ec.edelivery.smp.conversion; import eu.europa.ec.edelivery.smp.data.model.DBServiceMetadataId; +import eu.europa.ec.edelivery.smp.exceptions.SMPInitializationException; import eu.europa.ec.edelivery.smp.exceptions.XmlParsingException; import org.oasis_open.docs.bdxr.ns.smp._2016._05.DocumentIdentifier; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; @@ -58,7 +59,7 @@ public class ServiceMetadataConverter { JAXBContext jaxbContext = JAXBContext.newInstance(ServiceMetadata.class); return jaxbContext.createUnmarshaller(); }catch(JAXBException ex) { - throw new RuntimeException("Could not create ServiceGroup Unmarshaller!"); + throw new SMPInitializationException("Could not create ServiceGroup Unmarshaller!", ex); } } ); diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/SMPInitializationException.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/SMPInitializationException.java new file mode 100644 index 0000000000000000000000000000000000000000..1c45cde8a3ebcb13f79f1dc3b38a49b3e53333bd --- /dev/null +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/SMPInitializationException.java @@ -0,0 +1,22 @@ +/* + * Copyright 2017 European Commission | CEF eDelivery + * + * Licensed under the EUPL, Version 1.2 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 attached in file: LICENCE-EUPL-v1.2.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.exceptions; + + +public class SMPInitializationException extends RuntimeException { + + public SMPInitializationException(String message, Exception e){ + super(message, e); + } +} diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceUIData.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceUIData.java index d3ba0074bb082fa29a8bdb08c81573d332259ae5..1f8d6cbf3d2d3ccd7a76dd0cce436d9d040ea99f 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceUIData.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceUIData.java @@ -40,6 +40,21 @@ public class ServiceUIData { return sg; } + public void persistServiceGroup(ServiceGroupRO sg) { + uiDaoService.persist(sg); + } + + public void persistUser(UserRO ent) { + uiDaoService.persist(ent); + } + + public void persistDomain(DomainRO ent) { + uiDaoService.persist(ent); + } + public void persistMetaData(ServiceMetadataRO ent) { + uiDaoService.persist(ent); + } + /** * * @param page diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/H2JPATestConfiguration.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/H2JPATestConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..21a050be92e366cca0518c334c33b6e9cb4cef59 --- /dev/null +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/H2JPATestConfiguration.java @@ -0,0 +1,86 @@ +package eu.europa.ec.edelivery.smp.config; + + +import eu.europa.ec.edelivery.smp.data.dao.ui.UiDaoService; +import eu.europa.ec.edelivery.smp.services.ServiceUIData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.*; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.Database; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +@Configuration +@PropertySource("persistence-test-h2.properties") +@EnableTransactionManagement +public class H2JPATestConfiguration { + @Autowired + private Environment env; + + @Bean + @Profile("db-h2-integration-test") + public DataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + + + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.user")); + dataSource.setPassword(env.getProperty("jdbc.pass")); + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { + LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); + lef.setDataSource(dataSource); + lef.setJpaVendorAdapter(jpaVendorAdapter); + lef.setPackagesToScan("eu.europa.ec.edelivery.smp.data.ui"); + + return lef; + } + + @Bean + public JpaVendorAdapter jpaVendorAdapter() { + HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); + hibernateJpaVendorAdapter.setShowSql(false); + hibernateJpaVendorAdapter.setGenerateDdl(true); + hibernateJpaVendorAdapter.setDatabase(Database.H2); + + + return hibernateJpaVendorAdapter; + } + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { // TODO: Really need this? + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + + + @Bean + public ServiceUIData serviceUIData(){ + ServiceUIData serviceMetadat = new ServiceUIData(); + return serviceMetadat; + + } + + @Bean + public UiDaoService uiDaoService(){ + UiDaoService uiDaoService = new UiDaoService(); + return uiDaoService; + + } +} diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceUIDataIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceUIDataIntegrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a188a2ca99fb0aa00d4acf4e4fdbb623dcb43617 --- /dev/null +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceUIDataIntegrationTest.java @@ -0,0 +1,159 @@ +package eu.europa.ec.edelivery.smp.services; + +import eu.europa.ec.edelivery.smp.config.H2JPATestConfiguration; +import eu.europa.ec.edelivery.smp.data.ui.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { + H2JPATestConfiguration.class}) +@ActiveProfiles("db-h2-integration-test") +@Transactional +public class ServiceUIDataIntegrationTest { + + @Autowired + private ServiceUIData serviceUIData; + + @Test + public void getServiceGroupList() { + for (int i=0;i<20; i++) { + ServiceGroupRO sg = new ServiceGroupRO(); + sg.setServiceGroupROId(new ServiceGroupRO.ServiceGroupROId()); + sg.getServiceGroupROId().setParticipantId("ParticipantId" + i); + sg.getServiceGroupROId().setParticipantSchema("ParticipantId"); + sg.setDomain("test"); + serviceUIData.persistServiceGroup(sg); + } + + ServiceResult<ServiceGroupRO> lst = serviceUIData.getServiceGroupList(0,10,null,null); + assertEquals(10, lst.getServiceEntities().size()); + } + + @Test + public void getUserList() { + + for (int i=0;i<20; i++) { + UserRO ent = new UserRO(); + ent.setAdmin(false); + ent.setUsername("Username" +i); + ent.setPassword("Password"); + serviceUIData.persistUser(ent); + } + + ServiceResult<UserRO> lst = serviceUIData.getUserList(0,10,null,null); + assertEquals(10, lst.getServiceEntities().size()); + } + + @Test + public void getDomainList() { + for (int i=0;i<20; i++) { + DomainRO ent = new DomainRO(); + ent.setDomainId("DomainId" + i); + ent.setBdmslClientCertAlias("dmslClientCertAlias"); + ent.setBdmslClientCertHeader("dmslClientCertHeader"); + ent.setBdmslSmpId("BdmslSmpId"); + ent.setSignatureCertAlias("SignatureCertAlias"); + serviceUIData.persistDomain(ent); + } + + ServiceResult<DomainRO> lst = serviceUIData.getDomainList(0,10,null,null); + assertEquals(10, lst.getServiceEntities().size()); + } + + @Test + public void getServiceMetadataList() { + for (int i=0;i<20; i++) { + ServiceMetadataRO ent = new ServiceMetadataRO(); + ent.setServiceMetadataROId(new ServiceMetadataRO.ServiceMetadataROId()); + ent.getServiceMetadataROId().setDocumentIdScheme("DocumentIdScheme"); + ent.getServiceMetadataROId().setDocumentIdValue("DocumentIdValue"); + ent.getServiceMetadataROId().setParticipantId("ParticipantId" +i); + ent.getServiceMetadataROId().setParticipantSchema("ParticipantSchema"); + long cnt = serviceUIData.getServiceMetadataList(0, 10, null, null).getCount(); + serviceUIData.persistMetaData(ent); + } + + ServiceResult<ServiceMetadataRO> lst = serviceUIData.getServiceMetadataList(0,10,null,null); + assertEquals(10, lst.getServiceEntities().size()); + } + + + + + @Test + public void persistServiceGroup() { + + ServiceGroupRO sg = new ServiceGroupRO(); + sg.setServiceGroupROId(new ServiceGroupRO.ServiceGroupROId()); + sg.getServiceGroupROId().setParticipantId("ParticipantId"); + sg.getServiceGroupROId().setParticipantSchema("ParticipantId"); + sg.setDomain("test"); + long cnt = serviceUIData.getServiceGroupList(0,10,null,null).getCount(); + + serviceUIData.persistServiceGroup(sg); + + ServiceResult<ServiceGroupRO> lst = serviceUIData.getServiceGroupList(0,10,null,null); + assertEquals(cnt + 1, lst.getCount().longValue()); + } + + @Test + public void persistUser() { + + + UserRO ent = new UserRO(); + ent.setAdmin(false); + ent.setUsername("Username"); + ent.setPassword("Password"); + + long cnt = serviceUIData.getUserList(0,10,null,null).getCount(); + serviceUIData.persistUser(ent); + + ServiceResult<UserRO> lst = serviceUIData.getUserList(0,10,null,null); + assertEquals(cnt+1, lst.getCount().longValue()); + } + + @Test + public void persistDomain() { + + DomainRO ent = new DomainRO(); + ent.setDomainId("DomainId"); + ent.setBdmslClientCertAlias("dmslClientCertAlias"); + ent.setBdmslClientCertHeader("dmslClientCertHeader"); + ent.setBdmslSmpId("BdmslSmpId"); + ent.setSignatureCertAlias("SignatureCertAlias"); + long cnt = serviceUIData.getDomainList(0,10,null,null).getCount(); + + serviceUIData.persistDomain(ent); + + ServiceResult<DomainRO> lst = serviceUIData.getDomainList(0,10,null,null); + assertEquals(cnt+1, lst.getCount().longValue()); + } + + @Test + public void persistMetaData() { + ServiceMetadataRO ent = new ServiceMetadataRO(); + ent.setServiceMetadataROId(new ServiceMetadataRO.ServiceMetadataROId()); + ent.getServiceMetadataROId().setDocumentIdScheme("DocumentIdScheme"); + ent.getServiceMetadataROId().setDocumentIdValue("DocumentIdValue"); + ent.getServiceMetadataROId().setParticipantId("ParticipantId"); + ent.getServiceMetadataROId().setParticipantSchema("ParticipantSchema"); + long cnt = serviceUIData.getServiceMetadataList(0,10,null,null).getCount(); + serviceUIData.persistMetaData(ent); + + ServiceResult<ServiceMetadataRO> lst = serviceUIData.getServiceMetadataList(0,10,null,null); + assertEquals(cnt+1, lst.getCount().longValue()); + } + + + + +} \ No newline at end of file diff --git a/smp-server-library/src/test/resources/persistence-test-h2.properties b/smp-server-library/src/test/resources/persistence-test-h2.properties new file mode 100644 index 0000000000000000000000000000000000000000..7df06a06ad8c659453f96998b5b065430b236d0f --- /dev/null +++ b/smp-server-library/src/test/resources/persistence-test-h2.properties @@ -0,0 +1,12 @@ +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +jdbc.user=smp-dev +jdbc.pass=smp-dev +hibernate.dialect=org.hibernate.dialect.H2Dialect +spring.batch.initialize-schema=always +schema.sql=schema.sql +# Show all queries +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.generate-ddl=true +logging.level.org.hibernate.type=trace \ No newline at end of file diff --git a/smp-server-library/src/test/resources/schema.sql b/smp-server-library/src/test/resources/schema.sql new file mode 100755 index 0000000000000000000000000000000000000000..e6077f440eed6ce1225ad7d64678fbc8dffd3d73 --- /dev/null +++ b/smp-server-library/src/test/resources/schema.sql @@ -0,0 +1,278 @@ +-- Copyright 2018 European Commission | CEF eDelivery +-- +-- Licensed under the EUPL, Version 1.2 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 attached in file: LICENCE-EUPL-v1.2.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. + +CREATE TABLE smp_domain ( + domainId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + bdmslClientCertHeader VARCHAR(4000) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + bdmslClientCertAlias VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + bdmslSmpId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + signatureCertAlias VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + PRIMARY KEY(domainId) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + +CREATE TABLE smp_domain_AUD ( + domainId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + bdmslClientCertHeader VARCHAR(4000) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + bdmslClientCertAlias VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + bdmslSmpId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + signatureCertAlias VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NULL, + REV integer not null, + REVTYPE tinyint, + PRIMARY KEY(domainId, REV) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + + +CREATE TABLE smp_service_group ( + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + domainId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL + DEFAULT 'domain1', + extension TEXT NULL DEFAULT NULL, + PRIMARY KEY (businessIdentifier, businessIdentifierScheme), + CONSTRAINT FK_srv_group_domain FOREIGN KEY (domainId) + REFERENCES smp_domain (domainId) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + +CREATE TABLE smp_service_group_AUD ( + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + domainId VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL + DEFAULT 'domain1', + extension TEXT NULL DEFAULT NULL, + REV integer not null, + REVTYPE tinyint, + PRIMARY KEY (businessIdentifier, businessIdentifierScheme, REV) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + +CREATE TABLE smp_service_metadata ( + documentIdentifier VARCHAR(500) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + documentIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + xmlcontent TEXT, + PRIMARY KEY (documentIdentifier, documentIdentifierScheme, businessIdentifier, businessIdentifierScheme), + KEY FK_service_metadata_service_group (businessIdentifier, businessIdentifierScheme), + CONSTRAINT FK_service_metadata_service_group FOREIGN KEY (businessIdentifier, businessIdentifierScheme) REFERENCES smp_service_group (businessIdentifier, businessIdentifierScheme) + ON DELETE CASCADE + ON UPDATE CASCADE +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + +CREATE TABLE smp_service_metadata_AUD ( + documentIdentifier VARCHAR(500) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + documentIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + xmlcontent TEXT, + REV integer not null, + REVTYPE tinyint, + PRIMARY KEY (documentIdentifier, documentIdentifierScheme, businessIdentifier, businessIdentifierScheme, REV) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + +CREATE TABLE smp_user ( + username VARCHAR(256) NOT NULL, + password VARCHAR(256), + isadmin TINYINT(1) DEFAULT 0 NOT NULL, + PRIMARY KEY (username) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + +CREATE TABLE smp_user_AUD ( + username VARCHAR(256) NOT NULL, + password VARCHAR(256), + isadmin TINYINT(1) DEFAULT 0 NOT NULL, + REV integer not null, + REVTYPE tinyint, + PRIMARY KEY (username, REV) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + +CREATE TABLE smp_ownership ( + username VARCHAR(256) NOT NULL, + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + KEY FK_ownership_service_group (businessIdentifier, businessIdentifierScheme), + KEY FK_ownership_user (username), + CONSTRAINT FK_ownership_service_group FOREIGN KEY (businessIdentifier, businessIdentifierScheme) REFERENCES smp_service_group (businessIdentifier, businessIdentifierScheme) + ON DELETE CASCADE + ON UPDATE CASCADE, + CONSTRAINT FK_ownership_user FOREIGN KEY (username) REFERENCES smp_user (username) + ON DELETE CASCADE + ON UPDATE CASCADE +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + +CREATE TABLE smp_ownership_AUD ( + username VARCHAR(256) NOT NULL, + businessIdentifier VARCHAR(50) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + businessIdentifierScheme VARCHAR(100) + CHARACTER SET utf8 + COLLATE utf8_bin NOT NULL, + REV integer not null, + REVTYPE tinyint, + PRIMARY KEY (username, businessIdentifier, businessIdentifierScheme, REV) +) + ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + +CREATE TABLE SMP_REV_INFO ( + ID INT AUTO_INCREMENT NOT NULL, + TIMESTAMP BIGINT NULL, + REVISION_DATE timestamp NULL, + username VARCHAR(255) NULL, + CONSTRAINT PK_SMP_REV_INFO PRIMARY KEY (ID) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8; + + + +DELIMITER // + +DROP PROCEDURE IF EXISTS validate_new_user // +CREATE PROCEDURE validate_new_user (IN new_user_is_admin TINYINT(1)) +BEGIN + IF new_user_is_admin <> 0 AND new_user_is_admin <> 1 + THEN + SIGNAL SQLSTATE '99999' + SET MESSAGE_TEXT = '0 or 1 are the only allowed values for ISADMIN column'; + END IF; + END // + +DROP PROCEDURE IF EXISTS validate_new_domain // +CREATE PROCEDURE validate_new_domain (IN new_bdmsl_client_cert_alias varchar(50), IN new_bdmsl_client_cert_header varchar(4000)) +BEGIN + IF ((new_bdmsl_client_cert_alias > '' OR new_bdmsl_client_cert_alias = null) AND (new_bdmsl_client_cert_header > '' OR new_bdmsl_client_cert_header = null)) + THEN + SIGNAL SQLSTATE '99999' + SET MESSAGE_TEXT = 'Both BDMSL authentication ways cannot be switched ON at the same time: bdmslClientCertAlias and bdmslClientCertHeader'; + END IF; + END // + + +DROP TRIGGER IF EXISTS smp_domain_check_bdmsl_auth_before_insert // +DROP TRIGGER IF EXISTS smp_domain_check_bdmsl_auth_before_update // +CREATE TRIGGER smp_domain_check_bdmsl_auth_before_update +BEFORE UPDATE ON smp_domain +FOR EACH ROW + BEGIN + call validate_new_domain(NEW.bdmslClientCertAlias, NEW.bdmslClientCertHeader); + END // +CREATE TRIGGER smp_domain_check_bdmsl_auth_before_insert +BEFORE INSERT ON smp_domain +FOR EACH ROW + BEGIN + call validate_new_domain(NEW.bdmslClientCertAlias, NEW.bdmslClientCertHeader); + END // + + +DROP TRIGGER IF EXISTS smp_user_check_is_admin_value_before_insert // +DROP TRIGGER IF EXISTS smp_user_check_is_admin_value_before_update // + +CREATE TRIGGER smp_user_check_is_admin_value_before_insert +BEFORE INSERT ON smp_user +FOR EACH ROW + BEGIN + call validate_new_user(NEW.ISADMIN); + END // +CREATE TRIGGER smp_user_check_is_admin_value_before_update +BEFORE UPDATE ON smp_user +FOR EACH ROW + BEGIN + call validate_new_user(NEW.ISADMIN); + END // + +DELIMITER ; + +create table hibernate_sequence( + next_val BIGINT NOT NULL +); + +INSERT INTO hibernate_sequence(next_val) values(1); + +INSERT INTO smp_domain(domainId, bdmslSmpId) VALUES('domain1', 'DEFAULT-SMP-ID'); +-- default admin user with password "changeit" +INSERT INTO smp_user(username, password, isadmin) VALUES ('smp_admin', '$2a$10$SZXMo7K/wA.ULWxH7uximOxeNk4mf3zU6nxJx/2VfKA19QlqwSpNO', '1'); + +commit;