diff --git a/data/data-samples/pom.xml b/data/data-samples/pom.xml index a61258857..4d2c0102e 100644 --- a/data/data-samples/pom.xml +++ b/data/data-samples/pom.xml @@ -35,6 +35,7 @@ sample-data-postgres-book sample-data-mongodb-book sample-data-mongodb-reactive + sample-data-mssql-book sample-data-mssql-reactive-book sample-data-mysql-book sample-data-mysql-reactive-book diff --git a/data/data-samples/sample-data-mssql-book/pom.xml b/data/data-samples/sample-data-mssql-book/pom.xml new file mode 100644 index 000000000..c2c96f699 --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/pom.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + + io.americanexpress.synapse + data-samples + 0.3.32-SNAPSHOT + + + sample-data-mssql-book + + + + io.americanexpress.synapse + synapse-data-mssql + + + io.r2dbc + r2dbc-mssql + + + + + io.americanexpress.synapse + synapse-framework-test + + + io.projectreactor + reactor-test + test + + + + diff --git a/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/config/BookDataConfig.java b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/config/BookDataConfig.java new file mode 100644 index 000000000..68e6a057e --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/config/BookDataConfig.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.data.mssql.book.config; + +import io.americanexpress.synapse.data.mssql.config.BaseMicrosoftSQLDataConfig; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; + +/** + * {@code BookDataConfig} contains configurations for connecting to Mssql database. + */ +@Configuration +@EnableAutoConfiguration +@EnableJpaRepositories(basePackages = "io.americanexpress.data.mssql.book") +@PropertySource("classpath:data-mssql-book-application.properties") +public class BookDataConfig extends BaseMicrosoftSQLDataConfig { + + /** + * Scans package string. + */ + private static final String PACKAGE_SCAN_NAME = "io.americanexpress.data.mssql.book.entity"; + + /** + * Instantiates a new Book data config. + * + * @param environment the environment + */ + protected BookDataConfig(Environment environment) { + super(environment); + } + + /** + * Scans package containing jpa entity. + * @param entityManagerFactoryBean the entity manager factory bean + */ + @Override + protected void setPackagesToScan( + LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) { + entityManagerFactoryBean.setPackagesToScan(PACKAGE_SCAN_NAME); + } +} diff --git a/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/entity/BookEntity.java b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/entity/BookEntity.java new file mode 100644 index 000000000..bb3bfaba1 --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/entity/BookEntity.java @@ -0,0 +1,72 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.data.mssql.book.entity; + +import io.americanexpress.synapse.data.mssql.entity.BaseEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +/** + * The type Book entity. + */ +@Entity +@Table(name = "book") +public class BookEntity extends BaseEntity { + + /** + * The book title. + */ + private String title; + + /** + * The book author. + */ + private String author; + + /** + * Gets title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Sets title. + * + * @param title the title + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Gets author. + * + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * Sets author. + * + * @param author the author + */ + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/repository/BookRepository.java b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/repository/BookRepository.java new file mode 100644 index 000000000..fbcb299f8 --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/java/io/americanexpress/data/mssql/book/repository/BookRepository.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.data.mssql.book.repository; + +import io.americanexpress.data.mssql.book.entity.BookEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * {@code BookRepository} contains methods for accessing Book table in Mssql database. + */ +@Repository +public interface BookRepository extends JpaRepository { + + /** + * Finds A Book Entity in the database by title and author. + * @param title of the book. + * @param author of the book. + * @return A BookEntity of the provided title and author. + */ + BookEntity findByTitleAndAuthor(String title, String author); + + /** + * Finds a Book Entity in the database by title. + * @param title of the book. + * @return A BookEntity of the provided title. + */ + BookEntity findByTitle(String title); +} diff --git a/data/data-samples/sample-data-mssql-book/src/main/resources/data-mssql-book-application.properties b/data/data-samples/sample-data-mssql-book/src/main/resources/data-mssql-book-application.properties new file mode 100644 index 000000000..4225fcd7f --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/resources/data-mssql-book-application.properties @@ -0,0 +1,15 @@ +# Copyright 2020 American Express Travel Related Services Company, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under +# the License. + +spring.mssql.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=tempdb;trustServerCertificate=true; +spring.mssql.datasource.username=sa +spring.mssql.datasource.password= diff --git a/data/data-samples/sample-data-mssql-book/src/main/resources/data.sql b/data/data-samples/sample-data-mssql-book/src/main/resources/data.sql new file mode 100644 index 000000000..27c3eab3c --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/resources/data.sql @@ -0,0 +1,22 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +SET +SCHEMA_SEARCH_PATH TO synapse; + +INSERT INTO book (title, author, created_date_time, last_modified_date_time, created_by, last_modified_by, version) +VALUES ('Synapse', 'Gabriel', GETDATE(), GETDATE(), 'John-Appleseed@email.com', 'John-Appleseed@email.com', 0); + +INSERT INTO book (title, author, created_date_time, last_modified_date_time, created_by, last_modified_by, version) +VALUES ('Revenge of Synapse', 'John', GETDATE(), GETDATE(), 'John-Appleseed@email.com', 'John-Appleseed@email.com', 0); diff --git a/data/data-samples/sample-data-mssql-book/src/main/resources/schema.sql b/data/data-samples/sample-data-mssql-book/src/main/resources/schema.sql new file mode 100644 index 000000000..2e07e3c6f --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/main/resources/schema.sql @@ -0,0 +1,30 @@ +-- Copyright 2020 American Express Travel Related Services Company, Inc. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except +-- in compliance with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software distributed under the License +-- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +-- or implied. See the License for the specific language governing permissions and limitations under +-- the License. + +DROP SCHEMA IF EXISTS synapse CASCADE; +CREATE SCHEMA synapse; + +SET +SCHEMA_SEARCH_PATH TO synapse; + +DROP TABLE IF EXISTS book CASCADE; + +CREATE TABLE book ( + id INT IDENTITY(1,1) PRIMARY KEY NOT NULL, + title NVARCHAR(150) NOT NULL UNIQUE, + author NVARCHAR(100), + created_by NVARCHAR(100), + last_modified_by NVARCHAR(100), + created_date_time DATETIME DEFAULT GETDATE(), + last_modified_date_time DATETIME, + version INT NOT NULL +); diff --git a/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/config/BookDataTestConfig.java b/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/config/BookDataTestConfig.java new file mode 100644 index 000000000..0f9357595 --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/config/BookDataTestConfig.java @@ -0,0 +1,26 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.data.mssql.book.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * {@code BookDataTestConfig} class contains configurations for tests. + */ +@Configuration +@Import(BookDataConfig.class) +public class BookDataTestConfig { + +} diff --git a/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/repository/BookRepositoryIT.java b/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/repository/BookRepositoryIT.java new file mode 100644 index 000000000..2c14904ae --- /dev/null +++ b/data/data-samples/sample-data-mssql-book/src/test/java/io/americanexpress/data/mssql/book/repository/BookRepositoryIT.java @@ -0,0 +1,58 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.data.mssql.book.repository; + +import io.americanexpress.data.mssql.book.config.BookDataTestConfig; +import io.americanexpress.data.mssql.book.entity.BookEntity; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * {@code BookRepositoryIT} tests the {@link BookRepository} with local instance of Mssql database. + */ +@ContextConfiguration(classes = BookDataTestConfig.class) +@ExtendWith(SpringExtension.class) +class BookRepositoryIT { + + /** + * Book repository. + */ + @Autowired + private BookRepository bookRepository; + + /** + * Find All test. + */ + @Test + void findAll_givenBooksInDatabase_expectedBooksReturned() { + List bookEntityList = bookRepository.findAll(); + assertEquals(bookEntityList.size(), 2); + } + + /** + * Find by title test. + */ + @Test + void findBookByTitle_givenTitleInDatabase_expectedBookByTitleReturned() { + BookEntity bookRepositoryByTitle = bookRepository.findByTitle("Revenge of Synapse"); + assertNotNull(bookRepositoryByTitle); + } +} diff --git a/data/synapse-data-mssql/pom.xml b/data/synapse-data-mssql/pom.xml index b02dd7032..8c8c7ca9b 100644 --- a/data/synapse-data-mssql/pom.xml +++ b/data/synapse-data-mssql/pom.xml @@ -42,6 +42,10 @@ io.r2dbc r2dbc-mssql + + com.microsoft.sqlserver + mssql-jdbc + diff --git a/data/synapse-data-mssql/src/main/java/io/americanexpress/synapse/data/mssql/config/BaseMicrosoftSQLDataConfig.java b/data/synapse-data-mssql/src/main/java/io/americanexpress/synapse/data/mssql/config/BaseMicrosoftSQLDataConfig.java new file mode 100644 index 000000000..508899cea --- /dev/null +++ b/data/synapse-data-mssql/src/main/java/io/americanexpress/synapse/data/mssql/config/BaseMicrosoftSQLDataConfig.java @@ -0,0 +1,95 @@ +/* + * Copyright 2020 American Express Travel Related Services Company, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package io.americanexpress.synapse.data.mssql.config; + +import com.microsoft.sqlserver.jdbc.SQLServerDataSource; +import javax.sql.DataSource; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * {@code BaseMicrosoftSQLDataConfig} class is used to hold the common configuration for all data-mssql modules. + */ +@Configuration +@EnableJpaAuditing +@EnableTransactionManagement +public abstract class BaseMicrosoftSQLDataConfig { + + /** + * Used to acquire environment variables. + */ + private final Environment environment; + + /** + * Instantiates a new Base microsoft sql data config. + * + * @param environment the environment + */ + public BaseMicrosoftSQLDataConfig(Environment environment) { + this.environment = environment; + } + + /** + * Used to create and edit the DataSource bean. + * Create connection for connecting to Mssql database. + * Customization to Mssql database connection can be provided in the spring.datasource.url. + * Ref to Building the connection URL + * for customization options. + * Ex: jdbc:sqlserver://localhost:1433. + * + * @return DataSource bean. + */ + @Bean + @ConfigurationProperties(prefix = "spring.datasource") + public DataSource dataSource() { + SQLServerDataSource dataSource = DataSourceBuilder + .create() + .type(SQLServerDataSource.class) + .build(); + + dataSource.setURL(environment.getRequiredProperty("spring.mssql.datasource.url")); + dataSource.setUser(environment.getRequiredProperty("spring.mssql.datasource.username")); + dataSource.setPassword(environment.getRequiredProperty("spring.mssql.datasource.password")); + return dataSource; + } + + /** + * Used to create and edit the LocalContainerEntityManagerFactoryBean. + * @param dataSource Database connectivity + * + * @return LocalContainerEntityManagerFactoryBean + */ + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { + LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); + entityManagerFactoryBean.setDataSource(dataSource); + entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + this.setPackagesToScan(entityManagerFactoryBean); + return entityManagerFactoryBean; + } + + /** + * Set the packages to Scan property to the entityManagerFactory. + * + * @param entityManagerFactoryBean Used to set entity scanner for JPA. + */ + protected abstract void setPackagesToScan(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean); +} diff --git a/pom.xml b/pom.xml index 40a403d5c..6ac1c98a9 100644 --- a/pom.xml +++ b/pom.xml @@ -449,6 +449,11 @@ sample-data-mssql-reactive-book ${project.version} + + io.americanexpress.synapse + sample-data-mssql-book + ${project.version} + io.americanexpress.synapse sample-data-mysql-book