Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Microsoft SQL Server support #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<module>spring-test-rabbitmq</module>
<module>spring-test-activemq</module>
<module>spring-test-jpa</module>
<module>spring-test-mssql</module>
<module>spring-test-mysql</module>
<module>spring-test-postgres</module>
<module>spring-test-web</module>
Expand Down
25 changes: 25 additions & 0 deletions spring-test-mssql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
84 changes: 84 additions & 0 deletions spring-test-mssql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
</parent>

<artifactId>spring-test-mssql</artifactId>
<version>${parent.version}</version>
<packaging>jar</packaging>

<name>spring-test-mssql</name>
<description>JUnit5 utils and extensions to make integration tests for MSSQL with Spring</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<apache.commons.lang3.version>3.3.2</apache.commons.lang3.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-jpa</artifactId>
<version>${parent.version}</version>
</dependency>

<!-- TestContainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
<version>1.15.3</version>
<scope>compile</scope>
</dependency>


<!-- TestContainers -->

<!-- DB -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.2.1.jre8</version>
</dependency>

<!-- DB -->

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>

</dependencies>


<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jupiter.tools.spring.test.mssql.annotation;

import com.jupiter.tools.spring.test.jpa.extension.TraceSqlExtension;
import com.jupiter.tools.spring.test.mssql.extension.MsSqlTcExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ActiveProfiles;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created on 17.07.2018.
* <p>
* Start a MSSQL container in Docker, by a test-containers library.
*
* @author Korovin Anatoliy
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ExtendWith(MsSqlTcExtension.class)
@ExtendWith(TraceSqlExtension.class)
@ActiveProfiles("jupiter-tools.spring-test-mssql.test-containers")
public @interface EnableMsSqlTestContainers {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jupiter.tools.spring.test.mssql.annotation.meta;

import com.jupiter.tools.spring.test.jpa.annotation.EnableDataTest;
import com.jupiter.tools.spring.test.jpa.annotation.EnableRiderTest;
import com.jupiter.tools.spring.test.mssql.annotation.EnableMsSqlTestContainers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created on 19.07.2018.
*
* Meta-annotation for the combination of: DataTests, RiderTests
* and MSSQL docker container.
*
* @author Korovin Anatoliy
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@EnableDataTest
@EnableRiderTest
@EnableMsSqlTestContainers
public @interface EnableMsSqlDataTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jupiter.tools.spring.test.mssql.annotation.meta;


import com.jupiter.tools.spring.test.core.annotation.EnableIntegrationTest;
import com.jupiter.tools.spring.test.jpa.annotation.EnableRiderTest;
import com.jupiter.tools.spring.test.mssql.annotation.EnableMsSqlTestContainers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created on 19.07.2018.
*
* Meta-annotation for the combination of: IntegrationTests, RiderTests
* and MSSQL docker container.
*
* @author Korovin Anatoliy
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@EnableIntegrationTest
@EnableRiderTest
@EnableMsSqlTestContainers
public @interface EnableMsSqlIntegrationTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jupiter.tools.spring.test.mssql.extension;

import org.hibernate.dialect.SQLServer2012Dialect;
import org.junit.jupiter.api.extension.Extension;
import org.testcontainers.containers.MSSQLServerContainer;

/**
* Created on 06.08.2018.
*
* @author Korovin Anatoliy
*/
public class MsSqlTcExtension implements Extension {

static {
System.out.println("Start MsSql testcontainers extension...\n");

MSSQLServerContainer mssql = new MSSQLServerContainer("mcr.microsoft.com/mssql/server:2022-latest")
.acceptLicense();
mssql.start();

System.setProperty("spring.datasource.driver-class-name", mssql.getDriverClassName());
System.setProperty("spring.datasource.url", mssql.getJdbcUrl());
System.setProperty("spring.datasource.username", mssql.getUsername());
System.setProperty("spring.datasource.password", mssql.getPassword());
System.setProperty("spring.jpa.properties.hibernate.dialect", SQLServer2012Dialect.class.getCanonicalName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.jupiter.tools.spring.test.mssql;

import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.*;
import java.math.BigDecimal;

/**
* Created on 12.07.2018.
*
* Transaction Persistence layer configuration,
* for testing transactional methods in wrappers and service.
*
* @author Korovin Anatoliy
*/
@SpringBootApplication
@TestConfiguration
@EnableJpaRepositories(considerNestedRepositories = true)
@EntityScan(basePackageClasses = {TransactionalTestConfig.class})
public class TransactionalTestConfig {

@Repository
public interface FooRepository extends JpaRepository<Foo, Long> {
@Query(value = "SELECT RAND()", nativeQuery = true)
Double rand();

@Query(value = "SELECT DATALENGTH('123')", nativeQuery = true)
String nativeQuery();
}

@Table(name = "foo")
@Entity
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "decimal(9,0)")
private BigDecimal id;

@Column(nullable = false)
private String field;
}

@Service
public class TestService {
@Autowired
private FooRepository fooRepository;

@Transactional
public Foo ok() {
Foo foo = new Foo();
foo.setField("tru la la..");
return fooRepository.save(foo);
}

@Transactional
public void fail() {
fooRepository.save(new Foo());
}

@Transactional(readOnly = true)
public long size() {
return fooRepository.count();
}

@Transactional(isolation = Isolation.SERIALIZABLE)
public void clear() {
fooRepository.deleteAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jupiter.tools.spring.test.mssql.annotation;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
* Created on 26.01.2019.
*
* @author Korovin Anatoliy
*/
@DataJpaTest
@ExtendWith(SpringExtension.class)
@EnableMsSqlTestContainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class EnableMsSqlTestContainersTest {

@PersistenceContext
private EntityManager entityManager;

@Test
@Sql("/stored_procedures/test_procedure.sql")
void testStoredProcedure() {
// Arrange
Query query = entityManager.createNativeQuery("EXEC ANSWER_TO_THE_ULTIMATE_QUESTION");

// Act
int result = (int) query.getSingleResult();

//Assert
Assertions.assertThat(result).isEqualTo(42);
}
}
Loading