-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid setting the tenant id if one is preset
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.micronaut.data.jdbc.h2.multitenancy; | ||
|
||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.data.annotation.GeneratedValue; | ||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
import io.micronaut.data.annotation.TenantId; | ||
import io.micronaut.serde.annotation.Serdeable; | ||
|
||
@Serdeable // <1> | ||
@MappedEntity // <2> | ||
public record TenancyBook(@Nullable | ||
@Id // <3> | ||
@GeneratedValue // <4> | ||
Long id, | ||
String title, | ||
@TenantId // <5> | ||
String framework) { | ||
} |
26 changes: 26 additions & 0 deletions
26
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.micronaut.data.jdbc.h2.multitenancy; | ||
|
||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
import io.micronaut.scheduling.TaskExecutors; | ||
import io.micronaut.scheduling.annotation.ExecuteOn; | ||
|
||
import java.util.List; | ||
|
||
@Requires(property = "spec.name", value = "TenancyBookControllerSpec") | ||
@Controller("/books") // <1> | ||
class TenancyBookController { | ||
private final TenancyBookRepository bookRepository; | ||
|
||
TenancyBookController(TenancyBookRepository bookRepository) { // <2> | ||
this.bookRepository = bookRepository; | ||
} | ||
|
||
@ExecuteOn(TaskExecutors.BLOCKING) // <3> | ||
@Get | ||
// <4> | ||
List<TenancyBook> index() { | ||
return bookRepository.findAll(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...c/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.micronaut.data.jdbc.h2.multitenancy | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.core.type.Argument | ||
import io.micronaut.core.util.StringUtils | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.client.BlockingHttpClient | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import spock.lang.Specification | ||
|
||
import static org.junit.Assert.assertEquals | ||
|
||
@Property(name = "datasources.default.schema-generate", value = "CREATE_DROP") | ||
// <1> | ||
@Property(name = "datasources.default.url", value = "jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE") | ||
@Property(name = "datasources.default.username", value = "sa") | ||
@Property(name = "datasources.default.password", value = "") | ||
@Property(name = "datasources.default.dialect", value = "H2") | ||
@Property(name = "datasources.default.driver-class-name", value = "org.h2.Driver") | ||
@Property(name = "micronaut.multitenancy.tenantresolver.httpheader.enabled", value = StringUtils.TRUE) | ||
@Property(name = "spec.name", value = "TenancyBookControllerSpec") | ||
@MicronautTest(transactional = false) | ||
// <2> | ||
class TenancyBookControllerSpec extends Specification { | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient httpClient | ||
|
||
@Inject | ||
TenancyBookRepository bookRepository | ||
|
||
def multitenancyRequest() { | ||
|
||
given: | ||
BlockingHttpClient client = httpClient.toBlocking() | ||
save(bookRepository, client, "Building Microservices with Micronaut", "micronaut") | ||
save(bookRepository, client, "Introducing Micronaut", "micronaut") | ||
save(bookRepository, client, "Grails 3 - Step by Step", "grails") | ||
save(bookRepository, client, "Falando de Grail", "grails") | ||
save(bookRepository, client, "Grails Goodness Notebook", "grails") | ||
|
||
when: | ||
List<TenancyBook> books = fetchBooks(client, "micronaut") | ||
then: | ||
books | ||
books.size() == 2 | ||
|
||
when: | ||
books = fetchBooks(client, "grails") | ||
then: | ||
books | ||
books.size() == 3 | ||
|
||
cleanup: | ||
bookRepository.deleteAll() | ||
} | ||
|
||
List<TenancyBook> fetchBooks(BlockingHttpClient client, String framework) { | ||
HttpRequest<?> request = HttpRequest.GET("/books").header("tenantId", framework) | ||
Argument<List<TenancyBook>> responseArgument = Argument.listOf(TenancyBook.class) | ||
HttpResponse<List<TenancyBook>> response = client.exchange(request, responseArgument) | ||
assertEquals(HttpStatus.OK, response.getStatus()) | ||
return response.body() | ||
} | ||
|
||
void save(TenancyBookRepository bookRepository, BlockingHttpClient client, String title, String framework) { | ||
bookRepository.save(new TenancyBook(null, title, framework)) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.micronaut.data.jdbc.h2.multitenancy; | ||
|
||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.data.annotation.WithoutTenantId; | ||
import io.micronaut.data.jdbc.annotation.JdbcRepository; | ||
import io.micronaut.data.model.query.builder.sql.Dialect; | ||
import io.micronaut.data.repository.CrudRepository; | ||
|
||
@Requires(property = "spec.name", value = "TenancyBookControllerSpec") | ||
@JdbcRepository(dialect = Dialect.H2) // <1> | ||
public interface TenancyBookRepository extends CrudRepository<TenancyBook, Long> { // <2> | ||
Long save(String title); | ||
|
||
@WithoutTenantId | ||
@Override | ||
void deleteAll(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters