|
| 1 | +package io.micronaut.data.jdbc.h2.multitenancy |
| 2 | + |
| 3 | +import io.micronaut.context.annotation.Property |
| 4 | +import io.micronaut.core.type.Argument |
| 5 | +import io.micronaut.core.util.StringUtils |
| 6 | +import io.micronaut.http.HttpRequest |
| 7 | +import io.micronaut.http.HttpResponse |
| 8 | +import io.micronaut.http.HttpStatus |
| 9 | +import io.micronaut.http.client.BlockingHttpClient |
| 10 | +import io.micronaut.http.client.HttpClient |
| 11 | +import io.micronaut.http.client.annotation.Client |
| 12 | +import io.micronaut.test.extensions.spock.annotation.MicronautTest |
| 13 | +import jakarta.inject.Inject |
| 14 | +import spock.lang.Specification |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals |
| 17 | + |
| 18 | +@Property(name = "datasources.default.schema-generate", value = "CREATE_DROP") |
| 19 | +// <1> |
| 20 | +@Property(name = "datasources.default.url", value = "jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE") |
| 21 | +@Property(name = "datasources.default.username", value = "sa") |
| 22 | +@Property(name = "datasources.default.password", value = "") |
| 23 | +@Property(name = "datasources.default.dialect", value = "H2") |
| 24 | +@Property(name = "datasources.default.driver-class-name", value = "org.h2.Driver") |
| 25 | +@Property(name = "micronaut.multitenancy.tenantresolver.httpheader.enabled", value = StringUtils.TRUE) |
| 26 | +@Property(name = "spec.name", value = "TenancyBookControllerSpec") |
| 27 | +@MicronautTest(transactional = false) |
| 28 | +// <2> |
| 29 | +class TenancyBookControllerSpec extends Specification { |
| 30 | + |
| 31 | + @Inject |
| 32 | + @Client("/") |
| 33 | + HttpClient httpClient |
| 34 | + |
| 35 | + @Inject |
| 36 | + TenancyBookRepository bookRepository |
| 37 | + |
| 38 | + def multitenancyRequest() { |
| 39 | + |
| 40 | + given: |
| 41 | + BlockingHttpClient client = httpClient.toBlocking() |
| 42 | + save(bookRepository, client, "Building Microservices with Micronaut", "micronaut") |
| 43 | + save(bookRepository, client, "Introducing Micronaut", "micronaut") |
| 44 | + save(bookRepository, client, "Grails 3 - Step by Step", "grails") |
| 45 | + save(bookRepository, client, "Falando de Grail", "grails") |
| 46 | + save(bookRepository, client, "Grails Goodness Notebook", "grails") |
| 47 | + |
| 48 | + when: |
| 49 | + List<TenancyBook> books = fetchBooks(client, "micronaut") |
| 50 | + then: |
| 51 | + books |
| 52 | + books.size() == 2 |
| 53 | + |
| 54 | + when: |
| 55 | + books = fetchBooks(client, "grails") |
| 56 | + then: |
| 57 | + books |
| 58 | + books.size() == 3 |
| 59 | + |
| 60 | + cleanup: |
| 61 | + bookRepository.deleteAll() |
| 62 | + } |
| 63 | + |
| 64 | + List<TenancyBook> fetchBooks(BlockingHttpClient client, String framework) { |
| 65 | + HttpRequest<?> request = HttpRequest.GET("/books").header("tenantId", framework) |
| 66 | + Argument<List<TenancyBook>> responseArgument = Argument.listOf(TenancyBook.class) |
| 67 | + HttpResponse<List<TenancyBook>> response = client.exchange(request, responseArgument) |
| 68 | + assertEquals(HttpStatus.OK, response.getStatus()) |
| 69 | + return response.body() |
| 70 | + } |
| 71 | + |
| 72 | + void save(TenancyBookRepository bookRepository, BlockingHttpClient client, String title, String framework) { |
| 73 | + bookRepository.save(new TenancyBook(null, title, framework)) |
| 74 | + } |
| 75 | +} |
0 commit comments